-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.ts
110 lines (97 loc) · 3.55 KB
/
model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { Id, Params } from '@feathersjs/feathers';
import { cloneDeep, keys, omit, pick } from 'lodash';
import { Store } from 'vuex';
import { StateInterface } from './types';
export class Model {
[key: string]: any;
public static servicePath: string;
public static namespace: string;
public static serverAlias: string;
public static idField: string;
public static modelName = 'Model';
public static readonly store: Store<StateInterface>;
public static readonly models: Record<any, any>;
public static paramsForServer: string[] = [];
public static instance: Record<any, any> = {};
public static blacklist: string[] = [];
public static params: Params;
constructor(data: Record<any, any>, options?: any) {
for (const key in data) {
this[key] = Reflect.get(data, key);
}
}
public static find(params?: Params) {
const { namespace, store } = this;
return store.dispatch(`${namespace}/find`, params);
}
public static get(id: Id, params?: Params) {
const { namespace, store } = this;
return store.dispatch(`${namespace}/get`, { id, params });
}
public static count(params?: Params) {
const { namespace, store } = this;
return store.dispatch(`${namespace}/count`, params);
}
commit(params?: Params) {
const { namespace, store, idField, blacklist } = this.constructor as typeof Model;
const data = omit(this, blacklist);
store.commit(`${namespace}/update`, data);
return store.getters[`${namespace}/get`]({ id: this[idField], params });
}
create(params?: Params) {
const { namespace, store, instance, blacklist } = this.constructor as typeof Model;
const data = instance ?
pick(omit(this, blacklist), keys(instance)) : omit(this, blacklist);
return store.dispatch(`${namespace}/create`, { data, params });
}
remove(params?: Params) {
const { namespace, store, idField } = this.constructor as typeof Model;
const id = this[idField];
if (id !== null) {
return store.dispatch(`${namespace}/remove`, { id, params });
}
store.commit(`${namespace}/remove`, { id, params });
return Promise.resolve(this);
}
patch(params?: Params) {
const { namespace, store, idField, blacklist, instance } = this.constructor as typeof Model;
const id = this[idField];
if (id !== 0 && !id) {
const error = new Error(
`Missing ${idField} property. You must create the data before you can update with this data`
)
return Promise.reject(error)
}
const data = instance ?
pick(omit(this, blacklist), keys(instance)) : omit(this, blacklist);
return store.dispatch(`${namespace}/patch`, { id, data, params });
}
update(params?: Params) {
const { namespace, store, idField, blacklist, instance } = this.constructor as typeof Model;
const id = this[idField];
if (id !== 0 && !id) {
const error = new Error(
`Missing ${idField} property. You must create the data before you can update with this data`
)
return Promise.reject(error)
}
const data = instance ?
pick(omit(this, blacklist), keys(instance)) : omit(this, blacklist);
return store.dispatch(`${namespace}/update`, { id, data, params });
}
clone() {
const { idField } = this.constructor as typeof Model;
const id = this[idField];
if (id !== 0 && !id) {
const error = new Error(
`Missing ${idField} property. You must create the data before you can update with this data`
)
return Promise.reject(error)
}
return cloneDeep(this);
}
toJSON() {
const { blacklist } = this.constructor as typeof Model;
return omit(this, blacklist);
}
}