@cocreate/crud-server 1.24.7 → 1.25.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 CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.25.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.24.7...v1.25.0) (2023-08-16)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * crud attributes renamed ([757fa5a](https://github.com/CoCreate-app/CoCreate-crud-server/commit/757fa5a51bc511be8fd428ae9d64a1456cc536e8))
7
+ * Refactored *-target to *-selector ([fab5662](https://github.com/CoCreate-app/CoCreate-crud-server/commit/fab5662929f208bb5da7c47f0a10d673ef261fb7))
8
+
9
+
10
+ ### Features
11
+
12
+ * Add sorting functionality for returning sorted values from multiple storages. ([667ad70](https://github.com/CoCreate-app/CoCreate-crud-server/commit/667ad70f8704de0fc8ff504e831fa64d954da012))
13
+ * name attribute and variable renamed to key ([d5cf8bc](https://github.com/CoCreate-app/CoCreate-crud-server/commit/d5cf8bcd1bea68a480a9a8dec6cf129e6942c8ed))
14
+
1
15
  ## [1.24.7](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.24.6...v1.24.7) (2023-06-16)
2
16
 
3
17
 
@@ -4,8 +4,8 @@ module.exports = {
4
4
  "host": "",
5
5
  "sources": [
6
6
  {
7
- "collection": "files",
8
- "document": {
7
+ "array": "files",
8
+ "object": {
9
9
  "_id": "6204253480b409001727b73e",
10
10
  "name": "index.html",
11
11
  "path": "/docs/crud-server/index.html",
package/docs/index.html CHANGED
@@ -11,10 +11,10 @@
11
11
  sizes="32x32"
12
12
  href="https://cocreate.app/images/favicon.ico" />
13
13
  <meta
14
- name="description"
14
+ key="description"
15
15
  content="A simple HTML5 and pure javascript component. Easy configuration using data-attributes and highly styleable." />
16
16
  <meta
17
- name="keywords"
17
+ key="keywords"
18
18
  content="helper classes, utility classes, css framework, css library, inline style classes" />
19
19
  <meta name="robots" content="index,follow" />
20
20
 
@@ -26,7 +26,7 @@
26
26
  </head>
27
27
 
28
28
  <body>
29
- <div collection="" document_id="" name="" id="cocreate-crud-server">
29
+ <div array="" object="" key="" id="cocreate-crud-server">
30
30
  <div
31
31
  class="display:flex flex-wrap:wrap justify-content:space-between margin:10px">
32
32
  <div class="display:flex align-items:center">
@@ -151,9 +151,9 @@
151
151
  <textarea
152
152
  type="code"
153
153
  lang="html"
154
- collection="demos"
155
- document_id=""
156
- name="demo"
154
+ array="demos"
155
+ object=""
156
+ key="demo"
157
157
  save="false"
158
158
  id="demo"
159
159
  class="height:100% width:100% outline:none border:none resize:none padding:5px"></textarea>
@@ -176,7 +176,7 @@
176
176
  show="#eye-slash"
177
177
  hide="#eye, #demo-preview"
178
178
  toggle="code-height"
179
- toggle-target="#demo-code"
179
+ toggle-selector="#demo-code"
180
180
  ><i class="far fa-eye"></i
181
181
  ></a>
182
182
  <a
@@ -186,7 +186,7 @@
186
186
  show="#eye, #demo-preview"
187
187
  hide="#eye-slash"
188
188
  toggle="code-height"
189
- toggle-target="#demo-code"
189
+ toggle-selector="#demo-code"
190
190
  ><i class="fas fa-eye-slash"></i
191
191
  ></a>
192
192
  <a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/crud-server",
3
- "version": "1.24.7",
3
+ "version": "1.25.0",
4
4
  "description": "CoCreate-crud-server",
5
5
  "keywords": [
6
6
  "cocreate-crud",
package/src/index.js CHANGED
@@ -43,87 +43,24 @@ class CoCreateCrudServer {
43
43
  process.exit()
44
44
 
45
45
  if (this.wsManager) {
46
- this.wsManager.on('createDatabase', (socket, data) => this.crud(socket, 'createDatabase', data))
47
- this.wsManager.on('readDatabase', (socket, data) => this.crud(socket, 'readDatabase', data))
48
- this.wsManager.on('updateDatabase', (socket, data) => this.crud(socket, 'updateDatabase', data))
49
- this.wsManager.on('deleteDatabase', (socket, data) => this.crud(socket, 'deleteDatabase', data))
50
- this.wsManager.on('createCollection', (socket, data) => this.crud(socket, 'createCollection', data))
51
- this.wsManager.on('readCollection', (socket, data) => this.crud(socket, 'readCollection', data))
52
- this.wsManager.on('updateCollection', (socket, data) => this.crud(socket, 'updateCollection', data))
53
- this.wsManager.on('deleteCollection', (socket, data) => this.crud(socket, 'deleteCollection', data))
54
- this.wsManager.on('createDocument', (socket, data) => this.crud(socket, 'createDocument', data))
55
- this.wsManager.on('readDocument', (socket, data) => this.crud(socket, 'readDocument', data))
56
- this.wsManager.on('updateDocument', (socket, data) => this.crud(socket, 'updateDocument', data))
57
- this.wsManager.on('deleteDocument', (socket, data) => this.crud(socket, 'deleteDocument', data))
58
- }
59
- }
60
-
61
- async databaseStats(data) {
62
- data = await this.crud('', 'databaseStats', data)
63
- return data
64
- }
65
-
66
- async createDatabase(data) {
67
- data = await this.crud('', 'createDatabase', data)
68
- return data
69
- }
70
-
71
- async readDatabase(data) {
72
- data = await this.crud('', 'readDatabase', data)
73
- return data
74
- }
75
-
76
- async updateDatabase(data) {
77
- data = await this.crud('', 'updateDatabase', data)
78
- return data
79
- }
80
-
81
- async deleteDatabase(data) {
82
- data = await this.crud('', 'deleteDatabase', data)
83
- return data
84
- }
85
-
86
- async createCollection(data) {
87
- data = await this.crud('', 'createCollection', data)
88
- return data
89
- }
90
-
91
- async readCollection(data) {
92
- data = await this.crud('', 'readCollection', data)
93
- return data
94
- }
95
-
96
- async updateCollection(data) {
97
- data = await this.crud('', 'updateCollection', data)
98
- return data
99
- }
100
-
101
- async deleteCollection(data) {
102
- data = await this.crud('', 'deleteCollection', data)
103
- return data
104
- }
105
-
106
- async createDocument(data) {
107
- data = await this.crud('', 'createDocument', data)
108
- return data
109
- }
46
+ const method = ['create', 'read', 'update', 'delete'];
47
+ const type = ['storage', 'database', 'array', 'index', 'object'];
110
48
 
111
- async readDocument(data) {
112
- data = await this.crud('', 'readDocument', data)
113
- return data
114
- }
115
-
116
- async updateDocument(data) {
117
- data = await this.crud('', 'updateDocument', data)
118
- return data
49
+ for (let i = 0; i < method.length; i++) {
50
+ for (let j = 0; j < type.length; j++) {
51
+ const action = method[i] + '.' + type[j];
52
+ this.wsManager.on(action, (socket, data) => this.crud(socket, data))
53
+ }
54
+ }
55
+ }
119
56
  }
120
57
 
121
- async deleteDocument(data) {
122
- data = await this.crud('', 'deleteDocument', data)
58
+ async send(data) {
59
+ data = await this.crud('', data)
123
60
  return data
124
61
  }
125
62
 
126
- async crud(socket, action, data) {
63
+ async crud(socket, data) {
127
64
  return new Promise(async (resolve) => {
128
65
  try {
129
66
  if (!data.organization_id || !this.config)
@@ -155,14 +92,15 @@ class CoCreateCrudServer {
155
92
  if (storage)
156
93
  this.storages.set(data.organization_id, storage)
157
94
  } else {
158
- let organization = await this.readDocument({
95
+ let organization = await this.send({
96
+ method: 'read.object',
159
97
  database: this.config.organization_id,
160
- collection: 'organizations',
161
- document: [{ _id: data.organization_id }],
98
+ array: 'organizations',
99
+ object: [{ _id: data.organization_id }],
162
100
  organization_id: this.config.organization_id
163
101
  })
164
- if (organization && organization.document && organization.document[0])
165
- organization = organization.document[0]
102
+ if (organization && organization.object && organization.object[0])
103
+ organization = organization.object[0]
166
104
  if (organization && organization.storage) {
167
105
  storage = organization.storage
168
106
  this.storages.set(data.organization_id, storage)
@@ -179,34 +117,35 @@ class CoCreateCrudServer {
179
117
  if (!data['timeStamp'])
180
118
  data['timeStamp'] = new Date().toISOString()
181
119
 
182
- if (action == 'updateDocument' && data.upsert != false)
120
+ if (data.method.startsWith('update') && data.upsert != false)
183
121
  data.upsert = true
184
122
 
123
+ let action = data.method.replace(/\.([a-z])/g, (_, match) => match.toUpperCase());
185
124
  // TODO: support stats from multiple dbs
186
- if (data.collection || action === 'databaseStats') {
125
+ if (data.array || data.method === 'databaseStats') {
187
126
  if (!data.database)
188
127
  data['database'] = data.organization_id
189
128
 
190
- if (action === 'updateDocument' && data.organization_id !== this.config.organization_id) {
129
+ if (data.method.startsWith('update.object') && data.organization_id !== this.config.organization_id) {
191
130
  let syncKeys
192
- if (data.collection === 'organizations')
131
+ if (data.array === 'organizations')
193
132
  syncKeys = ['name', 'logo', 'databases', 'host', 'apis']
194
- else if (data.collection === 'users')
133
+ else if (data.array === 'users')
195
134
  syncKeys = ['name', 'email', 'password', 'avatar']
196
135
 
197
136
  if (syncKeys && syncKeys.length) {
198
137
  let platformUpdate = {
199
138
  database: this.config.organization_id,
200
- collection: data.collection,
201
- document: [{}],
139
+ array: data.array,
140
+ object: [{}],
202
141
  organization_id: this.config.organization_id
203
142
  }
204
143
 
205
- let document = data.document[0] || data.document
206
- if (document) {
144
+ let object = data.object[0] || data.object
145
+ if (object) {
207
146
  for (let key of syncKeys) {
208
- if (document[key])
209
- platformUpdate.document[0][key] = document[key]
147
+ if (object[key])
148
+ platformUpdate.object[0][key] = object[key]
210
149
  }
211
150
  }
212
151
 
@@ -234,7 +173,15 @@ class CoCreateCrudServer {
234
173
  data = await this.databases[db.provider][action](data)
235
174
  }
236
175
 
237
- //TODO: sorting should take place here in order to return sorted values from multiple dbs
176
+ if (data.filter) {
177
+ if (data.filter.sort && data.filter.sort.length)
178
+ data[data.type] = sortData(array, data.filter.sort)
179
+ if (data.filter.index && data.filter.limit) {
180
+ data[data.type] = data[data.type].slice(data.filter.index, data.filter.limit)
181
+ }
182
+ data.filter.count = data[data.type].length
183
+ }
184
+
238
185
  }
239
186
  }
240
187
  }
@@ -246,12 +193,12 @@ class CoCreateCrudServer {
246
193
  config: {
247
194
  organization_id: this.config.organization_id,
248
195
  }
249
- }, action, { ...data });
196
+ }, { ...data });
250
197
  data.organization_id = socket.config.organization_id
251
198
  }
252
199
 
253
- this.wsManager.broadcast(socket, action, data);
254
- process.emit('changed-document', data)
200
+ this.wsManager.broadcast(socket, data);
201
+ process.emit('changed-object', data)
255
202
  resolve()
256
203
  } else {
257
204
  resolve(data)
@@ -259,7 +206,7 @@ class CoCreateCrudServer {
259
206
  } catch (error) {
260
207
  if (socket) {
261
208
  this.errorHandler(data, error)
262
- this.wsManager.send(socket, action, data);
209
+ this.wsManager.send(socket, data);
263
210
  resolve()
264
211
  } else {
265
212
  resolve(data)
@@ -268,7 +215,7 @@ class CoCreateCrudServer {
268
215
  });
269
216
  }
270
217
 
271
- errorHandler(data, error, database, collection) {
218
+ errorHandler(data, error, database, array) {
272
219
  if (typeof error == 'object')
273
220
  error['storage'] = 'mongodb'
274
221
  else
package/src/backup.js DELETED
@@ -1,150 +0,0 @@
1
- const json2csv = require("json-2-csv")
2
- const csvtojson = require("csvtojson");
3
-
4
- class CoCreateBackup {
5
- constructor(wsManager, dbClient) {
6
- this.wsManager = wsManager
7
- this.dbClient = dbClient
8
- this.importCollection = '';
9
- this.importType = '';
10
- this.importDB = '';
11
- this.init();
12
- }
13
-
14
- init() {
15
- if (this.wsManager) {
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('downloadData', (socket, data) => this.downloadData(socket, data))
19
- }
20
- }
21
-
22
- /**
23
- * data: {
24
- collection: '',
25
- type: 'csv/json'
26
- data: JSON data
27
- }
28
- **/
29
- // async downloadData(socket, data) {
30
- // const export_type = data.type || "json";
31
-
32
- // try {
33
- // let binaryData = null;
34
- // const result = data.data;
35
- // const organization_id = socket.config.organization_id ;
36
- // if (export_type === 'csv') {
37
- // binaryData = await json2csv.json2csvAsync(JSON.parse(JSON.stringify(result)), {
38
- // emptyFieldValue: ''
39
- // });
40
- // } else {
41
- // binaryData = Buffer.from(JSON.stringify(result));
42
- // }
43
-
44
- // this.wsManager.send(socket, 'downloadFileInfo', {file_name: `backup_${data['collection']}.${export_type}`, binaryData, export_type}, organization_id);
45
-
46
- // this.wsManager.sendBinary(socket, binaryData, organization_id);
47
-
48
- // } catch (error) {
49
- // console.log('export error', error);
50
- // }
51
- // }
52
-
53
- /**
54
- CoCreateSocket.exportData({
55
- collection: '',
56
- db: '',
57
- export_type: json/csv,
58
-
59
- })
60
- **/
61
- // async exportData(socket, data) {
62
- // const self = this;
63
-
64
- // const export_type = data.export_type || "json";
65
-
66
- // try {
67
-
68
- // var collection = this.dbClient.db(data['namespace']).collection(data["collection"]);
69
- // const organization_id = socket.config.organization_id
70
-
71
- // var query = {};
72
-
73
- // collection.find(query).toArray(async function(error, result) {
74
- // if (!error) {
75
- // let binaryData = null;
76
- // self.wsManager.send(socket, 'downloadFileInfo', {file_name: `backup_${data['collection']}.${export_type}`}, organization_id);
77
- // if (export_type === 'csv') {
78
- // binaryData = await json2csv.json2csvAsync(JSON.parse(JSON.stringify(result)), {
79
- // emptyFieldValue: ''
80
- // });
81
- // } else {
82
- // binaryData = Buffer.from(JSON.stringify(result));
83
- // }
84
-
85
- // self.wsManager.sendBinary(socket, binaryData, organization_id);
86
- // }
87
- // });
88
- // } catch (error) {
89
- // console.log('export error', error);
90
- // }
91
- // }
92
-
93
- // async setImportData(socket, data) {
94
- // this.importCollection = data['collection']
95
- // this.importType = data['import_type'];
96
- // this.importDB = data['namespace'];
97
- // }
98
-
99
- // async importData(socket, data) {
100
- // const importCollection = data['collection']
101
- // const importType = data['import_type'];
102
- // const importFile = data['file'];
103
- // // const importDB = data['namespace'];
104
-
105
- // console.log('import:', importCollection, importType, importFile)
106
- // const self = this;
107
- // // const organization_id = socket.config.organization_id
108
- // if (!importCollection || !importType) {
109
- // return;
110
- // }
111
- // try {
112
- // console.log('data: ', data)
113
- // // return;
114
- // let jsonData = null;
115
- // if (this.importType === 'csv') {
116
- // jsonData = await csvtojson({ignoreEmpty: true}).fromString(data.toString())
117
- // }
118
- // else {
119
- // jsonData = JSON.parse(importFile.toString());
120
- // }
121
- // // TODO: validate json / if json is object error happens
122
- // jsonData.map((item) => delete item._id);
123
- // console.log('json: ', jsonData)
124
- // var collection = this.dbClient.db(organization_id).collection(importCollection);
125
- // // console.log(this.importCollection)
126
- // collection.insertMany(jsonData, function(err, result) {
127
- // if (!err) {
128
- // self.wsManager.send(socket, 'importedFile2DB', {
129
- // 'database': organization_id,
130
- // 'collection': importCollection,
131
- // 'import_type': importType,
132
- // 'data': result
133
- // }, organization_id)
134
- // }
135
- // })
136
-
137
- // // this.importCollection = '';
138
- // // this.importType = '';
139
-
140
- // } catch (error) {
141
- // console.log('import db error', error)
142
- // }
143
-
144
- // }
145
-
146
- }
147
- module.exports = CoCreateBackup;
148
-
149
-
150
-