Skip to content

Commit fbf31b8

Browse files
committed
chore: tidy up code
1 parent 47d6fc4 commit fbf31b8

File tree

6 files changed

+34
-35
lines changed

6 files changed

+34
-35
lines changed

src/mixins/Actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default function Actions(
66
actions: ActionsContract
77
): void {
88
/**
9-
* Trash records and persist to the store.
9+
* Soft delete records and persist to the store.
1010
*/
1111
actions.softDelete = async (
1212
context: ActionContext,

src/mixins/Query.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ export default function Query(
77
query: typeof BaseQuery
88
): void {
99
/**
10-
* Determine if trashed records should be filtered exclusively.
11-
* true = only trashed records
12-
* false = include trashed records
13-
* null = exclude trashed records
10+
* Determine if soft deleted models should be filtered exclusively.
11+
* true = only soft deletes
12+
* false = include soft deletes
13+
* null = exclude soft deletes
1414
*/
1515
query.prototype.softDeletesFilter = null
1616

1717
/**
18-
* Constraint includes trashed records.
18+
* Constraint includes soft deleted models.
1919
*/
2020
query.prototype.withTrashed = function() {
2121
this.softDeletesFilter = false
@@ -24,7 +24,7 @@ export default function Query(
2424
}
2525

2626
/**
27-
* Constraint restricts to only trashed records.
27+
* Constraint restricts to only soft deleted models.
2828
*/
2929
query.prototype.onlyTrashed = function() {
3030
this.softDeletesFilter = true
@@ -48,28 +48,28 @@ export default function Query(
4848
}
4949

5050
/**
51-
* Process the record(s) to be trashed.
52-
* The method expects the condition to be a single or collection of a primary
53-
* key, a single or collection of a composite key, or a expression closure.
51+
* Process the model(s) to be soft deleted.
5452
*/
5553
query.prototype.softDelete = function(condition: any) {
56-
const config = context.createConfig(this.model.softDeleteConfig)
54+
const { key, flagName, mutator } = context.createConfig(
55+
this.model.softDeleteConfig
56+
)
5757

5858
let value = Date.now()
5959

60-
value = typeof config.mutator === 'function' ? config.mutator(value) : value
60+
value = typeof mutator === 'function' ? mutator(value) : value
6161

6262
const data = {
63-
[config.key]: value,
64-
[config.flagName]: true
63+
[key]: value,
64+
[flagName]: true
6565
}
6666

6767
if (Array.isArray(condition)) {
6868
// Array of primary keys
6969
if (!this.model.isCompositePrimaryKey()) {
7070
return this.model.update({
7171
data,
72-
where: (r) => condition.includes(r[r.$primaryKey()])
72+
where: (record) => condition.includes(record[record.$primaryKey()])
7373
})
7474
}
7575

@@ -86,21 +86,20 @@ export default function Query(
8686
}
8787
}
8888

89-
return this.model.update({
90-
data,
91-
where: condition as any
92-
})
89+
return this.model.update({ data, where: condition })
9390
}
9491

9592
/**
96-
* Fetch all trashed records from the store.
93+
* Fetch all soft deletes from the store.
9794
*/
9895
query.prototype.allTrashed = function() {
99-
return this.onlyTrashed().get()
96+
return this.newQuery()
97+
.onlyTrashed()
98+
.get()
10099
}
101100

102101
/**
103-
* Fetch all trashed records from the store and group by entity.
102+
* Fetch all soft deletes from the store and group by entity.
104103
*/
105104
query.allTrashed = function(store) {
106105
const database = store.$db()
@@ -113,25 +112,25 @@ export default function Query(
113112
}
114113

115114
/**
116-
* Global select hook prevents trashed records from being selected unless
115+
* Global select hook prevents soft deleted models from being selected unless
117116
* queries are explicity chained with `withTrashed` or `onlyTrashed`.
118117
*/
119118
query.on('beforeSelect', function<T extends BaseQuery>(
120119
this: T,
121120
models: Data.Collection
122121
) {
123122
return models.filter((model) => {
124-
// Only trashed records
123+
// Only soft deletes
125124
if (this.softDeletesFilter === true) {
126125
return model.$trashed()
127126
}
128127

129-
// Include trashed records
128+
// Include soft deletes
130129
if (this.softDeletesFilter === false) {
131130
return models
132131
}
133132

134-
// Exclude trashed records
133+
// Exclude soft deletes
135134
return !model.$trashed()
136135
})
137136
})

src/mixins/RootActions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import {
77

88
export default function RootActions(
99
context: VuexORMSoftDelete,
10-
actions: RootActionsContract
10+
rootActions: RootActionsContract
1111
): void {
1212
/**
13-
* Trash records and persist the store.
13+
* Soft delete records and persist the store.
1414
*/
15-
actions.softDelete = async function(
15+
rootActions.softDelete = async function(
1616
this: Store<any>,
1717
_context: RootActionContext,
1818
payload: any

test/feature/VuexORMSoftDelete.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ describe('Feature - Vuex ORM Soft Delete', () => {
55
it('should have `key` and `flagName` attributes on new', async () => {
66
class User extends Model {
77
static entity = 'users'
8-
9-
deleted_at!: number
10-
$isDeleted!: boolean
118
}
129

1310
createStore([User])

test/feature/model/Delete.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ describe('Feature - Model - Delete', () => {
173173
]
174174
})
175175

176-
await User.softDelete((record: any) => record.age >= 20)
176+
await User.softDelete((record: User) => record.age >= 20)
177177

178178
const expected = createState({
179179
users: {
@@ -263,6 +263,7 @@ describe('Feature - Model - Delete', () => {
263263
}
264264
}
265265

266+
posts!: Post[]
266267
deleted_at!: number
267268
$isDeleted!: boolean
268269
}
@@ -288,14 +289,16 @@ describe('Feature - Model - Delete', () => {
288289

289290
expect(user.$isDeleted).toBe(false)
290291
expect(user.deleted_at).toBe(null)
292+
expect(user.posts).not.toEqual([])
291293

292294
await user.$softDelete()
293295

294296
expect(user.$isDeleted).toBe(true)
295297
expect(user.deleted_at).toBe(mockDate)
298+
expect(user.posts).not.toEqual([])
296299
})
297300

298-
it('should hydrate instance after deleting', async () => {
301+
it('can hydrate instance after deleting', async () => {
299302
class User extends Model {
300303
static entity = 'users'
301304

test/feature/module/Delete.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ describe('Feature - Module - Delete', () => {
177177

178178
await store.dispatch(
179179
'entities/users/softDelete',
180-
(record: any) => record.age >= 20
180+
(record: User) => record.age >= 20
181181
)
182182

183183
const expected = createState({

0 commit comments

Comments
 (0)