-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathparseClient.js
More file actions
149 lines (141 loc) · 5.73 KB
/
Copy pathparseClient.js
File metadata and controls
149 lines (141 loc) · 5.73 KB
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
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);
if (order === "DESC") query.descending(field);
else if (order === "ASEC") query.ascending(field);
Object.keys(filter).map(f => query.contains(f, filter[f]));
const results = await query.find();
return {
total: count,
data: 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);
const count = await query.count();
query.limit(perPage);
query.skip((page - 1) * perPage);
if (order === "DESC") query.descending(field);
else if (order === "ASEC") query.ascending(field);
const results = await query.find();
return {
total: count,
data: 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: {
var allPointersArr=[];
var pointerArr=[];
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)=>{
if(params.data[f].id==undefined){
// console.log("Not Pointer");
r[f] = params.data[f];
}
else{
// console.log("Pointer");
pointerArr["pointerCN"]=params.data[f].className;
pointerArr["pointerKey"]=params.data[f].id;
pointerArr["referenceCN"]=f;
allPointersArr.push(pointerArr);
pointerArr=[];
}
return r;
}, {});
for(var i=0;i<allPointersArr.length;i++){
var uObject = new Parse.Object(obj.className);
uObject.id=obj.id;
var pObject = new Parse.Object(allPointersArr[i].pointerCN);
pObject.id = allPointersArr[i].pointerKey;
uObject.set(allPointersArr[i].referenceCN, pObject);
await uObject.save();
}
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");
}
}
}
};
}