@cocreate/crud-server 1.9.2 → 1.10.0
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 +8 -0
- package/package.json +1 -1
- package/src/crud.js +37 -36
- package/src/list.js +95 -59
- package/src/utils.crud.js +0 -14
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
# [1.10.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.9.2...v1.10.0) (2022-08-31)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* filter, query and sort collections ([6399e8c](https://github.com/CoCreate-app/CoCreate-crud-server/commit/6399e8ccae1a1ff713335d47b1bb73c7af154e85))
|
|
7
|
+
* rename and delete keys from db ([03d5387](https://github.com/CoCreate-app/CoCreate-crud-server/commit/03d5387c30512b4723a60a7439b71b86c7a6834d))
|
|
8
|
+
|
|
1
9
|
## [1.9.2](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.9.1...v1.9.2) (2022-07-29)
|
|
2
10
|
|
|
3
11
|
|
package/package.json
CHANGED
package/src/crud.js
CHANGED
|
@@ -107,24 +107,17 @@ class CoCreateCrud {
|
|
|
107
107
|
console.log(err);
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
delete req_data['data']['_id']
|
|
110
|
+
if (req_data['data'] && req_data['data']['_id'])
|
|
111
|
+
delete req_data['data']['_id']
|
|
113
112
|
|
|
114
|
-
if(typeof req_data['data'] === 'object')
|
|
115
|
-
req_data['set'] = req_data['data']
|
|
116
|
-
|
|
117
|
-
if(Array.isArray(req_data['delete_fields']))
|
|
118
|
-
req_data['unset'] = req_data['delete_fields'];
|
|
119
|
-
|
|
120
113
|
|
|
121
114
|
const query = {"_id": objId };
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if( req_data['set'] ) {
|
|
125
|
-
let insertData = replaceArray(req_data['set']);
|
|
115
|
+
let update = {}, projection = {};
|
|
126
116
|
|
|
127
|
-
|
|
117
|
+
|
|
118
|
+
if( typeof req_data['data'] === 'object' ) {
|
|
119
|
+
update['$set'] = {}
|
|
120
|
+
for (const [key, value] of Object.entries(replaceArray(req_data['data']))) {
|
|
128
121
|
let val;
|
|
129
122
|
let valueType = typeof value;
|
|
130
123
|
switch(valueType) {
|
|
@@ -143,35 +136,43 @@ class CoCreateCrud {
|
|
|
143
136
|
default:
|
|
144
137
|
val = value
|
|
145
138
|
}
|
|
146
|
-
update
|
|
147
|
-
}
|
|
139
|
+
update['$set'][key] = val
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
update['$set']['organization_id'] = req_data['organization_id'];
|
|
143
|
+
|
|
144
|
+
Object.keys(update['$set']).forEach(x => {
|
|
145
|
+
projection[x] = 1
|
|
146
|
+
})
|
|
147
|
+
|
|
148
148
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
update['$unset'] = unsetData.reduce((r, d) => {r[d] = ""; return r}, {});
|
|
149
|
+
|
|
150
|
+
if( req_data['deleteName'] ) {
|
|
151
|
+
update['$unset'] = replaceArray(req_data['deleteName']);
|
|
153
152
|
}
|
|
154
|
-
update['$set']['organization_id'] = req_data['organization_id'];
|
|
155
153
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
154
|
+
if( req_data['updateName'] ) {
|
|
155
|
+
update['$rename'] = replaceArray(req_data['updateName'])
|
|
156
|
+
for (const [key, value] of Object.entries(update['$rename'])) {
|
|
157
|
+
let newValue = replaceArray({[value]: value})
|
|
158
|
+
let oldkey = key;
|
|
159
|
+
for (const [key] of Object.entries(newValue)) {
|
|
160
|
+
update['$rename'][oldkey] = key
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
160
164
|
|
|
161
|
-
collection.
|
|
162
|
-
returnOriginal : false,
|
|
165
|
+
collection.updateOne( query, update, {
|
|
163
166
|
upsert: req_data.upsert || false,
|
|
164
167
|
projection: projection,
|
|
165
168
|
}
|
|
166
|
-
).then((result) => {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
self.broadcast(socket, 'updateDocument', response, socketInfo)
|
|
169
|
+
).then((error, result) => {
|
|
170
|
+
if (!error) {
|
|
171
|
+
let response = { ...req_data, data: update['$set'] };
|
|
172
|
+
self.broadcast(socket, 'updateDocument', response, socketInfo)
|
|
173
|
+
} else {
|
|
174
|
+
self.wsManager.send(socket, 'ServerError', error, socketInfo);
|
|
175
|
+
}
|
|
175
176
|
}).catch((error) => {
|
|
176
177
|
console.log('error', error)
|
|
177
178
|
self.wsManager.send(socket, 'ServerError', error, socketInfo);
|
package/src/list.js
CHANGED
|
@@ -46,7 +46,7 @@ class CoCreateList {
|
|
|
46
46
|
data: [] // array
|
|
47
47
|
}
|
|
48
48
|
**/
|
|
49
|
-
async readDocuments(socket,
|
|
49
|
+
async readDocuments(socket, data, socketInfo) {
|
|
50
50
|
function sleep(ms) {
|
|
51
51
|
return new Promise((resolve) => {
|
|
52
52
|
setTimeout(resolve, ms);
|
|
@@ -55,65 +55,14 @@ class CoCreateList {
|
|
|
55
55
|
|
|
56
56
|
const self = this;
|
|
57
57
|
|
|
58
|
-
if (req_data['is_collection']) {
|
|
59
|
-
var result = await this.readCollections(socket, req_data, socketInfo);
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
58
|
try {
|
|
64
|
-
const db = this.dbClient.db(
|
|
65
|
-
const collection = db.collection(
|
|
66
|
-
|
|
67
|
-
filters: [],
|
|
68
|
-
orders: [],
|
|
69
|
-
search: {
|
|
70
|
-
value: [],
|
|
71
|
-
type: "or"
|
|
72
|
-
},
|
|
73
|
-
startIndex: 0,
|
|
74
|
-
...req_data.operator
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
var query = {};
|
|
78
|
-
query = this.readQuery(operator);
|
|
79
|
-
|
|
80
|
-
var sort = {};
|
|
81
|
-
operator.orders.forEach((order) => {
|
|
82
|
-
sort[order.name] = order.type
|
|
83
|
-
});
|
|
59
|
+
const db = this.dbClient.db(data['organization_id']);
|
|
60
|
+
const collection = db.collection(data["collection"]);
|
|
61
|
+
let {operator, query, sort} = this.getFilters(data);
|
|
84
62
|
collection.find(query).sort(sort).toArray(function(error, result) {
|
|
85
63
|
if (result) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
result = self.readAndSearch(result, operator['search']['value']);
|
|
89
|
-
} else {
|
|
90
|
-
result = self.readOrSearch(result, operator['search']['value']);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const total = result.length;
|
|
94
|
-
const startIndex = operator.startIndex;
|
|
95
|
-
const count = operator.count;
|
|
96
|
-
let result_data = [];
|
|
97
|
-
|
|
98
|
-
if (req_data.created_ids && req_data.created_ids.length > 0) {
|
|
99
|
-
let _nn = (count) ? startIndex : result.length;
|
|
100
|
-
|
|
101
|
-
for (let ii = 0; ii < _nn; ii++) {
|
|
102
|
-
|
|
103
|
-
const selected_item = result[ii];
|
|
104
|
-
req_data.created_ids.forEach((fetch_id, index) => {
|
|
105
|
-
if (fetch_id == selected_item['_id']) {
|
|
106
|
-
result_data.push({ item: selected_item, position: ii })
|
|
107
|
-
}
|
|
108
|
-
})
|
|
109
|
-
}
|
|
110
|
-
} else {
|
|
111
|
-
if (startIndex) result = result.slice(startIndex, total);
|
|
112
|
-
if (count) result = result.slice(0, count)
|
|
113
|
-
|
|
114
|
-
result_data = result;
|
|
115
|
-
}
|
|
116
|
-
self.wsManager.send(socket, 'readDocuments', { ...req_data, data: result_data, operator: {...operator, total: total}}, socketInfo);
|
|
64
|
+
let result_data = self.filterResponse(result, data, operator)
|
|
65
|
+
self.wsManager.send(socket, 'readDocuments', { ...data, data: result_data, operator, socketInfo });
|
|
117
66
|
} else {
|
|
118
67
|
console.log(error)
|
|
119
68
|
self.wsManager.send(socket, 'ServerError', error, socketInfo);
|
|
@@ -129,9 +78,39 @@ class CoCreateList {
|
|
|
129
78
|
try {
|
|
130
79
|
const self = this;
|
|
131
80
|
data['collection'] = 'collections'
|
|
81
|
+
|
|
82
|
+
let {operator, query, sort} = this.getFilters(data);
|
|
132
83
|
const db = this.dbClient.db(data['organization_id']);
|
|
133
|
-
db.listCollections().toArray(function(error, result) {
|
|
84
|
+
db.listCollections(query).toArray(function(error, result) {
|
|
134
85
|
if (!error && result && result.length > 0) {
|
|
86
|
+
let orderField = Object.keys(sort)[0]
|
|
87
|
+
if (orderField) {
|
|
88
|
+
let orderType = sort[orderField];
|
|
89
|
+
let orderValueType = "";
|
|
90
|
+
let sortData;
|
|
91
|
+
if (orderType == '-1') {
|
|
92
|
+
if (orderValueType == 'number')
|
|
93
|
+
sortData = result.sort((a, b) =>
|
|
94
|
+
b[orderField] - a[orderField]
|
|
95
|
+
);
|
|
96
|
+
else
|
|
97
|
+
sortData = result.sort((a, b) =>
|
|
98
|
+
b[orderField].localeCompare(a[orderField])
|
|
99
|
+
);
|
|
100
|
+
} else {
|
|
101
|
+
if (orderValueType == 'number')
|
|
102
|
+
sortData = result.sort((a, b) =>
|
|
103
|
+
a[orderField] - b[orderField]
|
|
104
|
+
);
|
|
105
|
+
else
|
|
106
|
+
sortData = result.sort((a, b) =>
|
|
107
|
+
a[orderField].localeCompare(b[orderField])
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
}
|
|
111
|
+
result = sortData
|
|
112
|
+
}
|
|
113
|
+
|
|
135
114
|
self.wsManager.send(socket, 'readCollections', {...data, data: result }, socketInfo);
|
|
136
115
|
}
|
|
137
116
|
})
|
|
@@ -145,7 +124,64 @@ class CoCreateList {
|
|
|
145
124
|
* function that make query from data
|
|
146
125
|
*
|
|
147
126
|
*/
|
|
148
|
-
|
|
127
|
+
filterResponse(result, data, operator) {
|
|
128
|
+
if (operator['search']['type'] == 'and') {
|
|
129
|
+
result = this.readAndSearch(result, operator['search']['value']);
|
|
130
|
+
} else {
|
|
131
|
+
result = this.readOrSearch(result, operator['search']['value']);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const total = result.length;
|
|
135
|
+
const startIndex = operator.startIndex;
|
|
136
|
+
const count = operator.count;
|
|
137
|
+
let result_data = [];
|
|
138
|
+
|
|
139
|
+
if (data.created_ids && data.created_ids.length > 0) {
|
|
140
|
+
let _nn = (count) ? startIndex : result.length;
|
|
141
|
+
|
|
142
|
+
for (let ii = 0; ii < _nn; ii++) {
|
|
143
|
+
|
|
144
|
+
const selected_item = result[ii];
|
|
145
|
+
data.created_ids.forEach((fetch_id, index) => {
|
|
146
|
+
if (fetch_id == selected_item['_id']) {
|
|
147
|
+
result_data.push({ item: selected_item, position: ii })
|
|
148
|
+
}
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
if (startIndex) result = result.slice(startIndex, total);
|
|
153
|
+
if (count) result = result.slice(0, count)
|
|
154
|
+
|
|
155
|
+
result_data = result;
|
|
156
|
+
}
|
|
157
|
+
operator.startIndex = startIndex
|
|
158
|
+
operator.count = count
|
|
159
|
+
operator.total = total
|
|
160
|
+
return result_data
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
getFilters(data) {
|
|
164
|
+
let operator = {
|
|
165
|
+
filters: [],
|
|
166
|
+
orders: [],
|
|
167
|
+
search: {
|
|
168
|
+
value: [],
|
|
169
|
+
type: "or"
|
|
170
|
+
},
|
|
171
|
+
startIndex: 0,
|
|
172
|
+
...data.operator
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
let query = this.createQuery(operator);
|
|
176
|
+
let sort = {}
|
|
177
|
+
operator.orders.forEach((order) => {
|
|
178
|
+
sort[order.name] = order.type
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
return {operator, query, sort}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
createQuery(data) {
|
|
149
185
|
var query = new Object();
|
|
150
186
|
|
|
151
187
|
var filters = data['filters'];
|
package/src/utils.crud.js
CHANGED
|
@@ -15,20 +15,6 @@ function replaceArray(data) {
|
|
|
15
15
|
});
|
|
16
16
|
|
|
17
17
|
return objectData;
|
|
18
|
-
|
|
19
|
-
// let keys = Object.keys(data)
|
|
20
|
-
|
|
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;
|
|
32
18
|
}
|
|
33
19
|
|
|
34
20
|
module.exports = {
|