mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
WIP pass the failed tests.
This commit is contained in:
55
server/src/lib/KnexFactory/index.js
Normal file
55
server/src/lib/KnexFactory/index.js
Normal file
@@ -0,0 +1,55 @@
|
||||
const { extend, isFunction, isObject } = require('lodash');
|
||||
|
||||
export default class KnexFactory {
|
||||
|
||||
constructor(knex) {
|
||||
this.knex = knex;
|
||||
|
||||
this.factories = [];
|
||||
}
|
||||
|
||||
define(name, tableName, defaultAttributes) {
|
||||
this.factories[name] = { tableName, defaultAttributes };
|
||||
}
|
||||
|
||||
async build(factoryName, attributes) {
|
||||
const factory = this.factories[factoryName];
|
||||
|
||||
if (!factory) {
|
||||
throw `Unkown factory: ${factoryName}`;
|
||||
}
|
||||
let { defaultAttributes } = factory;
|
||||
const insertData = {};
|
||||
|
||||
if( 'function' === typeof defaultAttributes) {
|
||||
defaultAttributes = await defaultAttributes();
|
||||
}
|
||||
extend(insertData, defaultAttributes, attributes);
|
||||
|
||||
for (let k in insertData) {
|
||||
const v = insertData[k];
|
||||
|
||||
if (isFunction(v)) {
|
||||
insertData[k] = await v();
|
||||
} else {
|
||||
insertData[k] = await v;
|
||||
}
|
||||
if (isObject(insertData[k]) && insertData[k].id) {
|
||||
insertData[k] = insertData[k].id;
|
||||
}
|
||||
};
|
||||
|
||||
return insertData;
|
||||
}
|
||||
|
||||
async create(factoryName, attributes) {
|
||||
const factory = this.factories[factoryName];
|
||||
const insertData = await this.build(factoryName, attributes);
|
||||
const { tableName } = factory;
|
||||
|
||||
const [id] = await this.knex(tableName).insert(insertData);
|
||||
const record = await this.knex(tableName).where({ id }).first();
|
||||
|
||||
return record;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user