Skip to content

Commit

Permalink
Migrated to the latest crstore version
Browse files Browse the repository at this point in the history
  • Loading branch information
Azarattum committed Jan 7, 2024
1 parent b48e3ba commit e20a785
Show file tree
Hide file tree
Showing 26 changed files with 425 additions and 173 deletions.
2 changes: 1 addition & 1 deletion apps/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"allowSyntheticDefaultImports": true,
"verbatimModuleSyntax": true,
"types": ["node", "vite/client"],
"moduleResolution": "node",
"moduleResolution": "Bundler",
"resolveJsonModule": true,
"esModuleInterop": true,
"module": "esnext",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"devDependencies": {
"@sveltejs/adapter-static": "3.0.1",
"crstore": "^0.19.3",
"crstore": "^0.20.2",
"murmurhash": "^2.0.1"
}
}
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"@sveltejs/kit": "2.0.6",
"@sveltejs/vite-plugin-svelte": "^3.0.1",
"@types/node": "20.10.6",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"@typescript-eslint/eslint-plugin": "^6.18.0",
"@typescript-eslint/parser": "^6.18.0",
"@vitest/coverage-v8": "^1.1.3",
"autoprefixer": "^10.4.16",
"cssnano": "^6.0.3",
Expand All @@ -32,13 +32,13 @@
"prettier": "^3.1.1",
"prettier-plugin-sort-imports": "^1.8.1",
"prettier-plugin-svelte": "^3.1.2",
"prettier-plugin-tailwindcss": "^0.5.10",
"prettier-plugin-tailwindcss": "^0.5.11",
"rimraf": "^5.0.5",
"rollup": "^4.9.3",
"rollup": "^4.9.4",
"rollup-plugin-node-externals": "^6.1.2",
"svelte": "^4.2.8",
"svelte-check": "^3.6.2",
"tailwindcss": "^3.4.0",
"tailwindcss": "^3.4.1",
"tailwindcss-contain": "^0.1.1",
"tslib": "^2.6.2",
"turbo": "^1.11.3",
Expand Down
7 changes: 4 additions & 3 deletions packages/crdata/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import { library } from "../stores/library";
import { type DB, schema } from "./schema";
import { tracks } from "../stores/tracks";
import { albums } from "../stores/albums";
import { database, sql } from "crstore";
import { database } from "crstore/svelte";
import { feed } from "../stores/feed";
import { sql } from "crstore";

const connections = new Map<string, Connection>();

Expand All @@ -32,7 +33,7 @@ function connect(options: Options) {
if (!connections.has(options.name)) {
const db = database(
options.local ? (nocrr(schema) as typeof schema) : schema,
options
options,
) as any as DB;
connections.set(options.name, { ...db, ...stores(db) });
const files = import.meta.glob("../sql/*.sql", { eager: true, as: "raw" });
Expand Down Expand Up @@ -62,7 +63,7 @@ function nocrr<T>(schema: T) {
Object.entries((schema as any).schema).map(([key, value]) => [
key,
{ ...(value as object), crr: false },
])
]),
),
} as T;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/crdata/data/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
artistBase,
playlistBase,
} from "@amadeus-music/protocol";
import { primary, crr, ordered, index, type Database } from "crstore";
import { primary, crr, ordered, index } from "crstore";
import { type SvelteDatabase } from "crstore/svelte";
import type { Struct } from "superstruct";

const id = integer;
Expand Down Expand Up @@ -126,7 +127,7 @@ type Views = {
};

export type Schema = Infer<typeof schema> & Views;
export type DB = Database<Schema>;
export type DB = SvelteDatabase<Schema>;
export const schema = object({
tracks,
albums,
Expand Down
2 changes: 1 addition & 1 deletion packages/crdata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"dependencies": {
"@amadeus-music/protocol": "workspace:*",
"crstore": "^0.19.3",
"crstore": "^0.20.2",
"superstruct": "^1.0.3"
}
}
6 changes: 3 additions & 3 deletions packages/crdata/stores/albums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { pushAlbums } from "../operations/push";
import { sanitize } from "../operations/utils";
import type { DB } from "../data/schema";

export const albums = ({ store }: DB) =>
store(
export const albums = ({ replicated }: DB) =>
replicated(
(db) =>
db
.with("source", source)
Expand Down Expand Up @@ -48,5 +48,5 @@ export const albums = ({ store }: DB) =>
.$castTo<Album>()
.executeTakeFirstOrThrow();
},
}
},
);
8 changes: 4 additions & 4 deletions packages/crdata/stores/artists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { sanitize } from "../operations/utils";
import { json, groupJSON } from "crstore";
import type { DB } from "../data/schema";

export const artists = ({ store }: DB) =>
store(
export const artists = ({ replicated }: DB) =>
replicated(
(db) =>
db
.with("source", source)
Expand All @@ -31,7 +31,7 @@ export const artists = ({ store }: DB) =>
size: qb.fn.count<number>("track.duration"),
duration: qb.fn.coalesce(
qb.fn.sum<number>("track.duration"),
qb.val(0)
qb.val(0),
),
tracks: groupJSON(qb, {
id: "track.id",
Expand Down Expand Up @@ -96,5 +96,5 @@ export const artists = ({ store }: DB) =>
.$castTo<Artist>()
.executeTakeFirstOrThrow();
},
}
},
);
10 changes: 5 additions & 5 deletions packages/crdata/stores/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { Feed, Playlist } from "@amadeus-music/protocol";
import { groupJSON, json } from "crstore";
import type { DB } from "../data/schema";

export const feed = ({ store }: DB) =>
store(
export const feed = ({ replicated }: DB) =>
replicated(
(db) =>
db
.with("source", source)
Expand All @@ -16,7 +16,7 @@ export const feed = ({ store }: DB) =>
.innerJoin("library", "library.track", "tracks.id")
.select(["library.playlist", "library.id as entry"])
.orderBy("library.order")
.orderBy("library.id")
.orderBy("library.id"),
)
.selectFrom("track")
.fullJoin("playlists", "playlists.id", "track.playlist")
Expand All @@ -32,7 +32,7 @@ export const feed = ({ store }: DB) =>
size: qb.fn.count<number>("track.duration"),
duration: qb.fn.coalesce(
qb.fn.sum<number>("track.duration"),
qb.val(0)
qb.val(0),
),
tracks: groupJSON(qb, {
id: "track.id",
Expand Down Expand Up @@ -62,5 +62,5 @@ export const feed = ({ store }: DB) =>
.execute()
.then((x) => x.map((y) => y.id as number));
},
}
},
);
43 changes: 23 additions & 20 deletions packages/crdata/stores/history.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import type { DB } from "../data/schema";

export const history = ({ store }: DB) =>
store((db) => db.selectFrom("history").orderBy("date", "desc").selectAll(), {
async log(db, query: string) {
if (!query.trim()) return;
await db
.insertInto("history")
.onConflict((x) => x.doUpdateSet({ date: Date.now() }))
.values({ query, date: Date.now() })
.execute();
export const history = ({ replicated }: DB) =>
replicated(
(db) => db.selectFrom("history").orderBy("date", "desc").selectAll(),
{
async log(db, query: string) {
if (!query.trim()) return;
await db
.insertInto("history")
.onConflict((x) => x.doUpdateSet({ date: Date.now() }))
.values({ query, date: Date.now() })
.execute();
},
async clear(db) {
await db.deleteFrom("history").execute();
},
get(db) {
return db
.selectFrom("history")
.selectAll()
.orderBy("date", "desc")
.execute();
},
},
async clear(db) {
await db.deleteFrom("history").execute();
},
get(db) {
return db
.selectFrom("history")
.selectAll()
.orderBy("date", "desc")
.execute();
},
});
);
8 changes: 4 additions & 4 deletions packages/crdata/stores/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { uuid } from "../operations/utils";
import type { DB } from "../data/schema";
import { APPEND, sql } from "crstore";

export const library = ({ store }: DB) =>
store((db) => db.selectFrom("library").selectAll(), {
export const library = ({ replicated }: DB) =>
replicated((db) => db.selectFrom("library").selectAll(), {
async push(db, tracks: Track[], playlist?: number) {
if (!tracks.length) return;
await pushTracks(db, tracks);
Expand All @@ -21,7 +21,7 @@ export const library = ({ store }: DB) =>
date: ~~(Date.now() / 1000),
track: id,
playlist,
}))
})),
)
.execute();
},
Expand All @@ -34,7 +34,7 @@ export const library = ({ store }: DB) =>
},
async purge(db, entries: number[]) {
const promises = entries.map((entry) =>
db.deleteFrom("library").where("id", "=", entry).execute()
db.deleteFrom("library").where("id", "=", entry).execute(),
);
await Promise.all(promises);
},
Expand Down
40 changes: 20 additions & 20 deletions packages/crdata/stores/playback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { pushTracks } from "../operations/push";
import type { DB } from "../data/schema";
import { json, sql } from "crstore";

export const preceding = ({ store }: DB) =>
store((db) =>
export const preceding = ({ replicated }: DB) =>
replicated((db) =>
db
.with("source", source)
.with("asset", asset)
Expand All @@ -30,8 +30,8 @@ export const preceding = ({ store }: DB) =>
.$castTo<Track & { entry: number }>(),
);

export const upcoming = ({ store }: DB) =>
store((db) =>
export const upcoming = ({ replicated }: DB) =>
replicated((db) =>
db
.with("source", source)
.with("asset", asset)
Expand All @@ -51,8 +51,8 @@ export const upcoming = ({ store }: DB) =>
.$castTo<Track & { entry: number }>(),
);

export const playback = ({ store }: DB) =>
store(
export const playback = ({ replicated }: DB) =>
replicated(
(db) =>
db
.with("source", source)
Expand Down Expand Up @@ -98,17 +98,17 @@ export const playback = ({ store }: DB) =>
? Number.isInteger(at)
? at
: at === "first"
? position.first
: at === "next"
? position.next
: position.last
? position.first
: at === "next"
? position.next
: position.last
: Number.isInteger(at)
? position.shift(+at)
: at === "first"
? position.last
: at === "next"
? position.before
: position.first;
? position.shift(+at)
: at === "first"
? position.last
: at === "next"
? position.before
: position.first;

await db
.insertInto("playback_fractindex" as any)
Expand All @@ -121,8 +121,8 @@ export const playback = ({ store }: DB) =>
at === "random"
? position.random(ids.slice(0, i))
: i > 0 && direction != 1
? ids[i - 1]
: order,
? ids[i - 1]
: order,
})),
)
.execute();
Expand Down Expand Up @@ -173,8 +173,8 @@ export const playback = ({ store }: DB) =>
direction != 1
? after
: after != null
? position.shift(after)
: position.last;
? position.shift(after)
: position.last;

await db
.updateTable("playback_fractindex" as any)
Expand Down
10 changes: 5 additions & 5 deletions packages/crdata/stores/playlists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { identify, type Playlist } from "@amadeus-music/protocol";
import { APPEND, groupJSON, json } from "crstore";
import type { DB } from "../data/schema";

export const playlists = ({ store }: DB) =>
store(
export const playlists = ({ replicated }: DB) =>
replicated(
(db) =>
db
.with("source", source)
Expand All @@ -16,7 +16,7 @@ export const playlists = ({ store }: DB) =>
.innerJoin("library", "library.track", "tracks.id")
.select(["library.playlist", "library.id as entry"])
.orderBy("library.order")
.orderBy("library.id")
.orderBy("library.id"),
)
.selectFrom("track")
.fullJoin("playlists", "playlists.id", "track.playlist")
Expand All @@ -32,7 +32,7 @@ export const playlists = ({ store }: DB) =>
size: qb.fn.count<number>("track.duration"),
duration: qb.fn.coalesce(
qb.fn.sum<number>("track.duration"),
qb.val(0)
qb.val(0),
),
tracks: groupJSON(qb, {
id: "track.id",
Expand Down Expand Up @@ -86,5 +86,5 @@ export const playlists = ({ store }: DB) =>
.where("id", "=", id)
.executeTakeFirstOrThrow();
},
}
},
);
Loading

0 comments on commit e20a785

Please sign in to comment.