@pi-r/atlas 0.7.3 → 0.8.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.
Files changed (2) hide show
  1. package/client/index.js +52 -28
  2. package/package.json +6 -6
package/client/index.js CHANGED
@@ -1,10 +1,14 @@
1
1
  "use strict";
2
- exports.getObjectId = exports.executeBatchQuery = exports.executeQuery = exports.createDatabaseClient = exports.validateDatabase = void 0;
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 types_1 = require("@e-mc/types");
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) {
@@ -17,18 +21,41 @@ function sanitizeCredentials(credential) {
17
21
  }
18
22
  function validateDatabase(credential, data) {
19
23
  const auth = credential.auth;
20
- return !!(data.table && Cloud.isURL(data.uri) && (auth?.username || credential.username || (0, util_1.hasBasicAuth)(data.uri)));
24
+ if (data.table && Cloud.isURL(data.uri)) {
25
+ if (auth?.username || credential.username || (0, util_1.hasBasicAuth)(data.uri)) {
26
+ return true;
27
+ }
28
+ const setAuthSource = () => credential.authSource ||= '$external';
29
+ switch (credential.authMechanism) {
30
+ case 'MONGODB-X509':
31
+ if (credential.tlsCertificateKeyFile) {
32
+ credential.tls = true;
33
+ setAuthSource();
34
+ return true;
35
+ }
36
+ break;
37
+ case 'GSSAPI':
38
+ case 'MONGODB-AWS':
39
+ if ((0, types_1.isPlainObject)(credential.authMechanismProperties)) {
40
+ setAuthSource();
41
+ return true;
42
+ }
43
+ break;
44
+ case 'PLAIN':
45
+ credential.maxPoolSize ??= 1;
46
+ setAuthSource();
47
+ return true;
48
+ }
49
+ }
50
+ return false;
21
51
  }
22
- exports.validateDatabase = validateDatabase;
23
52
  function createDatabaseClient(credential, data) {
24
53
  sanitizeCredentials(credential);
25
54
  return new mongodb.MongoClient(data.uri, credential);
26
55
  }
27
- exports.createDatabaseClient = createDatabaseClient;
28
56
  async function executeQuery(credential, data, sessionKey) {
29
57
  return (await executeBatchQuery.call(this, credential, [data], sessionKey))[0] || [];
30
58
  }
31
- exports.executeQuery = executeQuery;
32
59
  async function executeBatchQuery(credential, batch, sessionKey) {
33
60
  const length = batch.length;
34
61
  const result = new Array(length);
@@ -37,11 +64,11 @@ async function executeBatchQuery(credential, batch, sessionKey) {
37
64
  const coercing = this.hasCoerce("atlas", 'options');
38
65
  const cacheValue = { value: this.valueOfKey(credential, 'cache'), sessionKey };
39
66
  const createClient = async (item, options) => {
40
- return clients[item.uri + Cloud.asString(options, true)] ||= await createDatabaseClient.call(this, options, item).connect();
67
+ return clients[length > 1 ? (0, types_1.hashKey)(item.uri + Cloud.asString(options, true)) : '_'] ||= await createDatabaseClient.call(this, options, item).connect();
41
68
  };
42
69
  const closeClient = () => {
43
70
  for (const key in clients) {
44
- clients[key].close();
71
+ void clients[key].close();
45
72
  }
46
73
  };
47
74
  for (let i = 0; i < length; ++i) {
@@ -65,18 +92,15 @@ async function executeBatchQuery(credential, batch, sessionKey) {
65
92
  const getInstance = (target) => target.db(name, client?.db);
66
93
  const getCollection = (target) => getInstance(target).collection(table, client?.collection);
67
94
  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
95
  cacheValue.exclusiveOf = Array.isArray(ignoreCache) ? ignoreCache : undefined;
75
96
  const instance = await createClient(item, options);
76
- const command = item.command;
77
- if (command) {
78
- await Promise.all((!Array.isArray(command) ? [command] : command).map(async (cmd) => runCommand(instance, cmd)))
79
- .then(items => items.forEach(output => this.addLog(this.statusType.INFO, output, "atlas", 'runCommand')));
97
+ if (item.command) {
98
+ await Promise.all((!Array.isArray(item.command) ? [item.command] : item.command).map(async (cmd) => runCommand(instance, cmd)))
99
+ .then(items => {
100
+ for (const output of items) {
101
+ this.addLog(this.statusType.INFO, output, "atlas", 'runCommand');
102
+ }
103
+ });
80
104
  }
81
105
  const collection = getCollection(instance);
82
106
  if (update && coercing) {
@@ -101,20 +125,22 @@ async function executeBatchQuery(credential, batch, sessionKey) {
101
125
  if (pipeline || query) {
102
126
  if (queryString) {
103
127
  queryString += Cloud.asString(aggregate || query, true);
104
- if ((aggregate || !update) && (rows = getCache())) {
128
+ if ((aggregate || !update) && (rows = this.getCacheResult(service, auth, queryString, cacheValue, ignoreCache))) {
105
129
  result[i] = rows;
106
130
  continue;
107
131
  }
108
132
  }
109
133
  if (pipeline) {
110
- const aggregateOptions = (0, types_1.isPlainObject)(aggregate) ? aggregate.options : undefined;
134
+ const aggOptions = (0, types_1.isPlainObject)(aggregate) ? aggregate.options : undefined;
111
135
  if (coercing) {
112
- pipeline.forEach(doc => (0, types_1.coerceObject)(doc));
113
- if (aggregateOptions) {
114
- (0, types_1.coerceObject)(aggregateOptions);
136
+ for (const doc of pipeline) {
137
+ (0, types_1.coerceObject)(doc);
138
+ }
139
+ if (aggOptions) {
140
+ (0, types_1.coerceObject)(aggOptions);
115
141
  }
116
142
  }
117
- const items = collection.aggregate(pipeline, aggregateOptions);
143
+ const items = collection.aggregate(pipeline, aggOptions);
118
144
  if (sort) {
119
145
  items.sort((0, mongodb_1.getSortValue)(sort, coercing)[0]);
120
146
  }
@@ -150,7 +176,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
150
176
  }
151
177
  }
152
178
  else if (!aggregate) {
153
- if (queryString && (rows = getCache())) {
179
+ if (queryString && (rows = this.getCacheResult(service, auth, queryString, cacheValue, ignoreCache))) {
154
180
  result[i] = rows;
155
181
  continue;
156
182
  }
@@ -171,8 +197,6 @@ async function executeBatchQuery(credential, batch, sessionKey) {
171
197
  closeClient();
172
198
  return result;
173
199
  }
174
- exports.executeBatchQuery = executeBatchQuery;
175
200
  function getObjectId(value) {
176
201
  return { '_id': new mongodb.ObjectId(value) };
177
202
  }
178
- exports.getObjectId = getObjectId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/atlas",
3
- "version": "0.7.3",
3
+ "version": "0.8.1",
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.9.6",
24
- "@e-mc/db": "^0.9.6",
25
- "@e-mc/module": "^0.9.6",
26
- "@e-mc/types": "^0.9.6",
27
- "@pi-r/mongodb": "^0.7.3",
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
28
  "mongodb": "^6.8.0"
29
29
  }
30
30
  }