RepositorySystem.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. const express = require('express');
  2. const path = require('path');
  3. const dbconnector = require('../database/db');
  4. const Actions = require('./ActionSystem.js');
  5. let UserConfig = {
  6. primaryKey: 'username',
  7. fields: [
  8. {
  9. key: "username",
  10. type: "text"
  11. }
  12. ]
  13. }
  14. let Types = {
  15. Text: "text",
  16. Integer: "integer",
  17. Real: 3,
  18. Image: 4,
  19. Boolean: 5
  20. }
  21. var knex = undefined;
  22. const Repositories = []
  23. class RepositorySystem {
  24. constructor() {
  25. this.Types = Types;
  26. }
  27. create(table,config) {
  28. return new Repository(table, config);
  29. }
  30. setDB(db) {
  31. knex = dbconnector(db);
  32. return this;
  33. }
  34. }
  35. var PromiseChain = Promise.resolve(true);
  36. class Repository {
  37. constructor(table, config) {
  38. this.table = table;
  39. this.config = config;
  40. this.perPage = 10;
  41. this.checkIfExists();
  42. }
  43. getPage(page = 1, perPage = this.perPage) {
  44. page = page > 0 ? page - 1 : 0;
  45. return Actions.emitSync('getTable' + this.table, knex.table(this.table)).select('*').limit(perPage).offset(page * perPage);
  46. }
  47. get(field, value){
  48. return Actions.emitSync('getTable' + this.table, knex.table(this.table)).select('*').where(field, value);
  49. }
  50. insert(data){
  51. return knex.table(this.table).insert(data);
  52. }
  53. batchInsert(data) {
  54. return knex.batchInsert(this.table, data);
  55. }
  56. update(id, data) {
  57. return knex.table(this.table).where('id',id).update(data);
  58. }
  59. delete(id) {
  60. return knex.table(this.table).where('id', id).del();
  61. }
  62. count() {
  63. return knex.table(this.table).count('id as counter');
  64. }
  65. columns() {
  66. return knex.table(this.table).columnInfo();
  67. }
  68. raw() {
  69. return knex.table(this.table);
  70. }
  71. knex() {
  72. return knex;
  73. }
  74. checkIfExists() {
  75. if(!this.table)return;
  76. PromiseChain = PromiseChain.then(() => knex.schema.hasTable(this.table))
  77. .then((exists)=> {
  78. if (!exists) {
  79. console.log("Table" , this.table, " creating...");
  80. return knex.schema.createTable(this.table, (t) => {
  81. t.increments().primary();
  82. });
  83. } else {
  84. return true;
  85. }
  86. })
  87. .then(() => {
  88. let config = this.config || {};
  89. let fields = config.fields || [];
  90. let promises = [];
  91. for(let i in fields) {
  92. promises.push(
  93. knex.schema.hasColumn(this.table, fields[i].key)
  94. .then((exists) => {
  95. if(true) {
  96. // Implement full TO-DO TODO
  97. return knex.schema.alterTable(this.table,
  98. (t) => {
  99. let column = t[fields[i].type](fields[i].key, fields[i].keyLength);
  100. if(fields[i].default !== undefined) {
  101. column = column.defaultTo(fields[i].default);
  102. }
  103. if(fields[i].notNull) {
  104. column = column.notNullable();
  105. }
  106. if(fields[i].unique === true) {
  107. column = column.unique(fields[i].key);
  108. }
  109. if(fields[i].fk && false) {
  110. column = column
  111. .unsigned().index().references(fields[i].fk.key).inTable(fields[i].fk.table);
  112. }
  113. if(exists) {
  114. column = column.alter();
  115. }
  116. return column;
  117. }).catch((e) => {
  118. console.log("Column error change");
  119. console.log(fields[i]);
  120. console.log("Error: ", e.sqlMessage);
  121. console.log("Command: ", e.sql);
  122. });
  123. }
  124. return true;
  125. })
  126. );
  127. }
  128. if(config.auto_increment) {
  129. promises.push(
  130. knex.schema.raw('ALTER TABLE '+this.table+' AUTO_INCREMENT = ' + config.auto_increment)
  131. );
  132. }
  133. return promises.reduce((promiseChain, currentTask) => {
  134. return promiseChain.then(chainResults =>
  135. currentTask.then(currentResult =>
  136. [ ...chainResults, currentResult ]
  137. )
  138. );
  139. }, Promise.resolve([]));
  140. })
  141. .catch((e) => console.log("An error was detected", e));
  142. return PromiseChain;
  143. }
  144. }
  145. module.exports = new RepositorySystem()