Guest User

Untitled

a guest
Oct 4th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. import * as mysql from 'mysql';
  2. import { Sequelize } from 'sequelize';
  3. import { InterfacePackage, InterfacePoint } from './src/common';
  4. import Logger from './src/lib/logger';
  5.  
  6. const log = Logger.factory('test');
  7.  
  8. export interface InterfaceStorage {
  9. connect: (options: connectionConfig) => Promise<boolean | Error>;
  10. store: <T>(payload: IModel<T>) => Promise<boolean>;
  11. }
  12.  
  13. interface IDefineAttributeColumnOptions {
  14. allowNull: boolean;
  15. type: string;
  16. }
  17.  
  18. type DefineModelAttributes<T> = {
  19. [P in keyof T]: IDefineAttributeColumnOptions;
  20. };
  21.  
  22. interface IModel<T> {
  23. name: string;
  24. entity: DefineModelAttributes<T>;
  25. }
  26.  
  27. type connectionConfig = mysql.ConnectionConfig;
  28.  
  29. const config: connectionConfig = {
  30. database: 'bots',
  31. host: 'localhost',
  32. password: 'password',
  33. port: 3306,
  34. user: 'admin',
  35. };
  36.  
  37. class Storage implements InterfaceStorage {
  38.  
  39. public static defineModel = <T>(name, entity: DefineModelAttributes<T>): IModel<T> => ({
  40. entity,
  41. name,
  42. })
  43.  
  44. private connection: mysql.Connection;
  45.  
  46. public store = <T>(payload: IModel<T>) => {
  47. return Promise.resolve(true);
  48. }
  49.  
  50. public connect = async (options) => {
  51. this.connection = mysql.createConnection(options);
  52. return new Promise<boolean>((res, rej) => {
  53. this.connection.connect((err) => err ? rej(err) : res(true));
  54. });
  55. }
  56.  
  57. public async sync(models: Array<IModel<any>>): Promise<any> {
  58. const promises = models.map(async (model) => this.createTable(model));
  59. return Promise.all(promises);
  60. }
  61.  
  62. private createTable = async (model: IModel<any>): Promise<any> => {
  63. let query: string = '';
  64. query += `CREATE TABLE IF NOT EXISTS ${model.name} (`;
  65.  
  66. Object.keys(model.entity).map((val, index, arr) => {
  67. query += `${val} ${model.entity[val].type} `;
  68.  
  69. if (!model.entity[val].allowNull) {
  70. query += `not null `;
  71. }
  72.  
  73. if (arr.length !== (index + 1)) {
  74. query += `, `;
  75. }
  76. });
  77.  
  78. query += `)`;
  79. log('Executing %o', query);
  80.  
  81. return new Promise((res, rej) =>
  82. this.connection.query(query, (err) =>
  83. err ? rej(err) : res(model.name)));
  84. }
  85.  
  86. }
  87.  
  88. const orm = new Storage();
  89.  
  90. const defaultPackageOptions: DefineModelAttributes<InterfacePackage> = {
  91. cookie: {
  92. allowNull: false,
  93. type: 'VARCHAR(45)',
  94. },
  95. timestamp: {
  96. allowNull: false,
  97. type: 'TIMESTAMP',
  98. },
  99. type: {
  100. allowNull: false,
  101. type: 'VARCHAR(45)',
  102. },
  103. };
  104.  
  105. const mouseMoveOptions: DefineModelAttributes<InterfacePoint> = {
  106. x: {
  107. allowNull: false,
  108. type: 'INTEGER',
  109. },
  110. y: {
  111. allowNull: false,
  112. type: 'INTEGER',
  113. },
  114. ...defaultPackageOptions,
  115. };
  116.  
  117. const mouseClick = Storage.defineModel<InterfacePackage>('MOUSE_CLICK', defaultPackageOptions);
  118. const mouseMove = Storage.defineModel<InterfacePoint>('MOUSE_MOVE', mouseMoveOptions);
  119.  
  120. (async () => {
  121. try {
  122. await orm.connect(config);
  123. log('Connected to database!');
  124. await orm.sync([mouseMove, mouseClick]);
  125. } catch (e) {
  126. log('Connection to databse error %o', e);
  127. }
  128. })();
Add Comment
Please, Sign In to add comment