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