@cocreate/crud-server 1.18.0 → 1.19.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,16 @@
1
+ # [1.19.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.18.0...v1.19.0) (2023-04-11)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * renamed domans to hosts ([312353a](https://github.com/CoCreate-app/CoCreate-crud-server/commit/312353a9ec03d65b05d749675db9c73a1e16e069))
7
+
8
+
9
+ ### Features
10
+
11
+ * custom dburls stored and fetched from platform db organizations. sync fields from org and user to platform prototype ([183839b](https://github.com/CoCreate-app/CoCreate-crud-server/commit/183839b02655ac4d7edf24375b9012ad80feeee9))
12
+ * if dbUrls not defined query platform db ([05fecfe](https://github.com/CoCreate-app/CoCreate-crud-server/commit/05fecfe999f471541717765e606aa57649398821))
13
+
1
14
  # [1.18.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.17.29...v1.18.0) (2023-03-19)
2
15
 
3
16
 
@@ -12,7 +12,7 @@ module.exports = {
12
12
  "name": "index.html",
13
13
  "path": "/docs/crud-server/index.html",
14
14
  "src": "{{./docs/index.html}}",
15
- "domains": [
15
+ "hosts": [
16
16
  "*",
17
17
  "general.cocreate.app"
18
18
  ],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/crud-server",
3
- "version": "1.18.0",
3
+ "version": "1.19.0",
4
4
  "description": "CoCreate-crud-server",
5
5
  "keywords": [
6
6
  "cocreate-crud",
package/src/index.js CHANGED
@@ -7,6 +7,7 @@ class CoCreateCrudServer {
7
7
  this.wsManager = wsManager
8
8
  this.databases = databases
9
9
  this.ObjectId = ObjectId
10
+ this.databaseUrls = new Map();
10
11
  this.init();
11
12
  }
12
13
 
@@ -95,20 +96,49 @@ class CoCreateCrudServer {
95
96
  async db(socket, action, data) {
96
97
  return new Promise(async (resolve) => {
97
98
  try {
99
+ if (!data.organization_id)
100
+ resolve()
101
+
102
+ let dbUrl = this.databaseUrls.get(data.organization_id)
103
+ if (dbUrl == 'false')
104
+ resolve()
105
+
106
+ 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)
110
+ this.databaseUrls.set(data.organization_id, dbUrl)
111
+ } 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
+ }
129
+ }
130
+ }
131
+
98
132
  if (!data['timeStamp'])
99
133
  data['timeStamp'] = new Date().toISOString()
100
134
 
101
135
  if (action == 'updateDocument' && data.upsert != false)
102
136
  data.upsert = true
103
-
137
+
104
138
  // ToDo: support stats from multiple dbs
105
139
  if (data.collection || action == 'databaseStats') {
106
- if (!data.db)
107
- data['db'] = ['indexeddb', 'mongodb']
108
140
  if (!data.database)
109
- data['database'] = data.organization_id || process.env.organization_id
110
- if (!data.organization_id)
111
- data['organization_id'] = process.env.organization_id
141
+ data['database'] = data.organization_id
112
142
  }
113
143
 
114
144
  if (!data.db || !data.db.length)
@@ -117,30 +147,50 @@ class CoCreateCrudServer {
117
147
  for (let i = 0; i < data.db.length; i++) {
118
148
  dbsLength -= 1
119
149
  if (this.databases[data.db[i]]) {
120
- if (socket.dbs && socket.dbs[data.db[i]])
121
- data['dbs'] = socket.dbs[data.db[i]][0]
122
-
123
- this.databases[data.db[i]][action](data).then((data) => {
124
- //ToDo: sorting should take place here in order to return sorted values from multiple dbs
125
- if (!dbsLength) {
126
- if (socket) {
127
- this.wsManager.broadcast(socket, action, data);
128
- process.emit('changed-document', data)
129
- resolve()
130
- } else {
131
- resolve(data)
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
+ }
132
179
  }
180
+
181
+ this.databases['mongodb'][action]()
133
182
  }
134
- })
135
- } else {
136
- if (!dbsLength) {
137
- if (socket) {
138
- this.wsManager.broadcast(socket, action, data);
139
- process.emit('changed-document', data)
140
- resolve()
141
- } else {
142
- resolve(data)
143
- }
183
+ //ToDo: sorting should take place here in order to return sorted values from multiple dbs
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)
144
194
  }
145
195
  }
146
196
  }