Skip to content

Commit cb7a6f8

Browse files
committed
Added Model and Module Config to each database model
1 parent 22aef92 commit cb7a6f8

10 files changed

Lines changed: 260 additions & 2 deletions

File tree

dist/index.js

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions/Action.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import _ from 'lodash';
2+
import Context from '../common/context';
3+
import { ModuleConfig, ModelConfig } from '../support/interfaces';
4+
5+
export default class Action {
6+
/**
7+
* Transform Module to include ModuleConfig
8+
* @param {object} model
9+
*/
10+
static transformModule(module) {
11+
return _.merge({}, ModuleConfig, module);
12+
}
13+
14+
/**
15+
* Transform Model to include ModelConfig
16+
* @param {object} model
17+
*/
18+
static transformModel(model) {
19+
const context = Context.getInstance();
20+
ModelConfig.http = _.merge({}, ModelConfig.http, context.options.http);
21+
model.methodConf = _.merge({}, ModelConfig, model.methodConf);
22+
model.methodConf.http.url = (model.methodConf.http.url === '/') ? `/${model.entity}` : model.methodConf.http.url;
23+
return model;
24+
}
25+
26+
/**
27+
* Transform Params and Return Endpoint
28+
* @param {string} type
29+
* @param {object} model
30+
* @param {object} config
31+
*/
32+
static transformParams (type, model, config = {}) {
33+
let endpoint = `${model.methodConf.http.url}${model.methodConf.methods[type].http.url}`;
34+
if (config.params) _.forOwn(config.params, (value, param) => { endpoint = endpoint.replace(`:${param}`, value); });
35+
if (config.query) endpoint += `?${Object.keys(config.query).map(k => `${encodeURIComponent(k)}=${encodeURIComponent(config.query[k])}`).join('&')}`;
36+
return endpoint;
37+
}
38+
}

src/actions/Create.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Action from './Action'
2+
3+
export default class Create extends Action {
4+
static call ({ state, dispatch }, params = {}) {
5+
// console.log(state, dispatch, params);
6+
}
7+
}

src/actions/Delete.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Action from './Action'
2+
3+
export default class Delete extends Action {
4+
static call ({ state, dispatch }, params={}) {
5+
console.log(state, dispatch, params);
6+
}
7+
}

src/actions/Fetch.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Action from './Action'
2+
import Context from '../common/context'
3+
4+
export default class Fetch extends Action {
5+
static async call ({ state }, params = {}) {
6+
const context = Context.getInstance();
7+
const model = context.getModelFromState(state);
8+
const endpoint = Action.transformParams('$fetch', model, params);
9+
}
10+
}

src/actions/Get.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Action from './Action'
2+
3+
export default class Get extends Action {
4+
static call ({ state, dispatch }, params={}) {
5+
console.log(state, dispatch, params);
6+
}
7+
}

src/actions/Update.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Action from './Action'
2+
3+
export default class Update extends Action {
4+
static call ({ state, dispatch }, params={}) {
5+
console.log(state, dispatch, params);
6+
}
7+
}

src/common/context.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,14 @@ export default class Context {
3737
static getInstance() {
3838
return this.instance;
3939
}
40+
41+
/**
42+
* Get Model from State
43+
* @param {object} state
44+
*/
45+
getModelFromState(state) {
46+
return _.find(this.database.entities, {
47+
name: state.$name
48+
}).model;
49+
}
4050
}

src/support/interfaces.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,107 @@ export const VuexOrmPluginConfig = {
163163
*/
164164
http: AxiosRequestConfig
165165
};
166+
167+
export const ModuleConfig = {
168+
/**
169+
* Vuex Default Getters
170+
*/
171+
getters: {
172+
loading: state => state.loading,
173+
errors: state => state.errors,
174+
},
175+
176+
/**
177+
* Vuex Default Mutations
178+
*/
179+
mutations: {
180+
/**
181+
* On Default Request
182+
* @param {object} state
183+
*/
184+
onRequest(state) {
185+
state.loading = true;
186+
state.errors = [];
187+
},
188+
189+
/**
190+
* On Error Request
191+
* @param {object} state
192+
* @param {object} response
193+
*/
194+
onError(state, response) {
195+
state.loading = false;
196+
state.errors = response.data;
197+
},
198+
199+
/**
200+
* On Success Request
201+
* @param {object} state
202+
* @param {object} response
203+
*/
204+
onSuccess(state) {
205+
state.loading = false;
206+
state.errors = [];
207+
},
208+
},
209+
210+
/**
211+
* Vuex Defualt State
212+
*/
213+
state: {
214+
loading: false,
215+
errors: [],
216+
},
217+
};
218+
219+
export const FetchConfig = {
220+
name: 'fetch',
221+
http: {
222+
url: '',
223+
method: 'get',
224+
},
225+
};
226+
227+
export const GetConfig = {
228+
name: 'get',
229+
http: {
230+
url: '/:id',
231+
method: 'get',
232+
},
233+
};
234+
235+
export const CreateConfig = {
236+
name: 'create',
237+
alias: ['insert'],
238+
http: {
239+
url: '',
240+
method: 'post',
241+
},
242+
};
243+
244+
export const UpdateConfig = {
245+
name: 'update',
246+
http: {
247+
url: '/:id',
248+
method: 'put',
249+
},
250+
};
251+
252+
export const DeleteConfig = {
253+
name: 'delete',
254+
http: {
255+
url: '/:id',
256+
method: 'delete',
257+
},
258+
};
259+
260+
export const ModelConfig = {
261+
http: AxiosRequestConfig,
262+
methods: {
263+
$fetch: FetchConfig,
264+
$get: GetConfig,
265+
$create: CreateConfig,
266+
$update: UpdateConfig,
267+
$delete: DeleteConfig,
268+
},
269+
};

src/vuex-orm-axios.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,70 @@
1+
import Context from './common/context';
2+
import Action from './actions/Action'
3+
import Fetch from './actions/Fetch'
4+
import Get from './actions/Get'
5+
import Create from './actions/Create'
6+
import Update from './actions/Update'
7+
import Delete from './actions/Delete'
8+
19
export default class VuexOrmAxios {
210
/**
311
* @constructor
412
* @param {Components} components The Vuex-ORM Components collection
513
* @param {Options} options The options passed to VuexORM.install
614
*/
715
constructor(components, options) {
8-
//
16+
Context.setup(components, options);
17+
this.setupActions();
18+
this.setupModels();
19+
}
20+
21+
/**
22+
* This method will setup following Vuex actions: $fetch, $get, $create, $update, $delete
23+
*/
24+
setupActions () {
25+
const context = Context.getInstance();
26+
27+
context.components.Actions.$fetch = Fetch.call.bind(Fetch);
28+
context.components.Actions.$get = Get.call.bind(Get);
29+
context.components.Actions.$create = Create.call.bind(Create);
30+
context.components.Actions.$update = Update.call.bind(Update);
31+
context.components.Actions.$delete = Delete.call.bind(Delete);
32+
}
33+
34+
/**
35+
* This method will setup following model methods: Model.$fetch, Model.$get, Model.$create,
36+
* Model.$update, Model.$delete
37+
*/
38+
setupModels () {
39+
const context = Context.getInstance();
40+
41+
/**
42+
* Transform Model and Modules
43+
*/
44+
_.map(context.database.entities, entity => {
45+
entity.module = Action.transformModule(entity.module);
46+
entity.model = Action.transformModel(entity.model);
47+
return entity;
48+
});
49+
50+
context.components.Model.$fetch = async function (config = {}) {
51+
return this.dispatch('$fetch', config);
52+
};
53+
54+
context.components.Model.$get = async function (config = {}) {
55+
return this.dispatch('$get', config);
56+
};
57+
58+
context.components.Model.$create = async function (config = {}) {
59+
return this.dispatch('$create', config);
60+
};
61+
62+
context.components.Model.$update = async function (config = {}) {
63+
return this.dispatch('$update', config);
64+
};
65+
66+
context.components.Model.$delete = async function (config = {}) {
67+
return this.dispatch('$delete', config);
68+
};
969
}
1070
}

0 commit comments

Comments
 (0)