UNPKG

@avonjs/avonjs

Version:

A fluent Node.js API generator.

168 lines (167 loc) 5.21 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const luxon_1 = require("luxon"); const Contracts_1 = require("../Contracts"); const ModelNotFoundException_1 = __importDefault(require("../Exceptions/ModelNotFoundException")); exports.default = (Parent) => { class SoftDeletes extends Parent { constructor(...params) { super(params); this.applySoftDelete(); } /** * Store given model into the storage. */ async store(model) { //@ts-ignore return super.store(this.ensureOfSoftDeletes(model)); } /** * Ensure that "delete at" attribute is set. */ ensureOfSoftDeletes(model) { return !this.isSoftDeleted(model) ? this.resetSoftDeletes(model) : model; } /** * Delete model for the given key. */ async delete(key) { const model = await this.find(key); if (model === undefined) { return; } await this.update(this.fillSoftDeletes(model)); } /** * Fill the "deleted at" column. */ fillSoftDeletes(model) { model.setAttribute(this.getDeletedAtKey(), this.getDeletedAtValue()); return model; } /** * Reset the "deleted at" column. */ resetSoftDeletes(model) { model.setAttribute(this.getDeletedAtKey(), this.getDeletedAtValueOnRestore()); return model; } /** * Delete model for the given key. */ async forceDelete(key) { this.removeSoftDeleteQueries(); //@ts-ignore await super.delete(key); } /** * Restore the delete model for given key. */ async restore(key) { const model = await this.onlyTrashed().find(key); ModelNotFoundException_1.default.unless(model); return this.update(this.resetSoftDeletes(model)); } /** * Apply soft-delete constraint. */ applySoftDelete() { return this.where(this.scopeSoftDelete()); } /** * Apply soft-delete constraint. */ withoutTrashed() { return this.removeSoftDeleteQueries().applySoftDelete(); } /** * Ignore soft-delete constraint. */ withTrashed() { return this.removeSoftDeleteQueries(); } /** * Apply only trashed record constraints. */ onlyTrashed() { return this.removeSoftDeleteQueries().where(this.scopeTrashedRecords()); } /** * Removes all scopes. */ removeSoftDeleteQueries() { const softDeletes = JSON.stringify(this.scopeSoftDelete()); const trashes = JSON.stringify(this.scopeTrashedRecords()); const repository = this.setWheres(this.getWheres().filter((value) => { if (JSON.stringify(value) === softDeletes) { return false; } return JSON.stringify(value) !== trashes; })); return repository; } /** * Get soft-delete constraint. */ scopeSoftDelete() { return { key: this.getDeletedAtKey(), value: this.getSoftDeleteValue(), operator: Contracts_1.Operator.eq, }; } /** * Get only trashed records constraint. */ scopeTrashedRecords() { return { key: this.getDeletedAtKey(), value: this.getSoftDeleteValue(), operator: Contracts_1.Operator.not, }; } /** * Determine whether a given resource is "soft-deleted". */ isSoftDeleted(resource) { const deleteAt = resource.getAttribute(this.getDeletedAtKey()); return ![this.getDeletedAtValueOnRestore(), undefined].includes(deleteAt); } /** * Get name of `deleted_at` key. */ getDeletedAtKey() { return 'deleted_at'; } /** * Get value for `deleted_at` key. */ getDeletedAtValue() { return this.freshTimestamp(); } /** * Get a fresh timestamp for the model. */ freshTimestamp() { return luxon_1.DateTime.now().toSQL({ includeOffset: false }); } /** * Get value for available records. * * @deprecated Use `getDeletedAtValueOnRestore` instead. */ getSoftDeleteValue() { return this.getDeletedAtValueOnRestore(); } /** * Get value for available records. */ getDeletedAtValueOnRestore() { return null; } } return SoftDeletes; };