@cocreate/crud-server 1.10.0 → 1.11.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 +26 -0
- package/docs/index.html +1 -1
- package/package.json +2 -2
- package/src/backup.js +11 -11
- package/src/crud.js +282 -139
- package/src/database.js +5 -5
- package/src/index.js +0 -2
- package/src/list.js +0 -356
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
+
# [1.11.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.10.1...v1.11.0) (2022-10-01)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* broadcast params reduced to socket, message, data ([ec9c8f2](https://github.com/CoCreate-app/CoCreate-crud-server/commit/ec9c8f259e516424ef95433904a0e89d2aa0c727))
|
|
7
|
+
* bump dependencies ([b5b9350](https://github.com/CoCreate-app/CoCreate-crud-server/commit/b5b9350dbcb1b20fc945542a600d526ddb78fb91))
|
|
8
|
+
* createDocument inserts _id in the data object ([113c2ff](https://github.com/CoCreate-app/CoCreate-crud-server/commit/113c2ffb3d0a291f3af8ba645f709b1b5811089d))
|
|
9
|
+
* createQuery ObjectId ([ec79e40](https://github.com/CoCreate-app/CoCreate-crud-server/commit/ec79e40b81760e347ce5d8d70ed28f3494c49538))
|
|
10
|
+
* filter value can be an object or an array ([75cd195](https://github.com/CoCreate-app/CoCreate-crud-server/commit/75cd19560d8086f01a05f55e76102793789ec1d1))
|
|
11
|
+
* merged crud and list ([acff218](https://github.com/CoCreate-app/CoCreate-crud-server/commit/acff21831b2ca1274bc5af775f7d3758f2e55663))
|
|
12
|
+
* relocate and import filter.js from @cocreate/filter ([b8bab38](https://github.com/CoCreate-app/CoCreate-crud-server/commit/b8bab3882b5ece91115a4568aa80bc5eca2c80f3))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
* config now accessible from socket.config ([2d892ff](https://github.com/CoCreate-app/CoCreate-crud-server/commit/2d892ff961cb48187f8253a4f3778719cb764d1c))
|
|
18
|
+
* if data.returnDocument = false only defined names are returned ([652f624](https://github.com/CoCreate-app/CoCreate-crud-server/commit/652f6242c947d3b46d01deaea784f964f43450fd))
|
|
19
|
+
|
|
20
|
+
## [1.10.1](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.10.0...v1.10.1) (2022-09-01)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
### Bug Fixes
|
|
24
|
+
|
|
25
|
+
* bump dependencies ([15b088a](https://github.com/CoCreate-app/CoCreate-crud-server/commit/15b088ad50ecd57b77a442bf6f99bdae8fffdf09))
|
|
26
|
+
|
|
1
27
|
# [1.10.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.9.2...v1.10.0) (2022-08-31)
|
|
2
28
|
|
|
3
29
|
|
package/docs/index.html
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/crud-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "CoCreate-crud-server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cocreate-crud",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"homepage": "https://cocreate.app/docs/CoCreate-crud-server",
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@cocreate/docs": "^1.
|
|
43
|
+
"@cocreate/docs": "^1.3.14",
|
|
44
44
|
"csvtojson": "^2.0.10",
|
|
45
45
|
"json-2-csv": "^3.10.3",
|
|
46
46
|
"mongodb": "^4.4.0"
|
package/src/backup.js
CHANGED
|
@@ -13,11 +13,11 @@ class CoCreateBackup {
|
|
|
13
13
|
|
|
14
14
|
init() {
|
|
15
15
|
if (this.wsManager) {
|
|
16
|
-
// this.wsManager.on('exportDB', (socket, data
|
|
17
|
-
// this.wsManager.on('importDB', (socket, data
|
|
18
|
-
// this.wsManager.on('importFile2DB', (socket, data
|
|
16
|
+
// this.wsManager.on('exportDB', (socket, data) => this.exportData(socket, data));
|
|
17
|
+
// this.wsManager.on('importDB', (socket, data) => this.importData(socket, data));
|
|
18
|
+
// this.wsManager.on('importFile2DB', (socket, data) => this.importData(socket, data));
|
|
19
19
|
|
|
20
|
-
// this.wsManager.on('downloadData', (socket, data
|
|
20
|
+
// this.wsManager.on('downloadData', (socket, data) => this.downloadData(socket, data))
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
|
|
@@ -28,13 +28,13 @@ class CoCreateBackup {
|
|
|
28
28
|
data: JSON data
|
|
29
29
|
}
|
|
30
30
|
**/
|
|
31
|
-
// async downloadData(socket, data
|
|
31
|
+
// async downloadData(socket, data) {
|
|
32
32
|
// const export_type = data.type || "json";
|
|
33
33
|
|
|
34
34
|
// try {
|
|
35
35
|
// let binaryData = null;
|
|
36
36
|
// const result = data.data;
|
|
37
|
-
// const orgId =
|
|
37
|
+
// const orgId = socket.config.orgId ;
|
|
38
38
|
// if (export_type === 'csv') {
|
|
39
39
|
// binaryData = await json2csv.json2csvAsync(JSON.parse(JSON.stringify(result)), {
|
|
40
40
|
// emptyFieldValue: ''
|
|
@@ -60,7 +60,7 @@ class CoCreateBackup {
|
|
|
60
60
|
|
|
61
61
|
})
|
|
62
62
|
**/
|
|
63
|
-
// async exportData(socket, data
|
|
63
|
+
// async exportData(socket, data) {
|
|
64
64
|
// const self = this;
|
|
65
65
|
|
|
66
66
|
// const export_type = data.export_type || "json";
|
|
@@ -68,7 +68,7 @@ class CoCreateBackup {
|
|
|
68
68
|
// try {
|
|
69
69
|
|
|
70
70
|
// var collection = this.dbClient.db(data['namespace']).collection(data["collection"]);
|
|
71
|
-
// const orgId =
|
|
71
|
+
// const orgId = socket.config.orgId
|
|
72
72
|
|
|
73
73
|
// var query = {};
|
|
74
74
|
|
|
@@ -92,13 +92,13 @@ class CoCreateBackup {
|
|
|
92
92
|
// }
|
|
93
93
|
// }
|
|
94
94
|
|
|
95
|
-
// async setImportData(socket, data
|
|
95
|
+
// async setImportData(socket, data) {
|
|
96
96
|
// this.importCollection = data['collection']
|
|
97
97
|
// this.importType = data['import_type'];
|
|
98
98
|
// this.importDB = data['namespace'];
|
|
99
99
|
// }
|
|
100
100
|
|
|
101
|
-
// async importData(socket, data
|
|
101
|
+
// async importData(socket, data) {
|
|
102
102
|
// const importCollection = data['collection']
|
|
103
103
|
// const importType = data['import_type'];
|
|
104
104
|
// const importFile = data['file'];
|
|
@@ -106,7 +106,7 @@ class CoCreateBackup {
|
|
|
106
106
|
|
|
107
107
|
// console.log('import:', importCollection, importType, importFile)
|
|
108
108
|
// const self = this;
|
|
109
|
-
// //
|
|
109
|
+
// // const orgId = socket.config.orgId
|
|
110
110
|
// if (!importCollection || !importType) {
|
|
111
111
|
// return;
|
|
112
112
|
// }
|
package/src/crud.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
const {ObjectId} = require("mongodb");
|
|
2
2
|
const {replaceArray} = require("./utils.crud.js")
|
|
3
|
+
const {searchData, sortData} = require("@cocreate/filter")
|
|
4
|
+
|
|
3
5
|
|
|
4
6
|
|
|
5
7
|
class CoCreateCrud {
|
|
@@ -11,270 +13,411 @@ class CoCreateCrud {
|
|
|
11
13
|
|
|
12
14
|
init() {
|
|
13
15
|
if (this.wsManager) {
|
|
14
|
-
this.wsManager.on('createDocument',
|
|
15
|
-
this.wsManager.on('readDocument',
|
|
16
|
-
this.wsManager.on('updateDocument',
|
|
17
|
-
this.wsManager.on('deleteDocument',
|
|
18
|
-
this.wsManager.on('
|
|
19
|
-
this.wsManager.on('
|
|
20
|
-
this.wsManager.on('
|
|
16
|
+
this.wsManager.on('createDocument', (socket, data) => this.createDocument(socket, data))
|
|
17
|
+
this.wsManager.on('readDocument', (socket, data) => this.readDocument(socket, data))
|
|
18
|
+
this.wsManager.on('updateDocument', (socket, data) => this.updateDocument(socket, data))
|
|
19
|
+
this.wsManager.on('deleteDocument', (socket, data) => this.deleteDocument(socket, data))
|
|
20
|
+
this.wsManager.on('readDocuments', (socket, data) => this.readDocuments(socket, data))
|
|
21
|
+
this.wsManager.on('createCollection', (socket, data) => this.createCollection(socket, data))
|
|
22
|
+
this.wsManager.on('updateCollection', (socket, data) => this.updateCollection(socket, data))
|
|
23
|
+
this.wsManager.on('deleteCollection', (socket, data) => this.deleteCollection(socket, data))
|
|
24
|
+
this.wsManager.on('readCollections', (socket, data) => this.readCollections(socket, data))
|
|
21
25
|
}
|
|
22
26
|
}
|
|
23
27
|
|
|
24
28
|
/** Create Document **/
|
|
25
|
-
async createDocument(socket,
|
|
29
|
+
async createDocument(socket, data){
|
|
26
30
|
const self = this;
|
|
27
|
-
if(!
|
|
31
|
+
if(!data.data) return;
|
|
28
32
|
|
|
29
33
|
try{
|
|
30
|
-
const db = this.dbClient.db(
|
|
31
|
-
const collection = db.collection(
|
|
32
|
-
let insertData = replaceArray(
|
|
33
|
-
insertData['organization_id'] =
|
|
34
|
+
const db = this.dbClient.db(data['organization_id']);
|
|
35
|
+
const collection = db.collection(data["collection"]);
|
|
36
|
+
let insertData = replaceArray(data.data);
|
|
37
|
+
insertData['organization_id'] = data['organization_id'];
|
|
34
38
|
|
|
35
39
|
collection.insertOne(insertData, function(error, result) {
|
|
36
40
|
if(!error && result){
|
|
37
|
-
const response = {...
|
|
38
|
-
|
|
41
|
+
const response = {...data, document_id: `${result.insertedId}`, data: insertData }
|
|
42
|
+
response.data['_id'] = result.insertedId;
|
|
43
|
+
self.broadcast(socket, 'createDocument', response)
|
|
39
44
|
} else {
|
|
40
|
-
self.wsManager.send(socket, 'ServerError', error
|
|
45
|
+
self.wsManager.send(socket, 'ServerError', error);
|
|
41
46
|
}
|
|
42
47
|
});
|
|
43
48
|
}catch(error){
|
|
44
49
|
console.log('createDocument error', error);
|
|
45
|
-
self.wsManager.send(socket, 'ServerError', 'error'
|
|
50
|
+
self.wsManager.send(socket, 'ServerError', 'error');
|
|
46
51
|
}
|
|
47
52
|
}
|
|
48
53
|
|
|
49
54
|
/** Read Document **/
|
|
50
|
-
async readDocument(socket,
|
|
51
|
-
if (!
|
|
52
|
-
this.wsManager.send(socket, 'ServerError', 'error'
|
|
55
|
+
async readDocument(socket, data) {
|
|
56
|
+
if (!data['collection'] || data['collection'] == 'null' || typeof data['collection'] !== 'string') {
|
|
57
|
+
this.wsManager.send(socket, 'ServerError', 'error');
|
|
53
58
|
return;
|
|
54
59
|
}
|
|
55
60
|
const self = this;
|
|
56
61
|
|
|
57
62
|
try {
|
|
58
|
-
const db = this.dbClient.db(
|
|
59
|
-
const collection = db.collection(
|
|
63
|
+
const db = this.dbClient.db(data['organization_id']);
|
|
64
|
+
const collection = db.collection(data["collection"]);
|
|
60
65
|
|
|
61
|
-
const query =
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (req_data['organization_id']) {
|
|
65
|
-
query['organization_id'] = req_data['organization_id'];
|
|
66
|
+
const {query, sort} = this.getFilters(data);
|
|
67
|
+
if (data['organization_id']) {
|
|
68
|
+
query['organization_id'] = data['organization_id'];
|
|
66
69
|
}
|
|
67
70
|
|
|
68
|
-
collection.find(query).toArray(function(error, result) {
|
|
71
|
+
collection.find(query).sort(sort).toArray(function(error, result) {
|
|
69
72
|
if (!error && result && result.length > 0) {
|
|
70
73
|
let tmp = result[0];
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
})
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (req_data.data) {
|
|
74
|
+
|
|
75
|
+
// ToDo: returns values of defined names
|
|
76
|
+
if (data.returnDocument == false && data.data) {
|
|
78
77
|
let resp = {};
|
|
79
78
|
resp['_id'] = tmp['_id']
|
|
80
|
-
|
|
79
|
+
data.data.forEach((f) => resp[f] = tmp[f])
|
|
81
80
|
tmp = resp;
|
|
82
81
|
}
|
|
83
|
-
|
|
84
|
-
self.wsManager.send(socket, 'readDocument',
|
|
82
|
+
data.data = tmp
|
|
83
|
+
self.wsManager.send(socket, 'readDocument', data);
|
|
85
84
|
} else {
|
|
86
|
-
self.wsManager.send(socket, 'readDocument error',
|
|
85
|
+
self.wsManager.send(socket, 'readDocument error', data);
|
|
87
86
|
}
|
|
88
87
|
});
|
|
89
88
|
} catch (error) {
|
|
90
|
-
console.log('readDocument error', error,
|
|
91
|
-
self.wsManager.send(socket, 'ServerError', 'error'
|
|
89
|
+
console.log('readDocument error', error, data);
|
|
90
|
+
self.wsManager.send(socket, 'ServerError', 'error');
|
|
92
91
|
}
|
|
93
92
|
}
|
|
94
93
|
|
|
95
94
|
/** Update Document **/
|
|
96
|
-
async updateDocument(socket,
|
|
95
|
+
async updateDocument(socket, data) {
|
|
97
96
|
const self = this;
|
|
98
|
-
try {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
try {
|
|
103
|
-
if (req_data["document_id"]) {
|
|
104
|
-
objId = new ObjectId(req_data["document_id"]);
|
|
105
|
-
}
|
|
106
|
-
} catch (err) {
|
|
107
|
-
console.log(err);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (req_data['data'] && req_data['data']['_id'])
|
|
111
|
-
delete req_data['data']['_id']
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const query = {"_id": objId };
|
|
115
|
-
let update = {}, projection = {};
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if( typeof req_data['data'] === 'object' ) {
|
|
119
|
-
update['$set'] = {}
|
|
120
|
-
for (const [key, value] of Object.entries(replaceArray(req_data['data']))) {
|
|
121
|
-
let val;
|
|
122
|
-
let valueType = typeof value;
|
|
123
|
-
switch(valueType) {
|
|
124
|
-
case 'string':
|
|
125
|
-
val = value
|
|
126
|
-
break;
|
|
127
|
-
case 'number':
|
|
128
|
-
val = Number(value)
|
|
129
|
-
break;
|
|
130
|
-
case 'object':
|
|
131
|
-
if (Array.isArray(value))
|
|
132
|
-
val = new Array(...value)
|
|
133
|
-
else
|
|
134
|
-
val = new Object(value)
|
|
135
|
-
break;
|
|
136
|
-
default:
|
|
137
|
-
val = value
|
|
138
|
-
}
|
|
139
|
-
update['$set'][key] = val
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
update['$set']['organization_id'] = req_data['organization_id'];
|
|
97
|
+
try {
|
|
98
|
+
let {query, sort} = this.getFilters(data);
|
|
99
|
+
if (data['data'] && data['data']['_id'])
|
|
100
|
+
delete data['data']['_id']
|
|
143
101
|
|
|
102
|
+
let update = {}, projection = {}, returnNewDocument = false;
|
|
103
|
+
if (data.data) {
|
|
104
|
+
update['$set'] = this.valueTypes(data.data)
|
|
105
|
+
update['$set']['organization_id'] = data['organization_id'];
|
|
106
|
+
if (update['$set']['_id'])
|
|
107
|
+
delete update['$set']['_id']
|
|
144
108
|
Object.keys(update['$set']).forEach(x => {
|
|
145
109
|
projection[x] = 1
|
|
146
110
|
})
|
|
147
|
-
|
|
148
111
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
if( data['deleteName'] ) {
|
|
115
|
+
update['$unset'] = replaceArray(data['deleteName']);
|
|
152
116
|
}
|
|
153
|
-
|
|
154
|
-
if(
|
|
155
|
-
update['$rename'] = replaceArray(
|
|
117
|
+
|
|
118
|
+
if( data['updateName'] ) {
|
|
119
|
+
update['$rename'] = replaceArray(data['updateName'])
|
|
156
120
|
for (const [key, value] of Object.entries(update['$rename'])) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
121
|
+
if (/\.([0-9]*)/g.test(key) || /\[([0-9]*)\]/g.test(value)) {
|
|
122
|
+
console.log('key is array', /\[([0-9]*)\]/g.test(value), /\.([0-9]*)/g.test(key))
|
|
123
|
+
} else {
|
|
124
|
+
let newValue = replaceArray({[value]: value})
|
|
125
|
+
let oldkey = key;
|
|
126
|
+
for (const [key] of Object.entries(newValue)) {
|
|
127
|
+
update['$rename'][oldkey] = key
|
|
128
|
+
}
|
|
161
129
|
}
|
|
162
130
|
}
|
|
131
|
+
returnNewDocument == true
|
|
163
132
|
}
|
|
164
133
|
|
|
165
|
-
|
|
166
|
-
|
|
134
|
+
const db = this.dbClient.db(data['organization_id']);
|
|
135
|
+
const collection = db.collection(data["collection"]);
|
|
136
|
+
collection.findOneAndUpdate( query, update, {
|
|
137
|
+
returnOriginal: false,
|
|
138
|
+
returnNewDocument: returnNewDocument || false,
|
|
139
|
+
upsert: data.upsert || false,
|
|
167
140
|
projection: projection,
|
|
168
141
|
}
|
|
169
|
-
).then((
|
|
170
|
-
if (
|
|
171
|
-
|
|
172
|
-
|
|
142
|
+
).then((result) => {
|
|
143
|
+
if (result) {
|
|
144
|
+
update['$set']['_id'] = data.data._id || data.document_id
|
|
145
|
+
data.data = update['$set']
|
|
146
|
+
self.broadcast(socket, 'updateDocument', data)
|
|
173
147
|
} else {
|
|
174
|
-
self.wsManager.send(socket, 'ServerError', error
|
|
148
|
+
self.wsManager.send(socket, 'ServerError', error);
|
|
175
149
|
}
|
|
176
150
|
}).catch((error) => {
|
|
177
151
|
console.log('error', error)
|
|
178
|
-
self.wsManager.send(socket, 'ServerError', error
|
|
152
|
+
self.wsManager.send(socket, 'ServerError', error);
|
|
179
153
|
});
|
|
180
154
|
|
|
181
155
|
} catch (error) {
|
|
182
156
|
console.log(error)
|
|
183
|
-
self.wsManager.send(socket, 'updateDocumentError', error
|
|
157
|
+
self.wsManager.send(socket, 'updateDocumentError', error);
|
|
184
158
|
}
|
|
185
159
|
}
|
|
186
160
|
|
|
187
161
|
/** Delete Document **/
|
|
188
|
-
async deleteDocument(socket,
|
|
162
|
+
async deleteDocument(socket, data) {
|
|
189
163
|
const self = this;
|
|
190
164
|
|
|
191
165
|
try {
|
|
192
|
-
const db = this.dbClient.db(
|
|
193
|
-
const collection = db.collection(
|
|
166
|
+
const db = this.dbClient.db(data['organization_id']);
|
|
167
|
+
const collection = db.collection(data["collection"]);
|
|
194
168
|
const query = {
|
|
195
|
-
"_id": new ObjectId(
|
|
169
|
+
"_id": new ObjectId(data["document_id"])
|
|
196
170
|
};
|
|
197
171
|
|
|
198
172
|
collection.deleteOne(query, function(error, result) {
|
|
199
173
|
if (!error) {
|
|
200
|
-
|
|
201
|
-
self.broadcast(socket, 'deleteDocument', response, socketInfo)
|
|
174
|
+
self.broadcast(socket, 'deleteDocument', data)
|
|
202
175
|
} else {
|
|
203
|
-
self.wsManager.send(socket, 'ServerError', error
|
|
176
|
+
self.wsManager.send(socket, 'ServerError', error);
|
|
204
177
|
}
|
|
205
178
|
})
|
|
206
179
|
} catch (error) {
|
|
207
180
|
console.log(error);
|
|
208
|
-
self.wsManager.send(socket, 'ServerError', 'error'
|
|
181
|
+
self.wsManager.send(socket, 'ServerError', 'error');
|
|
209
182
|
}
|
|
210
183
|
}
|
|
211
184
|
|
|
185
|
+
async readDocuments(socket, data) {
|
|
186
|
+
function sleep(ms) {
|
|
187
|
+
return new Promise((resolve) => {
|
|
188
|
+
setTimeout(resolve, ms);
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const self = this;
|
|
193
|
+
|
|
194
|
+
try {
|
|
195
|
+
const db = this.dbClient.db(data['organization_id']);
|
|
196
|
+
const collection = db.collection(data["collection"]);
|
|
197
|
+
let {query, sort} = this.getFilters(data);
|
|
198
|
+
collection.find(query).sort(sort).toArray(function(error, result) {
|
|
199
|
+
if (result) {
|
|
200
|
+
data['data'] = searchData(result, data.filter)
|
|
201
|
+
self.wsManager.send(socket, 'readDocuments', data );
|
|
202
|
+
} else {
|
|
203
|
+
console.log(error)
|
|
204
|
+
self.wsManager.send(socket, 'ServerError', error);
|
|
205
|
+
}
|
|
206
|
+
})
|
|
207
|
+
} catch (error) {
|
|
208
|
+
console.log('readDocuments error', error);
|
|
209
|
+
this.wsManager.send(socket, 'ServerError', 'error');
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
212
213
|
/** Create Collection **/
|
|
213
|
-
async createCollection(socket,
|
|
214
|
+
async createCollection(socket, data) {
|
|
214
215
|
const self = this;
|
|
215
216
|
|
|
216
217
|
try {
|
|
217
|
-
const db = this.dbClient.db(
|
|
218
|
-
db.createCollection(
|
|
218
|
+
const db = this.dbClient.db(data['organization_id']);
|
|
219
|
+
db.createCollection(data.collection, function(error, result) {
|
|
219
220
|
if (!error) {
|
|
220
|
-
|
|
221
|
-
self.broadcast(socket, 'createCollection', response, socketInfo)
|
|
221
|
+
self.broadcast(socket, 'createCollection', data)
|
|
222
222
|
} else {
|
|
223
|
-
self.wsManager.send(socket, 'ServerError', error
|
|
223
|
+
self.wsManager.send(socket, 'ServerError', error);
|
|
224
224
|
}
|
|
225
225
|
})
|
|
226
226
|
} catch (error) {
|
|
227
227
|
console.log(error);
|
|
228
|
-
self.wsManager.send(socket, 'ServerError', 'error'
|
|
228
|
+
self.wsManager.send(socket, 'ServerError', 'error');
|
|
229
229
|
}
|
|
230
230
|
}
|
|
231
231
|
|
|
232
232
|
/** Update Collection **/
|
|
233
|
-
async updateCollection(socket,
|
|
233
|
+
async updateCollection(socket, data) {
|
|
234
234
|
const self = this;
|
|
235
235
|
|
|
236
236
|
try {
|
|
237
|
-
const db = this.dbClient.db(
|
|
238
|
-
const collection = db.collection(
|
|
239
|
-
collection.rename(
|
|
237
|
+
const db = this.dbClient.db(data['organization_id']);
|
|
238
|
+
const collection = db.collection(data["collection"]);
|
|
239
|
+
collection.rename(data.target, function(error, result) {
|
|
240
240
|
if (!error) {
|
|
241
|
-
|
|
242
|
-
self.broadcast(socket, 'updateCollection', response, socketInfo)
|
|
241
|
+
self.broadcast(socket, 'updateCollection', data)
|
|
243
242
|
} else {
|
|
244
|
-
self.wsManager.send(socket, 'ServerError', error
|
|
243
|
+
self.wsManager.send(socket, 'ServerError', error);
|
|
245
244
|
}
|
|
246
245
|
})
|
|
247
246
|
} catch (error) {
|
|
248
247
|
console.log(error);
|
|
249
|
-
self.wsManager.send(socket, 'ServerError', 'error'
|
|
248
|
+
self.wsManager.send(socket, 'ServerError', 'error');
|
|
250
249
|
}
|
|
251
250
|
}
|
|
252
251
|
|
|
253
252
|
/** Delete Collection **/
|
|
254
|
-
async deleteCollection(socket,
|
|
253
|
+
async deleteCollection(socket, data) {
|
|
255
254
|
const self = this;
|
|
256
255
|
|
|
257
256
|
try {
|
|
258
|
-
const db = this.dbClient.db(
|
|
259
|
-
const collection = db.collection(
|
|
257
|
+
const db = this.dbClient.db(data['organization_id']);
|
|
258
|
+
const collection = db.collection(data["collection"]);
|
|
260
259
|
collection.drop( function(error, result) {
|
|
261
260
|
if (!error) {
|
|
262
|
-
|
|
263
|
-
self.broadcast(socket, 'deleteCollection', response, socketInfo)
|
|
261
|
+
self.broadcast(socket, 'deleteCollection', data)
|
|
264
262
|
} else {
|
|
265
|
-
self.wsManager.send(socket, 'ServerError', error
|
|
263
|
+
self.wsManager.send(socket, 'ServerError', error);
|
|
266
264
|
}
|
|
267
265
|
})
|
|
268
266
|
} catch (error) {
|
|
269
267
|
console.log(error);
|
|
270
|
-
self.wsManager.send(socket, 'ServerError', 'error'
|
|
268
|
+
self.wsManager.send(socket, 'ServerError', 'error');
|
|
271
269
|
}
|
|
272
270
|
}
|
|
273
271
|
|
|
274
|
-
|
|
275
|
-
|
|
272
|
+
async readCollections(socket, data) {
|
|
273
|
+
try {
|
|
274
|
+
const self = this;
|
|
275
|
+
data['collection'] = 'collections'
|
|
276
|
+
|
|
277
|
+
let {query, sort} = this.getFilters(data);
|
|
278
|
+
const db = this.dbClient.db(data['organization_id']);
|
|
279
|
+
db.listCollections(query).toArray(function(error, result) {
|
|
280
|
+
if (!error && result && result.length > 0) {
|
|
281
|
+
data.data = sortData(result, sort)
|
|
282
|
+
self.wsManager.send(socket, 'readCollections', data);
|
|
283
|
+
}
|
|
284
|
+
})
|
|
285
|
+
} catch(error) {
|
|
286
|
+
console.log('readCollections error', error);
|
|
287
|
+
this.wsManager.send(socket, 'ServerError', 'error');
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
getFilters(data) {
|
|
292
|
+
let query = {}, sort = {}
|
|
293
|
+
data.filter = {
|
|
294
|
+
query: [],
|
|
295
|
+
sort: [],
|
|
296
|
+
search: {
|
|
297
|
+
value: [],
|
|
298
|
+
type: "or"
|
|
299
|
+
},
|
|
300
|
+
startIndex: 0,
|
|
301
|
+
...data.filter
|
|
302
|
+
};
|
|
303
|
+
if (data.filter) {
|
|
304
|
+
|
|
305
|
+
query = this.createQuery(data['filter'].query);
|
|
306
|
+
if (data["document_id"]) {
|
|
307
|
+
query['_id'] = ObjectId(data["document_id"]);
|
|
308
|
+
}
|
|
309
|
+
if (data.filter.sort)
|
|
310
|
+
data.filter.sort.forEach((order) => {
|
|
311
|
+
sort[order.name] = order.type
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
return {query, sort}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
createQuery(filters, data) {
|
|
318
|
+
let query = new Object();
|
|
319
|
+
|
|
320
|
+
filters.forEach((item) => {
|
|
321
|
+
if (!item.name) {
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
var key = item.name;
|
|
325
|
+
if (!query[key]) {
|
|
326
|
+
query[key] = {};
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (item.name == "_id")
|
|
330
|
+
item.value = ObjectId(item.value)
|
|
331
|
+
|
|
332
|
+
switch (item.operator) {
|
|
333
|
+
case '$contain':
|
|
334
|
+
var in_values = [];
|
|
335
|
+
item.value.forEach(function(v) {
|
|
336
|
+
in_values.push(new RegExp(".*" + v + ".*", "i"));
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
query[key] = {$in : in_values }
|
|
340
|
+
break;
|
|
341
|
+
|
|
342
|
+
case '$range':
|
|
343
|
+
if (item.value[0] !== null && item.value[1] !== null) {
|
|
344
|
+
query[key] = {$gte: item.value[0], $lte: item.value[1]};
|
|
345
|
+
} else if (item.value[0] !== null) {
|
|
346
|
+
query[key] = {$gte: item.value[0]};
|
|
347
|
+
} else if (item.value[1] !== null) {
|
|
348
|
+
query[key] = {$lte: item.value[1]};
|
|
349
|
+
}
|
|
350
|
+
break;
|
|
351
|
+
|
|
352
|
+
case '$eq':
|
|
353
|
+
case '$ne':
|
|
354
|
+
case '$lt':
|
|
355
|
+
case '$lte':
|
|
356
|
+
case '$gt':
|
|
357
|
+
case '$gte':
|
|
358
|
+
query[key][item.operator] = item.value[0] || item.value;
|
|
359
|
+
break;
|
|
360
|
+
case '$in':
|
|
361
|
+
case '$nin':
|
|
362
|
+
query[key][item.operator] = item.value;
|
|
363
|
+
break;
|
|
364
|
+
case '$geoWithin':
|
|
365
|
+
try {
|
|
366
|
+
let value = JSON.parse(item.value);
|
|
367
|
+
if (item.type) {
|
|
368
|
+
query[key]['$geoWithin'] = {
|
|
369
|
+
[item.type]: value
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
} catch(e) {
|
|
373
|
+
console.log('geowithin error');
|
|
374
|
+
}
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
})
|
|
378
|
+
|
|
379
|
+
//. global search
|
|
380
|
+
//. we have to set indexes in text fields ex: db.chart.createIndex({ "$**": "text" })
|
|
381
|
+
// if (data['searchKey']) {
|
|
382
|
+
// query["$text"] = {$search: "\"Ni\""};
|
|
383
|
+
// }
|
|
384
|
+
return query;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
valueTypes(data) {
|
|
388
|
+
let object = {}
|
|
389
|
+
if( typeof data === 'object' ) {
|
|
390
|
+
// update['$set'] = {}
|
|
391
|
+
for (const [key, value] of Object.entries(replaceArray(data))) {
|
|
392
|
+
let val;
|
|
393
|
+
let valueType = typeof value;
|
|
394
|
+
switch(valueType) {
|
|
395
|
+
case 'string':
|
|
396
|
+
val = value
|
|
397
|
+
break;
|
|
398
|
+
case 'number':
|
|
399
|
+
val = Number(value)
|
|
400
|
+
break;
|
|
401
|
+
case 'object':
|
|
402
|
+
if (Array.isArray(value))
|
|
403
|
+
val = new Array(...value)
|
|
404
|
+
else
|
|
405
|
+
val = new Object(value)
|
|
406
|
+
break;
|
|
407
|
+
default:
|
|
408
|
+
val = value
|
|
409
|
+
}
|
|
410
|
+
object[key] = val
|
|
411
|
+
}
|
|
412
|
+
return object;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
broadcast(socket, component, response) {
|
|
417
|
+
this.wsManager.broadcast(socket, component, response);
|
|
276
418
|
process.emit('changed-document', response)
|
|
277
419
|
}
|
|
420
|
+
|
|
278
421
|
}
|
|
279
422
|
|
|
280
423
|
module.exports = CoCreateCrud;
|
package/src/database.js
CHANGED
|
@@ -7,12 +7,12 @@ class CoCreateDatabases {
|
|
|
7
7
|
|
|
8
8
|
init() {
|
|
9
9
|
if (this.wsManager) {
|
|
10
|
-
this.wsManager.on('readDatabases', (socket, data
|
|
10
|
+
this.wsManager.on('readDatabases', (socket, data) => this.readDatabases(socket, data));
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
/** Create Document **/
|
|
15
|
-
async readDatabases(socket, req_data
|
|
15
|
+
async readDatabases(socket, req_data){
|
|
16
16
|
const self = this;
|
|
17
17
|
// if(!req_data.data) return;
|
|
18
18
|
|
|
@@ -29,13 +29,13 @@ class CoCreateDatabases {
|
|
|
29
29
|
})
|
|
30
30
|
} catch(error) {
|
|
31
31
|
console.log('readDatabases error', error);
|
|
32
|
-
self.wsManager.send(socket, 'ServerError', 'error'
|
|
32
|
+
self.wsManager.send(socket, 'ServerError', 'error');
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
|
|
37
|
-
broadcast(socket, component, response
|
|
38
|
-
this.wsManager.broadcast(socket,
|
|
37
|
+
broadcast(socket, component, response) {
|
|
38
|
+
this.wsManager.broadcast(socket, component, response);
|
|
39
39
|
process.emit('changed-document', response)
|
|
40
40
|
}
|
|
41
41
|
}
|
package/src/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const crud = require('./crud');
|
|
4
|
-
const list = require('./list');
|
|
5
4
|
const backup = require('./backup');
|
|
6
5
|
const database = require('./database');
|
|
7
6
|
|
|
@@ -14,7 +13,6 @@ class CoCreateCrudServer {
|
|
|
14
13
|
|
|
15
14
|
init() {
|
|
16
15
|
new crud(this.wsManager, this.dbClient);
|
|
17
|
-
new list(this.wsManager, this.dbClient);
|
|
18
16
|
new backup(this.wsManager, this.dbClient);
|
|
19
17
|
new database(this.wsManager, this.dbClient);
|
|
20
18
|
}
|
package/src/list.js
DELETED
|
@@ -1,356 +0,0 @@
|
|
|
1
|
-
const {ObjectId} = require("mongodb");
|
|
2
|
-
|
|
3
|
-
class CoCreateList {
|
|
4
|
-
constructor(wsManager, dbClient) {
|
|
5
|
-
this.wsManager = wsManager
|
|
6
|
-
this.dbClient = dbClient
|
|
7
|
-
this.init()
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
init() {
|
|
11
|
-
this.wsManager.on('readDocuments', (socket, data, socketInfo) => this.readDocuments(socket, data, socketInfo));
|
|
12
|
-
this.wsManager.on('readCollections', (socket, data, socketInfo) => this.readCollections(socket, data, socketInfo));
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
data: {
|
|
19
|
-
collection: "modules",
|
|
20
|
-
element: "xxxx",
|
|
21
|
-
metadata: "",
|
|
22
|
-
operator: {
|
|
23
|
-
filters: [{
|
|
24
|
-
name: 'field1',
|
|
25
|
-
operator: "$contain | $range | $eq | $ne | $lt | $lte | $gt | $gte | $in | $nin | $geoWithin",
|
|
26
|
-
value: [v1, v2, ...]
|
|
27
|
-
}, {
|
|
28
|
-
....
|
|
29
|
-
}],
|
|
30
|
-
orders: [{
|
|
31
|
-
name: 'field-x',
|
|
32
|
-
type: 1 | -1
|
|
33
|
-
}],
|
|
34
|
-
search: {
|
|
35
|
-
type: 'or | and',
|
|
36
|
-
value: [value1, value2]
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
startIndex: 0 (integer),
|
|
40
|
-
count: 0 (integer)
|
|
41
|
-
},
|
|
42
|
-
|
|
43
|
-
//. case fetch document case
|
|
44
|
-
export: true | false
|
|
45
|
-
-------- additional response data -----------
|
|
46
|
-
data: [] // array
|
|
47
|
-
}
|
|
48
|
-
**/
|
|
49
|
-
async readDocuments(socket, data, socketInfo) {
|
|
50
|
-
function sleep(ms) {
|
|
51
|
-
return new Promise((resolve) => {
|
|
52
|
-
setTimeout(resolve, ms);
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const self = this;
|
|
57
|
-
|
|
58
|
-
try {
|
|
59
|
-
const db = this.dbClient.db(data['organization_id']);
|
|
60
|
-
const collection = db.collection(data["collection"]);
|
|
61
|
-
let {operator, query, sort} = this.getFilters(data);
|
|
62
|
-
collection.find(query).sort(sort).toArray(function(error, result) {
|
|
63
|
-
if (result) {
|
|
64
|
-
let result_data = self.filterResponse(result, data, operator)
|
|
65
|
-
self.wsManager.send(socket, 'readDocuments', { ...data, data: result_data, operator, socketInfo });
|
|
66
|
-
} else {
|
|
67
|
-
console.log(error)
|
|
68
|
-
self.wsManager.send(socket, 'ServerError', error, socketInfo);
|
|
69
|
-
}
|
|
70
|
-
})
|
|
71
|
-
} catch (error) {
|
|
72
|
-
console.log('readDocuments error', error);
|
|
73
|
-
this.wsManager.send(socket, 'ServerError', 'error', socketInfo);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
async readCollections(socket, data, socketInfo) {
|
|
78
|
-
try {
|
|
79
|
-
const self = this;
|
|
80
|
-
data['collection'] = 'collections'
|
|
81
|
-
|
|
82
|
-
let {operator, query, sort} = this.getFilters(data);
|
|
83
|
-
const db = this.dbClient.db(data['organization_id']);
|
|
84
|
-
db.listCollections(query).toArray(function(error, result) {
|
|
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
|
-
|
|
114
|
-
self.wsManager.send(socket, 'readCollections', {...data, data: result }, socketInfo);
|
|
115
|
-
}
|
|
116
|
-
})
|
|
117
|
-
} catch(error) {
|
|
118
|
-
console.log('readCollections error', error);
|
|
119
|
-
this.wsManager.send(socket, 'ServerError', 'error', socketInfo);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* function that make query from data
|
|
125
|
-
*
|
|
126
|
-
*/
|
|
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) {
|
|
185
|
-
var query = new Object();
|
|
186
|
-
|
|
187
|
-
var filters = data['filters'];
|
|
188
|
-
|
|
189
|
-
filters.forEach((item) => {
|
|
190
|
-
if (!item.name) {
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
var key = item.name;
|
|
194
|
-
if (!query[key]) {
|
|
195
|
-
query[key] = {};
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
if (item.name == "_id") item.value = item.value.map(v => new ObjectId(v))
|
|
199
|
-
|
|
200
|
-
switch (item.operator) {
|
|
201
|
-
case '$contain':
|
|
202
|
-
var in_values = [];
|
|
203
|
-
item.value.forEach(function(v) {
|
|
204
|
-
in_values.push(new RegExp(".*" + v + ".*", "i"));
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
query[key] = {$in : in_values }
|
|
208
|
-
break;
|
|
209
|
-
|
|
210
|
-
case '$range':
|
|
211
|
-
if (item.value[0] !== null && item.value[1] !== null) {
|
|
212
|
-
query[key] = {$gte: item.value[0], $lte: item.value[1]};
|
|
213
|
-
} else if (item.value[0] !== null) {
|
|
214
|
-
query[key] = {$gte: item.value[0]};
|
|
215
|
-
} else if (item.value[1] !== null) {
|
|
216
|
-
query[key] = {$lte: item.value[1]};
|
|
217
|
-
}
|
|
218
|
-
break;
|
|
219
|
-
|
|
220
|
-
case '$eq':
|
|
221
|
-
case '$ne':
|
|
222
|
-
case '$lt':
|
|
223
|
-
case '$lte':
|
|
224
|
-
case '$gt':
|
|
225
|
-
case '$gte':
|
|
226
|
-
query[key][item.operator] = item.value[0];
|
|
227
|
-
break;
|
|
228
|
-
case '$in':
|
|
229
|
-
case '$nin':
|
|
230
|
-
query[key][item.operator] = item.value;
|
|
231
|
-
break;
|
|
232
|
-
case '$geoWithin':
|
|
233
|
-
try {
|
|
234
|
-
let value = JSON.parse(item.value);
|
|
235
|
-
if (item.type) {
|
|
236
|
-
query[key]['$geoWithin'] = {
|
|
237
|
-
[item.type]: value
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
} catch(e) {
|
|
241
|
-
console.log('geowithin error');
|
|
242
|
-
}
|
|
243
|
-
break;
|
|
244
|
-
}
|
|
245
|
-
})
|
|
246
|
-
|
|
247
|
-
//. global search
|
|
248
|
-
//. we have to set indexes in text fields ex: db.chart.createIndex({ "$**": "text" })
|
|
249
|
-
// if (data['searchKey']) {
|
|
250
|
-
// query["$text"] = {$search: "\"Ni\""};
|
|
251
|
-
// }
|
|
252
|
-
return query;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
//. or operator
|
|
257
|
-
readOrSearch(results, search) {
|
|
258
|
-
var tmp
|
|
259
|
-
if (search && search.length > 0) {
|
|
260
|
-
|
|
261
|
-
tmp = results.filter(function(item) {
|
|
262
|
-
|
|
263
|
-
for (var key in item) {
|
|
264
|
-
var value = item[key];
|
|
265
|
-
var __status = false;
|
|
266
|
-
|
|
267
|
-
var str_value = value;
|
|
268
|
-
|
|
269
|
-
if (Array.isArray(str_value) || typeof str_value == 'number') {
|
|
270
|
-
str_value = str_value.toString();
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
if (typeof str_value == 'string') {
|
|
274
|
-
str_value = str_value.toUpperCase();
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
for (let i = 0; i < search.length; i++) {
|
|
278
|
-
if (typeof search[i] == 'string' && typeof str_value == 'string') {
|
|
279
|
-
if (str_value.indexOf(search[i].toUpperCase()) > -1) {
|
|
280
|
-
__status = true;
|
|
281
|
-
break;
|
|
282
|
-
}
|
|
283
|
-
} else {
|
|
284
|
-
if (value == search[i]) {
|
|
285
|
-
__status = true;
|
|
286
|
-
break;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
if (__status) {
|
|
292
|
-
return true;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
return false;
|
|
297
|
-
})
|
|
298
|
-
} else {
|
|
299
|
-
tmp = results;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
return tmp;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
//. and operator
|
|
307
|
-
readAndSearch(results, search) {
|
|
308
|
-
var tmp
|
|
309
|
-
if (search && search.length > 0) {
|
|
310
|
-
|
|
311
|
-
tmp = results.filter(function(item) {
|
|
312
|
-
|
|
313
|
-
for (let i = 0; i < search.length; i++) {
|
|
314
|
-
var __status = false;
|
|
315
|
-
|
|
316
|
-
for (var key in item) {
|
|
317
|
-
var value = item[key];
|
|
318
|
-
|
|
319
|
-
if (typeof search[i] == 'string') {
|
|
320
|
-
|
|
321
|
-
if (Array.isArray(value) || typeof value == 'number' ) {
|
|
322
|
-
value = value.toString();
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
if (typeof value == 'string') {
|
|
326
|
-
value = value.toUpperCase();
|
|
327
|
-
if (value.indexOf(search[i].toUpperCase()) > -1) {
|
|
328
|
-
__status = true;
|
|
329
|
-
break;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
} else {
|
|
334
|
-
if (value == search[i]) {
|
|
335
|
-
__status = true;
|
|
336
|
-
break;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
if (!__status) {
|
|
342
|
-
return false;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
return true;
|
|
347
|
-
})
|
|
348
|
-
} else {
|
|
349
|
-
tmp = results;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
return tmp;
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
module.exports = CoCreateList;
|