@pi-r/atlas 0.11.3 → 0.12.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.
Files changed (2) hide show
  1. package/client/index.js +60 -47
  2. package/package.json +7 -7
package/client/index.js CHANGED
@@ -1,14 +1,10 @@
1
1
  "use strict";
2
- exports.validateDatabase = validateDatabase;
3
- exports.createDatabaseClient = createDatabaseClient;
4
- exports.executeQuery = executeQuery;
5
- exports.executeBatchQuery = executeBatchQuery;
6
- exports.getObjectId = getObjectId;
7
- const mongodb = require("mongodb");
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 || (0, util_1.hasBasicAuth)(data.uri)) {
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
- if (credential.authMechanismProperties) {
38
- setAuthSource();
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 ? (0, types_1.hashKey)(item.uri + Cloud.asString(options, true)) : '_';
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, client, ignoreCache } = item;
76
+ const { service, name, table, id, aggregate, distinct, withFields, sort, timeoutMS = 0, ignoreCache } = item;
84
77
  if (!table) {
85
78
  closeClient();
86
- throw (0, util_1.formatError)(item, "Missing database table");
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 mongodb_1.initCollection.call(this, instance, item);
94
+ const collection = await initCollection.call(this, instance, item);
102
95
  if (update && coercing) {
103
- (0, types_1.coerceObject)(update);
96
+ coerceObject(update);
104
97
  }
105
98
  if (Array.isArray(update)) {
106
99
  const insertOptions = item.execute?.insert;
107
100
  if (insertOptions && coercing) {
108
- (0, types_1.coerceObject)(insertOptions);
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
- const aggOptions = (0, types_1.isPlainObject)(aggregate) ? aggregate.options : undefined;
123
+ let aggOptions = isPlainObject(aggregate) ? aggregate.options : undefined;
131
124
  if (coercing) {
132
125
  for (const doc of pipeline) {
133
- (0, types_1.coerceObject)(doc);
126
+ coerceObject(doc);
134
127
  }
135
128
  if (aggOptions) {
136
- (0, types_1.coerceObject)(aggOptions);
129
+ coerceObject(aggOptions);
137
130
  }
138
131
  }
139
- const items = collection.aggregate(pipeline, aggOptions);
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
- items.sort((0, mongodb_1.getSortValue)(sort, coercing)[0]);
140
+ cursor.sort(getSortValue(sort, coercing)[0]);
142
141
  }
143
142
  if (limit > 0) {
144
- items.limit(limit);
143
+ cursor.limit(limit);
145
144
  }
146
- rows = await items.toArray();
145
+ rows = await cursor.toArray();
147
146
  }
148
147
  else {
149
- const [filter, operation = {}] = (0, mongodb_1.getFilterValue)(query, coercing);
148
+ const [filter, operation = {}] = getFilterValue(query, coercing);
150
149
  if (sort) {
151
- operation.sort = (0, mongodb_1.getSortValue)(sort, coercing)[0];
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
- const updateOptions = item.execute?.update;
160
- if (updateOptions && coercing) {
161
- (0, types_1.coerceObject)(updateOptions);
162
- }
163
- await collection.updateMany(filter, update, updateOptions);
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
- items.sort(...(0, mongodb_1.getSortValue)(sort, coercing));
168
+ cursor.sort(...getSortValue(sort, coercing));
168
169
  }
169
170
  if (limit > 1) {
170
- items.limit(limit);
171
+ cursor.limit(limit);
171
172
  }
172
- rows = await items.toArray();
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
- const [a, b, c] = Array.isArray(distinct) ? distinct : [distinct];
185
- rows = await (b ? c ? collection.distinct(a, b, c) : collection.distinct(a, b) : collection.distinct(a));
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 items = collection.find();
196
+ const cursor = timeoutMS > 0 ? collection.find({}, { timeoutMS }) : collection.find();
197
+ if (withFields) {
198
+ cursor.project(withFields);
199
+ }
193
200
  if (sort) {
194
- items.sort(...(0, mongodb_1.getSortValue)(sort, coercing));
201
+ cursor.sort(...getSortValue(sort, coercing));
195
202
  }
196
203
  if (limit > 0) {
197
- items.limit(limit);
204
+ cursor.limit(limit);
198
205
  }
199
- rows = await items.toArray();
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/atlas",
3
- "version": "0.11.3",
3
+ "version": "0.12.0",
4
4
  "description": "MongoDB Atlas cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "publishConfig": {
@@ -19,11 +19,11 @@
19
19
  "license": "MIT",
20
20
  "homepage": "https://github.com/anpham6/pi-r#readme",
21
21
  "dependencies": {
22
- "@e-mc/cloud": "^0.13.10",
23
- "@e-mc/db": "^0.13.10",
24
- "@e-mc/module": "^0.13.10",
25
- "@e-mc/types": "^0.13.10",
26
- "@pi-r/mongodb": "^0.11.3",
27
- "mongodb": "^6.21.0"
22
+ "@e-mc/cloud": "^0.14.0",
23
+ "@e-mc/db": "^0.14.0",
24
+ "@e-mc/module": "^0.14.0",
25
+ "@e-mc/types": "^0.14.0",
26
+ "@pi-r/mongodb": "^0.12.0",
27
+ "mongodb": "^7.2.0"
28
28
  }
29
29
  }