[feat] card grid is completely driven from search
This commit is contained in:
@@ -4,11 +4,16 @@ import { db, poolConnection } from '../src/db/index.ts';
|
||||
import { client } from '../src/db/typesense.ts';
|
||||
import { release } from 'node:os';
|
||||
|
||||
const DollarToInt = (dollar: any) => {
|
||||
if (dollar === null) return null;
|
||||
return Math.round(dollar * 100);
|
||||
}
|
||||
|
||||
async function createCollection(client: Client) {
|
||||
// Delete the collection if it already exists to ensure a clean slate
|
||||
try {
|
||||
const response = await client.collections('cards').delete();
|
||||
await client.collections('cards').delete();
|
||||
await client.collections('skus').delete();
|
||||
//console.log(`Collection "cards" deleted successfully:`, response);
|
||||
} catch (error) {
|
||||
//console.error(`Error deleting collection "cards":`, error);
|
||||
@@ -23,6 +28,7 @@ async function createCollection(client: Client) {
|
||||
await client.collections().create({
|
||||
name: 'cards',
|
||||
fields: [
|
||||
{ name: 'id', type: 'string' },
|
||||
{ name: 'cardId', type: 'int32' },
|
||||
{ name: 'productId', type: 'int32' },
|
||||
{ name: 'variant', type: 'string', facet: true },
|
||||
@@ -36,6 +42,7 @@ async function createCollection(client: Client) {
|
||||
{ name: 'Artist', type: 'string' },
|
||||
{ name: 'sealed', type: 'bool' },
|
||||
{ name: 'releaseDate', type: 'int32'},
|
||||
{ name: 'sku_id', type: 'string[]', optional: true, reference: 'skus.id', async_reference: true }
|
||||
],
|
||||
//default_sorting_field: 'productId',
|
||||
});
|
||||
@@ -45,18 +52,38 @@ async function createCollection(client: Client) {
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await client.collections('skus').retrieve();
|
||||
console.log(chalk.yellow('Collection "skus" already exists.'));
|
||||
} catch(error) {
|
||||
if (error instanceof Error && error.message.includes('404')) {
|
||||
await client.collections().create({
|
||||
name: 'skus',
|
||||
fields: [
|
||||
{ name: 'id', type: 'string' },
|
||||
{ name: 'condition', type: 'string' },
|
||||
{ name: 'highestPrice', type: 'int32', optional: true },
|
||||
{ name: 'lowestPrice', type: 'int32', optional: true },
|
||||
{ name: 'marketPrice', type: 'int32', optional: true },
|
||||
//{ name: 'card_id', type: 'string', reference: 'cards.id' },
|
||||
]
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function preloadSearchIndex() {
|
||||
const pokemon = await db.query.cards.findMany({
|
||||
with: { set: true, tcgdata: true },
|
||||
with: { set: true, tcgdata: true, prices: true },
|
||||
});
|
||||
|
||||
// Ensure the collection exists before importing documents
|
||||
await createCollection(client);
|
||||
|
||||
await client.collections('cards').documents().import(pokemon.map(card => ({
|
||||
id: card.cardId.toString(),
|
||||
cardId: card.cardId,
|
||||
productId: card.productId,
|
||||
variant: card.variant,
|
||||
@@ -70,8 +97,22 @@ async function preloadSearchIndex() {
|
||||
Artist: card.Artist || "",
|
||||
sealed: card.sealed,
|
||||
releaseDate: card.tcgdata?.releaseDate ? Math.floor(new Date(card.tcgdata.releaseDate).getTime() / 1000) : 0,
|
||||
sku_id: card.prices.map(price => price.skuId.toString())
|
||||
})), { action: 'upsert' });
|
||||
|
||||
const skus = await db.query.skus.findMany({
|
||||
with: { card: true }
|
||||
});
|
||||
|
||||
await client.collections('skus').documents().import(skus.map(sku => ({
|
||||
id: sku.skuId.toString(),
|
||||
condition: sku.condition,
|
||||
highestPrice: DollarToInt(sku.highestPrice),
|
||||
lowestPrice: DollarToInt(sku.lowestPrice),
|
||||
marketPrice: DollarToInt(sku.marketPrice),
|
||||
//card_id: sku.card?.cardId.toString()
|
||||
})));
|
||||
|
||||
console.log(chalk.green('Search index preloaded with Pokémon cards.'));
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user