/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ /* eslint-disable @typescript-eslint/explicit-function-return-type */ // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-nocheck import { Model, Page, QueryBuilder } from 'objection'; type Options = { columnName?: string; deletedValue?: Date; }; let options: Options; export class DeleteQueryBuilder extends QueryBuilder { // These are necessary. You can just copy-paste them and change the // name of the query builder class. public ArrayQueryBuilderType!: DeleteQueryBuilder; public SingleQueryBuilderType!: DeleteQueryBuilder; public MaybeSingleQueryBuilderType!: DeleteQueryBuilder; public NumberQueryBuilderType!: DeleteQueryBuilder; public PageQueryBuilderType!: DeleteQueryBuilder>; public execute(): Promise { if (this.isFind() && !this.context().includeDeleted) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const tableRef = this.tableRefFor(this.modelClass()); // qualify the column name void this.whereNull(`${tableRef}.${options.columnName}`); } return super.execute(); } public delete(): this['NumberQueryBuilderType'] { void this.context({ deletedAt: new Date(), }); const patch = {}; patch[options.columnName] = options.deletedValue; return this.patch(patch); } public hardDelete(): this['NumberQueryBuilderType'] { return super.delete(); } public undelete(): this['NumberQueryBuilderType'] { void this.context({ undelete: true, }); const patch = {}; patch[options.columnName] = null; return this.patch(patch); } public includeDeleted(): this { return this.context({ includeDeleted: true }); } } type Constructor = new (...args: any[]) => T; export const SoftDeleteMixin = (passedOptions?: Options) => { options = { columnName: 'deletedAt', deletedValue: new Date(), ...passedOptions, }; return function >(Base: T): T { return class extends Base { public QueryBuilderType!: DeleteQueryBuilder; public static QueryBuilder = DeleteQueryBuilder; public static modifiers = { includeDeleted(query: DeleteQueryBuilder): void { void query.includeDeleted(); }, }; }; }; };