[feat] dashboard shows inventory
This commit is contained in:
33
scripts/diagnose-join.ts
Normal file
33
scripts/diagnose-join.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import 'dotenv/config';
|
||||
import chalk from 'chalk';
|
||||
import util from 'node:util';
|
||||
import { client } from '../src/db/typesense.ts';
|
||||
|
||||
const variants = [
|
||||
'$skus(*, $cards(*))',
|
||||
'$skus(*,$cards(*))',
|
||||
'$skus(*, card_id, $cards(*))',
|
||||
'$skus(*, $cards(*, strategy:nest))',
|
||||
'$skus(*, $cards(*, strategy:merge))',
|
||||
];
|
||||
|
||||
const debug = await client.debug.retrieve();
|
||||
console.log(chalk.cyan(`Typesense server version: ${debug.version}`));
|
||||
console.log();
|
||||
|
||||
for (const include of variants) {
|
||||
console.log(chalk.yellow(`include_fields: ${include}`));
|
||||
try {
|
||||
const res: any = await client.collections('inventories').documents().search({
|
||||
q: '*',
|
||||
query_by: 'content',
|
||||
per_page: 1,
|
||||
include_fields: include,
|
||||
});
|
||||
const doc = res.hits?.[0]?.document;
|
||||
console.log(util.inspect(doc, { depth: null, colors: false }));
|
||||
} catch (err: any) {
|
||||
console.log(chalk.red(` ERROR: ${err.message ?? err}`));
|
||||
}
|
||||
console.log();
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import type { DBInstance } from '../src/db/index.ts';
|
||||
import fs from "node:fs/promises";
|
||||
import { sql } from 'drizzle-orm'
|
||||
|
||||
import * as util from 'util';
|
||||
|
||||
|
||||
const DollarToInt = (dollar: any) => {
|
||||
if (dollar === null) return null;
|
||||
@@ -83,7 +85,7 @@ export const createSkuCollection = async () => {
|
||||
{ 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' },
|
||||
{ name: 'card_id', type: 'string', reference: 'cards.id', async_reference: true },
|
||||
]
|
||||
});
|
||||
console.log(chalk.green('Collection "skus" created successfully.'));
|
||||
@@ -102,7 +104,15 @@ export const createInventoryCollection = async () => {
|
||||
{ name: 'id', type: 'string' },
|
||||
{ name: 'userId', type: 'string' },
|
||||
{ name: 'catalogName', type: 'string' },
|
||||
{ name: 'sku_id', type: 'string', reference: 'skus.id' },
|
||||
{ name: 'card_id', type: 'string', reference: 'cards.id', async_reference: true },
|
||||
{ name: 'sku_id', type: 'string', reference: 'skus.id', async_reference: true },
|
||||
// content,setName,productLineName,rarityName,energyType,cardType from cards for searching
|
||||
{ name: 'content', type: 'string', token_separators: ['/'] },
|
||||
{ name: 'setName', type: 'string' },
|
||||
{ name: 'productLineName', type: 'string' },
|
||||
{ name: 'rarityName', type: 'string' },
|
||||
{ name: 'energyType', type: 'string' },
|
||||
{ name: 'cardType', type: 'string' },
|
||||
]
|
||||
});
|
||||
console.log(chalk.green('Collection "inventories" created successfully.'));
|
||||
@@ -148,17 +158,33 @@ export const upsertSkuCollection = async (db:DBInstance) => {
|
||||
lowestPrice: DollarToInt(sku.lowestPrice),
|
||||
marketPrice: DollarToInt(sku.marketPrice),
|
||||
card_id: sku.cardId.toString(),
|
||||
})), { action: 'upsert' });
|
||||
})), { action: 'upsert' });
|
||||
console.log(chalk.green('Collection "skus" indexed successfully.'));
|
||||
}
|
||||
|
||||
export const upsertInventoryCollection = async (db:DBInstance) => {
|
||||
const inv = await db.query.inventory.findMany();
|
||||
const inv = await db.query.inventory.findMany({
|
||||
with: { sku: { with: { card: { with: { set: true } } } } }
|
||||
});
|
||||
await client.collections('inventories').documents().import(inv.map(i => ({
|
||||
id: i.inventoryId,
|
||||
userId: i.userId,
|
||||
catalogName: i.catalogName,
|
||||
card_id: i.sku?.cardId.toString(),
|
||||
sku_id: i.skuId.toString(),
|
||||
productLineName: i.sku?.card?.productLineName,
|
||||
rarityName: i.sku?.card?.rarityName,
|
||||
setName: i.sku?.card?.set?.setName || "",
|
||||
cardType: i.sku?.card?.cardType || "",
|
||||
energyType: i.sku?.card?.energyType || "",
|
||||
content: [
|
||||
i.sku?.card?.productName,
|
||||
i.sku?.card?.productLineName,
|
||||
i.sku?.card?.set?.setName || "",
|
||||
i.sku?.card?.number,
|
||||
i.sku?.card?.rarityName,
|
||||
i.sku?.card?.artist || ""
|
||||
].join(' '),
|
||||
})), { action: 'upsert' });
|
||||
console.log(chalk.green('Collection "inventories" indexed successfully.'));
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@ import { db, ClosePool } from '../src/db/index.ts';
|
||||
import * as Indexing from './pokemon-helper.ts';
|
||||
|
||||
|
||||
//await Indexing.createCardCollection();
|
||||
//await Indexing.createSkuCollection();
|
||||
// await Indexing.createCardCollection();
|
||||
await Indexing.createSkuCollection();
|
||||
await Indexing.createInventoryCollection();
|
||||
|
||||
//await Indexing.upsertCardCollection(db);
|
||||
//await Indexing.upsertSkuCollection(db);
|
||||
// await Indexing.upsertCardCollection(db);
|
||||
await Indexing.upsertSkuCollection(db);
|
||||
await Indexing.upsertInventoryCollection(db);
|
||||
await ClosePool();
|
||||
console.log(chalk.green('Pokémon reindex complete.'));
|
||||
|
||||
Reference in New Issue
Block a user