UNPKG

@dazejs/framework

Version:

Daze.js - A powerful web framework for Node.js

110 lines 4.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ModelBuilder = void 0; const container_1 = require("../../container"); const pagination_1 = require("../../pagination"); class ModelBuilder { constructor(model, repository) { this.app = container_1.Container.get('app'); this.throughs = ['insert', 'update', 'increment', 'decrement', 'delete', 'aggregate', 'count', 'max', 'min', 'sum', 'avg']; this.model = model; this.repository = repository; this.builder = this.newBuilderInstance(); return new Proxy(this, this.proxy); } get proxy() { return { get(target, p, receiver) { if (typeof p !== 'string' || Reflect.has(target, p)) return Reflect.get(target, p, receiver); if (target.builder && Reflect.has(target.builder, p) && typeof target.builder[p] === 'function') { return target.handleForwardCalls(p); } return Reflect.get(target, p, receiver); } }; } handleForwardCalls(p) { return (...args) => { if (this.throughs.includes(p)) { return this.builder[p](...args); } this.builder[p](...args); return new Proxy(this, this.proxy); }; } newBuilderInstance() { return this.app.get('db') .connection(this.model.getConnectionName()) .table(this.model.getTable()); } useEntityColumns() { this.builder.columns(...this.model.getColumns().keys()); return this; } getBuilder() { return this.builder; } with(relation, callback) { this.repository.with(relation, callback); return this; } async find() { if (this.model.isForceDelete() || this.repository.needWithTrashed) { const records = await this.builder.find(); return this.model.resultToRepositories(this.repository, records); } if (this.model.getSoftDeleteDefaultValue() === null) { this.builder.whereNull(this.model.getSoftDeleteKey()); } else { this.builder.where(this.model.getSoftDeleteKey(), this.model.getSoftDeleteDefaultValue()); } const records = await this.builder.find(); return this.model.resultToRepositories(this.repository, records); } async findAndCount() { if (this.model.isForceDelete() || this.repository.needWithTrashed) { const [records, count] = await this.builder.findAndCount(); const results = await this.model.resultToRepositories(this.repository, records); return [results, count]; } if (this.model.getSoftDeleteDefaultValue() === null) { this.builder.whereNull(this.model.getSoftDeleteKey()); } else { this.builder.where(this.model.getSoftDeleteKey(), this.model.getSoftDeleteDefaultValue()); } const [records, count] = await this.builder.findAndCount(); const results = await this.model.resultToRepositories(this.repository, records); return [results, count]; } async pagination(page, perPage = 10, option) { this.builder.take(perPage).skip((page - 1) * perPage); const [items, count] = await this.findAndCount(); const paginator = new pagination_1.Paginator(items.map(item => item.getAttributes()), count, page, perPage, option); return paginator; } async first() { if (!this.model.isForceDelete() && !this.repository.needWithTrashed) { if (this.model.getSoftDeleteDefaultValue() === null) { this.builder.whereNull(this.model.getSoftDeleteKey()); } else { this.builder.where(this.model.getSoftDeleteKey(), this.model.getSoftDeleteDefaultValue()); } } const record = await this.builder.first(); if (!record) return; return this.model.resultToRepository(this.repository, record); } async firstOrCreate(attributes) { const repos = await this.first(); if (repos) return repos; return await this.repository.create(attributes); } } exports.ModelBuilder = ModelBuilder; //# sourceMappingURL=builder.js.map