@pi-r/atlas 0.7.2 → 0.8.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/client/index.js +21 -23
- package/package.json +7 -7
package/client/index.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
exports.
|
|
2
|
+
exports.validateDatabase = validateDatabase;
|
|
3
|
+
exports.createDatabaseClient = createDatabaseClient;
|
|
4
|
+
exports.executeQuery = executeQuery;
|
|
5
|
+
exports.executeBatchQuery = executeBatchQuery;
|
|
6
|
+
exports.getObjectId = getObjectId;
|
|
3
7
|
const mongodb = require("mongodb");
|
|
4
|
-
const
|
|
8
|
+
const Cloud = require("@e-mc/cloud");
|
|
5
9
|
const util_1 = require("@e-mc/cloud/util");
|
|
10
|
+
const types_1 = require("@e-mc/types");
|
|
6
11
|
const mongodb_1 = require("@pi-r/mongodb");
|
|
7
|
-
const Cloud = require("@e-mc/cloud");
|
|
8
12
|
function sanitizeCredentials(credential) {
|
|
9
13
|
const { username, password } = credential;
|
|
10
14
|
if (username) {
|
|
@@ -19,16 +23,13 @@ function validateDatabase(credential, data) {
|
|
|
19
23
|
const auth = credential.auth;
|
|
20
24
|
return !!(data.table && Cloud.isURL(data.uri) && (auth?.username || credential.username || (0, util_1.hasBasicAuth)(data.uri)));
|
|
21
25
|
}
|
|
22
|
-
exports.validateDatabase = validateDatabase;
|
|
23
26
|
function createDatabaseClient(credential, data) {
|
|
24
27
|
sanitizeCredentials(credential);
|
|
25
28
|
return new mongodb.MongoClient(data.uri, credential);
|
|
26
29
|
}
|
|
27
|
-
exports.createDatabaseClient = createDatabaseClient;
|
|
28
30
|
async function executeQuery(credential, data, sessionKey) {
|
|
29
31
|
return (await executeBatchQuery.call(this, credential, [data], sessionKey))[0] || [];
|
|
30
32
|
}
|
|
31
|
-
exports.executeQuery = executeQuery;
|
|
32
33
|
async function executeBatchQuery(credential, batch, sessionKey) {
|
|
33
34
|
const length = batch.length;
|
|
34
35
|
const result = new Array(length);
|
|
@@ -37,11 +38,11 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
37
38
|
const coercing = this.hasCoerce("atlas", 'options');
|
|
38
39
|
const cacheValue = { value: this.valueOfKey(credential, 'cache'), sessionKey };
|
|
39
40
|
const createClient = async (item, options) => {
|
|
40
|
-
return clients[item.uri + Cloud.asString(options, true)] ||= await createDatabaseClient.call(this, options, item).connect();
|
|
41
|
+
return clients[(0, types_1.hashKey)(item.uri + Cloud.asString(options, true))] ||= await createDatabaseClient.call(this, options, item).connect();
|
|
41
42
|
};
|
|
42
43
|
const closeClient = () => {
|
|
43
44
|
for (const key in clients) {
|
|
44
|
-
clients[key].close();
|
|
45
|
+
void clients[key].close();
|
|
45
46
|
}
|
|
46
47
|
};
|
|
47
48
|
for (let i = 0; i < length; ++i) {
|
|
@@ -65,18 +66,15 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
65
66
|
const getInstance = (target) => target.db(name, client?.db);
|
|
66
67
|
const getCollection = (target) => getInstance(target).collection(table, client?.collection);
|
|
67
68
|
const runCommand = async (target, document) => getInstance(target).command(document, client?.command);
|
|
68
|
-
const getCache = () => {
|
|
69
|
-
if (ignoreCache !== 1) {
|
|
70
|
-
cacheValue.renewCache = ignoreCache === 0;
|
|
71
|
-
return this.getQueryResult(service, auth, queryString, cacheValue);
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
69
|
cacheValue.exclusiveOf = Array.isArray(ignoreCache) ? ignoreCache : undefined;
|
|
75
70
|
const instance = await createClient(item, options);
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
71
|
+
if (item.command) {
|
|
72
|
+
await Promise.all((!Array.isArray(item.command) ? [item.command] : item.command).map(async (cmd) => runCommand(instance, cmd)))
|
|
73
|
+
.then(items => {
|
|
74
|
+
for (const output of items) {
|
|
75
|
+
this.addLog(this.statusType.INFO, output, "atlas", 'runCommand');
|
|
76
|
+
}
|
|
77
|
+
});
|
|
80
78
|
}
|
|
81
79
|
const collection = getCollection(instance);
|
|
82
80
|
if (update && coercing) {
|
|
@@ -101,7 +99,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
101
99
|
if (pipeline || query) {
|
|
102
100
|
if (queryString) {
|
|
103
101
|
queryString += Cloud.asString(aggregate || query, true);
|
|
104
|
-
if ((aggregate || !update) && (rows =
|
|
102
|
+
if ((aggregate || !update) && (rows = this.getCacheResult(service, auth, queryString, cacheValue, ignoreCache))) {
|
|
105
103
|
result[i] = rows;
|
|
106
104
|
continue;
|
|
107
105
|
}
|
|
@@ -109,7 +107,9 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
109
107
|
if (pipeline) {
|
|
110
108
|
const aggregateOptions = (0, types_1.isPlainObject)(aggregate) ? aggregate.options : undefined;
|
|
111
109
|
if (coercing) {
|
|
112
|
-
|
|
110
|
+
for (const doc of pipeline) {
|
|
111
|
+
(0, types_1.coerceObject)(doc);
|
|
112
|
+
}
|
|
113
113
|
if (aggregateOptions) {
|
|
114
114
|
(0, types_1.coerceObject)(aggregateOptions);
|
|
115
115
|
}
|
|
@@ -150,7 +150,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
152
|
else if (!aggregate) {
|
|
153
|
-
if (queryString && (rows =
|
|
153
|
+
if (queryString && (rows = this.getCacheResult(service, auth, queryString, cacheValue, ignoreCache))) {
|
|
154
154
|
result[i] = rows;
|
|
155
155
|
continue;
|
|
156
156
|
}
|
|
@@ -171,8 +171,6 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
171
171
|
closeClient();
|
|
172
172
|
return result;
|
|
173
173
|
}
|
|
174
|
-
exports.executeBatchQuery = executeBatchQuery;
|
|
175
174
|
function getObjectId(value) {
|
|
176
175
|
return { '_id': new mongodb.ObjectId(value) };
|
|
177
176
|
}
|
|
178
|
-
exports.getObjectId = getObjectId;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/atlas",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "MongoDB Atlas cloud functions for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"publishConfig": {
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/cloud": "^0.
|
|
24
|
-
"@e-mc/db": "^0.
|
|
25
|
-
"@e-mc/module": "^0.
|
|
26
|
-
"@e-mc/types": "^0.
|
|
27
|
-
"@pi-r/mongodb": "^0.
|
|
28
|
-
"mongodb": "^6.
|
|
23
|
+
"@e-mc/cloud": "^0.10.0",
|
|
24
|
+
"@e-mc/db": "^0.10.0",
|
|
25
|
+
"@e-mc/module": "^0.10.0",
|
|
26
|
+
"@e-mc/types": "^0.10.0",
|
|
27
|
+
"@pi-r/mongodb": "^0.8.0",
|
|
28
|
+
"mongodb": "^6.8.0"
|
|
29
29
|
}
|
|
30
30
|
}
|