Merge branch 'master' of papi.tkpups.com:tmiller/pokemon

This commit is contained in:
zach
2026-02-27 15:21:28 -05:00
4 changed files with 22 additions and 8 deletions

View File

@@ -2,6 +2,7 @@ import { Client } from 'typesense';
import chalk from 'chalk'; import chalk from 'chalk';
import { db, poolConnection } from '../src/db/index.ts'; import { db, poolConnection } from '../src/db/index.ts';
import { client } from '../src/db/typesense.ts'; import { client } from '../src/db/typesense.ts';
import { release } from 'node:os';
async function createCollection(client: Client) { async function createCollection(client: Client) {
@@ -22,9 +23,10 @@ async function createCollection(client: Client) {
await client.collections().create({ await client.collections().create({
name: 'cards', name: 'cards',
fields: [ fields: [
{ name: 'cardId', type: 'int32' },
{ name: 'productId', type: 'int32' }, { name: 'productId', type: 'int32' },
{ name: 'variant', type: 'string' }, { name: 'variant', type: 'string', facet: true },
{ name: 'productName', type: 'string' }, { name: 'productName', type: 'string', sort: true },
{ name: 'productLineName', type: 'string', facet: true }, { name: 'productLineName', type: 'string', facet: true },
{ name: 'rarityName', type: 'string', facet: true }, { name: 'rarityName', type: 'string', facet: true },
{ name: 'setName', type: 'string', facet: true }, { name: 'setName', type: 'string', facet: true },
@@ -33,6 +35,7 @@ async function createCollection(client: Client) {
{ name: 'number', type: 'string' }, { name: 'number', type: 'string' },
{ name: 'Artist', type: 'string' }, { name: 'Artist', type: 'string' },
{ name: 'sealed', type: 'bool' }, { name: 'sealed', type: 'bool' },
{ name: 'releaseDate', type: 'int32'},
], ],
//default_sorting_field: 'productId', //default_sorting_field: 'productId',
}); });
@@ -47,13 +50,14 @@ async function createCollection(client: Client) {
async function preloadSearchIndex() { async function preloadSearchIndex() {
const pokemon = await db.query.cards.findMany({ const pokemon = await db.query.cards.findMany({
with: { set: true, } with: { set: true, tcgdata: true },
}); });
// Ensure the collection exists before importing documents // Ensure the collection exists before importing documents
await createCollection(client); await createCollection(client);
await client.collections('cards').documents().import(pokemon.map(card => ({ await client.collections('cards').documents().import(pokemon.map(card => ({
cardId: card.cardId,
productId: card.productId, productId: card.productId,
variant: card.variant, variant: card.variant,
productName: card.productName, productName: card.productName,
@@ -65,6 +69,7 @@ async function preloadSearchIndex() {
number: card.number, number: card.number,
Artist: card.Artist || "", Artist: card.Artist || "",
sealed: card.sealed, sealed: card.sealed,
releaseDate: card.tcgdata?.releaseDate ? Math.floor(new Date(card.tcgdata.releaseDate).getTime() / 1000) : 0,
})), { action: 'upsert' }); })), { action: 'upsert' });
console.log(chalk.green('Search index preloaded with Pokémon cards.')); console.log(chalk.green('Search index preloaded with Pokémon cards.'));

View File

@@ -53,8 +53,8 @@ async function syncPrices() {
await db.update(skus) await db.update(skus)
.set({ .set({
marketPrice: sku.marketPrice, marketPrice: sku.marketPrice,
lowestPrice: sku.lowPrice, lowestPrice: sku.lowestPrice,
highestPrice: sku.highPrice, highestPrice: sku.highestPrice,
priceCount: sku.priceCount, priceCount: sku.priceCount,
calculatedAt: sku.calculatedAt ? new Date(sku.calculatedAt) : null, calculatedAt: sku.calculatedAt ? new Date(sku.calculatedAt) : null,
}) })

View File

@@ -14,9 +14,16 @@ export const relations = defineRelations(schema, (r) => ({
from: r.cards.setId, from: r.cards.setId,
to: r.sets.setId, to: r.sets.setId,
}), }),
tcgdata: r.one.tcgcards({
from: r.cards.productId,
to: r.tcgcards.productId,
}),
}, },
sets: { sets: {
cards: r.many.cards(), cards: r.many.cards(),
}, },
tcgcards: {
cards: r.many.cards(),
},
})); }));

View File

@@ -33,16 +33,17 @@ const searchResults = await client.collections('cards').documents().search({
filter_by: `sealed:false${filterBy ? ` && ${filterBy}` : ''}`, filter_by: `sealed:false${filterBy ? ` && ${filterBy}` : ''}`,
query_by: 'productLineName,productName,setName,number,rarityName,Artist', query_by: 'productLineName,productName,setName,number,rarityName,Artist',
per_page: 20, per_page: 20,
facet_by: 'productLineName,setName,rarityName,cardType,energyType', facet_by: 'productLineName,setName,variant,rarityName,cardType,energyType',
page: Math.floor(start / 20) + 1, page: Math.floor(start / 20) + 1,
sort_by: '_text_match:asc, releaseDate:desc, productName:asc',
}); });
const productIds = searchResults.hits?.map((hit: any) => hit.document.productId) ?? []; const cardIds = searchResults.hits?.map((hit: any) => hit.document.cardId) ?? [];
const totalHits = searchResults.found; const totalHits = searchResults.found;
const facets = searchResults.facet_counts; const facets = searchResults.facet_counts;
// get pokemon data with prices and set info using searchResults and then query the database for each card to get the prices and set info // get pokemon data with prices and set info using searchResults and then query the database for each card to get the prices and set info
const pokemon = await db.query.cards.findMany({ const pokemon = await db.query.cards.findMany({
where: { productId: { in: productIds, }, }, where: { cardId: { in: cardIds, }, },
with: { with: {
prices: true, prices: true,
set: true, set: true,
@@ -74,6 +75,7 @@ const facetNames = (name:string) => {
return { return {
"productLineName": "Product Line", "productLineName": "Product Line",
"setName": "Set", "setName": "Set",
"variant": "Variant",
"rarityName": "Rarity", "rarityName": "Rarity",
"cardType": "Card Type", "cardType": "Card Type",
"energyType": "Energy Type" "energyType": "Energy Type"