mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-13 19:30:30 +00:00
add server to monorepo.
This commit is contained in:
48
packages/server/src/models/Model.ts
Normal file
48
packages/server/src/models/Model.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import { snakeCase, transform } from 'lodash';
|
||||
import { mapKeysDeep } from 'utils';
|
||||
import PaginationQueryBuilder from 'models/Pagination';
|
||||
import DateSession from 'models/DateSession';
|
||||
|
||||
export default class ModelBase extends mixin(Model, [DateSession]) {
|
||||
get timestamps() {
|
||||
return [];
|
||||
}
|
||||
|
||||
static get knexBinded() {
|
||||
return this.knexBindInstance;
|
||||
}
|
||||
|
||||
static set knexBinded(knex) {
|
||||
this.knexBindInstance = knex;
|
||||
}
|
||||
|
||||
static get collection() {
|
||||
return Array;
|
||||
}
|
||||
|
||||
static query(...args) {
|
||||
return super.query(...args).runAfter((result) => {
|
||||
if (Array.isArray(result)) {
|
||||
return this.collection.from(result);
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
static get QueryBuilder() {
|
||||
return PaginationQueryBuilder;
|
||||
}
|
||||
|
||||
static relationBindKnex(model) {
|
||||
return this.knexBinded ? model.bindKnex(this.knexBinded) : model;
|
||||
}
|
||||
|
||||
static changeAmount(whereAttributes, attribute, amount, trx) {
|
||||
const changeMethod = amount > 0 ? 'increment' : 'decrement';
|
||||
|
||||
return this.query(trx)
|
||||
.where(whereAttributes)
|
||||
[changeMethod](attribute, Math.abs(amount));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user