« get me outta code hell

simple data serialization & random pages fixes - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2022-03-12 19:07:22 -0400
committer(quasar) nebula <qznebula@protonmail.com>2022-03-12 19:07:22 -0400
commit6a86a6aca6215cb4d464b814c34233b001575cd9 (patch)
treecfa90beb1027beda33b4f37c0883bfc99e2d3fad /src/data
parentf27c98a228847d5f300a2d3c1252294d673b639e (diff)
simple data serialization & random pages fixes
Diffstat (limited to 'src/data')
-rw-r--r--src/data/serialize.js38
-rw-r--r--src/data/things.js78
2 files changed, 115 insertions, 1 deletions
diff --git a/src/data/serialize.js b/src/data/serialize.js
new file mode 100644
index 00000000..9d4e8885
--- /dev/null
+++ b/src/data/serialize.js
@@ -0,0 +1,38 @@
+// serialize-util.js: simple interface and utility functions for converting
+// Things into a directly serializeable format
+
+// Utility functions
+
+export function id(x) {
+    return x;
+}
+
+export function toRef(thing) {
+    return thing?.constructor.getReference(thing);
+}
+
+export function toRefs(things) {
+    return things?.map(toRef);
+}
+
+export function toContribRefs(contribs) {
+    return contribs?.map(({ who, what }) => ({who: toRef(who), what}));
+}
+
+// Interface
+
+export const serializeDescriptors = Symbol();
+
+export function serializeThing(thing) {
+    const descriptors = thing.constructor[serializeDescriptors];
+    if (!descriptors) {
+        throw new Error(`Constructor ${thing.constructor.name} does not provide serialize descriptors`);
+    }
+
+    return Object.fromEntries(Object.entries(descriptors)
+        .map(([ property, transform ]) => [property, transform(thing[property])]));
+}
+
+export function serializeThings(things) {
+    return things.map(serializeThing);
+}
diff --git a/src/data/things.js b/src/data/things.js
index 5889b119..2d199c10 100644
--- a/src/data/things.js
+++ b/src/data/things.js
@@ -28,6 +28,8 @@ import {
     validateReferenceList,
 } from './validators.js';
 
+import * as S from './serialize.js';
+
 import {
     getKebabCase,
 } from '../util/wiki-data.js';
@@ -508,6 +510,43 @@ Album.propertyDescriptors = {
     artTags: Thing.common.dynamicThingsFromReferenceList('artTagsByRef', 'artTagData', find.artTag),
 };
 
+Album[S.serializeDescriptors] = {
+    name: S.id,
+    color: S.id,
+    directory: S.id,
+    urls: S.id,
+
+    date: S.id,
+    coverArtDate: S.id,
+    trackArtDate: S.id,
+    dateAddedToWiki: S.id,
+
+    artistContribs: S.toContribRefs,
+    coverArtistContribs: S.toContribRefs,
+    trackCoverArtistContribs: S.toContribRefs,
+    wallpaperArtistContribs: S.toContribRefs,
+    bannerArtistContribs: S.toContribRefs,
+
+    coverArtFileExtension: S.id,
+    trackCoverArtFileExtension: S.id,
+    wallpaperStyle: S.id,
+    wallpaperFileExtension: S.id,
+    bannerStyle: S.id,
+    bannerFileExtension: S.id,
+    bannerDimensions: S.id,
+
+    hasTrackArt: S.id,
+    isMajorRelease: S.id,
+    isListedOnHomepage: S.id,
+
+    commentary: S.id,
+
+    tracks: S.toRefs,
+    groups: S.toRefs,
+    artTags: S.toRefs,
+    commentatorArtists: S.toRefs,
+};
+
 TrackGroup.propertyDescriptors = {
     // Update & expose
 
@@ -781,7 +820,7 @@ Track.prototype[inspect.custom] = function() {
     return (albumName
         ? base + ` (${color.yellow(trackNum)} in ${color.green(albumName)})`
         : base);
-}
+};
 
 // -> Artist
 
@@ -871,6 +910,7 @@ Artist.propertyDescriptors = {
         }
     },
 
+    albumsAsAlbumArtist: Artist.filterByContrib('albumData', 'artistContribs'),
     albumsAsCoverArtist: Artist.filterByContrib('albumData', 'coverArtistContribs'),
     albumsAsWallpaperArtist: Artist.filterByContrib('albumData', 'wallpaperArtistContribs'),
     albumsAsBannerArtist: Artist.filterByContrib('albumData', 'bannerArtistContribs'),
@@ -889,6 +929,31 @@ Artist.propertyDescriptors = {
     flashesAsContributor: Artist.filterByContrib('flashData', 'contributorContribs'),
 };
 
+Artist[S.serializeDescriptors] = {
+    name: S.id,
+    directory: S.id,
+    urls: S.id,
+    contextNotes: S.id,
+
+    hasAvatar: S.id,
+    avatarFileExtension: S.id,
+
+    aliasNames: S.id,
+
+    tracksAsArtist: S.toRefs,
+    tracksAsContributor: S.toRefs,
+    tracksAsCoverArtist: S.toRefs,
+    tracksAsCommentator: S.toRefs,
+
+    albumsAsAlbumArtist: S.toRefs,
+    albumsAsCoverArtist: S.toRefs,
+    albumsAsWallpaperArtist: S.toRefs,
+    albumsAsBannerArtist: S.toRefs,
+    albumsAsCommentator: S.toRefs,
+
+    flashesAsContributor: S.toRefs,
+};
+
 // -> Group
 
 Group.propertyDescriptors = {
@@ -1202,6 +1267,17 @@ Flash.propertyDescriptors = {
     },
 };
 
+Flash[S.serializeDescriptors] = {
+    name: S.id,
+    page: S.id,
+    directory: S.id,
+    date: S.id,
+    contributors: S.toContribRefs,
+    tracks: S.toRefs,
+    urls: S.id,
+    color: S.id,
+};
+
 FlashAct.propertyDescriptors = {
     // Update & expose