[feat] reindex added to admin

This commit is contained in:
2026-05-28 15:14:18 -04:00
parent ae0f3d6683
commit b0dbe7ced5
7 changed files with 648 additions and 472 deletions

View File

@@ -12,6 +12,9 @@ const DollarToInt = (dollar: any) => {
return Math.round(dollar * 100);
}
export type Logger = (msg: string) => void;
const defaultLogger: Logger = (msg) => console.log(chalk.green(msg));
export const Sleep = (ms: number) => {
@@ -39,7 +42,7 @@ export const GetNumberOrNull = (value: any): number | null => {
// Delete and recreate the 'cards' index
export const createCardCollection = async () => {
export const createCardCollection = async (log: Logger = defaultLogger) => {
try {
await client.collections('cards').delete();
} catch (error) {
@@ -68,11 +71,11 @@ export const createCardCollection = async () => {
// { name: 'sku_id', type: 'string[]', optional: true, reference: 'skus.id', async_reference: true }
],
});
console.log(chalk.green('Collection "cards" created successfully.'));
log('Collection "cards" created successfully.');
}
// Delete and recreate the 'skus' index
export const createSkuCollection = async () => {
export const createSkuCollection = async (log: Logger = defaultLogger) => {
try {
await client.collections('skus').delete();
} catch (error) {
@@ -89,11 +92,11 @@ export const createSkuCollection = async () => {
{ name: 'card_id', type: 'string', reference: 'cards.id', async_reference: true },
]
});
console.log(chalk.green('Collection "skus" created successfully.'));
log('Collection "skus" created successfully.');
}
// Delete and recreate the 'inventory' index
export const createInventoryCollection = async () => {
export const createInventoryCollection = async (log: Logger = defaultLogger) => {
try {
await client.collections('inventories').delete();
} catch (error) {
@@ -117,11 +120,11 @@ export const createInventoryCollection = async () => {
{ name: 'cardType', type: 'string' },
]
});
console.log(chalk.green('Collection "inventories" created successfully.'));
log('Collection "inventories" created successfully.');
}
export const upsertCardCollection = async (db:DBInstance) => {
export const upsertCardCollection = async (db:DBInstance, log: Logger = defaultLogger) => {
const pokemon = await db.query.cards.findMany({
with: { set: true, tcgdata: true, prices: true },
});
@@ -157,10 +160,10 @@ export const upsertCardCollection = async (db:DBInstance) => {
// sku_id: card.prices.map(price => price.skuId.toString())
};
}), { action: 'upsert' });
console.log(chalk.green('Collection "cards" indexed successfully.'));
log('Collection "cards" indexed successfully.');
}
export const upsertSkuCollection = async (db:DBInstance) => {
export const upsertSkuCollection = async (db:DBInstance, log: Logger = defaultLogger) => {
const skus = await db.query.skus.findMany();
await client.collections('skus').documents().import(skus.map(sku => ({
id: sku.skuId.toString(),
@@ -170,10 +173,10 @@ export const upsertSkuCollection = async (db:DBInstance) => {
marketPrice: DollarToInt(sku.marketPrice),
card_id: sku.cardId.toString(),
})), { action: 'upsert' });
console.log(chalk.green('Collection "skus" indexed successfully.'));
log('Collection "skus" indexed successfully.');
}
export const upsertInventoryCollection = async (db:DBInstance) => {
export const upsertInventoryCollection = async (db:DBInstance, log: Logger = defaultLogger) => {
const inv = await db.query.inventory.findMany({
with: { sku: { with: { card: { with: { set: true } } } } }
});
@@ -198,7 +201,7 @@ export const upsertInventoryCollection = async (db:DBInstance) => {
i.sku?.card?.artist || ""
].join(' '),
})), { action: 'upsert' });
console.log(chalk.green('Collection "inventories" indexed successfully.'));
log('Collection "inventories" indexed successfully.');
}