-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlibrary.ts
122 lines (114 loc) · 3.47 KB
/
library.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { database } from "../../lib/svelte";
import { groupJSON } from "../../lib";
import { schema } from "./schema";
import { trpc } from "../client";
const { replicated } = database(schema, {
name: "library.db",
push: trpc.library.push.mutate,
pull: trpc.library.pull.subscribe,
});
const all = replicated(
(db) =>
db
.selectFrom("tracks")
.leftJoin("artists", "tracks.artist", "artists.id")
.leftJoin("albums", "tracks.album", "albums.id")
.select([
"tracks.id as id",
"tracks.title as title",
"artists.title as artist",
"albums.title as album",
]),
{
add(db, title: string, artistId: string, albumId: string) {
const id = [...title, ...artistId, ...albumId]
.map((x) => x.charCodeAt(0))
.join("");
return db
.insertInto("tracks")
.values({ id, title, artist: artistId, album: albumId })
.execute();
},
},
);
const artists = replicated((db) => db.selectFrom("artists").selectAll(), {
add(db, title: string) {
const id = [...title].map((x) => x.charCodeAt(0)).join("");
return db.insertInto("artists").values({ id, title }).execute();
},
});
const albums = replicated((db) => db.selectFrom("albums").selectAll(), {
add(db, title: string) {
const id = [...title].map((x) => x.charCodeAt(0)).join("");
return db.insertInto("albums").values({ id, title }).execute();
},
});
const playlists = replicated((db) => db.selectFrom("playlists").selectAll(), {
add(db, title: string) {
const id = [...title].map((x) => x.charCodeAt(0)).join("");
return db.insertInto("playlists").values({ id, title }).execute();
},
async link(db, track: string, playlist: string) {
const id = Math.random().toString(36).slice(2);
const max = await db
.selectFrom("tracksByPlaylist")
.where("playlist", "=", playlist)
.select((db) => db.fn.max("order").as("order"))
.executeTakeFirst();
// Append "|" to make the next item
const order = max ? (max.order || "") + "|" : "|";
return db
.insertInto("tracksByPlaylist")
.values({ id, track, playlist, order })
.execute();
},
});
const grouped = replicated((db) =>
db
.selectFrom("tracks")
.leftJoin("artists", "tracks.artist", "artists.id")
.leftJoin("albums", "tracks.album", "albums.id")
.select([
"albums.title as album",
(qb) =>
groupJSON(qb, {
id: "tracks.id",
title: "tracks.title",
artist: "artists.title",
album: "albums.title",
}).as("tracks"),
])
.groupBy("album"),
);
const organized = replicated((db) =>
db
.selectFrom((db) =>
db
.selectFrom("playlists")
.innerJoin("tracksByPlaylist", "playlist", "playlists.id")
.innerJoin("tracks", "track", "tracks.id")
.leftJoin("artists", "tracks.artist", "artists.id")
.leftJoin("albums", "tracks.album", "albums.id")
.select([
"playlists.title as playlist",
"tracksByPlaylist.id as id",
"tracks.title as title",
"artists.title as artist",
"albums.title as album",
])
.orderBy("order")
.as("data"),
)
.select([
"playlist",
(qb) =>
groupJSON(qb, {
id: "id",
title: "title",
artist: "artist",
album: "album",
}).as("tracks"),
])
.groupBy("playlist"),
);
export { all, artists, albums, playlists, grouped, organized };