@pi-r/atlas 0.11.3 → 0.12.1
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/client/index.js +60 -47
- package/package.json +7 -10
package/client/index.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const Cloud = require("@e-mc/cloud");
|
|
9
|
-
const types_1 = require("@e-mc/types");
|
|
10
|
-
const util_1 = require("@e-mc/cloud/util");
|
|
11
|
-
const mongodb_1 = require("@pi-r/mongodb");
|
|
2
|
+
|
|
3
|
+
const mongodb = require('mongodb');
|
|
4
|
+
const Cloud = require('@e-mc/cloud');
|
|
5
|
+
const { coerceObject, hashKey, isPlainObject } = require('@e-mc/types');
|
|
6
|
+
const { formatError, hasBasicAuth } = require('@e-mc/cloud/util');
|
|
7
|
+
const { getFilterValue, getSortValue, initCollection } = require('@pi-r/mongodb');
|
|
12
8
|
function sanitizeCredentials(credential) {
|
|
13
9
|
const { username, password } = credential;
|
|
14
10
|
if (username) {
|
|
@@ -21,7 +17,7 @@ function sanitizeCredentials(credential) {
|
|
|
21
17
|
}
|
|
22
18
|
function validateDatabase(credential, data) {
|
|
23
19
|
if (data.table && /^mongodb(?:\+srv)?:\/\//i.test(data.uri)) {
|
|
24
|
-
if (credential.auth?.username || credential.username ||
|
|
20
|
+
if (credential.auth?.username || credential.username || hasBasicAuth(data.uri)) {
|
|
25
21
|
return true;
|
|
26
22
|
}
|
|
27
23
|
const setAuthSource = () => credential.authSource ||= '$external';
|
|
@@ -34,11 +30,8 @@ function validateDatabase(credential, data) {
|
|
|
34
30
|
}
|
|
35
31
|
break;
|
|
36
32
|
case 'MONGODB-AWS':
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return true;
|
|
40
|
-
}
|
|
41
|
-
break;
|
|
33
|
+
setAuthSource();
|
|
34
|
+
return true;
|
|
42
35
|
case 'PLAIN':
|
|
43
36
|
credential.maxPoolSize ??= 1;
|
|
44
37
|
setAuthSource();
|
|
@@ -62,7 +55,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
62
55
|
const coercing = this.hasCoerce("atlas", 'options');
|
|
63
56
|
const cacheValue = { value: this.valueOfKey(credential, 'cache'), sessionKey };
|
|
64
57
|
const createClient = async (item, options) => {
|
|
65
|
-
const key = length > 1 ?
|
|
58
|
+
const key = length > 1 ? hashKey(item.uri + Cloud.asString(options, true)) : '_';
|
|
66
59
|
let client = clients[key];
|
|
67
60
|
if (client) {
|
|
68
61
|
return client;
|
|
@@ -80,10 +73,10 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
80
73
|
};
|
|
81
74
|
for (let i = 0; i < length; ++i) {
|
|
82
75
|
const item = batch[i];
|
|
83
|
-
const { service, name, table, id, aggregate, distinct, sort,
|
|
76
|
+
const { service, name, table, id, aggregate, distinct, withFields, sort, timeoutMS = 0, ignoreCache } = item;
|
|
84
77
|
if (!table) {
|
|
85
78
|
closeClient();
|
|
86
|
-
throw
|
|
79
|
+
throw formatError(item, "Missing database table");
|
|
87
80
|
}
|
|
88
81
|
let { query, update, limit = 0 } = item;
|
|
89
82
|
if (id && (!query || limit === 1)) {
|
|
@@ -91,21 +84,21 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
91
84
|
limit = 1;
|
|
92
85
|
}
|
|
93
86
|
const options = item.options ? length === 1 ? Object.assign(item.options, credential) : { ...item.options, ...credential } : length === 1 ? credential : { ...credential };
|
|
94
|
-
let rows, queryString = caching && ignoreCache !== true ? (name || '') + '_' + table + '_' + (limit > 0 ? limit.toString() : '0') + Cloud.asString(options, true) + (sort ? Cloud.asString(sort, true) : '') + (client ? Cloud.asString(client, true) : '') : '', auth, pipeline;
|
|
87
|
+
let rows, queryString = caching && ignoreCache !== true ? (name || '') + '_' + table + '_' + (limit > 0 ? limit.toString() : '0') + Cloud.asString(options, true) + (withFields ? Cloud.asString(withFields, true) : '') + (sort ? Cloud.asString(sort, true) : '') + (item.client ? Cloud.asString(item.client, true) : '') : '', auth, pipeline;
|
|
95
88
|
if (queryString) {
|
|
96
89
|
sanitizeCredentials(options);
|
|
97
90
|
auth = { uri: item.uri, options };
|
|
98
91
|
}
|
|
99
92
|
cacheValue.exclusiveOf = Array.isArray(ignoreCache) ? ignoreCache : undefined;
|
|
100
93
|
const instance = await createClient(item, options);
|
|
101
|
-
const collection = await
|
|
94
|
+
const collection = await initCollection.call(this, instance, item);
|
|
102
95
|
if (update && coercing) {
|
|
103
|
-
|
|
96
|
+
coerceObject(update);
|
|
104
97
|
}
|
|
105
98
|
if (Array.isArray(update)) {
|
|
106
99
|
const insertOptions = item.execute?.insert;
|
|
107
100
|
if (insertOptions && coercing) {
|
|
108
|
-
|
|
101
|
+
coerceObject(insertOptions);
|
|
109
102
|
}
|
|
110
103
|
if (update.length === 1) {
|
|
111
104
|
await collection.insertOne(update[0], insertOptions);
|
|
@@ -127,28 +120,34 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
127
120
|
}
|
|
128
121
|
}
|
|
129
122
|
if (pipeline) {
|
|
130
|
-
|
|
123
|
+
let aggOptions = isPlainObject(aggregate) ? aggregate.options : undefined;
|
|
131
124
|
if (coercing) {
|
|
132
125
|
for (const doc of pipeline) {
|
|
133
|
-
|
|
126
|
+
coerceObject(doc);
|
|
134
127
|
}
|
|
135
128
|
if (aggOptions) {
|
|
136
|
-
|
|
129
|
+
coerceObject(aggOptions);
|
|
137
130
|
}
|
|
138
131
|
}
|
|
139
|
-
|
|
132
|
+
if (timeoutMS > 0) {
|
|
133
|
+
(aggOptions ||= {}).timeoutMS = timeoutMS;
|
|
134
|
+
}
|
|
135
|
+
const cursor = collection.aggregate(pipeline, aggOptions);
|
|
136
|
+
if (withFields) {
|
|
137
|
+
cursor.project(withFields);
|
|
138
|
+
}
|
|
140
139
|
if (sort) {
|
|
141
|
-
|
|
140
|
+
cursor.sort(getSortValue(sort, coercing)[0]);
|
|
142
141
|
}
|
|
143
142
|
if (limit > 0) {
|
|
144
|
-
|
|
143
|
+
cursor.limit(limit);
|
|
145
144
|
}
|
|
146
|
-
rows = await
|
|
145
|
+
rows = await cursor.toArray();
|
|
147
146
|
}
|
|
148
147
|
else {
|
|
149
|
-
const [filter, operation = {}] =
|
|
148
|
+
const [filter, operation = {}] = getFilterValue(query, coercing);
|
|
150
149
|
if (sort) {
|
|
151
|
-
operation.sort =
|
|
150
|
+
operation.sort = getSortValue(sort, coercing)[0];
|
|
152
151
|
}
|
|
153
152
|
if (limit === 1) {
|
|
154
153
|
const document = update ? await collection[item.updateType === 2 ? 'findOneAndReplace' : item.updateType === 3 ? 'findOneAndDelete' : 'findOneAndUpdate'](filter, update, operation) : await collection.findOne(filter, operation);
|
|
@@ -156,20 +155,22 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
156
155
|
}
|
|
157
156
|
else {
|
|
158
157
|
if (update && item.updateType === 1) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
158
|
+
await collection.updateMany(filter, update, item.execute?.update);
|
|
159
|
+
}
|
|
160
|
+
if (timeoutMS > 0) {
|
|
161
|
+
operation.timeoutMS = timeoutMS;
|
|
162
|
+
}
|
|
163
|
+
const cursor = collection.find(filter, operation);
|
|
164
|
+
if (withFields) {
|
|
165
|
+
cursor.project(withFields);
|
|
164
166
|
}
|
|
165
|
-
const items = collection.find(filter, operation);
|
|
166
167
|
if (sort) {
|
|
167
|
-
|
|
168
|
+
cursor.sort(...getSortValue(sort, coercing));
|
|
168
169
|
}
|
|
169
170
|
if (limit > 1) {
|
|
170
|
-
|
|
171
|
+
cursor.limit(limit);
|
|
171
172
|
}
|
|
172
|
-
rows = await
|
|
173
|
+
rows = await cursor.toArray();
|
|
173
174
|
}
|
|
174
175
|
}
|
|
175
176
|
}
|
|
@@ -181,22 +182,28 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
181
182
|
continue;
|
|
182
183
|
}
|
|
183
184
|
}
|
|
184
|
-
|
|
185
|
-
|
|
185
|
+
let [a, b, c] = Array.isArray(distinct) ? distinct : [distinct];
|
|
186
|
+
if (timeoutMS > 0) {
|
|
187
|
+
(c ||= {}).timeoutMS = timeoutMS;
|
|
188
|
+
}
|
|
189
|
+
rows = await (c ? b ? collection.distinct(a, b, c) : collection.distinct(a, c) : b ? collection.distinct(a, b) : collection.distinct(a));
|
|
186
190
|
}
|
|
187
191
|
else if (!aggregate) {
|
|
188
192
|
if (queryString && (rows = this.getCacheResult(service, auth, queryString, cacheValue, ignoreCache))) {
|
|
189
193
|
result[i] = rows;
|
|
190
194
|
continue;
|
|
191
195
|
}
|
|
192
|
-
const
|
|
196
|
+
const cursor = timeoutMS > 0 ? collection.find({}, { timeoutMS }) : collection.find();
|
|
197
|
+
if (withFields) {
|
|
198
|
+
cursor.project(withFields);
|
|
199
|
+
}
|
|
193
200
|
if (sort) {
|
|
194
|
-
|
|
201
|
+
cursor.sort(...getSortValue(sort, coercing));
|
|
195
202
|
}
|
|
196
203
|
if (limit > 0) {
|
|
197
|
-
|
|
204
|
+
cursor.limit(limit);
|
|
198
205
|
}
|
|
199
|
-
rows = await
|
|
206
|
+
rows = await cursor.toArray();
|
|
200
207
|
}
|
|
201
208
|
else {
|
|
202
209
|
queryString = '';
|
|
@@ -209,3 +216,9 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
209
216
|
function getObjectId(value) {
|
|
210
217
|
return { '_id': new mongodb.ObjectId(value) };
|
|
211
218
|
}
|
|
219
|
+
|
|
220
|
+
exports.createDatabaseClient = createDatabaseClient;
|
|
221
|
+
exports.executeBatchQuery = executeBatchQuery;
|
|
222
|
+
exports.executeQuery = executeQuery;
|
|
223
|
+
exports.getObjectId = getObjectId;
|
|
224
|
+
exports.validateDatabase = validateDatabase;
|
package/package.json
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/atlas",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.1",
|
|
4
4
|
"description": "MongoDB Atlas cloud functions for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
|
-
"publishConfig": {
|
|
7
|
-
"access": "public"
|
|
8
|
-
},
|
|
9
6
|
"repository": {
|
|
10
7
|
"type": "git",
|
|
11
8
|
"url": "git+https://github.com/anpham6/pi-r.git",
|
|
@@ -19,11 +16,11 @@
|
|
|
19
16
|
"license": "MIT",
|
|
20
17
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
21
18
|
"dependencies": {
|
|
22
|
-
"@e-mc/cloud": "^0.
|
|
23
|
-
"@e-mc/db": "^0.
|
|
24
|
-
"@e-mc/module": "^0.
|
|
25
|
-
"@e-mc/types": "^0.
|
|
26
|
-
"@pi-r/mongodb": "^0.
|
|
27
|
-
"mongodb": "^
|
|
19
|
+
"@e-mc/cloud": "^0.14.1",
|
|
20
|
+
"@e-mc/db": "^0.14.1",
|
|
21
|
+
"@e-mc/module": "^0.14.1",
|
|
22
|
+
"@e-mc/types": "^0.14.1",
|
|
23
|
+
"@pi-r/mongodb": "^0.12.1",
|
|
24
|
+
"mongodb": "^7.2.0"
|
|
28
25
|
}
|
|
29
26
|
}
|