@pi-r/atlas 0.5.0 → 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 +23 -17
  2. package/package.json +6 -6
package/client/index.js CHANGED
@@ -6,11 +6,10 @@ 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
- function sanitizeCredentials(item, credential) {
9
+ function sanitizeCredentials(credential) {
10
10
  const { username, password } = credential;
11
11
  if (username) {
12
- const auth = password ? { username, password } : { username };
13
- credential.auth || (credential.auth = auth);
12
+ credential.auth || (credential.auth = password ? { username, password } : { username });
14
13
  delete credential.username;
15
14
  }
16
15
  if (password) {
@@ -23,7 +22,7 @@ function validateDatabase(credential, data) {
23
22
  }
24
23
  exports.validateDatabase = validateDatabase;
25
24
  function createDatabaseClient(credential, data) {
26
- sanitizeCredentials(data, credential);
25
+ sanitizeCredentials(credential);
27
26
  return new mongodb.MongoClient(data.uri, credential);
28
27
  }
29
28
  exports.createDatabaseClient = createDatabaseClient;
@@ -36,7 +35,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
36
35
  const result = new Array(length);
37
36
  const clients = {};
38
37
  const caching = length > 0 && this.hasCache(batch[0].service, sessionKey);
39
- const coercing = this.hasCoerce('atlas', 'options');
38
+ const coercing = this.hasCoerce("atlas" /* STRINGS.MODULE_NAME */, 'options');
40
39
  const cacheValue = { value: this.valueOfKey(credential, 'cache'), sessionKey };
41
40
  const createClient = async (item, options) => {
42
41
  var _a;
@@ -63,17 +62,24 @@ async function executeBatchQuery(credential, batch, sessionKey) {
63
62
  const options = item.options ? length === 1 ? Object.assign(item.options, credential) : { ...item.options, ...credential } : length === 1 ? credential : { ...credential };
64
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;
65
64
  if (queryString) {
66
- sanitizeCredentials(item, options);
65
+ sanitizeCredentials(options);
67
66
  auth = { uri: item.uri, options };
68
67
  }
69
68
  const getCache = () => {
70
- if (ignoreCache === 1) {
71
- return;
69
+ if (ignoreCache !== 1) {
70
+ cacheValue.renewCache = renewCache;
71
+ return this.getQueryResult(service, auth, queryString, cacheValue);
72
72
  }
73
- cacheValue.renewCache = renewCache;
74
- return this.getQueryResult(service, auth, queryString, cacheValue);
75
73
  };
76
- 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
+ }
77
83
  if (aggregate) {
78
84
  pipeline = Array.isArray(aggregate) ? aggregate : aggregate.pipeline;
79
85
  }
@@ -85,9 +91,9 @@ async function executeBatchQuery(credential, batch, sessionKey) {
85
91
  continue;
86
92
  }
87
93
  }
88
- const collection = getCollection(await createClient(item, options));
94
+ const collection = getCollection(instance);
89
95
  if (pipeline) {
90
- const aggregateOptions = aggregate?.options;
96
+ const aggregateOptions = (0, types_1.isPlainObject)(aggregate) ? aggregate.options : undefined;
91
97
  if (coercing) {
92
98
  pipeline.forEach(doc => (0, types_1.coerceObject)(doc));
93
99
  if (aggregateOptions) {
@@ -105,7 +111,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
105
111
  }
106
112
  else {
107
113
  const updateType = item.updateType;
108
- const [filter, command] = (0, mongodb_1.getFilterValue)(query, coercing);
114
+ const [filter, operation] = (0, mongodb_1.getFilterValue)(query, coercing);
109
115
  if (update && coercing) {
110
116
  (0, types_1.coerceObject)(update);
111
117
  }
@@ -126,7 +132,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
126
132
  update = undefined;
127
133
  }
128
134
  if (limit === 1) {
129
- 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);
130
136
  rows = document ? [document] : [];
131
137
  }
132
138
  else {
@@ -137,7 +143,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
137
143
  }
138
144
  await collection.updateMany(filter, update, updateOptions);
139
145
  }
140
- const items = collection.find(filter, command);
146
+ const items = collection.find(filter, operation);
141
147
  if (sort) {
142
148
  items.sort(...(0, mongodb_1.getSortValue)(sort, coercing));
143
149
  }
@@ -153,7 +159,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
153
159
  result[i] = rows;
154
160
  continue;
155
161
  }
156
- const items = getCollection(await createClient(item, options)).find();
162
+ const items = getCollection(instance).find();
157
163
  if (sort) {
158
164
  items.sort(...(0, mongodb_1.getSortValue)(sort, coercing));
159
165
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/atlas",
3
- "version": "0.5.0",
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.7.0",
24
- "@e-mc/db": "^0.7.0",
25
- "@e-mc/module": "^0.7.0",
26
- "@e-mc/types": "^0.7.0",
27
- "@pi-r/mongodb": "^0.5.0",
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
28
  "mongodb": "^6.3.0"
29
29
  }
30
30
  }