✨ Update database queries
This commit is contained in:
parent
89faa530ec
commit
e9a58152d7
5 changed files with 28 additions and 56 deletions
|
@ -3,6 +3,9 @@ import * as database from './database';
|
|||
import products from './products';
|
||||
import writeLog from '../logger/writeLog';
|
||||
import createStatement from './createStatement';
|
||||
import IDatabaseRow from '../interfaces/IDatabaseRow';
|
||||
|
||||
const state: any = {};
|
||||
|
||||
export async function init() {
|
||||
await database.init();
|
||||
|
@ -11,29 +14,22 @@ export async function init() {
|
|||
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 }: { results: IDatabaseRow[]} = await database.query('SELECT * FROM ssn_versions');
|
||||
|
||||
const { results: existingRows } = await database.query('SELECT * FROM ssn_versions');
|
||||
console.log(existingRows);
|
||||
return;
|
||||
existingRows.forEach(function(row: IDatabaseRow) {
|
||||
if (!products.includes(row.product)) {
|
||||
writeLog(`Product ${row.product} in database table is invalid.`);
|
||||
return;
|
||||
}
|
||||
|
||||
existingRows.forEach((row: any) => {
|
||||
//TODO: match with products
|
||||
state[row.product] = { http: row.http, manifest: row.manifest };
|
||||
});
|
||||
|
||||
//If row does not exist, INSERT into table
|
||||
await Promise.all(missingProducts.map((product) => {
|
||||
return database.query(`INSERT INTO ssn_versions ${product}`);
|
||||
await Promise.all(products.filter((product) => state[product] === undefined).map(function(product) {
|
||||
state[product] = { http: -1, manifest: -1 };
|
||||
return database.query(`INSERT INTO \`ssn_versions\` (\`product\`, \`manifest\`, \`http\`) VALUES ('${product.replace(/'/g, '\\\'')}', '-1', '-1');`)
|
||||
}));
|
||||
|
||||
//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
|
||||
|
||||
return state;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue