🚀 Add TypeScript code

This commit is contained in:
C-3PO 2018-11-09 00:52:03 +01:00
parent 59f6ae0f11
commit 89faa530ec
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
17 changed files with 368 additions and 8 deletions

39
src/model/model.ts Normal file
View 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
}