Skip to content

Commit 47d6fc4

Browse files
committed
refactor: structure module mixins
1 parent 027c945 commit 47d6fc4

File tree

9 files changed

+135
-107
lines changed

9 files changed

+135
-107
lines changed

src/VuexORMSoftDelete.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import { Model, Query } from '@vuex-orm/core'
22
import Components from './contracts/Components'
33
import Config from './contracts/Config'
44
import Options from './contracts/Options'
5-
import Modules from './contracts/Modules'
5+
import * as Store from './contracts/Store'
66
import ModelMixin from './mixins/Model'
77
import QueryMixin from './mixins/Query'
8-
import ModulesMixin from './mixins/Modules'
8+
import ActionsMixin from './mixins/Actions'
9+
import GettersMixin from './mixins/Getters'
10+
import RootActionsMixin from './mixins/RootActions'
11+
import RootGettersMixin from './mixins/RootGetters'
912
import GlobalConfig from './config/GlobalConfig'
1013

1114
export default class VuexORMSoftDelete {
@@ -20,9 +23,24 @@ export default class VuexORMSoftDelete {
2023
query: typeof Query
2124

2225
/**
23-
* The module components.
26+
* The store action tree.
2427
*/
25-
modules: Modules
28+
actions: Store.Actions
29+
30+
/**
31+
* The store action tree.
32+
*/
33+
getters: Store.Getters
34+
35+
/**
36+
* The store action tree.
37+
*/
38+
rootGetters: Store.RootGetters
39+
40+
/**
41+
* The store action tree.
42+
*/
43+
rootActions: Store.RootActions
2644

2745
/**
2846
* The global configuration object.
@@ -36,12 +54,10 @@ export default class VuexORMSoftDelete {
3654
this.model = components.Model
3755
this.query = components.Query
3856

39-
this.modules = {
40-
actions: components.Actions,
41-
getters: components.Getters,
42-
rootGetters: components.RootGetters,
43-
rootActions: components.RootActions
44-
}
57+
this.actions = components.Actions
58+
this.getters = components.Getters
59+
this.rootActions = components.RootActions
60+
this.rootGetters = components.RootGetters
4561

4662
this.config = this.createConfig(config)
4763
}
@@ -64,6 +80,9 @@ export default class VuexORMSoftDelete {
6480
plugin(): void {
6581
ModelMixin(this, this.model)
6682
QueryMixin(this, this.query)
67-
ModulesMixin(this, this.modules)
83+
ActionsMixin(this, this.actions)
84+
GettersMixin(this, this.getters)
85+
RootActionsMixin(this, this.rootActions)
86+
RootGettersMixin(this, this.rootGetters)
6887
}
6988
}

src/contracts/Components.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Model, Query } from '@vuex-orm/core'
2-
import { Actions, Getters, RootGetters, RootActions } from './Modules'
2+
import { Actions, Getters, RootGetters, RootActions } from './Store'
33

44
export interface Components {
55
Model: typeof Model

src/contracts/Modules.ts renamed to src/contracts/Store.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,3 @@ export {
1717
State,
1818
RootState
1919
}
20-
21-
export interface Modules {
22-
actions: Actions
23-
getters: Getters
24-
rootGetters: RootGetters
25-
rootActions: RootActions
26-
}
27-
28-
export default Modules

src/mixins/Actions.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import VuexORMSoftDelete from '../VuexORMSoftDelete'
2+
import { Actions as ActionsContract, ActionContext } from '../contracts/Store'
3+
4+
export default function Actions(
5+
_context: VuexORMSoftDelete,
6+
actions: ActionsContract
7+
): void {
8+
/**
9+
* Trash records and persist to the store.
10+
*/
11+
actions.softDelete = async (
12+
context: ActionContext,
13+
payload: any
14+
): Promise<any> => {
15+
const state = context.state
16+
const entity = state.$name
17+
const where = payload.where ?? payload
18+
19+
return context.dispatch(
20+
`${state.$connection}/softDelete`,
21+
{ entity, where },
22+
{ root: true }
23+
)
24+
}
25+
}

src/mixins/Getters.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import VuexORMSoftDelete from '../VuexORMSoftDelete'
2+
import { Collection } from '../contracts/Data'
3+
import {
4+
Getters as GettersContract,
5+
State,
6+
RootState
7+
} from '../contracts/Store'
8+
9+
export default function Getters(
10+
_context: VuexORMSoftDelete,
11+
getters: GettersContract
12+
): void {
13+
/**
14+
* Get all trashed records from the store and group by entity.
15+
*/
16+
getters.allTrashed = (
17+
state: State,
18+
_getters: any,
19+
_rootState: RootState,
20+
rootGetters: any
21+
) => (): Collection => {
22+
return rootGetters[`${state.$connection}/allTrashed`](state.$name)
23+
}
24+
}

src/mixins/Modules.ts

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/mixins/RootActions.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Store } from 'vuex'
2+
import VuexORMSoftDelete from '../VuexORMSoftDelete'
3+
import {
4+
RootActions as RootActionsContract,
5+
RootActionContext
6+
} from '../contracts/Store'
7+
8+
export default function RootActions(
9+
context: VuexORMSoftDelete,
10+
actions: RootActionsContract
11+
): void {
12+
/**
13+
* Trash records and persist the store.
14+
*/
15+
actions.softDelete = async function(
16+
this: Store<any>,
17+
_context: RootActionContext,
18+
payload: any
19+
): Promise<any> {
20+
const { entity, where } = payload
21+
22+
return new context.query(this, entity).softDelete(where)
23+
}
24+
}

src/mixins/RootGetters.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Store } from 'vuex'
2+
import VuexORMSoftDelete from '../VuexORMSoftDelete'
3+
import { Collection, Collections } from '../contracts/Data'
4+
import {
5+
RootGetters as RootGettersContract,
6+
RootState
7+
} from '../contracts/Store'
8+
9+
export default function RootGetters(
10+
context: VuexORMSoftDelete,
11+
rootGetters: RootGettersContract
12+
): void {
13+
/**
14+
* Get all trashed records belonging to an entity.
15+
*/
16+
rootGetters.allTrashed = function(this: Store<any>, _state: RootState) {
17+
return (entity?: string): Collection | Collections => {
18+
if (entity) {
19+
return new context.query(this, entity).allTrashed()
20+
}
21+
22+
return context.query.allTrashed(this)
23+
}
24+
}
25+
}

src/types/vuex-orm.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,11 @@ declare module '@vuex-orm/core' {
1414
/**
1515
* Process the record being trashed.
1616
*/
17-
export function softDelete<T extends typeof Model>(
18-
this: T,
19-
condition: PrimaryKey
20-
): Promise<Data.Item>
21-
export function softDelete<T extends typeof Model>(
22-
this: T,
23-
condition: Predicate<Model>
24-
): Promise<Data.Collection>
25-
export function softDelete<T extends typeof Model>(
26-
this: T,
27-
condition: any
28-
): Promise<any>
17+
export function softDelete(condition: PrimaryKey): Promise<Data.Item>
18+
export function softDelete(condition: Predicate): Promise<Data.Collection>
19+
export function softDelete(condition: any): Promise<any>
20+
21+
export function restore(payload: any): Promise<any>
2922
}
3023

3124
interface Model {
@@ -63,7 +56,7 @@ declare module '@vuex-orm/core' {
6356
* Process the record(s) to be trashed.
6457
*/
6558
softDelete(condition: PrimaryKey): Promise<Data.Collections>
66-
softDelete(condition: Predicate<Model>): Promise<Data.Collections>
59+
softDelete(condition: Predicate): Promise<Data.Collections>
6760
softDelete(condition: any): any
6861

6962
/**

0 commit comments

Comments
 (0)