From 74e28d4bc202b486e51feb2d4b3baef9da344a1e Mon Sep 17 00:00:00 2001 From: Tsiry Sandratraina Date: Thu, 02 Oct 2025 05:13:48 +0000 Subject: [PATCH] refactor: streamline album processing logic and enhance logging in Meilisearch sync --- apps/api/src/scripts/genres.ts | 7 ++++--- apps/api/src/scripts/meili.ts | 27 ++++++++++++++++----------- 2 file(s) changed, 20 insertion(s)(+), 14 deletion(s)(-) diff --git a/apps/api/src/scripts/genres.ts b/apps/api/src/scripts/genres.ts --- a/apps/api/src/scripts/genres.ts +++ b/apps/api/src/scripts/genres.ts @@ -11,7 +11,7 @@ .from(tables.spotifyTokens) .leftJoin( tables.spotifyAccounts, - eq(tables.spotifyAccounts.userId, tables.spotifyTokens.userId), + eq(tables.spotifyAccounts.userId, tables.spotifyTokens.userId) ) .where(eq(tables.spotifyAccounts.isBetaUser, true)) .execute() @@ -51,7 +51,7 @@ headers: { Authorization: `Bearer ${token}`, }, - }, + } ) .then( (res) => @@ -64,7 +64,7 @@ images: Array<{ url: string }>; }>; }; - }>, + }> ) .then(async (data) => _.get(data, "artists.items.0")); @@ -92,6 +92,7 @@ // wait for a while before retrying await new Promise((resolve) => setTimeout(resolve, 1000)); } + // biome-ignore lint/correctness/noConstantCondition: true } while (true); // sleep for a while to avoid rate limiting diff --git a/apps/api/src/scripts/meili.ts b/apps/api/src/scripts/meili.ts --- a/apps/api/src/scripts/meili.ts +++ b/apps/api/src/scripts/meili.ts @@ -23,21 +23,26 @@ async function createAlbums() { const { meilisearch } = ctx; - const skip = 0; const size = 100; const total = await ctx.db .select({ value: count() }) .from(tables.albums) .execute() .then(([row]) => row.value); - const results = await ctx.db - .select() - .from(tables.albums) - .limit(size) - .offset(skip) - .execute(); + for (let i = 0; i < total; i += size) { + const skip = i; + console.log( + `Processing ${chalk.magentaBright("albums")}: ${chalk.magentaBright(skip)} to ${chalk.magentaBright(skip + size)}` + ); + const results = await ctx.db + .select() + .from(tables.albums) + .limit(size) + .offset(skip) + .execute(); - await meilisearch.post(`indexes/albums/documents?primaryKey=id`, results); + await meilisearch.post(`indexes/albums/documents?primaryKey=id`, results); + } } async function createArtists() { @@ -51,7 +56,7 @@ for (let i = 0; i < total; i += size) { const skip = i; console.log( - `Processing ${chalk.magentaBright("artists")}: ${chalk.magentaBright(skip)} to ${chalk.magentaBright(skip + size)}`, + `Processing ${chalk.magentaBright("artists")}: ${chalk.magentaBright(skip)} to ${chalk.magentaBright(skip + size)}` ); const results = await ctx.db .select() @@ -75,7 +80,7 @@ for (let i = 0; i < total; i += size) { const skip = i; console.log( - `Processing ${chalk.magentaBright("tracks")}: ${chalk.magentaBright(skip)} to ${chalk.magentaBright(skip + size)}`, + `Processing ${chalk.magentaBright("tracks")}: ${chalk.magentaBright(skip)} to ${chalk.magentaBright(skip + size)}` ); const results = await ctx.db .select() @@ -100,7 +105,7 @@ for (let i = 0; i < total; i += size) { const skip = i; console.log( - `Processing ${chalk.magentaBright("users")}: ${chalk.magentaBright(skip)} to ${chalk.magentaBright(skip + size)}`, + `Processing ${chalk.magentaBright("users")}: ${chalk.magentaBright(skip)} to ${chalk.magentaBright(skip + size)}` ); const results = await ctx.db .select() -- tangled.sh