@codenameryuu/adonis-lucid-soft-deletes 1.3.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* adonis-lucid-soft-deletes
|
|
3
|
+
*
|
|
4
|
+
* (c) Lookin Anton <alsd@lookinlab.ru>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
export async function configure(command) {
|
|
10
|
+
const codemods = await command.createCodemods();
|
|
11
|
+
await codemods.updateRcFile((rcFile) => {
|
|
12
|
+
rcFile.addProvider('@codenameryuu/adonis-lucid-soft-deletes/provider');
|
|
13
|
+
});
|
|
14
|
+
}
|
package/build/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* adonis-lucid-soft-deletes
|
|
3
|
+
*
|
|
4
|
+
* (c) Lookin Anton <alsd@lookinlab.ru>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
export { configure } from './configure.js';
|
|
10
|
+
export { SoftDeletes } from './src/mixin.js';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* adonis-lucid-soft-deletes
|
|
3
|
+
*
|
|
4
|
+
* (c) Lookin Anton <alsd@lookinlab.ru>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import { extendModelQueryBuilder } from '../src/bindings/model_query_builder.js';
|
|
10
|
+
export default class LucidSoftDeletesProvider {
|
|
11
|
+
app;
|
|
12
|
+
constructor(app) {
|
|
13
|
+
this.app = app;
|
|
14
|
+
}
|
|
15
|
+
async boot() {
|
|
16
|
+
const { ModelQueryBuilder } = await this.app.import('@adonisjs/lucid/orm');
|
|
17
|
+
extendModelQueryBuilder(ModelQueryBuilder);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* adonis-lucid-soft-deletes
|
|
3
|
+
*
|
|
4
|
+
* (c) Lookin Anton <alsd@lookinlab.ru>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import { Exception } from '@adonisjs/core/exceptions';
|
|
10
|
+
/**
|
|
11
|
+
* Raises exception when model not with soft delete
|
|
12
|
+
*/
|
|
13
|
+
function ensureModelWithSoftDeletes(model) {
|
|
14
|
+
if (!('ignoreDeleted' in model && 'ignoreDeletedPaginate' in model)) {
|
|
15
|
+
throw new Exception(`${model.name} model don't support Soft Deletes`, {
|
|
16
|
+
code: 'E_MODEL_SOFT_DELETE',
|
|
17
|
+
status: 500,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Define SoftDeletes binding to ModelQueryBuilder
|
|
23
|
+
*/
|
|
24
|
+
export function extendModelQueryBuilder(builder) {
|
|
25
|
+
builder.macro('restore', async function () {
|
|
26
|
+
ensureModelWithSoftDeletes(this.model);
|
|
27
|
+
const deletedAtColumn = this.model.$getColumn('deletedAt')?.columnName;
|
|
28
|
+
if (!deletedAtColumn)
|
|
29
|
+
return;
|
|
30
|
+
await this.update({ [deletedAtColumn]: null });
|
|
31
|
+
});
|
|
32
|
+
builder.macro('withTrashed', function () {
|
|
33
|
+
ensureModelWithSoftDeletes(this.model);
|
|
34
|
+
return this.model.disableIgnore(this);
|
|
35
|
+
});
|
|
36
|
+
builder.macro('onlyTrashed', function () {
|
|
37
|
+
ensureModelWithSoftDeletes(this.model);
|
|
38
|
+
const deletedAtColumn = this.model.$getColumn('deletedAt')?.columnName;
|
|
39
|
+
return this.model.disableIgnore(this).whereNotNull(`${this.model.table}.${deletedAtColumn}`);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* adonis-lucid-soft-deletes
|
|
3
|
+
*
|
|
4
|
+
* (c) Lookin Anton <alsd@lookinlab.ru>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
10
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
11
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
12
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
13
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
14
|
+
};
|
|
15
|
+
import { DateTime } from 'luxon';
|
|
16
|
+
import { Exception } from '@adonisjs/core/exceptions';
|
|
17
|
+
import { column, beforeFind, beforeFetch, beforePaginate } from '@adonisjs/lucid/orm';
|
|
18
|
+
export function SoftDeletes(superclass) {
|
|
19
|
+
class ModelWithSoftDeletes extends superclass {
|
|
20
|
+
static ignoreDeleted(query) {
|
|
21
|
+
if (query['ignoreDeleted'] === false) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const isGroupLimitQuery = query.clone().toQuery().includes('adonis_group_limit_counter');
|
|
25
|
+
const deletedAtColumn = query.model.$getColumn('deletedAt')?.columnName;
|
|
26
|
+
const queryIgnoreDeleted = isGroupLimitQuery
|
|
27
|
+
? query.knexQuery['_single'].table
|
|
28
|
+
: query;
|
|
29
|
+
queryIgnoreDeleted.whereNull(`${query.model.table}.${deletedAtColumn}`);
|
|
30
|
+
}
|
|
31
|
+
static ignoreDeletedPaginate([countQuery, query]) {
|
|
32
|
+
countQuery['ignoreDeleted'] = query['ignoreDeleted'];
|
|
33
|
+
this.ignoreDeleted(countQuery);
|
|
34
|
+
}
|
|
35
|
+
static disableIgnore(query) {
|
|
36
|
+
if (query['ignoreDeleted'] === false) {
|
|
37
|
+
return query;
|
|
38
|
+
}
|
|
39
|
+
query['ignoreDeleted'] = false;
|
|
40
|
+
return query;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Fetch all models without filter by deleted_at
|
|
44
|
+
*/
|
|
45
|
+
static withTrashed() {
|
|
46
|
+
const query = this.query();
|
|
47
|
+
return this.disableIgnore(query);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Fetch models only with deleted_at
|
|
51
|
+
*/
|
|
52
|
+
static onlyTrashed() {
|
|
53
|
+
const query = this.query();
|
|
54
|
+
const deletedAtColumn = query.model.$getColumn('deletedAt')?.columnName;
|
|
55
|
+
return this.disableIgnore(query).whereNotNull(`${query.model.table}.${deletedAtColumn}`);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Force delete instance property
|
|
59
|
+
*/
|
|
60
|
+
$forceDelete = false;
|
|
61
|
+
/**
|
|
62
|
+
* Computed trashed property
|
|
63
|
+
*/
|
|
64
|
+
get trashed() {
|
|
65
|
+
return this.deletedAt !== null;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Override default $getQueryFor method
|
|
69
|
+
*/
|
|
70
|
+
$getQueryFor(action, client) {
|
|
71
|
+
/**
|
|
72
|
+
* Soft Delete
|
|
73
|
+
*/
|
|
74
|
+
const softDelete = async () => {
|
|
75
|
+
this.deletedAt = DateTime.local();
|
|
76
|
+
await this.save();
|
|
77
|
+
};
|
|
78
|
+
if (action === 'delete' && !this.$forceDelete) {
|
|
79
|
+
return { del: softDelete, delete: softDelete };
|
|
80
|
+
}
|
|
81
|
+
if (action === 'insert') {
|
|
82
|
+
return super.$getQueryFor(action, client);
|
|
83
|
+
}
|
|
84
|
+
return super.$getQueryFor(action, client);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Override default delete method
|
|
88
|
+
*/
|
|
89
|
+
async delete() {
|
|
90
|
+
await super.delete();
|
|
91
|
+
this.$isDeleted = this.$forceDelete;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Restore model
|
|
95
|
+
*/
|
|
96
|
+
async restore() {
|
|
97
|
+
if (this.$isDeleted) {
|
|
98
|
+
throw new Exception('Cannot restore a model instance is was force deleted', {
|
|
99
|
+
code: 'E_MODEL_FORCE_DELETED',
|
|
100
|
+
status: 500,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
if (!this.trashed) {
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
this.deletedAt = null;
|
|
107
|
+
await this.save();
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Force delete model
|
|
112
|
+
*/
|
|
113
|
+
async forceDelete() {
|
|
114
|
+
this.$forceDelete = true;
|
|
115
|
+
await this.delete();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
__decorate([
|
|
119
|
+
column.dateTime()
|
|
120
|
+
], ModelWithSoftDeletes.prototype, "deletedAt", void 0);
|
|
121
|
+
__decorate([
|
|
122
|
+
beforeFind(),
|
|
123
|
+
beforeFetch()
|
|
124
|
+
], ModelWithSoftDeletes, "ignoreDeleted", null);
|
|
125
|
+
__decorate([
|
|
126
|
+
beforePaginate()
|
|
127
|
+
], ModelWithSoftDeletes, "ignoreDeletedPaginate", null);
|
|
128
|
+
return ModelWithSoftDeletes;
|
|
129
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codenameryuu/adonis-lucid-soft-deletes",
|
|
3
3
|
"description": "Addon for soft deletes Adonis JS 7 Lucid ORM",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.5.0",
|
|
5
5
|
"author": "codenameryuu",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/codenameryuu/adonis-lucid-soft-deletes#readme",
|
|
@@ -25,19 +25,15 @@
|
|
|
25
25
|
"./provider": "./build/providers/lucid_soft_deletes_provider.js"
|
|
26
26
|
},
|
|
27
27
|
"scripts": {
|
|
28
|
-
"lint": "eslint .",
|
|
29
|
-
"format": "prettier --write .",
|
|
30
28
|
"clean": "del-cli build",
|
|
31
|
-
"precompile": "npm run lint && npm run clean",
|
|
32
|
-
"compile": "tsc --emitDeclarationOnly --declaration",
|
|
33
|
-
"build": "npm run compile",
|
|
34
|
-
"pretest": "npm run lint",
|
|
35
|
-
"test": "c8 npm run quick:test",
|
|
36
29
|
"typecheck": "tsc --noEmit",
|
|
30
|
+
"lint": "eslint . --ext=.ts",
|
|
31
|
+
"format": "prettier --write .",
|
|
32
|
+
"prebuild": "npm run lint && npm run clean",
|
|
33
|
+
"build": "npm run clean && tsc",
|
|
34
|
+
"release": "release-it",
|
|
37
35
|
"version": "npm run build",
|
|
38
|
-
"prepublishOnly": "npm run build"
|
|
39
|
-
"release": "np",
|
|
40
|
-
"quick:test": "NODE_DEBUG=\"adonis-lucid-soft-deletes\" node --enable-source-maps --loader=ts-node/esm bin/test.ts"
|
|
36
|
+
"prepublishOnly": "npm run build"
|
|
41
37
|
},
|
|
42
38
|
"devDependencies": {
|
|
43
39
|
"@adonisjs/assembler": "^7.7.0",
|