[chore] refactor indexing scripts
This commit is contained in:
@@ -3,16 +3,11 @@ import 'dotenv/config';
|
||||
import chalk from 'chalk';
|
||||
import { db, ClosePool } from '../src/db/index.ts';
|
||||
import { sql, inArray, eq } from 'drizzle-orm';
|
||||
import { skus, processingSkus } from '../src/db/schema.ts';
|
||||
import { client } from '../src/db/typesense.ts';
|
||||
import { skus, processingSkus, priceHistory } from '../src/db/schema.ts';
|
||||
import { toSnakeCase } from 'drizzle-orm/casing';
|
||||
import * as Indexing from './indexing.ts';
|
||||
|
||||
|
||||
const DollarToInt = (dollar: any) => {
|
||||
if (dollar === null) return null;
|
||||
return Math.round(dollar * 100);
|
||||
}
|
||||
|
||||
function sleep(ms: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
@@ -73,7 +68,7 @@ async function syncPrices() {
|
||||
marketPrice: sku.marketPrice,
|
||||
priceCount: null,
|
||||
}});
|
||||
await db.insert(skus).values(skuUpdates).onConflictDoUpdate({
|
||||
const skuRows = await db.insert(skus).values(skuUpdates).onConflictDoUpdate({
|
||||
target: skus.skuId,
|
||||
set: {
|
||||
calculatedAt: sql.raw(`excluded.${toSnakeCase(skus.calculatedAt.name)}`),
|
||||
@@ -92,22 +87,10 @@ async function syncPrices() {
|
||||
|
||||
}
|
||||
|
||||
async function indexPrices() {
|
||||
const skus = await db.query.skus.findMany();
|
||||
|
||||
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),
|
||||
})), { action: 'upsert' });
|
||||
|
||||
}
|
||||
|
||||
const start = Date.now();
|
||||
await syncPrices();
|
||||
await indexPrices();
|
||||
await Indexing.upsertSkuCollection(db);
|
||||
await ClosePool();
|
||||
const end = Date.now();
|
||||
const duration = (end - start) / 1000;
|
||||
|
||||
Reference in New Issue
Block a user