@cocreate/crud-server 1.28.11 → 1.29.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.29.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.28.11...v1.29.0) (2023-11-03)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * removed limit slice ([26c6cc1](https://github.com/CoCreate-app/CoCreate-crud-server/commit/26c6cc1c3f8c760b0c8d90f9ab0923896c2cce59))
7
+ * update dependencies to the lates versions ([e2ddb24](https://github.com/CoCreate-app/CoCreate-crud-server/commit/e2ddb2440daa92bdcd547b355ef75933eced2947))
8
+ * update method to use object.update etc ([b6400da](https://github.com/CoCreate-app/CoCreate-crud-server/commit/b6400da90f16fd98d59ef18b06f40980f91b67d7))
9
+
10
+
11
+ ### Features
12
+
13
+ * get orgainization and storage once using promise ([1dac253](https://github.com/CoCreate-app/CoCreate-crud-server/commit/1dac2539cb5167da803130d0a94458e02318f63e))
14
+
1
15
  ## [1.28.11](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.28.10...v1.28.11) (2023-10-25)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/crud-server",
3
- "version": "1.28.11",
3
+ "version": "1.29.0",
4
4
  "description": "CoCreate-crud-server",
5
5
  "keywords": [
6
6
  "cocreate-crud",
@@ -40,8 +40,8 @@
40
40
  },
41
41
  "homepage": "https://cocreate.app/docs/CoCreate-crud-server",
42
42
  "dependencies": {
43
- "@cocreate/config": "^1.6.5",
44
- "@cocreate/utils": "^1.26.1",
43
+ "@cocreate/config": "^1.6.6",
44
+ "@cocreate/utils": "^1.26.2",
45
45
  "csvtojson": "^2.0.10",
46
46
  "json-2-csv": "^3.10.3"
47
47
  }
package/src/index.js CHANGED
@@ -8,7 +8,7 @@ class CoCreateCrudServer {
8
8
  this.wsManager = wsManager
9
9
  this.databases = databases
10
10
  this.ObjectId = ObjectId
11
- this.storages = new Map();
11
+ this.organizations = {};
12
12
  this.init()
13
13
  }
14
14
 
@@ -43,12 +43,12 @@ class CoCreateCrudServer {
43
43
  process.exit()
44
44
 
45
45
  if (this.wsManager) {
46
- const method = ['create', 'read', 'update', 'delete'];
47
46
  const type = ['storage', 'database', 'array', 'index', 'object'];
47
+ const method = ['create', 'read', 'update', 'delete'];
48
48
 
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];
49
+ for (let i = 0; i < type.length; i++) {
50
+ for (let j = 0; j < method.length; j++) {
51
+ const action = type[i] + '.' + method[j];
52
52
  this.wsManager.on(action, (data) =>
53
53
  this.send(data))
54
54
  }
@@ -76,35 +76,9 @@ class CoCreateCrudServer {
76
76
  }
77
77
  })
78
78
 
79
- let storages = this.storages.get(data.organization_id)
80
- if (storages === false)
81
- return resolve({ serverStorage: false, error: 'A storage or database could not be found' })
82
-
83
- if (!storages) {
84
- if (data.organization_id === this.config.organization_id) {
85
- storages = this.config.storage
86
- if (storages)
87
- this.storages.set(data.organization_id, storages)
88
- } else {
89
- let organization = await this.send({
90
- method: 'read.object',
91
- database: this.config.organization_id,
92
- array: 'organizations',
93
- object: [{ _id: data.organization_id }],
94
- organization_id: this.config.organization_id
95
- })
96
- if (organization
97
- && organization.object
98
- && organization.object[0]) {
99
- if (organization.object[0].storage)
100
- this.storages.set(data.organization_id, organization.object[0].storage)
101
- else
102
- return resolve({ serverStorage: false, error: 'A storage url could not be found' })
103
- } else {
104
- return resolve({ serverOrganization: false, error: 'An organization could not be found' })
105
- }
106
- }
107
- }
79
+ let storages = await this.getStorage(data)
80
+ if (storages.error)
81
+ return resolve(storages)
108
82
 
109
83
  if (data['timeStamp'])
110
84
  data['timeStamp'] = new Date(data['timeStamp'])
@@ -112,14 +86,14 @@ class CoCreateCrudServer {
112
86
  data['timeStamp'] = new Date()
113
87
 
114
88
  // TODO: manage error handling if if no method defined
115
- if (data.method.startsWith('update.') && data.upsert != false)
89
+ if (data.method.endsWith('.update') && data.upsert != false)
116
90
  data.upsert = true
117
91
 
118
92
  if (data.array) {
119
93
  if (!data.database)
120
94
  data['database'] = data.organization_id
121
95
 
122
- if (data.method.startsWith('update.object') && data.organization_id !== this.config.organization_id) {
96
+ if (data.method === 'object.update' && data.organization_id !== this.config.organization_id) {
123
97
  let syncKeys
124
98
  if (data.array === 'organizations')
125
99
  syncKeys = ['name', 'logo', 'databases', 'host', 'apis']
@@ -144,9 +118,7 @@ class CoCreateCrudServer {
144
118
 
145
119
  this.send(platformUpdate)
146
120
  }
147
-
148
121
  }
149
-
150
122
  }
151
123
 
152
124
  if (!data.storage || !data.storage.length) {
@@ -170,14 +142,11 @@ class CoCreateCrudServer {
170
142
  }
171
143
 
172
144
  if (data.$filter) {
173
- if (!data.type)
174
- data.type = data.method.split('.').pop()
145
+ let type = data.method.split(".")[0]
175
146
  if (data.$filter.sort && data.$filter.sort.length)
176
- data[data.type] = sortData(data[data.type], data.$filter.sort)
177
- if (data.$filter.index && data.$filter.limit)
178
- data[data.type] = data[data.type].slice(data.$filter.index, data.$filter.limit)
147
+ data[type] = sortData(data[type], data.$filter.sort)
179
148
 
180
- data.$filter.count = data[data.type].length
149
+ data.$filter.count = data[type].length
181
150
  }
182
151
 
183
152
  }
@@ -212,6 +181,48 @@ class CoCreateCrudServer {
212
181
  });
213
182
  }
214
183
 
184
+ async getStorage(data) {
185
+ if (this.organizations[data.organization_id])
186
+ return await this.organizations[data.organization_id]
187
+
188
+ if (data.organization_id === this.config.organization_id) {
189
+ this.organizations[data.organization_id] = this.config.storage
190
+ return this.config.storage
191
+ } else {
192
+ if (this.organizations[data.organization_id]) {
193
+ return await this.organizations[data.organization_id]
194
+ } else {
195
+ this.organizations[data.organization_id] = this.getOrganization(data)
196
+ this.organizations[data.organization_id] = await this.organizations[data.organization_id]
197
+ return this.organizations[data.organization_id]
198
+ }
199
+ }
200
+
201
+ }
202
+
203
+ async getOrganization(data) {
204
+ let organization = await this.send({
205
+ method: 'object.read',
206
+ database: this.config.organization_id,
207
+ array: 'organizations',
208
+ object: [{ _id: data.organization_id }],
209
+ organization_id: this.config.organization_id
210
+ })
211
+
212
+ if (organization
213
+ && organization.object
214
+ && organization.object[0]) {
215
+ if (organization.object[0].storage) {
216
+ return organization.object[0].storage
217
+ } else
218
+ return { serverStorage: false, error: 'A storage url could not be found' }
219
+ } else {
220
+ return { serverOrganization: false, error: 'An organization could not be found' }
221
+ }
222
+
223
+ }
224
+
225
+
215
226
  errorHandler(data, error, database, array) {
216
227
  if (typeof error == 'object')
217
228
  error['storage'] = 'mongodb'