@@ -78,111 +78,131 @@ function __generator(thisArg, body) {
78
78
79
79
function Model ( context , model ) {
80
80
/**
81
- * Flags are visible by default during model serialization.
82
- * They can be hidden by setting `exposeFlagsExternally` to false.
81
+ * Soft delete model(s) matching a condition.
83
82
*/
84
- var $fields = model . prototype . $fields ;
85
- model . prototype . $fields = function ( ) {
86
- var _a ;
87
- var fields = $fields . call ( this ) ;
88
- var _b = context . createConfig ( this . $self ( ) . softDeleteConfig ) , key = _b . key , flagName = _b . flagName ;
89
- return Object . assign ( { } , fields , ( _a = { } ,
90
- _a [ key ] = this . $self ( ) . attr ( null ) ,
91
- _a [ flagName ] = this . $self ( ) . attr ( false ) ,
92
- _a ) ) ;
93
- } ;
94
- /**
95
- * Flags are visible by default during model serialization.
96
- * They can be hidden by setting `exposeFlagsExternally` to false.
97
- */
98
- var $toJson = model . prototype . $toJson ;
99
- model . prototype . $toJson = function ( ) {
100
- var toJson = $toJson . call ( this ) ;
101
- var config = context . createConfig ( this . $self ( ) . softDeleteConfig ) ;
102
- if ( config . exposeFlagsExternally !== true ) {
103
- /* istanbul ignore next */
104
- var _a = toJson , _b = config . key , k = _a [ _b ] , _c = config . flagName , fn = _a [ _c ] , json = __rest ( _a , [ typeof _b === "symbol" ? _b : _b + "" , typeof _c === "symbol" ? _c : _c + "" ] ) ;
105
- return json ;
106
- }
107
- return toJson ;
108
- } ;
109
- /**
110
- * ...
111
- */
112
- var $fill = model . prototype . $fill ;
113
- model . prototype . $fill = function ( record ) {
114
- var _a , _b ;
115
- $fill . call ( this , record ) ;
116
- if ( record ) {
117
- var _c = context . createConfig ( this . $self ( ) . softDeleteConfig ) , key = _c . key , flagName = _c . flagName ;
118
- this [ key ] = ( _a = record [ key ] ) !== null && _a !== void 0 ? _a : null ;
119
- this [ flagName ] = ( _b = record [ flagName ] ) !== null && _b !== void 0 ? _b : false ;
120
- }
83
+ model . softDelete = function ( payload ) {
84
+ return this . dispatch ( 'softDelete' , payload ) ;
121
85
} ;
122
86
/**
123
- * Soft delete the record on a model instance.
87
+ * Soft delete a model instance.
124
88
*/
125
- model . prototype . $softDelete = function ( ) {
89
+ model . prototype . $softDelete = function ( hydrate ) {
126
90
return __awaiter ( this , void 0 , void 0 , function ( ) {
127
- var record , _a , key , flagName ;
91
+ var model , _a , key , flagName ;
128
92
return __generator ( this , function ( _b ) {
129
93
switch ( _b . label ) {
130
94
case 0 : return [ 4 /*yield*/ , this . $dispatch ( 'softDelete' , this . $self ( ) . getIdFromRecord ( this ) ) ] ;
131
95
case 1 :
132
- record = _b . sent ( ) ;
96
+ model = _b . sent ( ) ;
97
+ if ( hydrate ) {
98
+ this . $fill ( model . $getAttributes ( ) ) ;
99
+ return [ 2 /*return*/ , this ] ;
100
+ }
133
101
_a = context . createConfig ( this . $self ( ) . softDeleteConfig ) , key = _a . key , flagName = _a . flagName ;
134
- this [ key ] = record [ key ] ;
135
- this [ flagName ] = record [ flagName ] ;
136
- return [ 2 /*return*/ , record ] ;
102
+ this [ key ] = model [ key ] ;
103
+ this [ flagName ] = model [ flagName ] ;
104
+ return [ 2 /*return*/ , model ] ;
137
105
}
138
106
} ) ;
139
107
} ) ;
140
108
} ;
141
109
/**
142
- * Soft delete the record on a model instance.
143
- * This method is deprecated and will continue to warn dev environments of
144
- * this deprecation until completely removed.
110
+ * Restore a model instance.
111
+ */
112
+ model . prototype . $restore = function ( hydrate ) {
113
+ return __awaiter ( this , void 0 , void 0 , function ( ) {
114
+ var _a , key , flagName , model ;
115
+ var _b ;
116
+ return __generator ( this , function ( _c ) {
117
+ switch ( _c . label ) {
118
+ case 0 :
119
+ _a = context . createConfig ( this . $self ( ) . softDeleteConfig ) , key = _a . key , flagName = _a . flagName ;
120
+ return [ 4 /*yield*/ , this . $dispatch ( 'update' , {
121
+ where : this . $self ( ) . getIdFromRecord ( this ) ,
122
+ data : ( _b = { } ,
123
+ _b [ key ] = null ,
124
+ _b [ flagName ] = false ,
125
+ _b )
126
+ } ) ] ;
127
+ case 1 :
128
+ model = _c . sent ( ) ;
129
+ if ( hydrate ) {
130
+ this . $fill ( model . $getAttributes ( ) ) ;
131
+ return [ 2 /*return*/ , this ] ;
132
+ }
133
+ this [ key ] = model [ key ] ;
134
+ this [ flagName ] = model [ flagName ] ;
135
+ return [ 2 /*return*/ , this ] ;
136
+ }
137
+ } ) ;
138
+ } ) ;
139
+ } ;
140
+ /**
141
+ * Determine if the model instance has been soft deleted.
142
+ */
143
+ model . prototype . $trashed = function ( ) {
144
+ var flagName = context . createConfig ( this . $self ( ) . softDeleteConfig ) . flagName ;
145
+ return this [ flagName ] === true ;
146
+ } ;
147
+ /**
148
+ * Soft-delete a model instance.
149
+ * This method is deprecated and will warn users until retired.
145
150
* @deprecated since v1.2.0
146
151
*/
147
- model . prototype . softDelete = function ( ) {
152
+ model . prototype . softDelete = function ( hydrate ) {
148
153
/* istanbul ignore next */
149
154
if ( process . env . NODE_ENV !== 'production' ) {
150
155
console . warn ( 'The `softDelete` instance method has been deprecated. Please use `$softDelete`.' ) ;
151
156
}
152
- return this . $softDelete ( ) ;
157
+ return this . $softDelete ( hydrate ) ;
153
158
} ;
154
159
/**
155
- * Determine if the instance has been soft deleted .
160
+ * Add supporting attributes to model .
156
161
*/
157
- model . prototype . trashed = function ( ) {
158
- var flagName = context . createConfig ( this . $self ( ) . softDeleteConfig ) . flagName ;
159
- return this [ flagName ] === true ;
162
+ var $fields = model . prototype . $fields ;
163
+ model . prototype . $fields = function ( ) {
164
+ var _a ;
165
+ var fields = $fields . call ( this ) ;
166
+ var _b = context . createConfig ( this . $self ( ) . softDeleteConfig ) , key = _b . key , flagName = _b . flagName ;
167
+ return Object . assign ( { } , fields , ( _a = { } ,
168
+ _a [ key ] = this . $self ( ) . attr ( null ) ,
169
+ _a [ flagName ] = this . $self ( ) . attr ( false ) ,
170
+ _a ) ) ;
160
171
} ;
161
172
/**
162
- * Soft delete record(s) matching a condition.
173
+ * Flags are visible by default during model serialization. They can be hidden
174
+ * by setting `exposeFlagsExternally` to false.
163
175
*/
164
- model . softDelete = function ( payload ) {
165
- return this . dispatch ( 'softDelete' , payload ) ;
176
+ var $toJson = model . prototype . $toJson ;
177
+ model . prototype . $toJson = function ( ) {
178
+ var toJson = $toJson . call ( this ) ;
179
+ var config = context . createConfig ( this . $self ( ) . softDeleteConfig ) ;
180
+ if ( config . exposeFlagsExternally !== true ) {
181
+ /* istanbul ignore next */
182
+ var _a = toJson , _b = config . key , k = _a [ _b ] , _c = config . flagName , fn = _a [ _c ] , json = __rest ( _a , [ typeof _b === "symbol" ? _b : _b + "" , typeof _c === "symbol" ? _c : _c + "" ] ) ;
183
+ return json ;
184
+ }
185
+ return toJson ;
166
186
} ;
167
187
}
168
188
169
189
function Query ( context , query ) {
170
190
/**
171
- * Determine if soft deletes should be filtered exclusively.
191
+ * Determine if soft deleted models should be filtered exclusively.
172
192
* true = only soft deletes
173
193
* false = include soft deletes
174
194
* null = exclude soft deletes
175
195
*/
176
196
query . prototype . softDeletesFilter = null ;
177
197
/**
178
- * Constraint includes soft deletes .
198
+ * Constraint includes soft deleted models .
179
199
*/
180
200
query . prototype . withTrashed = function ( ) {
181
201
this . softDeletesFilter = false ;
182
202
return this ;
183
203
} ;
184
204
/**
185
- * Constraint restricts to only soft deletes .
205
+ * Constraint restricts to only soft deleted models .
186
206
*/
187
207
query . prototype . onlyTrashed = function ( ) {
188
208
this . softDeletesFilter = true ;
@@ -199,21 +219,24 @@ function Query(context, query) {
199
219
}
200
220
return this . onlyTrashed ( ) ;
201
221
} ;
222
+ /**
223
+ * Process the model(s) to be soft deleted.
224
+ */
202
225
query . prototype . softDelete = function ( condition ) {
203
226
var _a ;
204
- var config = context . createConfig ( this . model . softDeleteConfig ) ;
227
+ var _b = context . createConfig ( this . model . softDeleteConfig ) , key = _b . key , flagName = _b . flagName , mutator = _b . mutator ;
205
228
var value = Date . now ( ) ;
206
- value = typeof config . mutator === 'function' ? config . mutator ( value ) : value ;
229
+ value = typeof mutator === 'function' ? mutator ( value ) : value ;
207
230
var data = ( _a = { } ,
208
- _a [ config . key ] = value ,
209
- _a [ config . flagName ] = true ,
231
+ _a [ key ] = value ,
232
+ _a [ flagName ] = true ,
210
233
_a ) ;
211
234
if ( Array . isArray ( condition ) ) {
212
235
// Array of primary keys
213
236
if ( ! this . model . isCompositePrimaryKey ( ) ) {
214
237
return this . model . update ( {
215
238
data : data ,
216
- where : function ( r ) { return condition . includes ( r [ r . $primaryKey ( ) ] ) ; }
239
+ where : function ( record ) { return condition . includes ( record [ record . $primaryKey ( ) ] ) ; }
217
240
} ) ;
218
241
}
219
242
// Array of composite primary keys
@@ -227,19 +250,18 @@ function Query(context, query) {
227
250
} ) ;
228
251
}
229
252
}
230
- return this . model . update ( {
231
- data : data ,
232
- where : condition
233
- } ) ;
253
+ return this . model . update ( { data : data , where : condition } ) ;
234
254
} ;
235
255
/**
236
- * Fetch all soft deletes from the store
256
+ * Fetch all soft deletes from the store.
237
257
*/
238
258
query . prototype . allTrashed = function ( ) {
239
- return this . onlyTrashed ( ) . get ( ) ;
259
+ return this . newQuery ( )
260
+ . onlyTrashed ( )
261
+ . get ( ) ;
240
262
} ;
241
263
/**
242
- * Fetch all soft deletes from the store and group them by entity.
264
+ * Fetch all soft deletes from the store and group by entity.
243
265
*/
244
266
query . allTrashed = function ( store ) {
245
267
var _this = this ;
@@ -251,32 +273,32 @@ function Query(context, query) {
251
273
} , { } ) ;
252
274
} ;
253
275
/**
254
- * Global select hook prevents soft deletes being selected unless queries are
255
- * explicity used with `withTrashed` or `onlyTrashed`.
276
+ * Global select hook prevents soft deleted models from being selected unless
277
+ * queries are explicity chained with `withTrashed` or `onlyTrashed`.
256
278
*/
257
279
query . on ( 'beforeSelect' , function ( models ) {
258
280
var _this = this ;
259
281
return models . filter ( function ( model ) {
260
- // Only soft deletes.
282
+ // Only soft deletes
261
283
if ( _this . softDeletesFilter === true ) {
262
- return model . trashed ( ) ;
284
+ return model . $ trashed( ) ;
263
285
}
264
- // Include soft deletes.
286
+ // Include soft deletes
265
287
if ( _this . softDeletesFilter === false ) {
266
288
return models ;
267
289
}
268
- // Exclude soft deletes.
269
- return ! model . trashed ( ) ;
290
+ // Exclude soft deletes
291
+ return ! model . $ trashed( ) ;
270
292
} ) ;
271
293
} ) ;
272
294
}
273
295
274
- function Modules ( context , modules ) {
296
+ function Actions ( _context , actions ) {
275
297
var _this = this ;
276
298
/**
277
- * Soft delete records from the store.
299
+ * Soft delete records and persist to the store.
278
300
*/
279
- modules . actions . softDelete = function ( context , payload ) { return __awaiter ( _this , void 0 , void 0 , function ( ) {
301
+ actions . softDelete = function ( context , payload ) { return __awaiter ( _this , void 0 , void 0 , function ( ) {
280
302
var state , entity , where ;
281
303
var _a ;
282
304
return __generator ( this , function ( _b ) {
@@ -286,10 +308,22 @@ function Modules(context, modules) {
286
308
return [ 2 /*return*/ , context . dispatch ( state . $connection + "/softDelete" , { entity : entity , where : where } , { root : true } ) ] ;
287
309
} ) ;
288
310
} ) ; } ;
311
+ }
312
+
313
+ function Getters ( _context , getters ) {
314
+ /**
315
+ * Get all trashed records from the store and group by entity.
316
+ */
317
+ getters . allTrashed = function ( state , _getters , _rootState , rootGetters ) { return function ( ) {
318
+ return rootGetters [ state . $connection + "/allTrashed" ] ( state . $name ) ;
319
+ } ; } ;
320
+ }
321
+
322
+ function RootActions ( context , rootActions ) {
289
323
/**
290
- * Soft delete records from the store.
324
+ * Soft delete records and persist the store.
291
325
*/
292
- modules . rootActions . softDelete = function ( _context , payload ) {
326
+ rootActions . softDelete = function ( _context , payload ) {
293
327
return __awaiter ( this , void 0 , void 0 , function ( ) {
294
328
var entity , where ;
295
329
return __generator ( this , function ( _a ) {
@@ -298,16 +332,13 @@ function Modules(context, modules) {
298
332
} ) ;
299
333
} ) ;
300
334
} ;
335
+ }
336
+
337
+ function RootGetters ( context , rootGetters ) {
301
338
/**
302
- * Get all soft deletes keyed by entity.
303
- */
304
- modules . getters . allTrashed = function ( state , _getters , _rootState , rootGetters ) { return function ( ) {
305
- return rootGetters [ state . $connection + "/allTrashed" ] ( state . $name ) ;
306
- } ; } ;
307
- /**
308
- * Get all soft deletes of given entity.
339
+ * Get all trashed records belonging to an entity.
309
340
*/
310
- modules . rootGetters . allTrashed = function ( _state ) {
341
+ rootGetters . allTrashed = function ( _state ) {
311
342
var _this = this ;
312
343
return function ( entity ) {
313
344
if ( entity ) {
@@ -332,12 +363,10 @@ var VuexORMSoftDelete = /** @class */ (function () {
332
363
function VuexORMSoftDelete ( components , config ) {
333
364
this . model = components . Model ;
334
365
this . query = components . Query ;
335
- this . modules = {
336
- actions : components . Actions ,
337
- getters : components . Getters ,
338
- rootGetters : components . RootGetters ,
339
- rootActions : components . RootActions
340
- } ;
366
+ this . actions = components . Actions ;
367
+ this . getters = components . Getters ;
368
+ this . rootActions = components . RootActions ;
369
+ this . rootGetters = components . RootGetters ;
341
370
this . config = this . createConfig ( config ) ;
342
371
}
343
372
/**
@@ -353,7 +382,10 @@ var VuexORMSoftDelete = /** @class */ (function () {
353
382
VuexORMSoftDelete . prototype . plugin = function ( ) {
354
383
Model ( this , this . model ) ;
355
384
Query ( this , this . query ) ;
356
- Modules ( this , this . modules ) ;
385
+ Actions ( this , this . actions ) ;
386
+ Getters ( this , this . getters ) ;
387
+ RootActions ( this , this . rootActions ) ;
388
+ RootGetters ( this , this . rootGetters ) ;
357
389
} ;
358
390
return VuexORMSoftDelete ;
359
391
} ( ) ) ;
0 commit comments