2026-02-17 12:05:56 -05:00
|
|
|
import { Client } from 'typesense';
|
|
|
|
|
import chalk from 'chalk';
|
|
|
|
|
import { db, poolConnection } from '../src/db/index.ts';
|
|
|
|
|
import { client } from '../src/db/typesense.ts';
|
2026-02-27 09:29:47 -05:00
|
|
|
import { release } from 'node:os';
|
2026-02-17 12:05:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async function createCollection(client: Client) {
|
2026-02-17 13:42:00 -05:00
|
|
|
// Delete the collection if it already exists to ensure a clean slate
|
|
|
|
|
try {
|
|
|
|
|
const response = await client.collections('cards').delete();
|
2026-02-19 16:04:34 -05:00
|
|
|
//console.log(`Collection "cards" deleted successfully:`, response);
|
2026-02-17 13:42:00 -05:00
|
|
|
} catch (error) {
|
2026-02-19 16:04:34 -05:00
|
|
|
//console.error(`Error deleting collection "cards":`, error);
|
2026-02-17 13:42:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the collection with the specified schema
|
2026-02-17 12:05:56 -05:00
|
|
|
try {
|
|
|
|
|
await client.collections('cards').retrieve();
|
|
|
|
|
console.log(chalk.yellow('Collection "cards" already exists.'));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (error instanceof Error && error.message.includes('404')) {
|
|
|
|
|
await client.collections().create({
|
|
|
|
|
name: 'cards',
|
|
|
|
|
fields: [
|
2026-02-27 09:29:47 -05:00
|
|
|
{ name: 'cardId', type: 'int32' },
|
2026-02-17 12:05:56 -05:00
|
|
|
{ name: 'productId', type: 'int32' },
|
2026-02-27 09:29:47 -05:00
|
|
|
{ name: 'variant', type: 'string', facet: true },
|
2026-02-27 15:34:06 -05:00
|
|
|
{ name: 'productName', type: 'string' },
|
2026-02-17 13:42:00 -05:00
|
|
|
{ name: 'productLineName', type: 'string', facet: true },
|
|
|
|
|
{ name: 'rarityName', type: 'string', facet: true },
|
|
|
|
|
{ name: 'setName', type: 'string', facet: true },
|
|
|
|
|
{ name: 'cardType', type: 'string', facet: true },
|
|
|
|
|
{ name: 'energyType', type: 'string', facet: true },
|
2026-02-27 15:34:06 -05:00
|
|
|
{ name: 'number', type: 'string', sort: true },
|
2026-02-19 16:04:34 -05:00
|
|
|
{ name: 'Artist', type: 'string' },
|
2026-02-22 11:00:30 -05:00
|
|
|
{ name: 'sealed', type: 'bool' },
|
2026-02-27 09:29:47 -05:00
|
|
|
{ name: 'releaseDate', type: 'int32'},
|
2026-02-17 12:05:56 -05:00
|
|
|
],
|
2026-02-23 19:05:55 -05:00
|
|
|
//default_sorting_field: 'productId',
|
2026-02-17 12:05:56 -05:00
|
|
|
});
|
|
|
|
|
console.log(chalk.green('Collection "cards" created successfully.'));
|
|
|
|
|
} else {
|
|
|
|
|
console.error(chalk.red('Error checking/creating collection:'), error);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function preloadSearchIndex() {
|
|
|
|
|
const pokemon = await db.query.cards.findMany({
|
2026-02-27 09:29:47 -05:00
|
|
|
with: { set: true, tcgdata: true },
|
2026-02-17 12:05:56 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Ensure the collection exists before importing documents
|
|
|
|
|
await createCollection(client);
|
|
|
|
|
|
|
|
|
|
await client.collections('cards').documents().import(pokemon.map(card => ({
|
2026-02-27 09:29:47 -05:00
|
|
|
cardId: card.cardId,
|
2026-02-17 12:05:56 -05:00
|
|
|
productId: card.productId,
|
2026-02-23 19:05:55 -05:00
|
|
|
variant: card.variant,
|
2026-02-17 12:05:56 -05:00
|
|
|
productName: card.productName,
|
|
|
|
|
productLineName: card.productLineName,
|
|
|
|
|
rarityName: card.rarityName,
|
|
|
|
|
setName: card.set?.setName || "",
|
|
|
|
|
cardType: card.cardType || "",
|
|
|
|
|
energyType: card.energyType || "",
|
|
|
|
|
number: card.number,
|
2026-02-19 16:04:34 -05:00
|
|
|
Artist: card.Artist || "",
|
2026-02-22 11:00:30 -05:00
|
|
|
sealed: card.sealed,
|
2026-02-27 09:29:47 -05:00
|
|
|
releaseDate: card.tcgdata?.releaseDate ? Math.floor(new Date(card.tcgdata.releaseDate).getTime() / 1000) : 0,
|
2026-02-17 12:05:56 -05:00
|
|
|
})), { action: 'upsert' });
|
|
|
|
|
|
|
|
|
|
console.log(chalk.green('Search index preloaded with Pokémon cards.'));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await preloadSearchIndex().catch((error) => {
|
|
|
|
|
console.error(chalk.red('Error preloading search index:'), error);
|
|
|
|
|
for (const e of error.importResults) {
|
|
|
|
|
if (!e.success) {
|
|
|
|
|
console.error(chalk.red(`Error importing document ${e.id}:`), e.error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}).finally(() => {
|
|
|
|
|
poolConnection.end();
|
|
|
|
|
console.log(chalk.blue('Database connection closed.'));
|
|
|
|
|
process.exit(0);
|
|
|
|
|
});
|