@pi-r/atlas 0.3.1 → 0.6.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 +24 -34
  2. package/package.json +7 -7
package/client/index.js CHANGED
@@ -6,32 +6,15 @@ const types_1 = require("@e-mc/types");
6
6
  const util_1 = require("@e-mc/cloud/util");
7
7
  const mongodb_1 = require("@pi-r/mongodb");
8
8
  const Module = require("@e-mc/module");
9
- const MONGODB_V3 = Module.checkSemVer('mongodb', 1, 4);
10
- function sanitizeCredentials(item, credential) {
9
+ function sanitizeCredentials(credential) {
11
10
  const { username, password } = credential;
12
11
  if (username) {
13
- const auth = password ? { username, password } : { username };
14
- if (MONGODB_V3) {
15
- credential.auth = password || !credential.auth?.password ? auth : credential.auth;
16
- }
17
- else {
18
- credential.auth || (credential.auth = auth);
19
- }
12
+ credential.auth || (credential.auth = password ? { username, password } : { username });
20
13
  delete credential.username;
21
14
  }
22
15
  if (password) {
23
16
  delete credential.password;
24
17
  }
25
- if (MONGODB_V3 && credential.auth) {
26
- if (!(0, util_1.hasBasicAuth)(item.uri)) {
27
- const auth = (0, util_1.getBasicAuth)(credential.auth);
28
- if (auth) {
29
- const [protocol, hostname] = item.uri.split('://');
30
- item.uri = protocol + '://' + auth + hostname;
31
- }
32
- }
33
- delete credential.auth;
34
- }
35
18
  }
36
19
  function validateDatabase(credential, data) {
37
20
  const auth = credential.auth;
@@ -39,7 +22,7 @@ function validateDatabase(credential, data) {
39
22
  }
40
23
  exports.validateDatabase = validateDatabase;
41
24
  function createDatabaseClient(credential, data) {
42
- sanitizeCredentials(data, credential);
25
+ sanitizeCredentials(credential);
43
26
  return new mongodb.MongoClient(data.uri, credential);
44
27
  }
45
28
  exports.createDatabaseClient = createDatabaseClient;
@@ -52,7 +35,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
52
35
  const result = new Array(length);
53
36
  const clients = {};
54
37
  const caching = length > 0 && this.hasCache(batch[0].service, sessionKey);
55
- const coercing = this.hasCoerce('atlas', 'options');
38
+ const coercing = this.hasCoerce("atlas" /* STRINGS.MODULE_NAME */, 'options');
56
39
  const cacheValue = { value: this.valueOfKey(credential, 'cache'), sessionKey };
57
40
  const createClient = async (item, options) => {
58
41
  var _a;
@@ -76,20 +59,27 @@ async function executeBatchQuery(credential, batch, sessionKey) {
76
59
  limit = 1;
77
60
  }
78
61
  const renewCache = ignoreCache === 0;
79
- const options = item.options ? length === 1 && !MONGODB_V3 ? Object.assign(item.options, credential) : { ...item.options, ...credential } : length === 1 && !MONGODB_V3 ? credential : { ...credential };
62
+ const options = item.options ? length === 1 ? Object.assign(item.options, credential) : { ...item.options, ...credential } : length === 1 ? credential : { ...credential };
80
63
  let rows, queryString = caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache ? (name || '') + '_' + table + '_' + limit + Module.asString(options, true) + (sort ? Module.asString(sort, true) : '') + (client ? Module.asString(client, true) : '') : '', auth, pipeline;
81
64
  if (queryString) {
82
- sanitizeCredentials(item, options);
65
+ sanitizeCredentials(options);
83
66
  auth = { uri: item.uri, options };
84
67
  }
85
68
  const getCache = () => {
86
- if (ignoreCache === 1) {
87
- return;
69
+ if (ignoreCache !== 1) {
70
+ cacheValue.renewCache = renewCache;
71
+ return this.getQueryResult(service, auth, queryString, cacheValue);
88
72
  }
89
- cacheValue.renewCache = renewCache;
90
- return this.getQueryResult(service, auth, queryString, cacheValue);
91
73
  };
92
- const getCollection = (mongoClient) => mongoClient.db(name, client?.db).collection(table, client?.collection);
74
+ const getInstance = (target) => target.db(name, client?.db);
75
+ const getCollection = (target) => getInstance(target).collection(table, client?.collection);
76
+ const runCommand = async (target, document) => getInstance(target).command(document, client?.command);
77
+ const instance = await createClient(item, options);
78
+ const command = item.command;
79
+ if (command) {
80
+ await Promise.all((!Array.isArray(command) ? [command] : command).map(async (cmd) => runCommand(instance, cmd)))
81
+ .then(items => items.forEach(output => this.addLog(this.statusType.INFO, output, "atlas" /* STRINGS.MODULE_NAME */, 'runCommand')));
82
+ }
93
83
  if (aggregate) {
94
84
  pipeline = Array.isArray(aggregate) ? aggregate : aggregate.pipeline;
95
85
  }
@@ -101,9 +91,9 @@ async function executeBatchQuery(credential, batch, sessionKey) {
101
91
  continue;
102
92
  }
103
93
  }
104
- const collection = getCollection(await createClient(item, options));
94
+ const collection = getCollection(instance);
105
95
  if (pipeline) {
106
- const aggregateOptions = aggregate?.options;
96
+ const aggregateOptions = (0, types_1.isPlainObject)(aggregate) ? aggregate.options : undefined;
107
97
  if (coercing) {
108
98
  pipeline.forEach(doc => (0, types_1.coerceObject)(doc));
109
99
  if (aggregateOptions) {
@@ -121,7 +111,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
121
111
  }
122
112
  else {
123
113
  const updateType = item.updateType;
124
- const [filter, command] = (0, mongodb_1.getFilterValue)(query, coercing);
114
+ const [filter, operation] = (0, mongodb_1.getFilterValue)(query, coercing);
125
115
  if (update && coercing) {
126
116
  (0, types_1.coerceObject)(update);
127
117
  }
@@ -142,7 +132,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
142
132
  update = undefined;
143
133
  }
144
134
  if (limit === 1) {
145
- const document = update ? await collection[updateType === 2 ? 'findOneAndReplace' : updateType === 3 ? 'findOneAndDelete' : 'findOneAndUpdate'](filter, update, command) : await collection.findOne(filter, command);
135
+ const document = update ? await collection[updateType === 2 ? 'findOneAndReplace' : updateType === 3 ? 'findOneAndDelete' : 'findOneAndUpdate'](filter, update, operation) : await collection.findOne(filter, operation);
146
136
  rows = document ? [document] : [];
147
137
  }
148
138
  else {
@@ -153,7 +143,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
153
143
  }
154
144
  await collection.updateMany(filter, update, updateOptions);
155
145
  }
156
- const items = collection.find(filter, command);
146
+ const items = collection.find(filter, operation);
157
147
  if (sort) {
158
148
  items.sort(...(0, mongodb_1.getSortValue)(sort, coercing));
159
149
  }
@@ -169,7 +159,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
169
159
  result[i] = rows;
170
160
  continue;
171
161
  }
172
- const items = getCollection(await createClient(item, options)).find();
162
+ const items = getCollection(instance).find();
173
163
  if (sort) {
174
164
  items.sort(...(0, mongodb_1.getSortValue)(sort, coercing));
175
165
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/atlas",
3
- "version": "0.3.1",
3
+ "version": "0.6.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.6.0",
24
- "@e-mc/db": "^0.6.0",
25
- "@e-mc/module": "^0.6.0",
26
- "@e-mc/types": "^0.6.0",
27
- "@pi-r/mongodb": "^0.3.1",
28
- "mongodb": "^5.9.1"
23
+ "@e-mc/cloud": "^0.8.0",
24
+ "@e-mc/db": "^0.8.0",
25
+ "@e-mc/module": "^0.8.0",
26
+ "@e-mc/types": "^0.8.0",
27
+ "@pi-r/mongodb": "^0.6.0",
28
+ "mongodb": "^6.3.0"
29
29
  }
30
30
  }