🚀 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

View file

@ -0,0 +1,9 @@
//Generated by `SHOW CREATE TABLE ssn_versions`
const createStatement = `CREATE TABLE \`ssn_versions\` (
\`product\` tinytext COLLATE utf8mb4_unicode_520_ci NOT NULL,
\`manifest\` smallint(6) NOT NULL DEFAULT '-1',
\`http\` smallint(6) NOT NULL DEFAULT '-1',
PRIMARY KEY (\`product\`(25))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci`;
export default createStatement;

30
src/model/database.ts Normal file
View file

@ -0,0 +1,30 @@
import * as mysql from 'mysql';
import * as dbSettings from '../../dbSettings';
const connection = mysql.createConnection(dbSettings);
export function init() {
return new Promise((resolve, reject) => {
connection.connect(function(err) {
if (err) {
reject(err);
}
resolve();
});
});
}
export function query(command: string): Promise<{ results: any, fields?: mysql.FieldInfo[] }> {
return new Promise((resolve, reject) => {
connection.query(command, function(error, results, fields) {
if (error !== null) {
reject(error);
}
resolve({ results, fields });
});
});
}
export function exit(): void {
connection.end();
}

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
}

27
src/model/products.ts Normal file
View file

@ -0,0 +1,27 @@
import { Product } from 'ssn';
const productList: Product[] = [
'assets_swtor_de_de',
'assets_swtor_en_us',
'assets_swtor_fr_fr',
'assets_swtor_main',
'assets_swtor_test_de_de',
'assets_swtor_test_en_us',
'assets_swtor_test_fr_fr',
'assets_swtor_test_main',
'eualas',
'movies_de_de',
'movies_en_us',
'movies_fr_fr',
'patcher2014',
'patcher2017',
'retailclient_betatest',
'retailclient_cstraining',
'retailclient_liveeptest',
'retailclient_liveqatest',
'retailclient_publictest',
'retailclient_squadron157',
'retailclient_swtor',
];
export default productList;