forked from almahdi/ra-data-parse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseClient.js
146 lines (139 loc) · 4.35 KB
/
parseClient.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {
GET_LIST,
GET_ONE,
GET_MANY,
GET_MANY_REFERENCE,
CREATE,
UPDATE,
UPDATE_MANY,
DELETE,
DELETE_MANY,
} from "react-admin";
import Parse from "parse";
export default ({ URL, APP_ID, JAVASCRIPT_KEY }) => {
if (Parse.applicationId == null || Parse.javaScriptKey == null) {
Parse.initialize(APP_ID, JAVASCRIPT_KEY);
Parse.serverURL = URL;
}
return async (type, resource, params) => {
const resourceObj = Parse.Object.extend(resource);
const query = new Parse.Query(resourceObj);
switch (type) {
case GET_LIST: {
const { page, perPage } = params.pagination;
const { field, order } = params.sort;
const { filter } = params;
const count = await query.count();
query.limit(perPage);
query.skip((page - 1) * perPage);
query.withCount();
if (order === "DESC") query.descending(field);
else if (order === "ASEC") query.ascending(field);
Object.keys(filter).map((f) => {
if (filter[f].type === "reference") {
const objectId = filter[f].value;
const className = filter[f].className;
const ClassObject = Parse.Object.extend(className);
const pointer = ClassObject.createWithoutData(objectId);
query.equalTo(f, pointer);
} else {
query.matches(f, filter[f], "i");
}
});
const result = await query.find();
return {
total: result.count,
data: result.results.map((o) => ({ id: o.id, ...o.attributes })),
};
}
case GET_ONE: {
const result = await query.get(params.id);
return {
data: { id: result.id, ...result.attributes },
};
}
case GET_MANY: {
const results = params.ids.map((id) =>
new Parse.Query(resourceObj).get(id)
);
const data = await Promise.all(results);
return {
total: data.length,
data: data.map((o) => ({ id: o.id, ...o.attributes })),
};
}
case GET_MANY_REFERENCE: {
const { page, perPage } = params.pagination;
const { field, order } = params.sort;
query.equalTo(params.target, params.id);
query.withCount();
query.limit(perPage);
query.skip((page - 1) * perPage);
if (order === "DESC") query.descending(field);
else if (order === "ASEC") query.ascending(field);
const result = await query.find();
return {
total: result.count,
data: result.results.map((o) => ({ id: o.id, ...o.attributes })),
};
}
case CREATE: {
const resObj = new resourceObj();
try {
const r = await resObj.save(params.data);
return { data: { id: r.id, ...r.attributes } };
} catch (error) {
return error;
}
}
case UPDATE: {
try {
const obj = await query.get(params.id);
const keys = Object.keys(params.data).filter((o) =>
o == "id" || o == "createdAt" || o == "updatedAt" ? false : true
);
const data = keys.reduce((r, f, i) => {
r[f] = params.data[f];
return r;
}, {});
const r = await obj.save(data);
return { data: { id: r.id, ...r.attributes } };
} catch (error) {
throw Error(error.toString());
}
}
case UPDATE_MANY: {
try {
const qs = await Promise.all(
params.ids.map((id) => new Parse.Query(resourceObj).get(id))
);
qs.map((q) => q.save(params.data));
return { data: params.ids };
} catch {
throw Error("Failed to update all");
}
}
case DELETE: {
try {
const obj = await query.get(params.id);
const data = { data: { id: obj.id, ...obj.attributes } };
await obj.destroy();
return data;
} catch (error) {
throw Error("Unable to delete");
}
}
case DELETE_MANY: {
try {
const qs = await Promise.all(
params.ids.map((id) => new Parse.Query(resourceObj).get(id))
);
await Promise.all(qs.map((obj) => obj.destroy()));
return { data: params.ids };
} catch (error) {
throw Error("Unable to delete all");
}
}
}
};
};