ssn-auto/src/model/model.ts
2018-11-09 00:52:36 +01:00

39 lines
1.3 KiB
TypeScript

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
}