From 48d5e2e3c473ea7a346a742f04983d965a374845 Mon Sep 17 00:00:00 2001 From: C-3PO Date: Fri, 9 Nov 2018 02:45:36 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=92=20Prepare=20SQL=20statements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 13 ++++++------- src/interfaces/IDatabaseRow.ts | 4 ++-- src/model/model.ts | 33 +++++++++++++++++---------------- src/tslint.json | 7 ++++--- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8485c22..c242238 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,15 @@ -import { getSolidpkg, Product, getManifest } from 'ssn'; -import IProductData from "./interfaces/IProductData"; -import * as model from './model/model'; -import * as database from './model/database'; +import { getManifest, Product } from 'ssn'; +import IProductData from './interfaces/IProductData'; import writeLog from './logger/writeLog'; +import * as database from './model/database'; +import * as model from './model/model'; //create lock file to prevent conflicts? const newPatches: any[] = []; (async function() { - const productData: { [key in Product]: IProductData } = await model.init(); + const productData: { [key in Product]?: IProductData } = await model.init(); const productDataEntries = Object.entries(productData) as Array<[Product, IProductData]>; await Promise.all(productDataEntries.map(async ([product, { manifest, http}]) => { @@ -29,8 +29,7 @@ const newPatches: any[] = []; })); database.exit(); -}()) - +}()); /*//for each product, check new versions Object.entries(products).forEach(([product, { manifestVersion, httpVersion }]: [string, IDatabaseRow]) => { diff --git a/src/interfaces/IDatabaseRow.ts b/src/interfaces/IDatabaseRow.ts index 679a20a..6fa5255 100644 --- a/src/interfaces/IDatabaseRow.ts +++ b/src/interfaces/IDatabaseRow.ts @@ -1,5 +1,5 @@ -import { Product } from "ssn"; -import IProductData from "./IProductData"; +import { Product } from 'ssn'; +import IProductData from './IProductData'; export default interface IDatabaseRow extends IProductData { /** Name of the product. */ diff --git a/src/model/model.ts b/src/model/model.ts index 120ee97..a606c3f 100644 --- a/src/model/model.ts +++ b/src/model/model.ts @@ -1,18 +1,15 @@ +import * as mysql from 'mysql'; import { Product } from 'ssn'; -import * as database from './database'; -import products from './products'; -import writeLog from '../logger/writeLog'; -import createStatement from './createStatement'; import IDatabaseRow from '../interfaces/IDatabaseRow'; import IProductData from '../interfaces/IProductData'; +import writeLog from '../logger/writeLog'; +import createStatement from './createStatement'; +import * as database from './database'; +import products from './products'; -const state: any = {}; +export async function init(): Promise<{ [key in Product]?: IProductData }> { + const output: { [key in Product]?: IProductData } = {}; -function escapeProduct(product: Product) { - return String(product).replace(/'/g, '\\\''); -} - -export async function init(): Promise<{ [key in Product]: IProductData }> { await database.init(); //Only create table if it does not exist yet @@ -27,18 +24,22 @@ export async function init(): Promise<{ [key in Product]: IProductData }> { return; } - state[row.product] = { http: row.http, manifest: row.manifest }; + output[row.product] = { http: row.http, manifest: row.manifest }; }); //If row does not exist, INSERT into table - 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 ('${escapeProduct(product)}', '-1', '-1');`) + await Promise.all(products.filter((product) => output[product] === undefined).map(function(product) { + output[product] = { http: -1, manifest: -1 }; + const queryTemplate = `INSERT INTO \`ssn_versions\` (\`product\`, \`manifest\`, \`http\`) VALUES (?, '-1', '-1');`; + const query = mysql.format(queryTemplate, [String(product)]); + return database.query(query); })); - return state; + return output; } export function updateManifestVersion(product: Product, newVersion: number) { - return database.query(`UPDATE \`ssn_versions\` SET \`manifest\` = '${Number(newVersion)}' WHERE \`product\` = '${escapeProduct(product)}' COLLATE utf8mb4_bin;`); + const queryTemplate = `UPDATE \`ssn_versions\` SET \`manifest\` = ? WHERE \`product\` = ?;`; + const query = mysql.format(queryTemplate, [Number(newVersion), String(product)]); + return database.query(query); } diff --git a/src/tslint.json b/src/tslint.json index 188e8af..92558a1 100644 --- a/src/tslint.json +++ b/src/tslint.json @@ -1,7 +1,7 @@ { "defaultSeverity": "error", "extends": [ - "tslint:recommended", + "tslint:recommended" ], "jsRules": {}, "rules": { @@ -12,7 +12,8 @@ "no-console": false, "no-invalid-this": [true, "check-function-in-method"], "object-literal-sort-keys": [true, "match-declaration-order"], - "quotemark": [true, "single"], + "only-arrow-functions": false, + "quotemark": [true, "single"] }, - "rulesDirectory": [], + "rulesDirectory": [] }