@codenameryuu/adonis-lucid-soft-deletes 1.1.0 → 1.3.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.
- package/README.md +21 -44
- package/package.json +4 -18
- package/build/chunk-4FLDNTDF.js +0 -32
- package/build/chunk-4FLDNTDF.js.map +0 -1
- package/build/chunk-EUXUH3YW.js +0 -15
- package/build/chunk-EUXUH3YW.js.map +0 -1
- package/build/index.js +0 -128
- package/build/index.js.map +0 -1
- package/build/providers/lucid_soft_deletes_provider.js +0 -19
- package/build/providers/lucid_soft_deletes_provider.js.map +0 -1
- package/build/src/bindings/model_query_builder.js +0 -8
- package/build/src/bindings/model_query_builder.js.map +0 -1
- package/build/src/types/querybuilder.js +0 -1
- package/build/src/types/querybuilder.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,16 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @codenameryuu/adonis-lucid-soft-deletes
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Docs for [AdonisJS v5](https://github.com/lookinlab/adonis-lucid-soft-deletes/tree/v1)
|
|
6
|
-
|
|
7
|
-
[![npm-image]][npm-url] [![license-image]][license-url] [![typescript-image]][typescript-url]
|
|
8
|
-
|
|
9
|
-
This addon adds the functionality to soft deletes Lucid Models through the `deleted_at` flag
|
|
10
|
-
|
|
11
|
-
> Works with `@adonisjs/lucid@^21.1.*`
|
|
12
|
-
|
|
13
|
-
Sometimes use the `deleted_at` flag for soft deletes could be not good way. More [about it](https://brandur.org/soft-deletion)
|
|
3
|
+
This addon adds the functionality to soft deletes Lucid Models through the `deleted_at` flag in AdonisJS 7.
|
|
14
4
|
|
|
15
5
|
## Introduction
|
|
16
6
|
|
|
@@ -24,39 +14,32 @@ as Luxon / DateTime instance.
|
|
|
24
14
|
|
|
25
15
|
## Installation
|
|
26
16
|
|
|
27
|
-
Install it using `npm
|
|
17
|
+
Install it using `npm` , `yarn` or `pnpm` .
|
|
28
18
|
|
|
29
19
|
```bash
|
|
30
|
-
|
|
31
|
-
npm i adonis-lucid-soft-deletes
|
|
32
|
-
|
|
33
|
-
# yarn
|
|
34
|
-
yarn add adonis-lucid-soft-deletes
|
|
35
|
-
|
|
36
|
-
# pnpm
|
|
37
|
-
pnpm add adonis-lucid-soft-deletes
|
|
20
|
+
yarn add @codenameryuu/adonis-lucid-soft-deletes
|
|
38
21
|
```
|
|
39
22
|
|
|
40
|
-
After install call `configure
|
|
23
|
+
After install call `configure` :
|
|
41
24
|
|
|
42
25
|
```bash
|
|
43
|
-
node ace configure adonis-lucid-soft-deletes
|
|
26
|
+
node ace configure @codenameryuu/adonis-lucid-soft-deletes
|
|
44
27
|
```
|
|
45
28
|
|
|
46
29
|
## Usage
|
|
47
30
|
|
|
48
31
|
Make sure to register the provider inside `adonisrc.ts` file.
|
|
49
32
|
|
|
50
|
-
```
|
|
33
|
+
```typescript
|
|
51
34
|
providers: [
|
|
52
35
|
// ...
|
|
53
|
-
() => import('adonis-lucid-soft-deletes/provider'),
|
|
36
|
+
() => import('@codenameryuu/adonis-lucid-soft-deletes/provider'),
|
|
54
37
|
]
|
|
55
38
|
```
|
|
56
39
|
|
|
57
40
|
You should add the `deleted_at` column to your database tables for models with soft deletes.
|
|
58
41
|
|
|
59
|
-
```
|
|
42
|
+
```typescript
|
|
60
43
|
// migrations/1234566666_users.ts
|
|
61
44
|
import { BaseSchema } from '@adonisjs/lucid/schema'
|
|
62
45
|
|
|
@@ -75,7 +58,7 @@ export default class Users extends BaseSchema {
|
|
|
75
58
|
|
|
76
59
|
### Applying Soft Deletes to a Model
|
|
77
60
|
|
|
78
|
-
```
|
|
61
|
+
```typescript
|
|
79
62
|
import { compose } from '@adonisjs/core/helpers'
|
|
80
63
|
import { SoftDeletes } from 'adonis-lucid-soft-deletes'
|
|
81
64
|
|
|
@@ -84,10 +67,10 @@ export default class User extends compose(BaseModel, SoftDeletes) {
|
|
|
84
67
|
}
|
|
85
68
|
```
|
|
86
69
|
|
|
87
|
-
Now, when you call the `.delete()` method on the model, the `deleted_at` (`customDeletedAtColumn`) column
|
|
70
|
+
Now, when you call the `.delete()` method on the model, the `deleted_at` ( `customDeletedAtColumn` ) column
|
|
88
71
|
will be set to the current date and time. However, the model's database record will be left in the table.
|
|
89
72
|
|
|
90
|
-
```
|
|
73
|
+
```typescript
|
|
91
74
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
92
75
|
import User from '#models/user'
|
|
93
76
|
|
|
@@ -113,7 +96,7 @@ will automatically be excluded from all query results.
|
|
|
113
96
|
|
|
114
97
|
To determine if a given model instance has been soft deleted, you may use the `.trashed` getter:
|
|
115
98
|
|
|
116
|
-
```
|
|
99
|
+
```typescript
|
|
117
100
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
118
101
|
import User from '#models/user'
|
|
119
102
|
|
|
@@ -134,7 +117,7 @@ export default class UsersController {
|
|
|
134
117
|
|
|
135
118
|
### Set custom column name for `deletedAt`
|
|
136
119
|
|
|
137
|
-
```
|
|
120
|
+
```typescript
|
|
138
121
|
import { compose } from '@adonisjs/core/helpers'
|
|
139
122
|
import { SoftDeletes } from 'adonis-lucid-soft-deletes'
|
|
140
123
|
|
|
@@ -150,7 +133,8 @@ export default class User extends compose(BaseModel, SoftDeletes) {
|
|
|
150
133
|
|
|
151
134
|
To restore a soft deleted model, you may call the `.restore()` method on a model instance.
|
|
152
135
|
Also, method `.restore()` exists after methods `.withTrashed()` and `.onlyTrashed()`
|
|
153
|
-
|
|
136
|
+
|
|
137
|
+
The `restore` method will set the model's `deleted_at` column to `null` :
|
|
154
138
|
|
|
155
139
|
```ts
|
|
156
140
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
@@ -180,7 +164,7 @@ export default class TrashUsersController {
|
|
|
180
164
|
Sometimes you may need to truly remove a model from your database.
|
|
181
165
|
You may use the `.forceDelete()` method to permanently remove a soft deleted model from the database table:
|
|
182
166
|
|
|
183
|
-
```
|
|
167
|
+
```typescript
|
|
184
168
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
185
169
|
import User from '#models/user'
|
|
186
170
|
|
|
@@ -204,7 +188,7 @@ As noted above, soft deleted models will automatically be excluded from query re
|
|
|
204
188
|
However, you may force soft deleted models to be included in a query's results
|
|
205
189
|
by calling the `.withTrashed()` method on the model:
|
|
206
190
|
|
|
207
|
-
```
|
|
191
|
+
```typescript
|
|
208
192
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
209
193
|
import User from '#models/user'
|
|
210
194
|
|
|
@@ -233,7 +217,7 @@ export default class UsersController {
|
|
|
233
217
|
|
|
234
218
|
The `.onlyTrashed()` method will retrieve **only** soft deleted models:
|
|
235
219
|
|
|
236
|
-
```
|
|
220
|
+
```typescript
|
|
237
221
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
238
222
|
import User from '#models/user'
|
|
239
223
|
|
|
@@ -250,17 +234,10 @@ export default class TrashUsersController {
|
|
|
250
234
|
|
|
251
235
|
### Soft Deletes methods
|
|
252
236
|
|
|
253
|
-
Methods `.withTrashed()
|
|
237
|
+
Methods `.withTrashed()` , `.onlyTrashed()` and `.restore()` also available
|
|
254
238
|
in ModelQueryBuilder for models with soft delete, example:
|
|
255
239
|
|
|
256
|
-
```
|
|
240
|
+
```typescript
|
|
257
241
|
await User.query().withTrashed().exec()
|
|
258
242
|
await User.query().onlyTrashed().restore()
|
|
259
243
|
```
|
|
260
|
-
|
|
261
|
-
[npm-image]: https://img.shields.io/npm/v/adonis-lucid-soft-deletes?logo=npm&style=for-the-badge
|
|
262
|
-
[npm-url]: https://www.npmjs.com/package/adonis-lucid-soft-deletes
|
|
263
|
-
[license-image]: https://img.shields.io/npm/l/adonis-lucid-soft-deletes?style=for-the-badge&color=blueviolet
|
|
264
|
-
[license-url]: https://github.com/lookinlab/adonis-lucid-soft-deletes/blob/develop/LICENSE.md
|
|
265
|
-
[typescript-image]: https://img.shields.io/npm/types/adonis-lucid-soft-deletes?color=294E80&label=%20&logo=typescript&style=for-the-badge
|
|
266
|
-
[typescript-url]: https://github.com/lookinlab
|
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.3.0",
|
|
5
5
|
"author": "codenameryuu",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/codenameryuu/adonis-lucid-soft-deletes#readme",
|
|
@@ -29,15 +29,15 @@
|
|
|
29
29
|
"format": "prettier --write .",
|
|
30
30
|
"clean": "del-cli build",
|
|
31
31
|
"precompile": "npm run lint && npm run clean",
|
|
32
|
-
"compile": "
|
|
32
|
+
"compile": "tsc --emitDeclarationOnly --declaration",
|
|
33
33
|
"build": "npm run compile",
|
|
34
|
-
"quick:test": "NODE_DEBUG=\"adonis-lucid-soft-deletes\" node --enable-source-maps --loader=ts-node/esm bin/test.ts",
|
|
35
34
|
"pretest": "npm run lint",
|
|
36
35
|
"test": "c8 npm run quick:test",
|
|
37
36
|
"typecheck": "tsc --noEmit",
|
|
38
37
|
"version": "npm run build",
|
|
39
38
|
"prepublishOnly": "npm run build",
|
|
40
|
-
"release": "np"
|
|
39
|
+
"release": "np",
|
|
40
|
+
"quick:test": "NODE_DEBUG=\"adonis-lucid-soft-deletes\" node --enable-source-maps --loader=ts-node/esm bin/test.ts"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@adonisjs/assembler": "^7.7.0",
|
|
@@ -104,19 +104,5 @@
|
|
|
104
104
|
"tests/**",
|
|
105
105
|
"bin/**"
|
|
106
106
|
]
|
|
107
|
-
},
|
|
108
|
-
"tsup": {
|
|
109
|
-
"entry": [
|
|
110
|
-
"./index.ts",
|
|
111
|
-
"./src/types/*.ts",
|
|
112
|
-
"./src/bindings/model_query_builder.ts",
|
|
113
|
-
"./providers/lucid_soft_deletes_provider.ts"
|
|
114
|
-
],
|
|
115
|
-
"outDir": "./build",
|
|
116
|
-
"clean": true,
|
|
117
|
-
"format": "esm",
|
|
118
|
-
"dts": false,
|
|
119
|
-
"sourcemap": true,
|
|
120
|
-
"target": "esnext"
|
|
121
107
|
}
|
|
122
108
|
}
|
package/build/chunk-4FLDNTDF.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
// src/bindings/model_query_builder.ts
|
|
2
|
-
import { Exception } from "@adonisjs/core/exceptions";
|
|
3
|
-
function ensureModelWithSoftDeletes(model) {
|
|
4
|
-
if (!("ignoreDeleted" in model && "ignoreDeletedPaginate" in model)) {
|
|
5
|
-
throw new Exception(`${model.name} model don't support Soft Deletes`, {
|
|
6
|
-
code: "E_MODEL_SOFT_DELETE",
|
|
7
|
-
status: 500
|
|
8
|
-
});
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
function extendModelQueryBuilder(builder) {
|
|
12
|
-
builder.macro("restore", async function() {
|
|
13
|
-
ensureModelWithSoftDeletes(this.model);
|
|
14
|
-
const deletedAtColumn = this.model.$getColumn("deletedAt")?.columnName;
|
|
15
|
-
if (!deletedAtColumn) return;
|
|
16
|
-
await this.update({ [deletedAtColumn]: null });
|
|
17
|
-
});
|
|
18
|
-
builder.macro("withTrashed", function() {
|
|
19
|
-
ensureModelWithSoftDeletes(this.model);
|
|
20
|
-
return this.model.disableIgnore(this);
|
|
21
|
-
});
|
|
22
|
-
builder.macro("onlyTrashed", function() {
|
|
23
|
-
ensureModelWithSoftDeletes(this.model);
|
|
24
|
-
const deletedAtColumn = this.model.$getColumn("deletedAt")?.columnName;
|
|
25
|
-
return this.model.disableIgnore(this).whereNotNull(`${this.model.table}.${deletedAtColumn}`);
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export {
|
|
30
|
-
extendModelQueryBuilder
|
|
31
|
-
};
|
|
32
|
-
//# sourceMappingURL=chunk-4FLDNTDF.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/bindings/model_query_builder.ts"],"sourcesContent":["/*\n * adonis-lucid-soft-deletes\n *\n * (c) Lookin Anton <alsd@lookinlab.ru>\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type {\n LucidModel,\n ModelQueryBuilderContract,\n ModelWithSoftDeletes,\n} from '@adonisjs/lucid/types/model'\n\nimport { Exception } from '@adonisjs/core/exceptions'\n\n/**\n * Raises exception when model not with soft delete\n */\nfunction ensureModelWithSoftDeletes(model: LucidModel) {\n if (!('ignoreDeleted' in model && 'ignoreDeletedPaginate' in model)) {\n throw new Exception(`${model.name} model don't support Soft Deletes`, {\n code: 'E_MODEL_SOFT_DELETE',\n status: 500,\n })\n }\n}\n\n/**\n * Define SoftDeletes binding to ModelQueryBuilder\n */\nexport function extendModelQueryBuilder(builder: any) {\n builder.macro('restore', async function (this: ModelQueryBuilderContract<LucidModel>) {\n ensureModelWithSoftDeletes(this.model)\n\n const deletedAtColumn = this.model.$getColumn('deletedAt')?.columnName\n if (!deletedAtColumn) return\n\n await this.update({ [deletedAtColumn]: null })\n })\n\n builder.macro('withTrashed', function (this: ModelQueryBuilderContract<ModelWithSoftDeletes>) {\n ensureModelWithSoftDeletes(this.model)\n return this.model.disableIgnore(this)\n })\n\n builder.macro('onlyTrashed', function (this: ModelQueryBuilderContract<ModelWithSoftDeletes>) {\n ensureModelWithSoftDeletes(this.model)\n\n const deletedAtColumn = this.model.$getColumn('deletedAt')?.columnName\n return this.model.disableIgnore(this).whereNotNull(`${this.model.table}.${deletedAtColumn}`)\n })\n}\n"],"mappings":";AAeA,SAAS,iBAAiB;AAK1B,SAAS,2BAA2B,OAAmB;AACrD,MAAI,EAAE,mBAAmB,SAAS,2BAA2B,QAAQ;AACnE,UAAM,IAAI,UAAU,GAAG,MAAM,IAAI,qCAAqC;AAAA,MACpE,MAAM;AAAA,MACN,QAAQ;AAAA,IACV,CAAC;AAAA,EACH;AACF;AAKO,SAAS,wBAAwB,SAAc;AACpD,UAAQ,MAAM,WAAW,iBAA6D;AACpF,+BAA2B,KAAK,KAAK;AAErC,UAAM,kBAAkB,KAAK,MAAM,WAAW,WAAW,GAAG;AAC5D,QAAI,CAAC,gBAAiB;AAEtB,UAAM,KAAK,OAAO,EAAE,CAAC,eAAe,GAAG,KAAK,CAAC;AAAA,EAC/C,CAAC;AAED,UAAQ,MAAM,eAAe,WAAiE;AAC5F,+BAA2B,KAAK,KAAK;AACrC,WAAO,KAAK,MAAM,cAAc,IAAI;AAAA,EACtC,CAAC;AAED,UAAQ,MAAM,eAAe,WAAiE;AAC5F,+BAA2B,KAAK,KAAK;AAErC,UAAM,kBAAkB,KAAK,MAAM,WAAW,WAAW,GAAG;AAC5D,WAAO,KAAK,MAAM,cAAc,IAAI,EAAE,aAAa,GAAG,KAAK,MAAM,KAAK,IAAI,eAAe,EAAE;AAAA,EAC7F,CAAC;AACH;","names":[]}
|
package/build/chunk-EUXUH3YW.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __decorateClass = (decorators, target, key, kind) => {
|
|
4
|
-
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
5
|
-
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
6
|
-
if (decorator = decorators[i])
|
|
7
|
-
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
8
|
-
if (kind && result) __defProp(target, key, result);
|
|
9
|
-
return result;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export {
|
|
13
|
-
__decorateClass
|
|
14
|
-
};
|
|
15
|
-
//# sourceMappingURL=chunk-EUXUH3YW.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/build/index.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
__decorateClass
|
|
3
|
-
} from "./chunk-EUXUH3YW.js";
|
|
4
|
-
|
|
5
|
-
// configure.ts
|
|
6
|
-
async function configure(command) {
|
|
7
|
-
const codemods = await command.createCodemods();
|
|
8
|
-
await codemods.updateRcFile((rcFile) => {
|
|
9
|
-
rcFile.addProvider("@codenameryuu/adonis-lucid-soft-deletes/provider");
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// src/mixin.ts
|
|
14
|
-
import { DateTime } from "luxon";
|
|
15
|
-
import { Exception } from "@adonisjs/core/exceptions";
|
|
16
|
-
import { column, beforeFind, beforeFetch, beforePaginate } from "@adonisjs/lucid/orm";
|
|
17
|
-
function SoftDeletes(superclass) {
|
|
18
|
-
class ModelWithSoftDeletes extends superclass {
|
|
19
|
-
static ignoreDeleted(query) {
|
|
20
|
-
if (query["ignoreDeleted"] === false) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
const isGroupLimitQuery = query.clone().toQuery().includes("adonis_group_limit_counter");
|
|
24
|
-
const deletedAtColumn = query.model.$getColumn("deletedAt")?.columnName;
|
|
25
|
-
const queryIgnoreDeleted = isGroupLimitQuery ? query.knexQuery["_single"].table : query;
|
|
26
|
-
queryIgnoreDeleted.whereNull(`${query.model.table}.${deletedAtColumn}`);
|
|
27
|
-
}
|
|
28
|
-
static ignoreDeletedPaginate([countQuery, query]) {
|
|
29
|
-
countQuery["ignoreDeleted"] = query["ignoreDeleted"];
|
|
30
|
-
this.ignoreDeleted(countQuery);
|
|
31
|
-
}
|
|
32
|
-
static disableIgnore(query) {
|
|
33
|
-
if (query["ignoreDeleted"] === false) {
|
|
34
|
-
return query;
|
|
35
|
-
}
|
|
36
|
-
query["ignoreDeleted"] = false;
|
|
37
|
-
return query;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Fetch all models without filter by deleted_at
|
|
41
|
-
*/
|
|
42
|
-
static withTrashed() {
|
|
43
|
-
const query = this.query();
|
|
44
|
-
return this.disableIgnore(query);
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Fetch models only with deleted_at
|
|
48
|
-
*/
|
|
49
|
-
static onlyTrashed() {
|
|
50
|
-
const query = this.query();
|
|
51
|
-
const deletedAtColumn = query.model.$getColumn("deletedAt")?.columnName;
|
|
52
|
-
return this.disableIgnore(query).whereNotNull(`${query.model.table}.${deletedAtColumn}`);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Force delete instance property
|
|
56
|
-
*/
|
|
57
|
-
$forceDelete = false;
|
|
58
|
-
/**
|
|
59
|
-
* Computed trashed property
|
|
60
|
-
*/
|
|
61
|
-
get trashed() {
|
|
62
|
-
return this.deletedAt !== null;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Override default $getQueryFor method
|
|
66
|
-
*/
|
|
67
|
-
$getQueryFor(action, client) {
|
|
68
|
-
const softDelete = async () => {
|
|
69
|
-
this.deletedAt = DateTime.local();
|
|
70
|
-
await this.save();
|
|
71
|
-
};
|
|
72
|
-
if (action === "delete" && !this.$forceDelete) {
|
|
73
|
-
return { del: softDelete, delete: softDelete };
|
|
74
|
-
}
|
|
75
|
-
if (action === "insert") {
|
|
76
|
-
return super.$getQueryFor(action, client);
|
|
77
|
-
}
|
|
78
|
-
return super.$getQueryFor(action, client);
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Override default delete method
|
|
82
|
-
*/
|
|
83
|
-
async delete() {
|
|
84
|
-
await super.delete();
|
|
85
|
-
this.$isDeleted = this.$forceDelete;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Restore model
|
|
89
|
-
*/
|
|
90
|
-
async restore() {
|
|
91
|
-
if (this.$isDeleted) {
|
|
92
|
-
throw new Exception("Cannot restore a model instance is was force deleted", {
|
|
93
|
-
code: "E_MODEL_FORCE_DELETED",
|
|
94
|
-
status: 500
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
if (!this.trashed) {
|
|
98
|
-
return this;
|
|
99
|
-
}
|
|
100
|
-
this.deletedAt = null;
|
|
101
|
-
await this.save();
|
|
102
|
-
return this;
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Force delete model
|
|
106
|
-
*/
|
|
107
|
-
async forceDelete() {
|
|
108
|
-
this.$forceDelete = true;
|
|
109
|
-
await this.delete();
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
__decorateClass([
|
|
113
|
-
column.dateTime()
|
|
114
|
-
], ModelWithSoftDeletes.prototype, "deletedAt", 2);
|
|
115
|
-
__decorateClass([
|
|
116
|
-
beforeFind(),
|
|
117
|
-
beforeFetch()
|
|
118
|
-
], ModelWithSoftDeletes, "ignoreDeleted", 1);
|
|
119
|
-
__decorateClass([
|
|
120
|
-
beforePaginate()
|
|
121
|
-
], ModelWithSoftDeletes, "ignoreDeletedPaginate", 1);
|
|
122
|
-
return ModelWithSoftDeletes;
|
|
123
|
-
}
|
|
124
|
-
export {
|
|
125
|
-
SoftDeletes,
|
|
126
|
-
configure
|
|
127
|
-
};
|
|
128
|
-
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../configure.ts","../src/mixin.ts"],"sourcesContent":["/*\n * adonis-lucid-soft-deletes\n *\n * (c) Lookin Anton <alsd@lookinlab.ru>\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type Configure from '@adonisjs/core/commands/configure'\n\nexport async function configure(command: Configure) {\n const codemods = await command.createCodemods()\n\n await codemods.updateRcFile((rcFile) => {\n rcFile.addProvider('@codenameryuu/adonis-lucid-soft-deletes/provider')\n })\n}\n","/*\n * adonis-lucid-soft-deletes\n *\n * (c) Lookin Anton <alsd@lookinlab.ru>\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { DateTime } from 'luxon'\nimport type { NormalizeConstructor } from '@adonisjs/core/types/helpers'\nimport type { LucidModel, ModelQueryBuilderContract } from '@adonisjs/lucid/types/model'\nimport type { QueryClientContract } from '@adonisjs/lucid/types/database'\nimport { Exception } from '@adonisjs/core/exceptions'\nimport { column, beforeFind, beforeFetch, beforePaginate, BaseModel } from '@adonisjs/lucid/orm'\n\ntype ModelQueryBuilderContractWithIgnoreDeleted<\n T extends LucidModel,\n R = InstanceType<T>,\n> = ModelQueryBuilderContract<T, R> & {\n ignoreDeleted: boolean\n}\n\nexport function SoftDeletes<T extends NormalizeConstructor<typeof BaseModel>>(superclass: T) {\n class ModelWithSoftDeletes extends superclass {\n @beforeFind()\n @beforeFetch()\n static ignoreDeleted<Model extends typeof ModelWithSoftDeletes>(\n query: ModelQueryBuilderContractWithIgnoreDeleted<Model, InstanceType<Model>>\n ): void {\n if (query['ignoreDeleted'] === false) {\n return\n }\n const isGroupLimitQuery = query.clone().toQuery().includes('adonis_group_limit_counter')\n const deletedAtColumn = query.model.$getColumn('deletedAt')?.columnName\n\n const queryIgnoreDeleted = isGroupLimitQuery\n ? (query.knexQuery as any)['_single'].table\n : query\n\n queryIgnoreDeleted.whereNull(`${query.model.table}.${deletedAtColumn}`)\n }\n\n @beforePaginate()\n static ignoreDeletedPaginate<Model extends typeof ModelWithSoftDeletes>([countQuery, query]: [\n ModelQueryBuilderContractWithIgnoreDeleted<Model, InstanceType<Model>>,\n ModelQueryBuilderContractWithIgnoreDeleted<Model, InstanceType<Model>>,\n ]): void {\n countQuery['ignoreDeleted'] = query['ignoreDeleted']\n this.ignoreDeleted(countQuery)\n }\n\n static disableIgnore<Model extends typeof ModelWithSoftDeletes, Result = InstanceType<Model>>(\n this: Model,\n query: ModelQueryBuilderContractWithIgnoreDeleted<Model, Result>\n ): ModelQueryBuilderContractWithIgnoreDeleted<Model, Result> {\n if (query['ignoreDeleted'] === false) {\n return query\n }\n query['ignoreDeleted'] = false\n return query\n }\n\n /**\n * Fetch all models without filter by deleted_at\n */\n static withTrashed<Model extends typeof ModelWithSoftDeletes>(\n this: Model\n ): ModelQueryBuilderContractWithIgnoreDeleted<Model, InstanceType<T>> {\n const query = this.query() as ModelQueryBuilderContractWithIgnoreDeleted<\n Model,\n InstanceType<Model>\n >\n return this.disableIgnore(query)\n }\n\n /**\n * Fetch models only with deleted_at\n */\n static onlyTrashed<Model extends typeof ModelWithSoftDeletes>(\n this: Model\n ): ModelQueryBuilderContractWithIgnoreDeleted<Model, InstanceType<Model>> {\n const query = this.query() as ModelQueryBuilderContractWithIgnoreDeleted<\n Model,\n InstanceType<Model>\n >\n\n const deletedAtColumn = query.model.$getColumn('deletedAt')?.columnName\n return this.disableIgnore(query).whereNotNull(`${query.model.table}.${deletedAtColumn}`)\n }\n\n /**\n * Force delete instance property\n */\n $forceDelete = false\n\n /**\n * Soft deleted property\n */\n @column.dateTime()\n declare deletedAt?: DateTime | null\n\n /**\n * Computed trashed property\n */\n get trashed(): boolean {\n return this.deletedAt !== null\n }\n\n /**\n * Override default $getQueryFor method\n */\n $getQueryFor(\n action: 'insert' | 'update' | 'delete' | 'refresh',\n client: QueryClientContract\n ): any {\n /**\n * Soft Delete\n */\n const softDelete = async (): Promise<void> => {\n this.deletedAt = DateTime.local()\n await this.save()\n }\n\n if (action === 'delete' && !this.$forceDelete) {\n return { del: softDelete, delete: softDelete }\n }\n if (action === 'insert') {\n return super.$getQueryFor(action, client)\n }\n return super.$getQueryFor(action, client)\n }\n\n /**\n * Override default delete method\n */\n async delete(): Promise<void> {\n await super.delete()\n this.$isDeleted = this.$forceDelete\n }\n\n /**\n * Restore model\n */\n async restore(): Promise<this> {\n if (this.$isDeleted) {\n throw new Exception('Cannot restore a model instance is was force deleted', {\n code: 'E_MODEL_FORCE_DELETED',\n status: 500,\n })\n }\n if (!this.trashed) {\n return this\n }\n this.deletedAt = null\n await this.save()\n\n return this\n }\n\n /**\n * Force delete model\n */\n async forceDelete(): Promise<void> {\n this.$forceDelete = true\n await this.delete()\n }\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|
-
import {
|
|
2
|
-
extendModelQueryBuilder
|
|
3
|
-
} from "../chunk-4FLDNTDF.js";
|
|
4
|
-
import "../chunk-EUXUH3YW.js";
|
|
5
|
-
|
|
6
|
-
// providers/lucid_soft_deletes_provider.ts
|
|
7
|
-
var LucidSoftDeletesProvider = class {
|
|
8
|
-
constructor(app) {
|
|
9
|
-
this.app = app;
|
|
10
|
-
}
|
|
11
|
-
async boot() {
|
|
12
|
-
const { ModelQueryBuilder } = await this.app.import("@adonisjs/lucid/orm");
|
|
13
|
-
extendModelQueryBuilder(ModelQueryBuilder);
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
export {
|
|
17
|
-
LucidSoftDeletesProvider as default
|
|
18
|
-
};
|
|
19
|
-
//# sourceMappingURL=lucid_soft_deletes_provider.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../providers/lucid_soft_deletes_provider.ts"],"sourcesContent":["/*\n * adonis-lucid-soft-deletes\n *\n * (c) Lookin Anton <alsd@lookinlab.ru>\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\n/// <reference path=\"../src/types/querybuilder.ts\" />\n\nimport type { ApplicationService } from '@adonisjs/core/types'\nimport { extendModelQueryBuilder } from '../src/bindings/model_query_builder.js'\n\nexport default class LucidSoftDeletesProvider {\n constructor(protected app: ApplicationService) {}\n\n async boot() {\n const { ModelQueryBuilder } = await this.app.import('@adonisjs/lucid/orm')\n extendModelQueryBuilder(ModelQueryBuilder)\n }\n}\n"],"mappings":";;;;;;AAcA,IAAqB,2BAArB,MAA8C;AAAA,EAC5C,YAAsB,KAAyB;AAAzB;AAAA,EAA0B;AAAA,EAEhD,MAAM,OAAO;AACX,UAAM,EAAE,kBAAkB,IAAI,MAAM,KAAK,IAAI,OAAO,qBAAqB;AACzE,4BAAwB,iBAAiB;AAAA,EAC3C;AACF;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
//# sourceMappingURL=querybuilder.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|