@cocreate/crud-server 1.19.0 → 1.20.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.20.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.19.1...v1.20.0) (2023-04-11)
2
+
3
+
4
+ ### Features
5
+
6
+ * use platform database to get databases from organization ([8b2f9b3](https://github.com/CoCreate-app/CoCreate-crud-server/commit/8b2f9b39538448ff1d9a718b580d81cbcbaf52d5))
7
+
8
+ ## [1.19.1](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.19.0...v1.19.1) (2023-04-11)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * pass dbUrl to constructor ([7900c05](https://github.com/CoCreate-app/CoCreate-crud-server/commit/7900c0539998cb5f42cd27aed3844d0d43f5370b))
14
+
1
15
  # [1.19.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.18.0...v1.19.0) (2023-04-11)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/crud-server",
3
- "version": "1.19.0",
3
+ "version": "1.20.0",
4
4
  "description": "CoCreate-crud-server",
5
5
  "keywords": [
6
6
  "cocreate-crud",
package/src/index.js CHANGED
@@ -3,9 +3,10 @@
3
3
  const {ObjectId, searchData, sortData} = require("@cocreate/utils");
4
4
 
5
5
  class CoCreateCrudServer {
6
- constructor(wsManager, databases) {
6
+ constructor(wsManager, databases, database) {
7
7
  this.wsManager = wsManager
8
8
  this.databases = databases
9
+ this.database = database
9
10
  this.ObjectId = ObjectId
10
11
  this.databaseUrls = new Map();
11
12
  this.init();
@@ -100,32 +101,26 @@ class CoCreateCrudServer {
100
101
  resolve()
101
102
 
102
103
  let dbUrl = this.databaseUrls.get(data.organization_id)
103
- if (dbUrl == 'false')
104
+ if (dbUrl === 'false' || dbUrl === false)
104
105
  resolve()
105
106
 
106
107
  if (!dbUrl) {
107
- if (data.organization_id === process.env.organization_id) {
108
- dbUrl = {mongodb: [process.env.MONGO_URL]}
109
- console.log('platform dbUrl crud', dbUrl)
108
+ let organization = await this.databases[this.database.name]['readDocument']({
109
+ dbUrl: this.database.url[0],
110
+ database: process.env.organization_id,
111
+ collection: 'organizations',
112
+ document: [{_id: data.organization_id}],
113
+ organization_id: process.env.organization_id
114
+ })
115
+ if (organization && organization.document && organization.document[0])
116
+ organization = organization.document[0]
117
+ if (organization && organization.databases) {
118
+ dbUrl = organization.databases
110
119
  this.databaseUrls.set(data.organization_id, dbUrl)
111
120
  } else {
112
- let organization = await this.databases['mongodb']['readDocument']({
113
- database: process.env.organization_id,
114
- collection: 'organizations',
115
- document: [{_id: data.organization_id}],
116
- organization_id: process.env.organization_id
117
- })
118
- if (organization && organization.document && organization.document[0])
119
- organization = organization.document[0]
120
- if (organization && organization.databases) {
121
- dbUrl = organization.databases
122
- this.databaseUrls.set(data.organization_id, dbUrl)
123
- console.log('organization dbUrl queried from platform')
124
- } else {
125
- this.databaseUrls.set(data.organization_id, 'false')
126
- console.log('organization or dbUrl urls could not be found')
127
- resolve()
128
- }
121
+ this.databaseUrls.set(data.organization_id, 'false')
122
+ console.log('organization or dbUrl urls could not be found')
123
+ resolve()
129
124
  }
130
125
  }
131
126
 
@@ -136,64 +131,68 @@ class CoCreateCrudServer {
136
131
  data.upsert = true
137
132
 
138
133
  // ToDo: support stats from multiple dbs
139
- if (data.collection || action == 'databaseStats') {
134
+ if (data.collection || action === 'databaseStats') {
140
135
  if (!data.database)
141
136
  data['database'] = data.organization_id
137
+
138
+ if (action === 'updateDocument' && data.organization_id !== process.env.organization_id) {
139
+ let syncKeys
140
+ if (data.collection === 'organizations')
141
+ syncKeys = ['name', 'logo', 'databases', 'hosts', 'apis']
142
+ else if (data.collection === 'users')
143
+ syncKeys = ['name', 'email', 'password', 'avatar']
144
+
145
+ if (syncKeys && syncKeys.length) {
146
+ let platformUpdate = {
147
+ dbUrl: this.database.url,
148
+ database: process.env.organization_id,
149
+ collection: data.collection,
150
+ document: [{}],
151
+ organization_id: process.env.organization_id
152
+ }
153
+
154
+ let document = data.document[0] || data.document
155
+ if (document) {
156
+ for (let key of syncKeys) {
157
+ if (document[key])
158
+ platformUpdate.document[0][key] = document[key]
159
+ }
160
+ }
161
+
162
+ this.databases[this.database.name][action](platformUpdate)
163
+ }
164
+
165
+ }
166
+
142
167
  }
143
168
 
144
169
  if (!data.db || !data.db.length)
145
170
  data.db = ['mongodb']
146
- let dbsLength = data.db.length
171
+
147
172
  for (let i = 0; i < data.db.length; i++) {
148
- dbsLength -= 1
149
- if (this.databases[data.db[i]]) {
150
- // ToDo: for loop on each dbUrl
151
- if (dbUrl && dbUrl[data.db[i]]) {
152
- data['dbUrl'] = dbUrl[data.db[i]][0]
153
- data = await this.databases[data.db[i]][action](data)
154
-
155
- // ToDo: sync orgdb and platformdb specified keys from collections organizations and users
156
- if (data.collection === 'organization' || data.collection === 'user') {
157
- let syncKeys
158
- if (data.collection === 'organizations')
159
- syncKeys = ['name', 'logo', 'databases', 'hosts', 'apis']
160
- else if (data.collection === 'users')
161
- syncKeys = ['name', 'email', 'password', 'avatar']
162
-
163
- if (syncKeys && syncKeys.length) {
164
- let platformUpdate = {
165
- dbUrl: process.env.MONGO_URL,
166
- database: process.env.organization_id,
167
- collection: data.collection,
168
- document: [{}],
169
- organization_id: process.env.organization_id
170
- }
171
-
172
- let document = data.document[0] || data.document
173
- if (document) {
174
- for (let key of syncKeys) {
175
- if (document[key])
176
- platformUpdate.document[0][key] = document[key]
177
- }
178
- }
179
- }
173
+ if (dbUrl && dbUrl[data.db[i]]) {
174
+ let db = dbUrl[data.db[i]]
180
175
 
181
- this.databases['mongodb'][action]()
182
- }
176
+ if (db.name && this.databases[db.name]) {
177
+
178
+ for (let i = 0; i < db.urls.length; i++) {
179
+ data['dbUrl'] = db.urls[i]
180
+ data = await this.databases[db.name][action](data)
181
+ }
182
+
183
183
  //ToDo: sorting should take place here in order to return sorted values from multiple dbs
184
184
  }
185
- }
186
-
187
- if (!dbsLength) {
188
- if (socket) {
189
- this.wsManager.broadcast(socket, action, data);
190
- process.emit('changed-document', data)
191
- resolve()
192
- } else {
193
- resolve(data)
194
- }
195
185
  }
196
186
  }
187
+
188
+ delete data.dbUrl
189
+ if (socket) {
190
+ this.wsManager.broadcast(socket, action, data);
191
+ process.emit('changed-document', data)
192
+ resolve()
193
+ } else {
194
+ resolve(data)
195
+ }
197
196
  } catch(error) {
198
197
  if (socket) {
199
198
  errorHandler(data, error)