@codenameryuu/adonis-lucid-soft-deletes 1.5.0 → 1.7.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 +65 -74
- package/build/src/mixin.js +4 -3
- package/package.json +12 -8
package/README.md
CHANGED
|
@@ -1,40 +1,33 @@
|
|
|
1
1
|
# @codenameryuu/adonis-lucid-soft-deletes
|
|
2
2
|
|
|
3
|
-
This addon adds the functionality to soft deletes Lucid Models through the `deleted_at` flag in AdonisJS
|
|
3
|
+
This addon adds the functionality to soft deletes Lucid Models through the `deleted_at` flag in AdonisJS.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Requirement
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
Instead, a `deleted_at` attribute is set on the model indicating the date
|
|
10
|
-
and time at which the model was "deleted".
|
|
11
|
-
|
|
12
|
-
:point_right: The SoftDeletes mixin will automatically add the `deleted_at` attribute
|
|
13
|
-
as Luxon / DateTime instance.
|
|
7
|
+
- Adonis Js 7
|
|
8
|
+
- Lucid 22 or higher
|
|
14
9
|
|
|
15
10
|
## Installation
|
|
16
11
|
|
|
17
|
-
Install
|
|
12
|
+
- Install the package
|
|
18
13
|
|
|
19
14
|
```bash
|
|
20
15
|
yarn add @codenameryuu/adonis-lucid-soft-deletes
|
|
21
16
|
```
|
|
22
17
|
|
|
23
|
-
|
|
18
|
+
- Configure the package
|
|
24
19
|
|
|
25
20
|
```bash
|
|
26
21
|
node ace configure @codenameryuu/adonis-lucid-soft-deletes
|
|
27
22
|
```
|
|
28
23
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
Make sure to register the provider inside `adonisrc.ts` file.
|
|
24
|
+
- Make sure to register the provider inside `adonisrc.ts` file.
|
|
32
25
|
|
|
33
26
|
```typescript
|
|
34
27
|
providers: [
|
|
35
28
|
// ...
|
|
36
29
|
() => import('@codenameryuu/adonis-lucid-soft-deletes/provider'),
|
|
37
|
-
]
|
|
30
|
+
],
|
|
38
31
|
```
|
|
39
32
|
|
|
40
33
|
You should add the `deleted_at` column to your database tables for models with soft deletes.
|
|
@@ -43,8 +36,8 @@ You should add the `deleted_at` column to your database tables for models with s
|
|
|
43
36
|
// migrations/1234566666_users.ts
|
|
44
37
|
import { BaseSchema } from '@adonisjs/lucid/schema'
|
|
45
38
|
|
|
46
|
-
export default class
|
|
47
|
-
protected tableName = '
|
|
39
|
+
export default class extends BaseSchema {
|
|
40
|
+
protected tableName = 'products'
|
|
48
41
|
|
|
49
42
|
async up() {
|
|
50
43
|
this.schema.createTable(this.tableName, (table) => {
|
|
@@ -60,9 +53,9 @@ export default class Users extends BaseSchema {
|
|
|
60
53
|
|
|
61
54
|
```typescript
|
|
62
55
|
import { compose } from '@adonisjs/core/helpers'
|
|
63
|
-
import { SoftDeletes } from 'adonis-lucid-soft-deletes'
|
|
56
|
+
import { SoftDeletes } from '@codenameryuu/adonis-lucid-soft-deletes'
|
|
64
57
|
|
|
65
|
-
export default class
|
|
58
|
+
export default class Product extends compose(BaseModel, SoftDeletes) {
|
|
66
59
|
// ...columns and props
|
|
67
60
|
}
|
|
68
61
|
```
|
|
@@ -72,23 +65,23 @@ will be set to the current date and time. However, the model's database record w
|
|
|
72
65
|
|
|
73
66
|
```typescript
|
|
74
67
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
75
|
-
import
|
|
68
|
+
import Product from '#models/product'
|
|
76
69
|
|
|
77
|
-
export default class
|
|
70
|
+
export default class ProductsController {
|
|
78
71
|
/**
|
|
79
|
-
* Delete
|
|
80
|
-
* DELETE /
|
|
72
|
+
* Delete product by id
|
|
73
|
+
* DELETE /products/:id
|
|
81
74
|
*/
|
|
82
|
-
async destroy({
|
|
83
|
-
const
|
|
84
|
-
await
|
|
75
|
+
async destroy({ request, response }: HttpContext) {
|
|
76
|
+
const product = await Product.findOrFail(request.input('id'))
|
|
77
|
+
await product.delete()
|
|
85
78
|
|
|
86
|
-
return
|
|
79
|
+
return product // or response.noContent()
|
|
87
80
|
}
|
|
88
81
|
}
|
|
89
82
|
```
|
|
90
83
|
|
|
91
|
-
> :boom: Soft delete only works for model instances. `await
|
|
84
|
+
> :boom: Soft delete only works for model instances. `await Product.query().delete()` as before
|
|
92
85
|
> will delete models from database
|
|
93
86
|
|
|
94
87
|
:point_right: When querying a model that uses soft deletes, the soft deleted models
|
|
@@ -98,19 +91,19 @@ To determine if a given model instance has been soft deleted, you may use the `.
|
|
|
98
91
|
|
|
99
92
|
```typescript
|
|
100
93
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
101
|
-
import
|
|
94
|
+
import Product from '#models/product'
|
|
102
95
|
|
|
103
|
-
export default class
|
|
96
|
+
export default class ProductsController {
|
|
104
97
|
/**
|
|
105
|
-
* Get
|
|
106
|
-
* GET /
|
|
98
|
+
* Get product by id
|
|
99
|
+
* GET /products/:id
|
|
107
100
|
*/
|
|
108
|
-
async show({
|
|
109
|
-
const
|
|
110
|
-
if (
|
|
101
|
+
async show({ request, response }: HttpContext) {
|
|
102
|
+
const product = await Product.withTrashed().where('id', request.input('id')).firstOrFail()
|
|
103
|
+
if (product.trashed) {
|
|
111
104
|
return response.forbidden()
|
|
112
105
|
}
|
|
113
|
-
return
|
|
106
|
+
return product
|
|
114
107
|
}
|
|
115
108
|
}
|
|
116
109
|
```
|
|
@@ -119,9 +112,9 @@ export default class UsersController {
|
|
|
119
112
|
|
|
120
113
|
```typescript
|
|
121
114
|
import { compose } from '@adonisjs/core/helpers'
|
|
122
|
-
import { SoftDeletes } from 'adonis-lucid-soft-deletes'
|
|
115
|
+
import { SoftDeletes } from '@codenameryuu/adonis-lucid-soft-deletes'
|
|
123
116
|
|
|
124
|
-
export default class
|
|
117
|
+
export default class Product extends compose(BaseModel, SoftDeletes) {
|
|
125
118
|
// ...columns and props
|
|
126
119
|
|
|
127
120
|
@column.dateTime({ columnName: 'customDeletedAtColumn' })
|
|
@@ -138,23 +131,23 @@ The `restore` method will set the model's `deleted_at` column to `null` :
|
|
|
138
131
|
|
|
139
132
|
```ts
|
|
140
133
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
141
|
-
import
|
|
134
|
+
import Product from '#models/product'
|
|
142
135
|
|
|
143
|
-
export default class
|
|
136
|
+
export default class TrashProductsController {
|
|
144
137
|
/**
|
|
145
|
-
* Update trashed
|
|
146
|
-
* PUT /trash/
|
|
138
|
+
* Update trashed product by id
|
|
139
|
+
* PUT /trash/products/:id
|
|
147
140
|
*/
|
|
148
|
-
async update({
|
|
149
|
-
const
|
|
150
|
-
await
|
|
141
|
+
async update({ request }: HttpContext) {
|
|
142
|
+
const product = await Product.withTrashed().where('id', request.input('id')).firstOrFail()
|
|
143
|
+
await product.restore()
|
|
151
144
|
|
|
152
|
-
return
|
|
145
|
+
return product
|
|
153
146
|
|
|
154
147
|
// or
|
|
155
148
|
|
|
156
|
-
await
|
|
157
|
-
await
|
|
149
|
+
await Product.withTrashed().where('id', request.input('id')).restore()
|
|
150
|
+
await Product.query().withTrashed().where('id', request.input('id')).restore()
|
|
158
151
|
}
|
|
159
152
|
}
|
|
160
153
|
```
|
|
@@ -166,16 +159,16 @@ You may use the `.forceDelete()` method to permanently remove a soft deleted mod
|
|
|
166
159
|
|
|
167
160
|
```typescript
|
|
168
161
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
169
|
-
import
|
|
162
|
+
import Product from '#models/product'
|
|
170
163
|
|
|
171
|
-
export default class
|
|
164
|
+
export default class ProductsController {
|
|
172
165
|
/**
|
|
173
|
-
* Delete
|
|
174
|
-
* DELETE /
|
|
166
|
+
* Delete product by id
|
|
167
|
+
* DELETE /products/:id
|
|
175
168
|
*/
|
|
176
|
-
async destroy({
|
|
177
|
-
const
|
|
178
|
-
await
|
|
169
|
+
async destroy({ request, response }: HttpContext) {
|
|
170
|
+
const product = await Product.findOrFail(request.input('id'))
|
|
171
|
+
await product.forceDelete()
|
|
179
172
|
|
|
180
173
|
return response.noContent()
|
|
181
174
|
}
|
|
@@ -190,25 +183,19 @@ by calling the `.withTrashed()` method on the model:
|
|
|
190
183
|
|
|
191
184
|
```typescript
|
|
192
185
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
193
|
-
import
|
|
186
|
+
import Product from '#models/product'
|
|
194
187
|
|
|
195
|
-
export default class
|
|
188
|
+
export default class ProductsController {
|
|
196
189
|
/**
|
|
197
|
-
* Get a list
|
|
198
|
-
* GET /
|
|
190
|
+
* Get a list products
|
|
191
|
+
* GET /products?withTrashed=1
|
|
199
192
|
*/
|
|
200
193
|
async index({ request }: HttpContext) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
return usersQuery.exec()
|
|
204
|
-
|
|
205
|
-
// or
|
|
206
|
-
|
|
207
|
-
return User.query()
|
|
194
|
+
return Product.query()
|
|
208
195
|
.if(request.input('withTrashed'), (query) => {
|
|
209
196
|
query.withTrashed()
|
|
210
197
|
})
|
|
211
|
-
.
|
|
198
|
+
.paginate(request.input('page', 1), request.input('perPage', 10))
|
|
212
199
|
}
|
|
213
200
|
}
|
|
214
201
|
```
|
|
@@ -219,15 +206,15 @@ The `.onlyTrashed()` method will retrieve **only** soft deleted models:
|
|
|
219
206
|
|
|
220
207
|
```typescript
|
|
221
208
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
222
|
-
import
|
|
209
|
+
import Product from '#models/product'
|
|
223
210
|
|
|
224
|
-
export default class
|
|
211
|
+
export default class TrashProductsController {
|
|
225
212
|
/**
|
|
226
|
-
* Get a list trashed
|
|
227
|
-
* GET /trash/
|
|
213
|
+
* Get a list trashed products
|
|
214
|
+
* GET /trash/products
|
|
228
215
|
*/
|
|
229
216
|
async index({ request }: HttpContext) {
|
|
230
|
-
return
|
|
217
|
+
return Product.onlyTrashed().paginate(request.input('page', 1), request.input('perPage', 10))
|
|
231
218
|
}
|
|
232
219
|
}
|
|
233
220
|
```
|
|
@@ -238,6 +225,10 @@ Methods `.withTrashed()` , `.onlyTrashed()` and `.restore()` also available
|
|
|
238
225
|
in ModelQueryBuilder for models with soft delete, example:
|
|
239
226
|
|
|
240
227
|
```typescript
|
|
241
|
-
await
|
|
242
|
-
await
|
|
228
|
+
await Product.query().withTrashed().paginate(request.input('page', 1), request.input('perPage', 10))
|
|
229
|
+
await Product.query().onlyTrashed().paginate(request.input('page', 1), request.input('perPage', 10))
|
|
243
230
|
```
|
|
231
|
+
|
|
232
|
+
## License
|
|
233
|
+
|
|
234
|
+
This package is open-sourced software licensed under the [MIT license](LICENSE.md).
|
package/build/src/mixin.js
CHANGED
|
@@ -23,9 +23,10 @@ export function SoftDeletes(superclass) {
|
|
|
23
23
|
}
|
|
24
24
|
const isGroupLimitQuery = query.clone().toQuery().includes('adonis_group_limit_counter');
|
|
25
25
|
const deletedAtColumn = query.model.$getColumn('deletedAt')?.columnName;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
if (!deletedAtColumn) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const queryIgnoreDeleted = isGroupLimitQuery ? query.knexQuery : query;
|
|
29
30
|
queryIgnoreDeleted.whereNull(`${query.model.table}.${deletedAtColumn}`);
|
|
30
31
|
}
|
|
31
32
|
static ignoreDeletedPaginate([countQuery, query]) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codenameryuu/adonis-lucid-soft-deletes",
|
|
3
|
-
"description": "Addon for soft deletes Adonis JS
|
|
4
|
-
"version": "1.
|
|
3
|
+
"description": "Addon for soft deletes Adonis JS with Lucid ORM",
|
|
4
|
+
"version": "1.7.0",
|
|
5
5
|
"author": "codenameryuu",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/codenameryuu/adonis-lucid-soft-deletes#readme",
|
|
@@ -25,15 +25,19 @@
|
|
|
25
25
|
"./provider": "./build/providers/lucid_soft_deletes_provider.js"
|
|
26
26
|
},
|
|
27
27
|
"scripts": {
|
|
28
|
+
"lint": "eslint .",
|
|
29
|
+
"format": "prettier --write .",
|
|
28
30
|
"clean": "del-cli build",
|
|
31
|
+
"precompile": "npm run lint && npm run clean",
|
|
32
|
+
"compile": "tsc",
|
|
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
|
+
"pretest": "npm run lint",
|
|
36
|
+
"test": "c8 npm run quick:test",
|
|
29
37
|
"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",
|
|
35
38
|
"version": "npm run build",
|
|
36
|
-
"prepublishOnly": "npm run build"
|
|
39
|
+
"prepublishOnly": "npm run build",
|
|
40
|
+
"release": "np"
|
|
37
41
|
},
|
|
38
42
|
"devDependencies": {
|
|
39
43
|
"@adonisjs/assembler": "^7.7.0",
|