1
- import { PatchedModel , Options , ActionParams } from "./support/interfaces" ;
1
+ import { Options } from "./support/interfaces" ;
2
2
import Context from "./common/context" ;
3
3
import { PluginComponents } from "@vuex-orm/core/lib/plugins/use" ;
4
- import { Destroy , Fetch , Mutate , Persist , Push } from "./actions" ;
5
- import Query from "./actions/query" ;
6
- import SimpleQuery from "./actions/simple-query" ;
7
- import SimpleMutation from "./actions/simple-mutation" ;
8
- import { isPlainObject , toNumber } from "./support/utils" ;
4
+ import {
5
+ Destroy ,
6
+ Fetch ,
7
+ Mutate ,
8
+ Persist ,
9
+ Push ,
10
+ Query ,
11
+ SimpleQuery ,
12
+ SimpleMutation
13
+ } from "./actions" ;
9
14
10
15
/**
11
16
* Main class of the plugin. Setups the internal context, Vuex actions and model methods
@@ -19,7 +24,6 @@ export default class VuexORMGraphQL {
19
24
public constructor ( components : PluginComponents , options : Options ) {
20
25
Context . setup ( components , options ) ;
21
26
VuexORMGraphQL . setupActions ( ) ;
22
- VuexORMGraphQL . setupModelMethods ( ) ;
23
27
}
24
28
25
29
/**
@@ -30,86 +34,19 @@ export default class VuexORMGraphQL {
30
34
}
31
35
32
36
/**
33
- * This method will setup following Vuex actions: fetch, persist, push, destroy, mutate
37
+ * This method will setup:
38
+ * - Vuex actions: fetch, persist, push, destroy, mutate
39
+ * - Model methods: fetch(), mutate(), customQuery()
40
+ * - Record method: $mutate(), $persist(), $push(), $destroy(), $deleteAndDestroy(), $customQuery()
34
41
*/
35
42
private static setupActions ( ) {
36
- const context = Context . getInstance ( ) ;
37
-
38
- context . components . RootActions . simpleQuery = SimpleQuery . call . bind ( SimpleQuery ) ;
39
- context . components . RootActions . simpleMutation = SimpleMutation . call . bind ( SimpleMutation ) ;
40
-
41
- context . components . Actions . fetch = Fetch . call . bind ( Fetch ) ;
42
- context . components . Actions . persist = Persist . call . bind ( Persist ) ;
43
- context . components . Actions . push = Push . call . bind ( Push ) ;
44
- context . components . Actions . destroy = Destroy . call . bind ( Destroy ) ;
45
- context . components . Actions . mutate = Mutate . call . bind ( Mutate ) ;
46
- context . components . Actions . query = Query . call . bind ( Query ) ;
47
- }
48
-
49
- /**
50
- * This method will setup following model methods: Model.fetch, Model.mutate, Model.customQuery, record.$mutate,
51
- * record.$persist, record.$push, record.$destroy and record.$deleteAndDestroy, record.$customQuery
52
- */
53
- private static setupModelMethods ( ) {
54
- const context = Context . getInstance ( ) ;
55
-
56
- // Register static model convenience methods
57
- ( context . components . Model as typeof PatchedModel ) . fetch = async function (
58
- filter : any ,
59
- bypassCache = false
60
- ) {
61
- let filterObj = filter ;
62
- if ( ! isPlainObject ( filterObj ) ) {
63
- filterObj = { id : filter } ;
64
- }
65
- return this . dispatch ( "fetch" , { filter : filterObj , bypassCache } ) ;
66
- } ;
67
-
68
- ( context . components . Model as typeof PatchedModel ) . mutate = async function (
69
- params : ActionParams
70
- ) {
71
- return this . dispatch ( "mutate" , params ) ;
72
- } ;
73
-
74
- ( context . components . Model as typeof PatchedModel ) . customQuery = async function ( {
75
- name,
76
- filter,
77
- multiple,
78
- bypassCache
79
- } : ActionParams ) {
80
- return this . dispatch ( "query" , { name, filter, multiple, bypassCache } ) ;
81
- } ;
82
-
83
- // Register model convenience methods
84
- const model : PatchedModel = context . components . Model . prototype as PatchedModel ;
85
-
86
- model . $mutate = async function ( { name, args, multiple } : ActionParams ) {
87
- args = args || { } ;
88
- if ( ! args [ "id" ] ) args [ "id" ] = toNumber ( this . $id ) ;
89
- return this . $dispatch ( "mutate" , { name, args, multiple } ) ;
90
- } ;
91
-
92
- model . $customQuery = async function ( { name, filter, multiple, bypassCache } : ActionParams ) {
93
- filter = filter || { } ;
94
- if ( ! filter [ "id" ] ) filter [ "id" ] = toNumber ( this . $id ) ;
95
- return this . $dispatch ( "query" , { name, filter, multiple, bypassCache } ) ;
96
- } ;
97
-
98
- model . $persist = async function ( args : any ) {
99
- return this . $dispatch ( "persist" , { id : this . $id , args } ) ;
100
- } ;
101
-
102
- model . $push = async function ( args : any ) {
103
- return this . $dispatch ( "push" , { data : this , args } ) ;
104
- } ;
105
-
106
- model . $destroy = async function ( ) {
107
- return this . $dispatch ( "destroy" , { id : toNumber ( this . $id ) } ) ;
108
- } ;
109
-
110
- model . $deleteAndDestroy = async function ( ) {
111
- await this . $delete ( ) ;
112
- return this . $destroy ( ) ;
113
- } ;
43
+ Fetch . setup ( ) ;
44
+ Persist . setup ( ) ;
45
+ Push . setup ( ) ;
46
+ Destroy . setup ( ) ;
47
+ Mutate . setup ( ) ;
48
+ Query . setup ( ) ;
49
+ SimpleQuery . setup ( ) ;
50
+ SimpleMutation . setup ( ) ;
114
51
}
115
52
}
0 commit comments