Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as mysql from 'mysql';
- import { Sequelize } from 'sequelize';
- import { InterfacePackage, InterfacePoint } from './src/common';
- import Logger from './src/lib/logger';
- const log = Logger.factory('test');
- export interface InterfaceStorage {
- connect: (options: connectionConfig) => Promise<boolean | Error>;
- store: <T>(payload: IModel<T>) => Promise<boolean>;
- }
- interface IDefineAttributeColumnOptions {
- allowNull: boolean;
- type: string;
- }
- type DefineModelAttributes<T> = {
- [P in keyof T]: IDefineAttributeColumnOptions;
- };
- interface IModel<T> {
- name: string;
- entity: DefineModelAttributes<T>;
- }
- type connectionConfig = mysql.ConnectionConfig;
- const config: connectionConfig = {
- database: 'bots',
- host: 'localhost',
- password: 'password',
- port: 3306,
- user: 'admin',
- };
- class Storage implements InterfaceStorage {
- public static defineModel = <T>(name, entity: DefineModelAttributes<T>): IModel<T> => ({
- entity,
- name,
- })
- private connection: mysql.Connection;
- public store = <T>(payload: IModel<T>) => {
- return Promise.resolve(true);
- }
- public connect = async (options) => {
- this.connection = mysql.createConnection(options);
- return new Promise<boolean>((res, rej) => {
- this.connection.connect((err) => err ? rej(err) : res(true));
- });
- }
- public async sync(models: Array<IModel<any>>): Promise<any> {
- const promises = models.map(async (model) => this.createTable(model));
- return Promise.all(promises);
- }
- private createTable = async (model: IModel<any>): Promise<any> => {
- let query: string = '';
- query += `CREATE TABLE IF NOT EXISTS ${model.name} (`;
- Object.keys(model.entity).map((val, index, arr) => {
- query += `${val} ${model.entity[val].type} `;
- if (!model.entity[val].allowNull) {
- query += `not null `;
- }
- if (arr.length !== (index + 1)) {
- query += `, `;
- }
- });
- query += `)`;
- log('Executing %o', query);
- return new Promise((res, rej) =>
- this.connection.query(query, (err) =>
- err ? rej(err) : res(model.name)));
- }
- }
- const orm = new Storage();
- const defaultPackageOptions: DefineModelAttributes<InterfacePackage> = {
- cookie: {
- allowNull: false,
- type: 'VARCHAR(45)',
- },
- timestamp: {
- allowNull: false,
- type: 'TIMESTAMP',
- },
- type: {
- allowNull: false,
- type: 'VARCHAR(45)',
- },
- };
- const mouseMoveOptions: DefineModelAttributes<InterfacePoint> = {
- x: {
- allowNull: false,
- type: 'INTEGER',
- },
- y: {
- allowNull: false,
- type: 'INTEGER',
- },
- ...defaultPackageOptions,
- };
- const mouseClick = Storage.defineModel<InterfacePackage>('MOUSE_CLICK', defaultPackageOptions);
- const mouseMove = Storage.defineModel<InterfacePoint>('MOUSE_MOVE', mouseMoveOptions);
- (async () => {
- try {
- await orm.connect(config);
- log('Connected to database!');
- await orm.sync([mouseMove, mouseClick]);
- } catch (e) {
- log('Connection to databse error %o', e);
- }
- })();
Add Comment
Please, Sign In to add comment