lionode
Version:
The Lionode Framework
55 lines (38 loc) • 994 B
JavaScript
const pluralize = require('pluralize');
const {snakeCase} = require('change-case');
const bookshelf = use('config/knexDatabase.js');
class Model {
constructor(){
this.tableName = null;
this.modelInstance = [];
return this.query();
}
getTableName(){
if(this.tableName === null){
this.tableName = pluralize(snakeCase(this.constructor.name));
}else{
this.tableName = pluralize(snakeCase(this.tableName));
}
return this;
}
query(){
this.getTableName().getRelationShips();
return bookshelf.model(this.constructor.name, {
tableName: this.tableName,
...this.modelInstance
});
}
getRelationShips(){
const data = Object.getOwnPropertyNames(this.constructor);
let originalName = []
data && data.map(d => {
originalName.push(d);
})
originalName.map(original => {
if(typeof this.constructor[original] == "function"){
this.modelInstance[[original]] = this.constructor[original];
}
})
}
}
module.exports = Model