Skip to content

Commit a24dee6

Browse files
committed
Refactoring of actions
1 parent 310cea9 commit a24dee6

10 files changed

+169
-99
lines changed

src/actions/destroy.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
1-
import { ActionParams, Data } from "../support/interfaces";
1+
import { ActionParams, Data, PatchedModel } from "../support/interfaces";
22
import Action from "./action";
33
import { Store } from "../orm/store";
44
import Context from "../common/context";
5+
import { toNumber } from "../support/utils";
56

67
/**
78
* Destroy action for sending a delete mutation. Will be used for record.$destroy().
89
*/
910
export default class Destroy extends Action {
11+
/**
12+
* Registers the record.$destroy() and record.$deleteAndDestroy() methods and
13+
* the destroy Vuex Action.
14+
*/
15+
public static setup() {
16+
const context = Context.getInstance();
17+
const record: PatchedModel = context.components.Model.prototype as PatchedModel;
18+
19+
context.components.Actions.destroy = Destroy.call.bind(Destroy);
20+
21+
record.$destroy = async function() {
22+
return this.$dispatch("destroy", { id: toNumber(this.$id) });
23+
};
24+
25+
record.$deleteAndDestroy = async function() {
26+
await this.$delete();
27+
return this.$destroy();
28+
};
29+
}
30+
1031
/**
1132
* @param {State} state The Vuex state
1233
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model

src/actions/fetch.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,31 @@ import QueryBuilder from "../graphql/query-builder";
22
import Context from "../common/context";
33
import { Store } from "../orm/store";
44
import Transformer from "../graphql/transformer";
5-
import { ActionParams, Arguments, Data } from "../support/interfaces";
5+
import { ActionParams, Data, PatchedModel } from "../support/interfaces";
66
import Action from "./action";
7+
import { isPlainObject } from "../support/utils";
78

89
/**
910
* Fetch action for sending a query. Will be used for Model.fetch().
1011
*/
1112
export default class Fetch extends Action {
13+
/**
14+
* Registers the Model.fetch() method and the fetch Vuex Action.
15+
*/
16+
public static setup() {
17+
const context = Context.getInstance();
18+
const model: typeof PatchedModel = context.components.Model as typeof PatchedModel;
19+
20+
context.components.Actions.fetch = Fetch.call.bind(Fetch);
21+
22+
model.fetch = async function(filter: any, bypassCache = false) {
23+
let filterObj = filter;
24+
if (!isPlainObject(filterObj)) filterObj = { id: filter };
25+
26+
return this.dispatch("fetch", { filter: filterObj, bypassCache });
27+
};
28+
}
29+
1230
/**
1331
* @param {any} state The Vuex state
1432
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model

src/actions/index.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import Action from './action';
2-
import Destroy from './destroy';
3-
import Fetch from './fetch';
4-
import Mutate from './mutate';
5-
import Persist from './persist';
6-
import Push from './push';
1+
import Action from "./action";
2+
import Destroy from "./destroy";
3+
import Fetch from "./fetch";
4+
import Mutate from "./mutate";
5+
import Persist from "./persist";
6+
import Push from "./push";
7+
import Query from "./query";
8+
import SimpleQuery from "./simple-query";
9+
import SimpleMutation from "./simple-mutation";
710

8-
export { Action, Destroy, Fetch, Mutate, Persist, Push };
11+
export { SimpleQuery, SimpleMutation, Query, Action, Destroy, Fetch, Mutate, Persist, Push };

src/actions/mutate.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
1-
import { ActionParams, Arguments, Data } from "../support/interfaces";
1+
import { ActionParams, PatchedModel, Data } from "../support/interfaces";
22
import Action from "./action";
33
import Context from "../common/context";
44
import Schema from "../graphql/schema";
55
import { Store } from "../orm/store";
6+
import { toNumber } from "../support/utils";
67

78
/**
89
* Mutate action for sending a custom mutation. Will be used for Model.mutate() and record.$mutate().
910
*/
1011
export default class Mutate extends Action {
12+
/**
13+
* Registers the Model.mutate() and the record.$mutate() methods and the mutate Vuex Action.
14+
*/
15+
public static setup() {
16+
const context = Context.getInstance();
17+
const model: typeof PatchedModel = context.components.Model as typeof PatchedModel;
18+
const record: PatchedModel = context.components.Model.prototype as PatchedModel;
19+
20+
context.components.Actions.mutate = Mutate.call.bind(Mutate);
21+
22+
model.mutate = async function(params: ActionParams) {
23+
return this.dispatch("mutate", params);
24+
};
25+
26+
record.$mutate = async function({ name, args, multiple }: ActionParams) {
27+
args = args || {};
28+
if (!args["id"]) args["id"] = toNumber(this.$id);
29+
30+
return this.$dispatch("mutate", { name, args, multiple });
31+
};
32+
}
33+
1134
/**
1235
* @param {any} state The Vuex state
1336
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model

src/actions/persist.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Context from "../common/context";
2-
import { ActionParams, Data } from "../support/interfaces";
2+
import { ActionParams, Data, PatchedModel } from "../support/interfaces";
33
import Action from "./action";
44
import Model from "../orm/model";
55
import { Store } from "../orm/store";
@@ -9,6 +9,20 @@ import { toNumber } from "../support/utils";
99
* Persist action for sending a create mutation. Will be used for record.$persist().
1010
*/
1111
export default class Persist extends Action {
12+
/**
13+
* Registers the record.$persist() method and the persist Vuex Action.
14+
*/
15+
public static setup() {
16+
const context = Context.getInstance();
17+
const record: PatchedModel = context.components.Model.prototype as PatchedModel;
18+
19+
context.components.Actions.persist = Persist.call.bind(Persist);
20+
21+
record.$persist = async function(args: any) {
22+
return this.$dispatch("persist", { id: this.$id, args });
23+
};
24+
}
25+
1226
/**
1327
* @param {any} state The Vuex state
1428
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model

src/actions/push.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ActionParams, Data } from "../support/interfaces";
1+
import { ActionParams, Data, PatchedModel } from "../support/interfaces";
22
import Action from "./action";
33
import { Store } from "../orm/store";
44
import Context from "../common/context";
@@ -7,6 +7,20 @@ import Context from "../common/context";
77
* Push action for sending a update mutation. Will be used for record.$push().
88
*/
99
export default class Push extends Action {
10+
/**
11+
* Registers the record.$push() method and the push Vuex Action.
12+
*/
13+
public static setup() {
14+
const context = Context.getInstance();
15+
const model: PatchedModel = context.components.Model.prototype as PatchedModel;
16+
17+
context.components.Actions.push = Push.call.bind(Push);
18+
19+
model.$push = async function(args: any) {
20+
return this.$dispatch("push", { data: this, args });
21+
};
22+
}
23+
1024
/**
1125
* @param {any} state The Vuex state
1226
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model

src/actions/query.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,38 @@ import QueryBuilder from "../graphql/query-builder";
22
import Context from "../common/context";
33
import { Store } from "../orm/store";
44
import Transformer from "../graphql/transformer";
5-
import { ActionParams, Data } from "../support/interfaces";
5+
import { ActionParams, Data, PatchedModel } from "../support/interfaces";
66
import Action from "./action";
77
import Schema from "../graphql/schema";
8+
import { toNumber } from "../support/utils";
89

910
/**
1011
* Query action for sending a custom query. Will be used for Model.customQuery() and record.$customQuery.
1112
*/
1213
export default class Query extends Action {
14+
/**
15+
* Registers the Model.customQuery and the record.$customQuery() methods and the
16+
* query Vuex Action.
17+
*/
18+
public static setup() {
19+
const context = Context.getInstance();
20+
const model: typeof PatchedModel = context.components.Model as typeof PatchedModel;
21+
const record: PatchedModel = context.components.Model.prototype as PatchedModel;
22+
23+
context.components.Actions.query = Query.call.bind(Query);
24+
25+
model.customQuery = async function({ name, filter, multiple, bypassCache }: ActionParams) {
26+
return this.dispatch("query", { name, filter, multiple, bypassCache });
27+
};
28+
29+
record.$customQuery = async function({ name, filter, multiple, bypassCache }: ActionParams) {
30+
filter = filter || {};
31+
if (!filter["id"]) filter["id"] = toNumber(this.$id);
32+
33+
return this.$dispatch("query", { name, filter, multiple, bypassCache });
34+
};
35+
}
36+
1337
/**
1438
* @param {any} state The Vuex state
1539
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model

src/actions/simple-mutation.ts

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ import { clone, graphQlDocumentToString, parseQuery } from "../support/utils";
77
* SimpleMutation action for sending a model unrelated simple mutation.
88
*/
99
export default class SimpleMutation extends Action {
10+
/**
11+
* Registers the Model.simpleMutation() Vuex Root Action.
12+
*/
13+
public static setup() {
14+
const context = Context.getInstance();
15+
context.components.RootActions.simpleMutation = SimpleMutation.call.bind(SimpleMutation);
16+
}
17+
1018
/**
1119
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model
1220
* @param {string} query The query to send

src/actions/simple-query.ts

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ import { clone, graphQlDocumentToString, parseQuery, removeSymbols } from "../su
77
* SimpleQuery action for sending a model unrelated simple query.
88
*/
99
export default class SimpleQuery extends Action {
10+
/**
11+
* Registers the Model.simpleQuery() Vuex Root Action.
12+
*/
13+
public static setup() {
14+
const context = Context.getInstance();
15+
context.components.RootActions.simpleQuery = SimpleQuery.call.bind(SimpleQuery);
16+
}
17+
1018
/**
1119
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model
1220
* @param {string} query The query to send

src/vuex-orm-graphql.ts

+23-86
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import { PatchedModel, Options, ActionParams } from "./support/interfaces";
1+
import { Options } from "./support/interfaces";
22
import Context from "./common/context";
33
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";
914

1015
/**
1116
* Main class of the plugin. Setups the internal context, Vuex actions and model methods
@@ -19,7 +24,6 @@ export default class VuexORMGraphQL {
1924
public constructor(components: PluginComponents, options: Options) {
2025
Context.setup(components, options);
2126
VuexORMGraphQL.setupActions();
22-
VuexORMGraphQL.setupModelMethods();
2327
}
2428

2529
/**
@@ -30,86 +34,19 @@ export default class VuexORMGraphQL {
3034
}
3135

3236
/**
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()
3441
*/
3542
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();
11451
}
11552
}

0 commit comments

Comments
 (0)