@cocreate/crud-server 1.8.0 → 1.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/docs/index.html +1 -1
- package/package.json +1 -1
- package/src/crud.js +91 -15
- package/src/database.js +0 -4
- package/src/list.js +1 -0
- package/src/utils.crud.js +24 -114
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
+
## [1.9.2](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.9.1...v1.9.2) (2022-07-29)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* set collection as collections for function readCollections ([73341cf](https://github.com/CoCreate-app/CoCreate-crud-server/commit/73341cfc11cab2c10f5f21ac791806f099fcdea0))
|
|
7
|
+
|
|
8
|
+
## [1.9.1](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.9.0...v1.9.1) (2022-07-27)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* typo with broadcasting collection messageName ([a3a9bea](https://github.com/CoCreate-app/CoCreate-crud-server/commit/a3a9beab885014fd0f90ce1c2b7e3c73014aa221))
|
|
14
|
+
|
|
15
|
+
# [1.9.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.8.0...v1.9.0) (2022-07-25)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* crud support on collection names ([c569657](https://github.com/CoCreate-app/CoCreate-crud-server/commit/c569657f0ab339952c4aa72bbab2a7667c0f9986))
|
|
21
|
+
* update arrayValues to mongoDb dotNotation, removed isFlat ([ab2fb60](https://github.com/CoCreate-app/CoCreate-crud-server/commit/ab2fb601f660b3b96f7584ca66be1cb3902095eb))
|
|
22
|
+
|
|
1
23
|
# [1.8.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.7.0...v1.8.0) (2022-05-29)
|
|
2
24
|
|
|
3
25
|
|
package/docs/index.html
CHANGED
package/package.json
CHANGED
package/src/crud.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const {ObjectId} = require("mongodb");
|
|
2
|
-
const {
|
|
2
|
+
const {replaceArray} = require("./utils.crud.js")
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
class CoCreateCrud {
|
|
@@ -15,6 +15,9 @@ class CoCreateCrud {
|
|
|
15
15
|
this.wsManager.on('readDocument', (socket, data, socketInfo) => this.readDocument(socket, data, socketInfo))
|
|
16
16
|
this.wsManager.on('updateDocument', (socket, data, socketInfo) => this.updateDocument(socket, data, socketInfo))
|
|
17
17
|
this.wsManager.on('deleteDocument', (socket, data, socketInfo) => this.deleteDocument(socket, data, socketInfo))
|
|
18
|
+
this.wsManager.on('createCollection', (socket, data, socketInfo) => this.createCollection(socket, data, socketInfo));
|
|
19
|
+
this.wsManager.on('updateCollection', (socket, data, socketInfo) => this.updateCollection(socket, data, socketInfo))
|
|
20
|
+
this.wsManager.on('deleteCollection', (socket, data, socketInfo) => this.deleteCollection(socket, data, socketInfo))
|
|
18
21
|
}
|
|
19
22
|
}
|
|
20
23
|
|
|
@@ -32,9 +35,6 @@ class CoCreateCrud {
|
|
|
32
35
|
collection.insertOne(insertData, function(error, result) {
|
|
33
36
|
if(!error && result){
|
|
34
37
|
const response = {...req_data, document_id: `${result.insertedId}`, data: insertData }
|
|
35
|
-
// let isFlat = req_data.isFlat == false ? false : true;
|
|
36
|
-
// const response_data = isFlat ? encodeObject(response) : response;
|
|
37
|
-
// const response_data = response;
|
|
38
38
|
self.broadcast(socket, 'createDocument', response, socketInfo)
|
|
39
39
|
} else {
|
|
40
40
|
self.wsManager.send(socket, 'ServerError', error, socketInfo);
|
|
@@ -81,8 +81,7 @@ class CoCreateCrud {
|
|
|
81
81
|
tmp = resp;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
|
|
85
|
-
self.wsManager.send(socket, 'readDocument', { ...req_data, data: isFlat ? encodeObject(tmp) : tmp }, socketInfo);
|
|
84
|
+
self.wsManager.send(socket, 'readDocument', { ...req_data, data: tmp }, socketInfo);
|
|
86
85
|
} else {
|
|
87
86
|
self.wsManager.send(socket, 'readDocument error', req_data, socketInfo);
|
|
88
87
|
}
|
|
@@ -107,12 +106,25 @@ class CoCreateCrud {
|
|
|
107
106
|
} catch (err) {
|
|
108
107
|
console.log(err);
|
|
109
108
|
}
|
|
110
|
-
|
|
111
|
-
|
|
109
|
+
|
|
110
|
+
// let requestData = req_data
|
|
111
|
+
if (req_data['data']['_id'])
|
|
112
|
+
delete req_data['data']['_id']
|
|
112
113
|
|
|
114
|
+
if(typeof req_data['data'] === 'object')
|
|
115
|
+
req_data['set'] = req_data['data']
|
|
113
116
|
|
|
114
|
-
if(
|
|
115
|
-
|
|
117
|
+
if(Array.isArray(req_data['delete_fields']))
|
|
118
|
+
req_data['unset'] = req_data['delete_fields'];
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
const query = {"_id": objId };
|
|
122
|
+
const update = {"$set": {}};
|
|
123
|
+
|
|
124
|
+
if( req_data['set'] ) {
|
|
125
|
+
let insertData = replaceArray(req_data['set']);
|
|
126
|
+
|
|
127
|
+
for (const [key, value] of Object.entries(insertData)) {
|
|
116
128
|
let val;
|
|
117
129
|
let valueType = typeof value;
|
|
118
130
|
switch(valueType) {
|
|
@@ -133,9 +145,12 @@ class CoCreateCrud {
|
|
|
133
145
|
}
|
|
134
146
|
update.$set[key] = val
|
|
135
147
|
}
|
|
136
|
-
|
|
137
|
-
if( req_data['unset'] )
|
|
138
|
-
|
|
148
|
+
}
|
|
149
|
+
if( req_data['unset'] ) {
|
|
150
|
+
let unsetData = replaceArray(req_data['unset']);
|
|
151
|
+
|
|
152
|
+
update['$unset'] = unsetData.reduce((r, d) => {r[d] = ""; return r}, {});
|
|
153
|
+
}
|
|
139
154
|
update['$set']['organization_id'] = req_data['organization_id'];
|
|
140
155
|
|
|
141
156
|
let projection = {}
|
|
@@ -149,10 +164,9 @@ class CoCreateCrud {
|
|
|
149
164
|
projection: projection,
|
|
150
165
|
}
|
|
151
166
|
).then((result) => {
|
|
152
|
-
let isFlat = req_data.isFlat == true ? true : false;
|
|
153
167
|
let response_data = result.value || {};
|
|
154
168
|
|
|
155
|
-
let response = { ...req_data, document_id: response_data._id, data:
|
|
169
|
+
let response = { ...req_data, document_id: response_data._id, data: req_data['set'] };
|
|
156
170
|
|
|
157
171
|
if(req_data['unset'])
|
|
158
172
|
response['delete_fields'] = req_data['unset'];
|
|
@@ -193,6 +207,68 @@ class CoCreateCrud {
|
|
|
193
207
|
self.wsManager.send(socket, 'ServerError', 'error', socketInfo);
|
|
194
208
|
}
|
|
195
209
|
}
|
|
210
|
+
|
|
211
|
+
/** Create Collection **/
|
|
212
|
+
async createCollection(socket, req_data, socketInfo) {
|
|
213
|
+
const self = this;
|
|
214
|
+
|
|
215
|
+
try {
|
|
216
|
+
const db = this.dbClient.db(req_data['organization_id']);
|
|
217
|
+
db.createCollection(req_data.collection, function(error, result) {
|
|
218
|
+
if (!error) {
|
|
219
|
+
let response = { ...req_data }
|
|
220
|
+
self.broadcast(socket, 'createCollection', response, socketInfo)
|
|
221
|
+
} else {
|
|
222
|
+
self.wsManager.send(socket, 'ServerError', error, socketInfo);
|
|
223
|
+
}
|
|
224
|
+
})
|
|
225
|
+
} catch (error) {
|
|
226
|
+
console.log(error);
|
|
227
|
+
self.wsManager.send(socket, 'ServerError', 'error', socketInfo);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Update Collection **/
|
|
232
|
+
async updateCollection(socket, req_data, socketInfo) {
|
|
233
|
+
const self = this;
|
|
234
|
+
|
|
235
|
+
try {
|
|
236
|
+
const db = this.dbClient.db(req_data['organization_id']);
|
|
237
|
+
const collection = db.collection(req_data["collection"]);
|
|
238
|
+
collection.rename(req_data.target, function(error, result) {
|
|
239
|
+
if (!error) {
|
|
240
|
+
let response = { ...req_data }
|
|
241
|
+
self.broadcast(socket, 'updateCollection', response, socketInfo)
|
|
242
|
+
} else {
|
|
243
|
+
self.wsManager.send(socket, 'ServerError', error, socketInfo);
|
|
244
|
+
}
|
|
245
|
+
})
|
|
246
|
+
} catch (error) {
|
|
247
|
+
console.log(error);
|
|
248
|
+
self.wsManager.send(socket, 'ServerError', 'error', socketInfo);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Delete Collection **/
|
|
253
|
+
async deleteCollection(socket, req_data, socketInfo) {
|
|
254
|
+
const self = this;
|
|
255
|
+
|
|
256
|
+
try {
|
|
257
|
+
const db = this.dbClient.db(req_data['organization_id']);
|
|
258
|
+
const collection = db.collection(req_data["collection"]);
|
|
259
|
+
collection.drop( function(error, result) {
|
|
260
|
+
if (!error) {
|
|
261
|
+
let response = { ...req_data }
|
|
262
|
+
self.broadcast(socket, 'deleteCollection', response, socketInfo)
|
|
263
|
+
} else {
|
|
264
|
+
self.wsManager.send(socket, 'ServerError', error, socketInfo);
|
|
265
|
+
}
|
|
266
|
+
})
|
|
267
|
+
} catch (error) {
|
|
268
|
+
console.log(error);
|
|
269
|
+
self.wsManager.send(socket, 'ServerError', 'error', socketInfo);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
196
272
|
|
|
197
273
|
broadcast(socket, component, response, socketInfo) {
|
|
198
274
|
this.wsManager.broadcast(socket, response.namespace || response['organization_id'], response.room, component, response, socketInfo);
|
package/src/database.js
CHANGED
package/src/list.js
CHANGED
|
@@ -128,6 +128,7 @@ class CoCreateList {
|
|
|
128
128
|
async readCollections(socket, data, socketInfo) {
|
|
129
129
|
try {
|
|
130
130
|
const self = this;
|
|
131
|
+
data['collection'] = 'collections'
|
|
131
132
|
const db = this.dbClient.db(data['organization_id']);
|
|
132
133
|
db.listCollections().toArray(function(error, result) {
|
|
133
134
|
if (!error && result && result.length > 0) {
|
package/src/utils.crud.js
CHANGED
|
@@ -1,126 +1,36 @@
|
|
|
1
|
-
function
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
for (let key of Object.keys(source)) {
|
|
5
|
-
if (source[key] instanceof Object) {
|
|
6
|
-
Object.assign(source[key], __mergeObject(target[key], source[key]))
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
Object.assign(target || {}, source)
|
|
11
|
-
return target
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function __createObject(data, path)
|
|
15
|
-
{
|
|
16
|
-
if (!path) return data;
|
|
17
|
-
|
|
18
|
-
let keys = path.split('.')
|
|
19
|
-
let newObject = data;
|
|
20
|
-
|
|
21
|
-
for (var i = keys.length - 1; i >= 0; i--) {
|
|
22
|
-
newObject = {[keys[i]]: newObject}
|
|
23
|
-
}
|
|
24
|
-
return newObject;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function __createArray(key, data)
|
|
28
|
-
{
|
|
29
|
-
try {
|
|
30
|
-
let item = /([\w\W]+)\[(\d+)\]/gm.exec(key)
|
|
31
|
-
if (item && item.length == 3) {
|
|
32
|
-
let arrayKey = item[1];
|
|
33
|
-
let index = parseInt(item[2]);
|
|
34
|
-
|
|
35
|
-
if (!data[arrayKey] || !Array.isArray(data[arrayKey])) {
|
|
36
|
-
data[arrayKey] = [];
|
|
37
|
-
}
|
|
38
|
-
data[arrayKey][index] = data[key];
|
|
39
|
-
delete data[key];
|
|
40
|
-
key = arrayKey;
|
|
41
|
-
}
|
|
42
|
-
} catch {
|
|
43
|
-
console.log('create array error');
|
|
44
|
-
}
|
|
45
|
-
return key;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
function isObject(item) {
|
|
51
|
-
return (!!item) && (item.constructor === Object);
|
|
52
|
-
}
|
|
53
|
-
function isArray(item) {
|
|
54
|
-
return (!!item) && (item.constructor === Array);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function replaceArray(data)
|
|
58
|
-
{
|
|
59
|
-
let keys = Object.keys(data)
|
|
1
|
+
function replaceArray(data) {
|
|
2
|
+
let keys = Object.keys(data);
|
|
3
|
+
let objectData = {};
|
|
60
4
|
|
|
61
5
|
keys.forEach((k) => {
|
|
62
|
-
let
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
6
|
+
let nk = k
|
|
7
|
+
if (/\[([0-9]*)\]/g.test(k)) {
|
|
8
|
+
nk = nk.replace(/\[/g, '.');
|
|
9
|
+
if (nk.endsWith(']'))
|
|
10
|
+
nk = nk.slice(0, -1)
|
|
11
|
+
nk = nk.replace(/\]./g, '.');
|
|
12
|
+
nk = nk.replace(/\]/g, '.');
|
|
69
13
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function decodeObject(data) {
|
|
75
|
-
let keys = Object.keys(data)
|
|
76
|
-
let objectData = {};
|
|
14
|
+
objectData[nk] = data[k];
|
|
15
|
+
});
|
|
77
16
|
|
|
78
|
-
keys.forEach((k) => {
|
|
79
|
-
k = __createArray(k, data);
|
|
80
|
-
if (k.split('.').length > 1) {
|
|
81
|
-
let newData = __createObject(data[k], k);
|
|
82
|
-
delete data[k];
|
|
83
|
-
|
|
84
|
-
objectData = __mergeObject(objectData, newData);
|
|
85
|
-
} else {
|
|
86
|
-
objectData[k] = data[k];
|
|
87
|
-
}
|
|
88
|
-
})
|
|
89
17
|
return objectData;
|
|
90
|
-
}
|
|
91
18
|
|
|
92
|
-
|
|
93
|
-
let keys = Object.keys(data);
|
|
94
|
-
let newData = {};
|
|
95
|
-
keys.forEach((k) => {
|
|
96
|
-
let data_value = data[k];
|
|
97
|
-
if (isObject(data[k])) {
|
|
98
|
-
let new_obj = encodeObject(data[k]);
|
|
99
|
-
let newKeys = Object.keys(new_obj);
|
|
100
|
-
newKeys.forEach((newKey) => {
|
|
101
|
-
let value = new_obj[newKey];
|
|
102
|
-
if (isNaN(parseInt(newKey))) {
|
|
103
|
-
newKey = `${k}.${newKey}`;
|
|
104
|
-
} else {
|
|
105
|
-
newKey = `${k}[${newKey}]`;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
newData[newKey] = value;
|
|
109
|
-
})
|
|
19
|
+
// let keys = Object.keys(data)
|
|
110
20
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
21
|
+
// keys.forEach((k) => {
|
|
22
|
+
// let reg = /\[(\d+)\]/gm.exec(k)
|
|
23
|
+
// let newKey = null;
|
|
24
|
+
// if (reg && reg.length == 2) {
|
|
25
|
+
// newKey = k.replace(reg[0], "." + reg[1]);
|
|
26
|
+
// let newData = data[k];
|
|
27
|
+
// delete data[k];
|
|
28
|
+
// data[newKey] = newData
|
|
29
|
+
// }
|
|
30
|
+
// })
|
|
31
|
+
// return data;
|
|
120
32
|
}
|
|
121
33
|
|
|
122
34
|
module.exports = {
|
|
123
|
-
decodeObject,
|
|
124
|
-
encodeObject,
|
|
125
35
|
replaceArray
|
|
126
36
|
};
|