@pi-r/atlas 0.6.0 → 0.6.2
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/README.md +2 -0
- package/client/index.js +27 -30
- package/package.json +6 -6
package/README.md
CHANGED
package/client/index.js
CHANGED
|
@@ -18,7 +18,7 @@ function sanitizeCredentials(credential) {
|
|
|
18
18
|
}
|
|
19
19
|
function validateDatabase(credential, data) {
|
|
20
20
|
const auth = credential.auth;
|
|
21
|
-
return !!(data.table && (auth?.username || credential.username ||
|
|
21
|
+
return !!(data.table && Module.isURL(data.uri) && (auth?.username || credential.username || (0, util_1.hasBasicAuth)(data.uri)));
|
|
22
22
|
}
|
|
23
23
|
exports.validateDatabase = validateDatabase;
|
|
24
24
|
function createDatabaseClient(credential, data) {
|
|
@@ -58,28 +58,45 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
58
58
|
query = getObjectId(id);
|
|
59
59
|
limit = 1;
|
|
60
60
|
}
|
|
61
|
-
const renewCache = ignoreCache === 0;
|
|
62
61
|
const options = item.options ? length === 1 ? Object.assign(item.options, credential) : { ...item.options, ...credential } : length === 1 ? credential : { ...credential };
|
|
63
|
-
let rows, queryString = caching && ignoreCache !== true
|
|
62
|
+
let rows, queryString = caching && ignoreCache !== true ? (name || '') + '_' + table + '_' + limit + Module.asString(options, true) + (sort ? Module.asString(sort, true) : '') + (client ? Module.asString(client, true) : '') : '', auth, pipeline;
|
|
64
63
|
if (queryString) {
|
|
65
64
|
sanitizeCredentials(options);
|
|
66
65
|
auth = { uri: item.uri, options };
|
|
67
66
|
}
|
|
67
|
+
const getInstance = (target) => target.db(name, client?.db);
|
|
68
|
+
const getCollection = (target) => getInstance(target).collection(table, client?.collection);
|
|
69
|
+
const runCommand = async (target, document) => getInstance(target).command(document, client?.command);
|
|
68
70
|
const getCache = () => {
|
|
69
71
|
if (ignoreCache !== 1) {
|
|
70
|
-
cacheValue.renewCache =
|
|
72
|
+
cacheValue.renewCache = ignoreCache === 0;
|
|
71
73
|
return this.getQueryResult(service, auth, queryString, cacheValue);
|
|
72
74
|
}
|
|
73
75
|
};
|
|
74
|
-
|
|
75
|
-
const getCollection = (target) => getInstance(target).collection(table, client?.collection);
|
|
76
|
-
const runCommand = async (target, document) => getInstance(target).command(document, client?.command);
|
|
76
|
+
cacheValue.exclusiveOf = Array.isArray(ignoreCache) ? ignoreCache : undefined;
|
|
77
77
|
const instance = await createClient(item, options);
|
|
78
78
|
const command = item.command;
|
|
79
79
|
if (command) {
|
|
80
80
|
await Promise.all((!Array.isArray(command) ? [command] : command).map(async (cmd) => runCommand(instance, cmd)))
|
|
81
81
|
.then(items => items.forEach(output => this.addLog(this.statusType.INFO, output, "atlas" /* STRINGS.MODULE_NAME */, 'runCommand')));
|
|
82
82
|
}
|
|
83
|
+
const collection = getCollection(instance);
|
|
84
|
+
if (update && coercing) {
|
|
85
|
+
(0, types_1.coerceObject)(update);
|
|
86
|
+
}
|
|
87
|
+
if (Array.isArray(update)) {
|
|
88
|
+
const insertOptions = item.execute?.insert;
|
|
89
|
+
if (insertOptions && coercing) {
|
|
90
|
+
(0, types_1.coerceObject)(insertOptions);
|
|
91
|
+
}
|
|
92
|
+
if (update.length === 1) {
|
|
93
|
+
await collection.insertOne(update[0], insertOptions);
|
|
94
|
+
}
|
|
95
|
+
else if (update.length > 1) {
|
|
96
|
+
await collection.insertMany(update, insertOptions);
|
|
97
|
+
}
|
|
98
|
+
update = undefined;
|
|
99
|
+
}
|
|
83
100
|
if (aggregate) {
|
|
84
101
|
pipeline = Array.isArray(aggregate) ? aggregate : aggregate.pipeline;
|
|
85
102
|
}
|
|
@@ -91,7 +108,6 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
91
108
|
continue;
|
|
92
109
|
}
|
|
93
110
|
}
|
|
94
|
-
const collection = getCollection(instance);
|
|
95
111
|
if (pipeline) {
|
|
96
112
|
const aggregateOptions = (0, types_1.isPlainObject)(aggregate) ? aggregate.options : undefined;
|
|
97
113
|
if (coercing) {
|
|
@@ -110,33 +126,14 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
110
126
|
rows = await items.toArray();
|
|
111
127
|
}
|
|
112
128
|
else {
|
|
113
|
-
const updateType = item.updateType;
|
|
114
129
|
const [filter, operation] = (0, mongodb_1.getFilterValue)(query, coercing);
|
|
115
|
-
|
|
116
|
-
(0, types_1.coerceObject)(update);
|
|
117
|
-
}
|
|
118
|
-
if ((0, types_1.isArray)(update)) {
|
|
119
|
-
const insertOptions = item.execute?.insert;
|
|
120
|
-
if (insertOptions && coercing) {
|
|
121
|
-
(0, types_1.coerceObject)(insertOptions);
|
|
122
|
-
}
|
|
123
|
-
if (update.length === 1) {
|
|
124
|
-
await collection.insertOne(update[0], insertOptions);
|
|
125
|
-
}
|
|
126
|
-
else {
|
|
127
|
-
await collection.insertMany(update, insertOptions);
|
|
128
|
-
}
|
|
129
|
-
update = undefined;
|
|
130
|
-
}
|
|
131
|
-
else if (updateType === 1) {
|
|
132
|
-
update = undefined;
|
|
133
|
-
}
|
|
130
|
+
const updateType = item.updateType;
|
|
134
131
|
if (limit === 1) {
|
|
135
132
|
const document = update ? await collection[updateType === 2 ? 'findOneAndReplace' : updateType === 3 ? 'findOneAndDelete' : 'findOneAndUpdate'](filter, update, operation) : await collection.findOne(filter, operation);
|
|
136
133
|
rows = document ? [document] : [];
|
|
137
134
|
}
|
|
138
135
|
else {
|
|
139
|
-
if (update) {
|
|
136
|
+
if (update && updateType === 1) {
|
|
140
137
|
const updateOptions = item.execute?.update;
|
|
141
138
|
if (updateOptions && coercing) {
|
|
142
139
|
(0, types_1.coerceObject)(updateOptions);
|
|
@@ -159,7 +156,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
159
156
|
result[i] = rows;
|
|
160
157
|
continue;
|
|
161
158
|
}
|
|
162
|
-
const items =
|
|
159
|
+
const items = collection.find();
|
|
163
160
|
if (sort) {
|
|
164
161
|
items.sort(...(0, mongodb_1.getSortValue)(sort, coercing));
|
|
165
162
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/atlas",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
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.8.
|
|
24
|
-
"@e-mc/db": "^0.8.
|
|
25
|
-
"@e-mc/module": "^0.8.
|
|
26
|
-
"@e-mc/types": "^0.8.
|
|
27
|
-
"@pi-r/mongodb": "^0.6.
|
|
23
|
+
"@e-mc/cloud": "^0.8.2",
|
|
24
|
+
"@e-mc/db": "^0.8.2",
|
|
25
|
+
"@e-mc/module": "^0.8.2",
|
|
26
|
+
"@e-mc/types": "^0.8.2",
|
|
27
|
+
"@pi-r/mongodb": "^0.6.2",
|
|
28
28
|
"mongodb": "^6.3.0"
|
|
29
29
|
}
|
|
30
30
|
}
|