forked from
rocksky.app/rocksky
A decentralized music tracking and discovery platform built on AT Protocol 馃幍
1import chalk from "chalk";
2import { consola } from "consola";
3import { ctx } from "context";
4import { count } from "drizzle-orm";
5import tables from "schema";
6
7async function main() {
8 consola.info(chalk.cyan("Starting Meilisearch sync..."));
9
10 try {
11 await Promise.all([
12 createAlbums(),
13 createArtists(),
14 createTracks(),
15 createUsers(),
16 ]);
17 consola.info(chalk.green("Meilisearch sync completed successfully."));
18 } catch (error) {
19 consola.error(chalk.red("Error during Meilisearch sync:"), error);
20 }
21}
22
23await main();
24
25async function createAlbums() {
26 const { meilisearch } = ctx;
27 const size = 100;
28 const total = await ctx.db
29 .select({ value: count() })
30 .from(tables.albums)
31 .execute()
32 .then(([row]) => row.value);
33 for (let i = 0; i < total; i += size) {
34 const skip = i;
35 consola.info(
36 `Processing ${chalk.magentaBright("albums")}: ${chalk.magentaBright(skip)} to ${chalk.magentaBright(skip + size)}`,
37 );
38 const results = await ctx.db
39 .select()
40 .from(tables.albums)
41 .limit(size)
42 .offset(skip)
43 .execute();
44
45 await meilisearch.post(`indexes/albums/documents?primaryKey=id`, results);
46 }
47}
48
49async function createArtists() {
50 const { meilisearch } = ctx;
51 const size = 100;
52 const total = await ctx.db
53 .select({ value: count() })
54 .from(tables.artists)
55 .execute()
56 .then(([row]) => row.value);
57 for (let i = 0; i < total; i += size) {
58 const skip = i;
59 consola.info(
60 `Processing ${chalk.magentaBright("artists")}: ${chalk.magentaBright(skip)} to ${chalk.magentaBright(skip + size)}`,
61 );
62 const results = await ctx.db
63 .select()
64 .from(tables.artists)
65 .limit(size)
66 .offset(skip)
67 .execute();
68
69 await meilisearch.post(`indexes/artists/documents?primaryKey=id`, results);
70 }
71}
72
73async function createTracks() {
74 const { meilisearch } = ctx;
75 const size = 100;
76 const total = await ctx.db
77 .select({ value: count() })
78 .from(tables.tracks)
79 .execute()
80 .then(([row]) => row.value);
81 for (let i = 0; i < total; i += size) {
82 const skip = i;
83 consola.info(
84 `Processing ${chalk.magentaBright("tracks")}: ${chalk.magentaBright(skip)} to ${chalk.magentaBright(skip + size)}`,
85 );
86 const results = await ctx.db
87 .select()
88 .from(tables.tracks)
89 .limit(size)
90 .offset(skip)
91 .execute();
92
93 await meilisearch.post(`/indexes/tracks/documents?primaryKey=id`, results);
94 }
95}
96
97async function createUsers() {
98 const { meilisearch } = ctx;
99 const size = 100;
100 const total = await ctx.db
101 .select({ value: count() })
102 .from(tables.users)
103 .execute()
104 .then(([row]) => row.value);
105
106 for (let i = 0; i < total; i += size) {
107 const skip = i;
108 consola.info(
109 `Processing ${chalk.magentaBright("users")}: ${chalk.magentaBright(skip)} to ${chalk.magentaBright(skip + size)}`,
110 );
111 const results = await ctx.db
112 .select()
113 .from(tables.users)
114 .limit(size)
115 .offset(skip)
116 .execute();
117
118 await meilisearch.post(`/indexes/users/documents?primaryKey=id`, results);
119 }
120}
121
122process.exit(0);