const express = require('express'); const path = require('path'); const dbconnector = require('../database/db'); const Actions = require('./ActionSystem.js'); let UserConfig = { primaryKey: 'username', fields: [ { key: "username", type: "text" } ] } let Types = { Text: "text", Integer: "integer", Real: 3, Image: 4, Boolean: 5 } var knex = undefined; const Repositories = [] class RepositorySystem { constructor() { this.Types = Types; } create(table,config) { return new Repository(table, config); } setDB(db) { knex = dbconnector(db); return this; } } var PromiseChain = Promise.resolve(true); class Repository { constructor(table, config) { this.table = table; this.config = config; this.perPage = 10; this.checkIfExists(); } getPage(page = 1, perPage = this.perPage) { page = page > 0 ? page - 1 : 0; return Actions.emitSync('getTable' + this.table, knex.table(this.table)).select('*').limit(perPage).offset(page * perPage); } get(field, value){ return Actions.emitSync('getTable' + this.table, knex.table(this.table)).select('*').where(field, value); } insert(data){ return knex.table(this.table).insert(data); } batchInsert(data) { return knex.batchInsert(this.table, data); } update(id, data) { return knex.table(this.table).where('id',id).update(data); } delete(id) { return knex.table(this.table).where('id', id).del(); } count() { return knex.table(this.table).count('id as counter'); } columns() { return knex.table(this.table).columnInfo(); } raw() { return knex.table(this.table); } knex() { return knex; } checkIfExists() { if(!this.table)return; PromiseChain = PromiseChain.then(() => knex.schema.hasTable(this.table)) .then((exists)=> { if (!exists) { console.log("Table" , this.table, " creating..."); return knex.schema.createTable(this.table, (t) => { t.increments().primary(); }); } else { return true; } }) .then(() => { let config = this.config || {}; let fields = config.fields || []; let promises = []; for(let i in fields) { promises.push( knex.schema.hasColumn(this.table, fields[i].key) .then((exists) => { if(true) { // Implement full TO-DO TODO return knex.schema.alterTable(this.table, (t) => { let column = t[fields[i].type](fields[i].key, fields[i].keyLength); if(fields[i].default !== undefined) { column = column.defaultTo(fields[i].default); } if(fields[i].notNull) { column = column.notNullable(); } if(fields[i].unique === true) { column = column.unique(fields[i].key); } if(fields[i].fk && false) { column = column .unsigned().index().references(fields[i].fk.key).inTable(fields[i].fk.table); } if(exists) { column = column.alter(); } return column; }).catch((e) => { console.log("Column error change"); console.log(fields[i]); console.log("Error: ", e.sqlMessage); console.log("Command: ", e.sql); }); } return true; }) ); } if(config.auto_increment) { promises.push( knex.schema.raw('ALTER TABLE '+this.table+' AUTO_INCREMENT = ' + config.auto_increment) ); } return promises.reduce((promiseChain, currentTask) => { return promiseChain.then(chainResults => currentTask.then(currentResult => [ ...chainResults, currentResult ] ) ); }, Promise.resolve([])); }) .catch((e) => console.log("An error was detected", e)); return PromiseChain; } } module.exports = new RepositorySystem()