🔒 Prepare SQL statements
This commit is contained in:
parent
334b52f4c6
commit
48d5e2e3c4
4 changed files with 29 additions and 28 deletions
13
src/index.ts
13
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]) => {
|
||||
|
|
|
@ -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. */
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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": []
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue