@cocreate/crud-server 1.12.0 → 1.13.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,23 @@
1
+ # [1.13.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.12.0...v1.13.0) (2022-11-21)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * bump [@cocreate](https://github.com/cocreate) dependencies ([a7abd3b](https://github.com/CoCreate-app/CoCreate-crud-server/commit/a7abd3bb30a8d6c46dce0033a2fa67e33ff6e548))
7
+ * data.request takes priority ([d8f584e](https://github.com/CoCreate-app/CoCreate-crud-server/commit/d8f584ee978c4ae6dcd66e30a46f66724d60bb04))
8
+ * removed data.data ([cc66783](https://github.com/CoCreate-app/CoCreate-crud-server/commit/cc66783f6733ea67ae990f9894a94cea0993f069))
9
+ * renamed data.data to data.document ([ed5348f](https://github.com/CoCreate-app/CoCreate-crud-server/commit/ed5348f9a5da8055b885b7e142acd2eec5e02be8))
10
+ * set _id using ObjectId ([f2420f6](https://github.com/CoCreate-app/CoCreate-crud-server/commit/f2420f6ac26bc636c76a81fc152e474cdb3075a4))
11
+
12
+
13
+ ### Features
14
+
15
+ * broadcast crud even if no database exist in server ([5b53fc2](https://github.com/CoCreate-app/CoCreate-crud-server/commit/5b53fc27a353f00617638fb36da278e76aa88b79))
16
+ * crud can connect to multiple databases, mongodb adapter for crud ([b4e6b61](https://github.com/CoCreate-app/CoCreate-crud-server/commit/b4e6b618e4c6197a9fe1cb2a32f7419898e86d30))
17
+ * data response also available as data.document, improved handeling of data[type] ([bb25d9f](https://github.com/CoCreate-app/CoCreate-crud-server/commit/bb25d9f57875bbaa03bd2cafd9333e5f9d0c57f9))
18
+ * mongodb - crud multiple databases, collections, and documents ([1ec4b27](https://github.com/CoCreate-app/CoCreate-crud-server/commit/1ec4b278e4ce830d8e452944c144ecb434027c37))
19
+ * return documents containing db, database, and collection ([d13db96](https://github.com/CoCreate-app/CoCreate-crud-server/commit/d13db962500fc196af68f6c8e227ce9ba3241188))
20
+
1
21
  # [1.12.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.11.1...v1.12.0) (2022-10-02)
2
22
 
3
23
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/crud-server",
3
- "version": "1.12.0",
3
+ "version": "1.13.0",
4
4
  "description": "CoCreate-crud-server",
5
5
  "keywords": [
6
6
  "cocreate-crud",
@@ -40,7 +40,7 @@
40
40
  },
41
41
  "homepage": "https://cocreate.app/docs/CoCreate-crud-server",
42
42
  "dependencies": {
43
- "@cocreate/docs": "^1.3.18",
43
+ "@cocreate/docs": "^1.3.21",
44
44
  "csvtojson": "^2.0.10",
45
45
  "json-2-csv": "^3.10.3",
46
46
  "mongodb": "^4.4.0"
package/src/index.js CHANGED
@@ -1,21 +1,162 @@
1
1
  'use strict';
2
2
 
3
- const crud = require('./crud');
4
- const backup = require('./backup');
5
- const database = require('./database');
3
+ const {replaceArray} = require("./utils.crud.js")
4
+ const {searchData, sortData} = require("@cocreate/filter")
5
+ const mongodb = require('./mongodb/mongodb');
6
6
 
7
7
  class CoCreateCrudServer {
8
- constructor(wsManager, dbClient) {
8
+ constructor(wsManager) {
9
9
  this.wsManager = wsManager
10
- this.dbClient = dbClient
11
- this.init()
10
+ this.dbs = ['mongodb']
11
+ this.mongodb = mongodb
12
+ this.init();
13
+ }
14
+
15
+ init() {
16
+ if (this.wsManager) {
17
+ this.wsManager.on('createDatabase', (socket, data) => this.db(socket, 'createDatabase', data))
18
+ this.wsManager.on('readDatabase', (socket, data) => this.db(socket, 'readDatabase', data))
19
+ this.wsManager.on('updateDatabase', (socket, data) => this.db(socket, 'updateDatabase', data))
20
+ this.wsManager.on('deleteDatabase', (socket, data) => this.db(socket, 'deleteDatabase', data))
21
+ this.wsManager.on('createCollection', (socket, data) => this.db(socket, 'createCollection', data))
22
+ this.wsManager.on('readCollection', (socket, data) => this.db(socket, 'readCollection', data))
23
+ this.wsManager.on('updateCollection', (socket, data) => this.db(socket, 'updateCollection', data))
24
+ this.wsManager.on('deleteCollection', (socket, data) => this.db(socket, 'deleteCollection', data))
25
+ this.wsManager.on('createDocument', (socket, data) => this.db(socket, 'createDocument', data))
26
+ this.wsManager.on('readDocument', (socket, data) => this.db(socket, 'readDocument', data))
27
+ this.wsManager.on('updateDocument', (socket, data) => this.db(socket, 'updateDocument', data))
28
+ this.wsManager.on('deleteDocument', (socket, data) => this.db(socket, 'deleteDocument', data))
29
+ }
30
+ }
31
+
32
+ async databaseStats(data) {
33
+ data = await this.db('', 'databaseStats', data)
34
+ return data
35
+ }
36
+
37
+ async createCollection(data) {
38
+ data = await this.db('', 'createCollection', data)
39
+ return data
40
+ }
41
+
42
+ async readCollection(data) {
43
+ data = await this.db('', 'readCollection', data)
44
+ return data
45
+ }
46
+
47
+ async readCollections(data) {
48
+ data = await this.db('', 'readCollections', data)
49
+ return data
50
+ }
51
+
52
+ async updateCollection(data) {
53
+ data = await this.db('', 'updateCollection', data)
54
+ return data
12
55
  }
13
56
 
14
- init() {
15
- new crud(this.wsManager, this.dbClient);
16
- new backup(this.wsManager, this.dbClient);
17
- new database(this.wsManager, this.dbClient);
18
- }
57
+ async deleteCollection(data) {
58
+ data = await this.db('', 'deleteCollection', data)
59
+ return data
60
+ }
61
+
62
+ async createDocument(data) {
63
+ data = await this.db('', 'createDocument', data)
64
+ return data
65
+ }
66
+
67
+ async readDocument(data) {
68
+ data = await this.db('', 'readDocument', data)
69
+ return data
70
+ }
71
+
72
+ async updateDocument(data) {
73
+ data = await this.db('', 'updateDocument', data)
74
+ return data
75
+ }
76
+
77
+ async deleteDocument(data) {
78
+ data = await this.db('', 'deleteDocument', data)
79
+ return data
80
+ }
81
+
82
+ async db(socket, action, data) {
83
+ return new Promise(async (resolve) => {
84
+ try {
85
+ data['timeStamp'] = new Date().toISOString()
86
+
87
+ if (action == 'updateDocument' && data.upsert != false)
88
+ data.upsert = true
89
+
90
+ // ToDo: support stats from multiple dbs
91
+ if (data.collection || action == 'databaseStats') {
92
+ if (!data.db)
93
+ data['db'] = ['indexeddb', 'mongodb']
94
+ if (!data.database)
95
+ data['database'] = data.organization_id || process.env.organization_id
96
+ if (!data.organization_id)
97
+ data['organization_id'] = process.env.organization_id
98
+ }
99
+
100
+ if (!data.db || !data.db.length)
101
+ data.db = this.dbs
102
+ let dbsLength = data.db.length
103
+ for (let i = 0; i < data.db.length; i++) {
104
+ dbsLength -= 1
105
+ if (this.dbs.includes(data.db[i])) {
106
+ this[data.db[i]][action](data).then((data) => {
107
+ //ToDo: sorting should take place here in order to return sorted values from multiple dbs
108
+ if (!dbsLength) {
109
+ if (socket) {
110
+ this.wsManager.broadcast(socket, action, data);
111
+ process.emit('changed-document', data)
112
+ resolve()
113
+ } else {
114
+ resolve(data)
115
+ }
116
+ }
117
+ })
118
+ } else {
119
+ if (!dbsLength) {
120
+ if (socket) {
121
+ this.wsManager.broadcast(socket, action, data);
122
+ process.emit('changed-document', data)
123
+ resolve()
124
+ } else {
125
+ resolve(data)
126
+ }
127
+ }
128
+ }
129
+ }
130
+ } catch(error) {
131
+ if (socket) {
132
+ errorHandler(data, error)
133
+ this.wsManager.send(socket, action, data);
134
+ resolve()
135
+ } else {
136
+ resolve(data)
137
+ }
138
+ }
139
+ });
140
+ }
141
+
142
+ ObjectId = (rnd = r16 => Math.floor(r16).toString(16)) =>
143
+ rnd(Date.now()/1000) + ' '.repeat(16).replace(/./g, () => rnd(Math.random()*16))
144
+
145
+
146
+ errorHandler(data, error, database, collection){
147
+ if (typeof error == 'object')
148
+ error['db'] = 'mongodb'
149
+ else
150
+ error = {location: 'crudServer', message: error}
151
+
152
+ if (database)
153
+ error['database'] = database
154
+
155
+ if(data.error)
156
+ data.error.push(error)
157
+ else
158
+ data.error = [error]
159
+ }
19
160
  }
20
161
 
21
162
  module.exports = CoCreateCrudServer;