mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
WIP server side.
This commit is contained in:
@@ -1,34 +1,81 @@
|
||||
import bookshelf from './bookshelf';
|
||||
/* eslint-disable global-require */
|
||||
import { Model } from 'objection';
|
||||
import { flatten } from 'lodash';
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
const Account = bookshelf.Model.extend({
|
||||
export default class Account extends BaseModel {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'accounts',
|
||||
static get tableName() {
|
||||
return 'accounts';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
* Model modifiers.
|
||||
*/
|
||||
hasTimestamps: ['created_at', 'updated_at'],
|
||||
static get modifiers() {
|
||||
return {
|
||||
filterAccountTypes(query, typesIds) {
|
||||
if (typesIds.length > 0) {
|
||||
query.whereIn('accoun_type_id', typesIds);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Account model may belongs to account type.
|
||||
* Relationship mapping.
|
||||
*/
|
||||
type() {
|
||||
return this.belongsTo('AccountType', 'account_type_id');
|
||||
},
|
||||
static get relationMappings() {
|
||||
const AccountType = require('@/models/AccountType');
|
||||
const AccountBalance = require('@/models/AccountBalance');
|
||||
const AccountTransaction = require('@/models/AccountTransaction');
|
||||
|
||||
/**
|
||||
* Account model may has many balances accounts.
|
||||
*/
|
||||
balances() {
|
||||
return this.hasMany('AccountBalance', 'account_id');
|
||||
},
|
||||
}, {
|
||||
/**
|
||||
* Cascade delete dependents.
|
||||
*/
|
||||
dependents: ['balances'],
|
||||
});
|
||||
return {
|
||||
/**
|
||||
* Account model may belongs to account type.
|
||||
*/
|
||||
type: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: AccountType.default,
|
||||
join: {
|
||||
from: 'accounts.accountTypeId',
|
||||
to: 'account_types.id',
|
||||
},
|
||||
},
|
||||
|
||||
export default bookshelf.model('Account', Account);
|
||||
/**
|
||||
* Account model may has many balances accounts.
|
||||
*/
|
||||
balance: {
|
||||
relation: Model.HasOneRelation,
|
||||
modelClass: AccountBalance.default,
|
||||
join: {
|
||||
from: 'accounts.id',
|
||||
to: 'account_balances.accountId',
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* Account model may has many transactions.
|
||||
*/
|
||||
transactions: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: AccountTransaction.default,
|
||||
join: {
|
||||
from: 'accounts.id',
|
||||
to: 'accounts_transactions.accountId',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
static collectJournalEntries(accounts) {
|
||||
return flatten(accounts.map((account) => account.transactions.map((transaction) => ({
|
||||
accountId: account.id,
|
||||
...transaction,
|
||||
accountNormal: account.type.normal,
|
||||
}))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,29 @@
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const AccountBalance = bookshelf.Model.extend({
|
||||
import { Model } from 'objection';
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
export default class AccountBalance extends BaseModel {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'account_balance',
|
||||
static get tableName() {
|
||||
return 'account_balances';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
* Relationship mapping.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
static get relationMappings() {
|
||||
const Account = require('@/models/Account');
|
||||
|
||||
|
||||
account() {
|
||||
return this.belongsTo('Account', 'account_id');
|
||||
},
|
||||
});
|
||||
|
||||
export default bookshelf.model('AccountBalance', AccountBalance);
|
||||
return {
|
||||
account: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Account.default,
|
||||
join: {
|
||||
from: 'account_balance.account_id',
|
||||
to: 'accounts.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
81
server/src/models/AccountTransaction.js
Normal file
81
server/src/models/AccountTransaction.js
Normal file
@@ -0,0 +1,81 @@
|
||||
import { Model } from 'objection';
|
||||
import moment from 'moment';
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
export default class AccountTransaction extends BaseModel {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'accounts_transactions';
|
||||
}
|
||||
|
||||
/**
|
||||
* Model modifiers.
|
||||
*/
|
||||
static get modifiers() {
|
||||
return {
|
||||
filterAccounts(query, accountsIds) {
|
||||
if (accountsIds.length > 0) {
|
||||
query.whereIn('account_id', accountsIds);
|
||||
}
|
||||
},
|
||||
filterTransactionTypes(query, types) {
|
||||
if (Array.isArray(types) && types.length > 0) {
|
||||
query.whereIn('reference_type', types);
|
||||
} else if (typeof types === 'string') {
|
||||
query.where('reference_type', types);
|
||||
}
|
||||
},
|
||||
filterDateRange(query, startDate, endDate, type = 'day') {
|
||||
const dateFormat = 'YYYY-MM-DD HH:mm:ss';
|
||||
const fromDate = moment(startDate).startOf(type).format(dateFormat);
|
||||
const toDate = moment(endDate).endOf(type).format(dateFormat);
|
||||
|
||||
if (startDate) {
|
||||
query.where('date', '>=', fromDate);
|
||||
}
|
||||
if (endDate) {
|
||||
query.where('date', '<=', toDate);
|
||||
}
|
||||
},
|
||||
filterAmountRange(query, fromAmount, toAmount) {
|
||||
if (fromAmount) {
|
||||
query.andWhere((q) => {
|
||||
q.where('credit', '>=', fromAmount);
|
||||
q.orWhere('debit', '>=', fromAmount);
|
||||
});
|
||||
}
|
||||
if (toAmount) {
|
||||
query.andWhere((q) => {
|
||||
q.where('credit', '<=', toAmount);
|
||||
q.orWhere('debit', '<=', toAmount);
|
||||
});
|
||||
}
|
||||
},
|
||||
sumationCreditDebit(query) {
|
||||
query.sum('credit as credit');
|
||||
query.sum('debit as debit');
|
||||
query.groupBy('account_id');
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const Account = require('@/models/Account');
|
||||
|
||||
return {
|
||||
account: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Account.default,
|
||||
join: {
|
||||
from: 'accounts_transactions.accountId',
|
||||
to: 'accounts.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,33 @@
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const AccountType = bookshelf.Model.extend({
|
||||
// import path from 'path';
|
||||
import { Model } from 'objection';
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
export default class AccountType extends BaseModel {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'accounts',
|
||||
static get tableName() {
|
||||
return 'account_types';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
* Relationship mapping.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
static get relationMappings() {
|
||||
const Account = require('@/models/Account');
|
||||
|
||||
/**
|
||||
* Account type may has many associated accounts.
|
||||
*/
|
||||
accounts() {
|
||||
return this.hasMany('Account', 'account_type_id');
|
||||
},
|
||||
});
|
||||
|
||||
export default bookshelf.model('AccountType', AccountType);
|
||||
return {
|
||||
/**
|
||||
* Account type may has many associated accounts.
|
||||
*/
|
||||
accounts: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: Account.default,
|
||||
join: {
|
||||
from: 'account_types.id',
|
||||
to: 'accounts.accountTypeId',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
60
server/src/models/Budget.js
Normal file
60
server/src/models/Budget.js
Normal file
@@ -0,0 +1,60 @@
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
export default class Budget extends BaseModel {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'budgets';
|
||||
}
|
||||
|
||||
static get virtualAttributes() {
|
||||
return ['rangeBy', 'rangeIncrement'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Model modifiers.
|
||||
*/
|
||||
static get modifiers() {
|
||||
return {
|
||||
filterByYear(query, year) {
|
||||
query.where('year', year);
|
||||
},
|
||||
filterByIncomeStatement(query) {
|
||||
query.where('account_types', 'income_statement');
|
||||
},
|
||||
filterByProfitLoss(query) {
|
||||
query.where('accounts_types', 'profit_loss');
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
get rangeBy() {
|
||||
switch (this.period) {
|
||||
case 'half-year':
|
||||
case 'quarter':
|
||||
return 'month';
|
||||
default:
|
||||
return this.period;
|
||||
}
|
||||
}
|
||||
|
||||
get rangeIncrement() {
|
||||
switch (this.period) {
|
||||
case 'half-year':
|
||||
return 6;
|
||||
case 'quarter':
|
||||
return 3;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
get rangeOffset() {
|
||||
switch (this.period) {
|
||||
case 'half-year': return 5;
|
||||
case 'quarter': return 2;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
10
server/src/models/BudgetEntry.js
Normal file
10
server/src/models/BudgetEntry.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
export default class Budget extends BaseModel {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'budget_entries';
|
||||
}
|
||||
}
|
||||
86
server/src/models/Expense.js
Normal file
86
server/src/models/Expense.js
Normal file
@@ -0,0 +1,86 @@
|
||||
import { Model } from 'objection';
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
export default class Expense extends BaseModel {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'expenses';
|
||||
}
|
||||
|
||||
static get referenceType() {
|
||||
return 'Expense';
|
||||
}
|
||||
|
||||
/**
|
||||
* Model modifiers.
|
||||
*/
|
||||
static get modifiers() {
|
||||
return {
|
||||
filterByDateRange(query, startDate, endDate) {
|
||||
if (startDate) {
|
||||
query.where('date', '>=', startDate);
|
||||
}
|
||||
if (endDate) {
|
||||
query.where('date', '<=', endDate);
|
||||
}
|
||||
},
|
||||
filterByAmountRange(query, from, to) {
|
||||
if (from) {
|
||||
query.where('amount', '>=', from);
|
||||
}
|
||||
if (to) {
|
||||
query.where('amount', '<=', to);
|
||||
}
|
||||
},
|
||||
filterByExpenseAccount(query, accountId) {
|
||||
if (accountId) {
|
||||
query.where('expense_account_id', accountId);
|
||||
}
|
||||
},
|
||||
filterByPaymentAccount(query, accountId) {
|
||||
if (accountId) {
|
||||
query.where('payment_account_id', accountId);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const Account = require('@/models/Account');
|
||||
const User = require('@/models/User');
|
||||
|
||||
return {
|
||||
paymentAccount: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Account.default,
|
||||
join: {
|
||||
from: 'expenses.paymentAccountId',
|
||||
to: 'accounts.id',
|
||||
},
|
||||
},
|
||||
|
||||
expenseAccount: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Account.default,
|
||||
join: {
|
||||
from: 'expenses.expenseAccountId',
|
||||
to: 'accounts.id',
|
||||
},
|
||||
},
|
||||
|
||||
user: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: User.default,
|
||||
join: {
|
||||
from: 'expenses.userId',
|
||||
to: 'users.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,34 +1,43 @@
|
||||
import bookshelf from './bookshelf';
|
||||
import { Model } from 'objection';
|
||||
import path from 'path';
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
const Item = bookshelf.Model.extend({
|
||||
export default class Item extends BaseModel {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'items',
|
||||
static get tableName() {
|
||||
return 'items';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
* Relationship mapping.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
static get relationMappings() {
|
||||
return {
|
||||
/**
|
||||
* Item may has many meta data.
|
||||
*/
|
||||
metadata: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelBase: path.join(__dirname, 'ItemMetadata'),
|
||||
join: {
|
||||
from: 'items.id',
|
||||
to: 'items_metadata.item_id',
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* Item may has many meta data.
|
||||
*/
|
||||
metadata() {
|
||||
return this.hasMany('ItemMetadata', 'item_id');
|
||||
},
|
||||
|
||||
/**
|
||||
* Item may belongs to the item category.
|
||||
*/
|
||||
category() {
|
||||
return this.belongsTo('ItemCategory', 'category_id');
|
||||
},
|
||||
}, {
|
||||
/**
|
||||
* Cascade delete dependents.
|
||||
*/
|
||||
dependents: ['ItemMetadata'],
|
||||
});
|
||||
|
||||
export default bookshelf.model('Item', Item);
|
||||
/**
|
||||
* Item may belongs to cateogory model.
|
||||
*/
|
||||
category: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelBase: path.join(__dirname, 'ItemCategory'),
|
||||
join: {
|
||||
from: 'items.categoryId',
|
||||
to: 'items_categories.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,31 @@
|
||||
import bookshelf from './bookshelf';
|
||||
import path from 'path';
|
||||
import { Model } from 'objection';
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
const ItemCategory = bookshelf.Model.extend({
|
||||
export default class ItemCategory extends BaseModel {
|
||||
/**
|
||||
* Table name.
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'items_categories';
|
||||
}
|
||||
|
||||
/**
|
||||
* Table name
|
||||
* Relationship mapping.
|
||||
*/
|
||||
tableName: 'items_categories',
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: ['created_at', 'updated_at'],
|
||||
|
||||
/**
|
||||
* Item category may has many items.
|
||||
*/
|
||||
items() {
|
||||
return this.hasMany('Item', 'category_id');
|
||||
},
|
||||
});
|
||||
|
||||
export default bookshelf.model('ItemCategory', ItemCategory);
|
||||
static get relationMappings() {
|
||||
return {
|
||||
/**
|
||||
* Item category may has many items.
|
||||
*/
|
||||
items: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelBase: path.join(__dirname, 'Item'),
|
||||
join: {
|
||||
from: 'items_categories.item_id',
|
||||
to: 'items.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,38 @@
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const ItemMetadata = bookshelf.Model.extend({
|
||||
import path from 'path';
|
||||
import { Model } from 'objection';
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
export default class ItemMetadata extends BaseModel {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'items_metadata',
|
||||
static get tableName() {
|
||||
return 'items_metadata';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: ['created_at', 'updated_at'],
|
||||
static get hasTimestamps() {
|
||||
return ['created_at', 'updated_at'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Item category may has many items.
|
||||
* Relationship mapping.
|
||||
*/
|
||||
items() {
|
||||
return this.belongsTo('Item', 'item_id');
|
||||
},
|
||||
});
|
||||
|
||||
export default bookshelf.model('ItemMetadata', ItemMetadata);
|
||||
static get relationMappings() {
|
||||
return {
|
||||
/**
|
||||
* Item category may has many items.
|
||||
*/
|
||||
items: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelBase: path.join(__dirname, 'Item'),
|
||||
join: {
|
||||
from: 'items_metadata.item_id',
|
||||
to: 'items.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
10
server/src/models/JournalEntry.js
Normal file
10
server/src/models/JournalEntry.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
export default class JournalEntry extends BaseModel {
|
||||
/**
|
||||
* Table name.
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'manual_journals';
|
||||
}
|
||||
}
|
||||
@@ -36,16 +36,7 @@ export default {
|
||||
setExtraColumns(columns) {
|
||||
this.extraColumns = columns;
|
||||
},
|
||||
|
||||
/**
|
||||
* Retrieve the cache namespace.
|
||||
*/
|
||||
getCacheNamespace() {
|
||||
const { metadataCacheNamespace: cacheName } = this;
|
||||
return typeof cacheName === 'function'
|
||||
? cacheName() : cacheName;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Metadata database query.
|
||||
* @param {Object} query -
|
||||
@@ -126,7 +117,7 @@ export default {
|
||||
metadata.markAsDeleted = true;
|
||||
}
|
||||
this.shouldReload = true;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Remove all meta data of the given group.
|
||||
|
||||
14
server/src/models/Model.js
Normal file
14
server/src/models/Model.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Model } from 'objection';
|
||||
|
||||
export default class ModelBase extends Model {
|
||||
|
||||
static get collection() {
|
||||
return Array;
|
||||
}
|
||||
|
||||
static query(...args) {
|
||||
return super.query(...args).runAfter((result) => {
|
||||
return this.collection.from(result);
|
||||
});
|
||||
}
|
||||
}
|
||||
16
server/src/models/Option.js
Normal file
16
server/src/models/Option.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import { mixin } from 'objection';
|
||||
import BaseModel from '@/models/Model';
|
||||
import MetableCollection from '@/lib/Metable/MetableCollection';
|
||||
|
||||
export default class Option extends mixin(BaseModel, [mixin]) {
|
||||
/**
|
||||
* Table name.
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'options';
|
||||
}
|
||||
|
||||
static get collection() {
|
||||
return MetableCollection;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,10 @@
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const PasswordResets = bookshelf.Model.extend({
|
||||
import Model from '@/models/Model';
|
||||
|
||||
export default class PasswordResets extends Model {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'password_resets',
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
});
|
||||
|
||||
export default bookshelf.model('PasswordResets', PasswordResets);
|
||||
static get tableName() {
|
||||
return 'password_resets';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,41 @@
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const Permission = bookshelf.Model.extend({
|
||||
import { Model } from 'objection';
|
||||
import path from 'path';
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
export default class Permission extends BaseModel {
|
||||
/**
|
||||
* Table name of Role model.
|
||||
* @type {String}
|
||||
*/
|
||||
tableName: 'permissions',
|
||||
static get tableName() {
|
||||
return 'permissions';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
* Relationship mapping.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
static get relationMappings() {
|
||||
return {
|
||||
/**
|
||||
* Permission model may belongs to role model.
|
||||
*/
|
||||
role: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelBase: path.join(__dirname, 'Role'),
|
||||
join: {
|
||||
from: 'permissions.role_id',
|
||||
to: 'roles.id',
|
||||
},
|
||||
},
|
||||
|
||||
role() {
|
||||
return this.belongsTo('Role', 'role_id');
|
||||
},
|
||||
|
||||
resource() {
|
||||
return this.belongsTo('Resource', 'resource_id');
|
||||
},
|
||||
});
|
||||
|
||||
export default bookshelf.model('Permission', Permission);
|
||||
// resource: {
|
||||
// relation: Model.BelongsToOneRelation,
|
||||
// modelBase: path.join(__dirname, 'Resource'),
|
||||
// join: {
|
||||
// from: 'permissions.',
|
||||
// to: '',
|
||||
// }
|
||||
// }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,70 @@
|
||||
import bookshelf from './bookshelf';
|
||||
import path from 'path';
|
||||
import { Model } from 'objection';
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
const Resource = bookshelf.Model.extend({
|
||||
export default class Resource extends BaseModel {
|
||||
/**
|
||||
* Table name.
|
||||
*/
|
||||
tableName: 'resources',
|
||||
static get tableName() {
|
||||
return 'resources';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
static get hasTimestamps() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resource model may has many views.
|
||||
* Relationship mapping.
|
||||
*/
|
||||
views() {
|
||||
return this.hasMany('View', 'resource_id');
|
||||
},
|
||||
static get relationMappings() {
|
||||
const View = require('@/models/View');
|
||||
const ResourceField = require('@/models/ResourceField');
|
||||
const Permission = require('@/models/Permission');
|
||||
|
||||
/**
|
||||
* Resource model may has many fields.
|
||||
*/
|
||||
fields() {
|
||||
return this.hasMany('ResourceField', 'resource_id');
|
||||
},
|
||||
return {
|
||||
/**
|
||||
* Resource model may has many views.
|
||||
*/
|
||||
views: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: View.default,
|
||||
join: {
|
||||
from: 'resources.id',
|
||||
to: 'views.resourceId',
|
||||
},
|
||||
},
|
||||
|
||||
permissions() {
|
||||
return this.belongsToMany('Permission', 'role_has_permissions', 'resource_id', 'permission_id');
|
||||
},
|
||||
});
|
||||
/**
|
||||
* Resource model may has many fields.
|
||||
*/
|
||||
fields: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: ResourceField.default,
|
||||
join: {
|
||||
from: 'resources.id',
|
||||
to: 'resource_fields.resourceId',
|
||||
},
|
||||
},
|
||||
|
||||
export default bookshelf.model('Resource', Resource);
|
||||
/**
|
||||
* Resource model may has many associated permissions.
|
||||
*/
|
||||
permissions: {
|
||||
relation: Model.ManyToManyRelation,
|
||||
modelClass: Permission.default,
|
||||
join: {
|
||||
from: 'resources.id',
|
||||
through: {
|
||||
from: 'role_has_permissions.resourceId',
|
||||
to: 'role_has_permissions.permissionId',
|
||||
},
|
||||
to: 'permissions.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,37 +1,53 @@
|
||||
import { snakeCase } from 'lodash';
|
||||
import bookshelf from './bookshelf';
|
||||
import { Model } from 'objection';
|
||||
import path from 'path';
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
const ResourceField = bookshelf.Model.extend({
|
||||
export default class ResourceField extends BaseModel {
|
||||
/**
|
||||
* Table name.
|
||||
*/
|
||||
tableName: 'resource_fields',
|
||||
static get tableName() {
|
||||
return 'resource_fields';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
|
||||
virtuals: {
|
||||
/**
|
||||
* Resource field key.
|
||||
*/
|
||||
key() {
|
||||
return snakeCase(this.attributes.label_name);
|
||||
},
|
||||
},
|
||||
static get hasTimestamps() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resource field may belongs to resource model.
|
||||
* Virtual attributes.
|
||||
*/
|
||||
resource() {
|
||||
return this.belongsTo('Resource', 'resource_id');
|
||||
},
|
||||
}, {
|
||||
/**
|
||||
* JSON Columns.
|
||||
*/
|
||||
jsonColumns: ['options'],
|
||||
});
|
||||
static get virtualAttributes() {
|
||||
return ['key'];
|
||||
}
|
||||
|
||||
export default bookshelf.model('ResourceField', ResourceField);
|
||||
/**
|
||||
* Resource field key.
|
||||
*/
|
||||
key() {
|
||||
return snakeCase(this.labelName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
return {
|
||||
/**
|
||||
* Resource field may belongs to resource model.
|
||||
*/
|
||||
resource: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelBase: path.join(__dirname, 'Resource'),
|
||||
join: {
|
||||
from: 'resource_fields.resource_id',
|
||||
to: 'resources.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +1,78 @@
|
||||
import bookshelf from './bookshelf';
|
||||
|
||||
const Role = bookshelf.Model.extend({
|
||||
import { Model } from 'objection';
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
export default class Role extends BaseModel {
|
||||
/**
|
||||
* Table name of Role model.
|
||||
* @type {String}
|
||||
*/
|
||||
tableName: 'roles',
|
||||
static get tableName() {
|
||||
return 'roles';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
static get hasTimestamps() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Role may has many permissions.
|
||||
* Relationship mapping.
|
||||
*/
|
||||
permissions() {
|
||||
return this.belongsToMany('Permission', 'role_has_permissions', 'role_id', 'permission_id');
|
||||
},
|
||||
static get relationMappings() {
|
||||
const Permission = require('@/models/Permission');
|
||||
const Resource = require('@/models/Resource');
|
||||
const User = require('@/models/User');
|
||||
|
||||
/**
|
||||
* Role may has many resources.
|
||||
*/
|
||||
resources() {
|
||||
return this.belongsToMany('Resource', 'role_has_permissions', 'role_id', 'resource_id');
|
||||
},
|
||||
return {
|
||||
/**
|
||||
* Role may has many permissions.
|
||||
*/
|
||||
permissions: {
|
||||
relation: Model.ManyToManyRelation,
|
||||
modelClass: Permission.default,
|
||||
join: {
|
||||
from: 'roles.id',
|
||||
through: {
|
||||
from: 'role_has_permissions.roleId',
|
||||
to: 'role_has_permissions.permissionId',
|
||||
},
|
||||
to: 'permissions.id',
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* Role model may has many users.
|
||||
*/
|
||||
users() {
|
||||
return this.belongsToMany('User', 'user_has_roles');
|
||||
},
|
||||
});
|
||||
/**
|
||||
* Role may has many resources.
|
||||
*/
|
||||
resources: {
|
||||
relation: Model.ManyToManyRelation,
|
||||
modelClass: Resource.default,
|
||||
join: {
|
||||
from: 'roles.id',
|
||||
through: {
|
||||
from: 'role_has_permissions.roleId',
|
||||
to: 'role_has_permissions.resourceId',
|
||||
},
|
||||
to: 'resources.id',
|
||||
},
|
||||
},
|
||||
|
||||
export default bookshelf.model('Role', Role);
|
||||
/**
|
||||
* Role may has many associated users.
|
||||
*/
|
||||
users: {
|
||||
relation: Model.ManyToManyRelation,
|
||||
modelClass: User.default,
|
||||
join: {
|
||||
from: 'roles.id',
|
||||
through: {
|
||||
from: 'user_has_roles.roleId',
|
||||
to: 'user_has_roles.userId',
|
||||
},
|
||||
to: 'users.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,28 @@
|
||||
import bookshelf from './bookshelf';
|
||||
import Metable from './Metable';
|
||||
import BaseModel from '@/models/Model';
|
||||
import Auth from './Auth';
|
||||
|
||||
const Setting = bookshelf.Model.extend({
|
||||
export default class Setting extends BaseModel {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'settings',
|
||||
static get tableName() {
|
||||
return 'settings';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
static get hasTimestamps() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extra metadata query to query with the current authenticate user.
|
||||
* @param {Object} query
|
||||
*/
|
||||
extraMetadataQuery(query) {
|
||||
static extraMetadataQuery(query) {
|
||||
if (Auth.isLogged()) {
|
||||
query.where('user_id', Auth.userId());
|
||||
}
|
||||
},
|
||||
}, {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'settings',
|
||||
|
||||
...Metable,
|
||||
});
|
||||
|
||||
export default bookshelf.model('Setting', Setting);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,39 @@
|
||||
import bcrypt from 'bcryptjs';
|
||||
import bookshelf from './bookshelf';
|
||||
import PermissionsService from '@/services/PermissionsService';
|
||||
import { Model } from 'objection';
|
||||
import BaseModel from '@/models/Model';
|
||||
// import PermissionsService from '@/services/PermissionsService';
|
||||
|
||||
const User = bookshelf.Model.extend({
|
||||
...PermissionsService,
|
||||
export default class User extends BaseModel {
|
||||
// ...PermissionsService
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
tableName: 'users',
|
||||
static get tableName() {
|
||||
return 'users';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
* Relationship mapping.
|
||||
*/
|
||||
hasTimestamps: ['created_at', 'updated_at'],
|
||||
static get relationMappings() {
|
||||
const Role = require('@/models/Role');
|
||||
|
||||
initialize() {
|
||||
this.initializeCache();
|
||||
},
|
||||
return {
|
||||
roles: {
|
||||
relation: Model.ManyToManyRelation,
|
||||
modelClass: Role.default,
|
||||
join: {
|
||||
from: 'users.id',
|
||||
through: {
|
||||
from: 'user_has_roles.userId',
|
||||
to: 'user_has_roles.roleId',
|
||||
},
|
||||
to: 'roles.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify the password of the user.
|
||||
@@ -25,15 +41,6 @@ const User = bookshelf.Model.extend({
|
||||
* @return {Boolean}
|
||||
*/
|
||||
verifyPassword(password) {
|
||||
return bcrypt.compareSync(password, this.get('password'));
|
||||
},
|
||||
|
||||
/**
|
||||
* User model may has many associated roles.
|
||||
*/
|
||||
roles() {
|
||||
return this.belongsToMany('Role', 'user_has_roles', 'user_id', 'role_id');
|
||||
},
|
||||
});
|
||||
|
||||
export default bookshelf.model('User', User);
|
||||
return bcrypt.compareSync(password, this.password);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +1,67 @@
|
||||
import bookshelf from './bookshelf';
|
||||
import path from 'path';
|
||||
import { Model } from 'objection';
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
const View = bookshelf.Model.extend({
|
||||
export default class View extends BaseModel {
|
||||
/**
|
||||
* Table name.
|
||||
*/
|
||||
tableName: 'views',
|
||||
static get tableName() {
|
||||
return 'views';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
* Relationship mapping.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
static get relationMappings() {
|
||||
return {
|
||||
/**
|
||||
* View model belongs to resource model.
|
||||
*/
|
||||
resource: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelBase: path.join(__dirname, 'Resource'),
|
||||
join: {
|
||||
from: 'views.resource_id',
|
||||
to: 'resources.id',
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* View model belongs to resource model.
|
||||
*/
|
||||
resource() {
|
||||
return this.belongsTo('Resource', 'resource_id');
|
||||
},
|
||||
/**
|
||||
* View model may has many columns.
|
||||
*/
|
||||
// columns: {
|
||||
// relation: Model.ManyToManyRelation,
|
||||
// modelBase: path.join(__dirname, 'ResourceField'),
|
||||
// join: {
|
||||
// from: 'id',
|
||||
// through: {
|
||||
// from: 'view_has_columns.view_id',
|
||||
// to: 'view_has_columns.field_id',
|
||||
// },
|
||||
// to: 'resource_fields.view_id',
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* View model may has many columns.
|
||||
*/
|
||||
columns() {
|
||||
return this.belongsToMany('ResourceField', 'view_has_columns', 'view_id', 'field_id');
|
||||
},
|
||||
/**
|
||||
* View model may has many view roles.
|
||||
*/
|
||||
viewRoles: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelBase: path.join(__dirname, 'ViewRole'),
|
||||
join: {
|
||||
from: 'views.id',
|
||||
to: 'view_id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* View model may has many view roles.
|
||||
*/
|
||||
viewRoles() {
|
||||
return this.hasMany('ViewRole', 'view_id');
|
||||
},
|
||||
}, {
|
||||
dependents: ['columns', 'viewRoles'],
|
||||
});
|
||||
// columns() {
|
||||
// return this.belongsToMany('ResourceField', 'view_has_columns', 'view_id', 'field_id');
|
||||
// },
|
||||
|
||||
export default bookshelf.model('View', View);
|
||||
// viewRoles() {
|
||||
// return this.hasMany('ViewRole', 'view_id');
|
||||
// },
|
||||
}
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
import bookshelf from './bookshelf';
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
const ViewColumn = bookshelf.Model.extend({
|
||||
export default class ViewColumn extends BaseModel {
|
||||
/**
|
||||
* Table name.
|
||||
*/
|
||||
tableName: 'view_columns',
|
||||
static get tableName() {
|
||||
return 'view_columns';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
|
||||
view() {
|
||||
|
||||
static get hasTimestamps() {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
export default bookshelf.model('ViewColumn', ViewColumn);
|
||||
}
|
||||
|
||||
@@ -1,22 +1,38 @@
|
||||
import bookshelf from './bookshelf';
|
||||
import { Model } from 'objection';
|
||||
import path from 'path';
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
const ViewRole = bookshelf.Model.extend({
|
||||
export default class ViewRole extends BaseModel {
|
||||
/**
|
||||
* Table name.
|
||||
*/
|
||||
tableName: 'view_roles',
|
||||
static get tableName() {
|
||||
return 'view_roles';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp columns.
|
||||
*/
|
||||
hasTimestamps: false,
|
||||
static get hasTimestamps() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* View role model may belongs to view model.
|
||||
* Relationship mapping.
|
||||
*/
|
||||
view() {
|
||||
return this.belongsTo('View', 'view_id');
|
||||
},
|
||||
});
|
||||
|
||||
export default bookshelf.model('ViewRole', ViewRole);
|
||||
static get relationMappings() {
|
||||
return {
|
||||
/**
|
||||
* View role model may belongs to view model.
|
||||
*/
|
||||
view: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelBase: path.join(__dirname, 'View'),
|
||||
join: {
|
||||
from: 'view_roles.view_id',
|
||||
to: 'views.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
import Bookshelf from 'bookshelf';
|
||||
import jsonColumns from 'bookshelf-json-columns';
|
||||
import bookshelfParanoia from 'bookshelf-paranoia';
|
||||
import bookshelfModelBase from 'bookshelf-modelbase';
|
||||
import cascadeDelete from 'bookshelf-cascade-delete';
|
||||
import knex from '../database/knex';
|
||||
|
||||
const bookshelf = Bookshelf(knex);
|
||||
|
||||
bookshelf.plugin('pagination');
|
||||
bookshelf.plugin('visibility');
|
||||
bookshelf.plugin('registry');
|
||||
bookshelf.plugin('virtuals');
|
||||
bookshelf.plugin(jsonColumns);
|
||||
bookshelf.plugin(bookshelfParanoia);
|
||||
bookshelf.plugin(bookshelfModelBase.pluggable);
|
||||
bookshelf.plugin(cascadeDelete);
|
||||
|
||||
export default bookshelf;
|
||||
7
server/src/models/index.js
Normal file
7
server/src/models/index.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Model } from 'objection';
|
||||
import knex from '@/database/knex';
|
||||
|
||||
// Bind all Models to a knex instance. If you only have one database in
|
||||
// your server this is all you have to do. For multi database systems, see
|
||||
// the Model.bindKnex() method.
|
||||
Model.knex(knex);
|
||||
Reference in New Issue
Block a user