import { random } from 'lodash'; import config from 'lib/Config'; import { Log } from 'lib/Log'; import { client, connect, getInstance } from 'lib/DB'; import * as Disqualification from 'lib/DB/tables/Disqualification/seed'; import * as Duplicate from 'lib/DB/tables/Duplicate/seed'; import * as DNC from 'lib/DB/tables/DNC/seed'; const log = Log; process.on('SIGINT', () => { process.exit(0); }); void (async () => { // NOTE: Only for local environments if (config.env !== 'development') return; await connect(); log.info('[DEV] Syncing DB definitions'); try { await client.sync({ force: process.env.DB_SYNC === 'true' }); log.info('Sync complete'); } catch (err) { log.error('Sync error', err); process.exit(1); } if (process.env.DB_SEED !== 'true') { process.exit(0); return; } // eslint-disable-next-line no-constant-condition while (true) { const DB = getInstance(); await new Promise((resolve) => setTimeout(resolve, 100)); const branch = Math.random(); let dnc: { id: number; createdAt: Date } | undefined; if (branch > 0.9) { const result = await DB.DNC.findAll({ order: client.random(), limit: 1, }); if (result.length > 0) { dnc = result[0]; } } if (!dnc) { if (branch > 0.2) { dnc = await DB.DNC.create(DNC.seedGlobal()); } else { dnc = await DB.DNC.create(DNC.seedBuyer()); } } await DB.Duplicate.bulkCreate(Array.from(new Array(random(0, 10)), () => Duplicate.seed(dnc!.id, dnc!.createdAt))); await DB.Disqualification.bulkCreate( Array.from(new Array(random(0, 10)), () => Disqualification.seed(dnc!.id, dnc!.createdAt)) ); } })();