@cocreate/mongodb 1.24.1 → 1.25.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.25.0](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.24.1...v1.25.0) (2026-07-18)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * update bandwidth event types to 'ingress' and 'egress' ([5ed0df9](https://github.com/CoCreate-app/CoCreate-mongodb/commit/5ed0df9cd4547315bfb1652300e458350bc858c2))
7
+
8
+
9
+ ### Features
10
+
11
+ * add isolated CRUD processors for array, database, and object management ([b8b50e9](https://github.com/CoCreate-app/CoCreate-mongodb/commit/b8b50e986aa3668a4163e3678e4dce4c8d2b1eb7))
12
+ * enhance MongoDB client connection management with improved caching and cleanup on org deletion ([ad8fa09](https://github.com/CoCreate-app/CoCreate-mongodb/commit/ad8fa09b8110152fee829b70e145516c19955f86))
13
+
1
14
  ## [1.24.1](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.24.0...v1.24.1) (2026-07-17)
2
15
 
3
16
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/mongodb",
3
- "version": "1.24.1",
3
+ "version": "1.25.0",
4
4
  "description": "A simple mongodb component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "mongodb",
package/src/array.js ADDED
@@ -0,0 +1,201 @@
1
+ /********************************************************************************
2
+ * Copyright (C) 2023 CoCreate and Contributors.
3
+ *
4
+ * This program is free software: you can redistribute it and/or modify
5
+ * it under the terms of the GNU Affero General Public License as published
6
+ * by the Free Software Foundation, either version 3 of the License, or
7
+ * (at your option) any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ * GNU Affero General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Affero General Public License
15
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
+ ********************************************************************************/
17
+
18
+ import { queryData } from "@cocreate/utils";
19
+ import {
20
+ dbClient,
21
+ createFilter,
22
+ createData,
23
+ errorHandler
24
+ } from "./utilities.js";
25
+
26
+ /**
27
+ * Isolated CRUD Processor for Array/Collection Management.
28
+ *
29
+ * @param {string} method - Operation targeting database collections (read, create, update, delete)
30
+ * @param {Object} data - Context query details containing database and collection matrices
31
+ */
32
+ function array(method, data) {
33
+ return new Promise(
34
+ async (resolve, reject) => {
35
+ let type = "array";
36
+ let arrayArray = [];
37
+
38
+ try {
39
+ const client = await dbClient(data);
40
+ if (!client || client.status === false) return resolve(data);
41
+
42
+ if (data.request) data.array = data.request;
43
+
44
+ let databases = data.database;
45
+ if (!Array.isArray(databases)) databases = [databases];
46
+
47
+ for (let database of databases) {
48
+ const db = client.db(database);
49
+
50
+ if (method == "read") {
51
+ let { query, sort } = await createFilter(data);
52
+
53
+ try {
54
+ const result = await db.listCollections().toArray();
55
+ if (result) {
56
+ process.emit("usage", {
57
+ type: "ingress",
58
+ data: result,
59
+ organization_id: data.organization_id
60
+ });
61
+
62
+ for (let res of result) {
63
+ if (data.$filter && data.$filter.query) {
64
+ let isFilter = queryData(
65
+ res,
66
+ data.$filter.query
67
+ );
68
+ if (isFilter)
69
+ arrayArray.push({
70
+ name: res.name,
71
+ database,
72
+ storage: data.storageName
73
+ });
74
+ } else
75
+ arrayArray.push({
76
+ name: res.name,
77
+ database,
78
+ storage: data.storageName
79
+ });
80
+ }
81
+ }
82
+ } catch (error) {
83
+ errorHandler(data, error, database);
84
+ }
85
+ } else {
86
+ let arrays;
87
+ let value;
88
+ if (method == "update")
89
+ arrays = Object.entries(data.array);
90
+ else arrays = data.array;
91
+
92
+ if (!Array.isArray(arrays)) arrays = [arrays];
93
+
94
+ for (let array of arrays) {
95
+ if (method == "create") {
96
+ process.emit("usage", {
97
+ type: "egress",
98
+ data: array,
99
+ organization_id: data.organization_id
100
+ });
101
+
102
+ try {
103
+ const result = await db.createCollection(array);
104
+ if (result) {
105
+ process.emit("usage", {
106
+ type: "ingress",
107
+ data: { status: "created", name: array },
108
+ organization_id: data.organization_id
109
+ });
110
+
111
+ arrayArray.push({
112
+ name: array,
113
+ database,
114
+ storage: data.storageName
115
+ });
116
+ }
117
+ } catch (error) {
118
+ errorHandler(data, error, database, array);
119
+ }
120
+ } else {
121
+ if (method == "update") {
122
+ [array, value] = array;
123
+ }
124
+
125
+ const arrayObj = db.collection(array);
126
+
127
+ if (method == "update") {
128
+ process.emit("usage", {
129
+ type: "egress",
130
+ data: { rename: value },
131
+ organization_id: data.organization_id
132
+ });
133
+
134
+ try {
135
+ const result = await arrayObj.rename(value);
136
+ if (result) {
137
+ process.emit("usage", {
138
+ type: "ingress",
139
+ data: { status: "renamed", from: array, to: value },
140
+ organization_id: data.organization_id
141
+ });
142
+
143
+ arrayArray.push({
144
+ name: value,
145
+ oldName: array,
146
+ database,
147
+ storage: data.storageName
148
+ });
149
+ }
150
+ } catch (error) {
151
+ errorHandler(data, error, database, array);
152
+ }
153
+ }
154
+
155
+ if (method == "delete") {
156
+ process.emit("usage", {
157
+ type: "egress",
158
+ data: { drop: array },
159
+ organization_id: data.organization_id
160
+ });
161
+
162
+ try {
163
+ const result = await arrayObj.drop();
164
+ if (result) {
165
+ process.emit("usage", {
166
+ type: "ingress",
167
+ data: { status: "dropped", name: array },
168
+ organization_id: data.organization_id
169
+ });
170
+
171
+ arrayArray.push({
172
+ name: array,
173
+ database,
174
+ storage: data.storageName
175
+ });
176
+ }
177
+ } catch (error) {
178
+ errorHandler(data, error, database, array);
179
+ }
180
+ }
181
+ }
182
+ }
183
+ }
184
+ }
185
+
186
+ data = createData(
187
+ data,
188
+ arrayArray,
189
+ type
190
+ );
191
+ resolve(data);
192
+ } catch (error) {
193
+ errorHandler(data, error);
194
+ console.log(method, "error", error);
195
+ resolve(data);
196
+ }
197
+ }
198
+ );
199
+ }
200
+
201
+ export default array;
@@ -0,0 +1,110 @@
1
+ /********************************************************************************
2
+ * Copyright (C) 2023 CoCreate and Contributors.
3
+ *
4
+ * This program is free software: you can redistribute it and/or modify
5
+ * it under the terms of the GNU Affero General Public License as published
6
+ * by the Free Software Foundation, either version 3 of the License, or
7
+ * (at your option) any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ * GNU Affero General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Affero General Public License
15
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
+ ********************************************************************************/
17
+
18
+ import { queryData } from "@cocreate/utils";
19
+ import {
20
+ dbClient,
21
+ createData,
22
+ errorHandler
23
+ } from "./utilities.js";
24
+
25
+ /**
26
+ * Isolated CRUD Processor for Database Management.
27
+ *
28
+ * @param {string} method - Operation targeting database layers (read, delete)
29
+ * @param {Object} data - Context query details and transactional metrics
30
+ */
31
+ function database(method, data) {
32
+ return new Promise(
33
+ async (resolve, reject) => {
34
+ let type = "database";
35
+ let databaseArray = [];
36
+
37
+ try {
38
+ const client = await dbClient(data);
39
+ if (!client || client.status === false) return resolve(data);
40
+
41
+ if (method == "read") {
42
+ const db = client.db().admin();
43
+
44
+ // List all the available databases
45
+ try {
46
+ const dbs = await db.listDatabases();
47
+
48
+ // Emit ingress telemetry with raw list databases payload
49
+ process.emit("usage", {
50
+ type: "ingress",
51
+ data: dbs,
52
+ organization_id: data.organization_id
53
+ });
54
+
55
+ for (let database of dbs.databases) {
56
+ if (data.$filter && data.$filter.query) {
57
+ let isFilter = queryData(
58
+ database,
59
+ data.$filter.query
60
+ );
61
+ if (isFilter)
62
+ databaseArray.push({
63
+ database,
64
+ storage: data.storageName
65
+ });
66
+ } else
67
+ databaseArray.push({
68
+ database,
69
+ storage: data.storageName
70
+ });
71
+ }
72
+
73
+ resolve(createData(data, databaseArray, type));
74
+ } catch (error) {
75
+ errorHandler(data, error);
76
+ resolve(data);
77
+ }
78
+ }
79
+
80
+ if (method == "delete") {
81
+ const db = client.db(data.database);
82
+ try {
83
+ const result = await db.dropDatabase();
84
+
85
+ // Emit ingress telemetry with raw deletion success payload
86
+ process.emit("usage", {
87
+ type: "ingress",
88
+ data: result,
89
+ organization_id: data.organization_id
90
+ });
91
+
92
+ resolve(result);
93
+ } catch(error) {
94
+ errorHandler(data, error);
95
+ resolve(data);
96
+ }
97
+ }
98
+ } catch (error) {
99
+ errorHandler(data, error);
100
+ console.log(method, "error", error);
101
+ resolve(data);
102
+ }
103
+ },
104
+ (error) => {
105
+ errorHandler(data, error);
106
+ }
107
+ );
108
+ }
109
+
110
+ export default database;