@codenameryuu/adonis-lucid-soft-deletes 1.5.0 → 1.6.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.
Files changed (2) hide show
  1. package/README.md +65 -74
  2. 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 7.
3
+ This addon adds the functionality to soft deletes Lucid Models through the `deleted_at` flag in AdonisJS.
4
4
 
5
- ## Introduction
5
+ ## Requirement
6
6
 
7
- Sometimes you may wish to "no-delete" a model from database.
8
- When models are soft deleted, they are not actually removed from your database.
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 it using `npm` , `yarn` or `pnpm` .
12
+ - Install the package
18
13
 
19
14
  ```bash
20
15
  yarn add @codenameryuu/adonis-lucid-soft-deletes
21
16
  ```
22
17
 
23
- After install call `configure` :
18
+ - Configure the package
24
19
 
25
20
  ```bash
26
21
  node ace configure @codenameryuu/adonis-lucid-soft-deletes
27
22
  ```
28
23
 
29
- ## Usage
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 Users extends BaseSchema {
47
- protected tableName = 'users'
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 User extends compose(BaseModel, SoftDeletes) {
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 User from '#models/user'
68
+ import Product from '#models/product'
76
69
 
77
- export default class UsersController {
70
+ export default class ProductsController {
78
71
  /**
79
- * Delete user by id
80
- * DELETE /users/:id
72
+ * Delete product by id
73
+ * DELETE /products/:id
81
74
  */
82
- async destroy({ params, response }: HttpContext) {
83
- const user = await User.findOrFail(params.id)
84
- await user.delete()
75
+ async destroy({ request, response }: HttpContext) {
76
+ const product = await Product.findOrFail(request.input('id'))
77
+ await product.delete()
85
78
 
86
- return user // or response.noContent()
79
+ return product // or response.noContent()
87
80
  }
88
81
  }
89
82
  ```
90
83
 
91
- > :boom: Soft delete only works for model instances. `await User.query().delete()` as before
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 User from '#models/user'
94
+ import Product from '#models/product'
102
95
 
103
- export default class UsersController {
96
+ export default class ProductsController {
104
97
  /**
105
- * Get user by id
106
- * GET /users/:id
98
+ * Get product by id
99
+ * GET /products/:id
107
100
  */
108
- async show({ params }: HttpContext) {
109
- const user = await User.withTrashed().where('id', params.id).firstOrFail()
110
- if (user.trashed) {
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 user
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 User extends compose(BaseModel, SoftDeletes) {
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 User from '#models/user'
134
+ import Product from '#models/product'
142
135
 
143
- export default class TrashUsersController {
136
+ export default class TrashProductsController {
144
137
  /**
145
- * Update trashed user by id
146
- * PUT /trash/users/:id
138
+ * Update trashed product by id
139
+ * PUT /trash/products/:id
147
140
  */
148
- async update({ params }: HttpContext) {
149
- const user = await User.withTrashed().where('id', params.id).firstOrFail()
150
- await user.restore()
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 user
145
+ return product
153
146
 
154
147
  // or
155
148
 
156
- await User.withTrashed().where('id', params.id).restore()
157
- await User.query().withTrashed().where('id', params.id).restore()
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 User from '#models/user'
162
+ import Product from '#models/product'
170
163
 
171
- export default class UsersController {
164
+ export default class ProductsController {
172
165
  /**
173
- * Delete user by id
174
- * DELETE /users/:id
166
+ * Delete product by id
167
+ * DELETE /products/:id
175
168
  */
176
- async destroy({ params, response }: HttpContext) {
177
- const user = await User.findOrFail(params.id)
178
- await user.forceDelete()
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 User from '#models/user'
186
+ import Product from '#models/product'
194
187
 
195
- export default class UsersController {
188
+ export default class ProductsController {
196
189
  /**
197
- * Get a list users
198
- * GET /users?withTrashed=1
190
+ * Get a list products
191
+ * GET /products?withTrashed=1
199
192
  */
200
193
  async index({ request }: HttpContext) {
201
- const usersQuery = request.input('withTrashed') ? User.withTrashed() : User.query()
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
- .exec()
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 User from '#models/user'
209
+ import Product from '#models/product'
223
210
 
224
- export default class TrashUsersController {
211
+ export default class TrashProductsController {
225
212
  /**
226
- * Get a list trashed users
227
- * GET /trash/users
213
+ * Get a list trashed products
214
+ * GET /trash/products
228
215
  */
229
216
  async index({ request }: HttpContext) {
230
- return User.onlyTrashed().exec()
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 User.query().withTrashed().exec()
242
- await User.query().onlyTrashed().restore()
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/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 7 Lucid ORM",
4
- "version": "1.5.0",
3
+ "description": "Addon for soft deletes Adonis JS with Lucid ORM",
4
+ "version": "1.6.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",