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