@pi-r/atlas 0.5.0 → 0.6.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 +42 -39
  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;
@@ -59,21 +58,45 @@ async function executeBatchQuery(credential, batch, sessionKey) {
59
58
  query = getObjectId(id);
60
59
  limit = 1;
61
60
  }
62
- const renewCache = ignoreCache === 0;
63
61
  const options = item.options ? length === 1 ? Object.assign(item.options, credential) : { ...item.options, ...credential } : length === 1 ? credential : { ...credential };
64
- 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;
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;
65
63
  if (queryString) {
66
- sanitizeCredentials(item, options);
64
+ sanitizeCredentials(options);
67
65
  auth = { uri: item.uri, options };
68
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);
69
70
  const getCache = () => {
70
- if (ignoreCache === 1) {
71
- return;
71
+ if (ignoreCache !== 1) {
72
+ cacheValue.renewCache = ignoreCache === 0;
73
+ return this.getQueryResult(service, auth, queryString, cacheValue);
72
74
  }
73
- cacheValue.renewCache = renewCache;
74
- return this.getQueryResult(service, auth, queryString, cacheValue);
75
75
  };
76
- const getCollection = (mongoClient) => mongoClient.db(name, client?.db).collection(table, client?.collection);
76
+ cacheValue.exclusiveOf = Array.isArray(ignoreCache) ? ignoreCache : undefined;
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
+ }
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
+ }
77
100
  if (aggregate) {
78
101
  pipeline = Array.isArray(aggregate) ? aggregate : aggregate.pipeline;
79
102
  }
@@ -85,9 +108,8 @@ async function executeBatchQuery(credential, batch, sessionKey) {
85
108
  continue;
86
109
  }
87
110
  }
88
- const collection = getCollection(await createClient(item, options));
89
111
  if (pipeline) {
90
- const aggregateOptions = aggregate?.options;
112
+ const aggregateOptions = (0, types_1.isPlainObject)(aggregate) ? aggregate.options : undefined;
91
113
  if (coercing) {
92
114
  pipeline.forEach(doc => (0, types_1.coerceObject)(doc));
93
115
  if (aggregateOptions) {
@@ -104,40 +126,21 @@ async function executeBatchQuery(credential, batch, sessionKey) {
104
126
  rows = await items.toArray();
105
127
  }
106
128
  else {
129
+ const [filter, operation] = (0, mongodb_1.getFilterValue)(query, coercing);
107
130
  const updateType = item.updateType;
108
- const [filter, command] = (0, mongodb_1.getFilterValue)(query, coercing);
109
- if (update && coercing) {
110
- (0, types_1.coerceObject)(update);
111
- }
112
- if ((0, types_1.isArray)(update)) {
113
- const insertOptions = item.execute?.insert;
114
- if (insertOptions && coercing) {
115
- (0, types_1.coerceObject)(insertOptions);
116
- }
117
- if (update.length === 1) {
118
- await collection.insertOne(update[0], insertOptions);
119
- }
120
- else {
121
- await collection.insertMany(update, insertOptions);
122
- }
123
- update = undefined;
124
- }
125
- else if (updateType === 1) {
126
- update = undefined;
127
- }
128
131
  if (limit === 1) {
129
- const document = update ? await collection[updateType === 2 ? 'findOneAndReplace' : updateType === 3 ? 'findOneAndDelete' : 'findOneAndUpdate'](filter, update, command) : await collection.findOne(filter, command);
132
+ const document = update ? await collection[updateType === 2 ? 'findOneAndReplace' : updateType === 3 ? 'findOneAndDelete' : 'findOneAndUpdate'](filter, update, operation) : await collection.findOne(filter, operation);
130
133
  rows = document ? [document] : [];
131
134
  }
132
135
  else {
133
- if (update) {
136
+ if (update && updateType === 1) {
134
137
  const updateOptions = item.execute?.update;
135
138
  if (updateOptions && coercing) {
136
139
  (0, types_1.coerceObject)(updateOptions);
137
140
  }
138
141
  await collection.updateMany(filter, update, updateOptions);
139
142
  }
140
- const items = collection.find(filter, command);
143
+ const items = collection.find(filter, operation);
141
144
  if (sort) {
142
145
  items.sort(...(0, mongodb_1.getSortValue)(sort, coercing));
143
146
  }
@@ -153,7 +156,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
153
156
  result[i] = rows;
154
157
  continue;
155
158
  }
156
- const items = getCollection(await createClient(item, options)).find();
159
+ const items = collection.find();
157
160
  if (sort) {
158
161
  items.sort(...(0, mongodb_1.getSortValue)(sort, coercing));
159
162
  }
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.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.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.1",
24
+ "@e-mc/db": "^0.8.1",
25
+ "@e-mc/module": "^0.8.1",
26
+ "@e-mc/types": "^0.8.1",
27
+ "@pi-r/mongodb": "^0.6.1",
28
28
  "mongodb": "^6.3.0"
29
29
  }
30
30
  }