feat: rewrite repositories with base entity repository class.

feat: sales and purchases status.
feat: sales and purchases auto-increment number.
fix: settings find query with extra columns.
This commit is contained in:
Ahmed Bouhuolia
2020-12-13 19:50:59 +02:00
parent e9e4ddaee0
commit 188e411f02
78 changed files with 1634 additions and 869 deletions

View File

@@ -1,60 +1,19 @@
import { IView } from 'interfaces';
import { View } from 'models';
import TenantRepository from 'repositories/TenantRepository';
export default class ViewRepository extends TenantRepository {
/**
* Retrieve view model by the given id.
* @param {number} id -
* Constructor method.
*/
getById(id: number) {
const { View } = this.models;
return this.cache.get(`customView.id.${id}`, () => {
return View.query().findById(id)
.withGraphFetched('columns')
.withGraphFetched('roles');
});
constructor(knex, cache) {
super(knex, cache);
this.model = View;
}
/**
* Retrieve all views of the given resource id.
*/
allByResource(resourceModel: string) {
const { View } = this.models;
return this.cache.get(`customView.resourceModel.${resourceModel}`, () => {
return View.query().where('resource_model', resourceModel)
.withGraphFetched('columns')
.withGraphFetched('roles');
});
}
/**
* Inserts a new view to the storage.
* @param {IView} view
*/
async insert(view: IView): Promise<IView> {
const { View } = this.models;
const insertedView = await View.query().insertGraph({ ...view });
this.flushCache();
return insertedView;
}
async update(viewId: number, view: IView): Promise<IView> {
const { View } = this.models;
const updatedView = await View.query().upsertGraph({
id: viewId,
...view
});
this.flushCache();
return updatedView;
}
/**
* Flushes repository cache.
*/
flushCache() {
this.cache.delStartWith('customView');
allByResource(resourceModel: string, withRelations?) {
return super.find({ resource_mode: resourceModel }, withRelations);
}
}