🚀 Add TypeScript code
This commit is contained in:
parent
59f6ae0f11
commit
89faa530ec
17 changed files with 368 additions and 8 deletions
39
src/model/model.ts
Normal file
39
src/model/model.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
import { Product } from 'ssn';
|
||||
import * as database from './database';
|
||||
import products from './products';
|
||||
import writeLog from '../logger/writeLog';
|
||||
import createStatement from './createStatement';
|
||||
|
||||
export async function init() {
|
||||
await database.init();
|
||||
|
||||
//Only create table if it does not exist yet
|
||||
const createIfNotExists = createStatement.replace(/^CREATE TABLE/, 'CREATE TABLE IF NOT EXISTS');
|
||||
await database.query(createIfNotExists);
|
||||
|
||||
/**Products that are missing from the database and need to be inserted. */
|
||||
const missingProducts: Product[] = [];
|
||||
/** Products inside the database table that are not valid products. Will be printed to error log and ignored */
|
||||
const erroneousProducts: Product[] = [];
|
||||
|
||||
const { results: existingRows } = await database.query('SELECT * FROM ssn_versions');
|
||||
console.log(existingRows);
|
||||
return;
|
||||
|
||||
existingRows.forEach((row: any) => {
|
||||
//TODO: match with products
|
||||
});
|
||||
|
||||
//If row does not exist, INSERT into table
|
||||
await Promise.all(missingProducts.map((product) => {
|
||||
return database.query(`INSERT INTO ssn_versions ${product}`);
|
||||
}));
|
||||
|
||||
//If row exists that is not in products, ignore it and output error
|
||||
erroneousProducts.forEach((product) => {
|
||||
writeLog(`Product ${product} in database table is invalid.`);
|
||||
});
|
||||
|
||||
//TODO: insert products
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue