@e-mc/cloud 0.0.1 → 0.0.3

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.
package/index.js CHANGED
@@ -235,7 +235,7 @@ class Cloud extends core_1.ClientDb {
235
235
  this.addDownload(size = value.length);
236
236
  }
237
237
  this.add(destUri);
238
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, data.service, ["Download success" /* CMD_CLOUD.DOWNLOAD_FILE */, (0, types_1.formatSize)(size)], destUri, { ...Cloud.LOG_CLOUD_DOWNLOAD });
238
+ this.formatMessage(64 /* LOG_TYPE.CLOUD */, data.service, ["Download success" /* VAL_CLOUD.DOWNLOAD_FILE */, (0, types_1.formatSize)(size)], destUri, { ...Cloud.LOG_CLOUD_DOWNLOAD });
239
239
  result || (result = destUri);
240
240
  }
241
241
  catch (err) {
@@ -753,11 +753,10 @@ class Cloud extends core_1.ClientDb {
753
753
  return (SERVICE_DOWNLOAD[service] || (SERVICE_DOWNLOAD[service] = require(this.resolveService(service, 'download')))).call(this, credential, service);
754
754
  }
755
755
  resolveService(service, folder) {
756
- let result, sep = path.sep;
756
+ let result;
757
757
  if (service[0] === '@') {
758
758
  result = service;
759
759
  folder || (folder = 'client');
760
- sep = '/';
761
760
  }
762
761
  else {
763
762
  switch (service) {
@@ -768,12 +767,23 @@ class Cloud extends core_1.ClientDb {
768
767
  service = 'gcp';
769
768
  break;
770
769
  }
771
- if (!Cloud.isDir(result = path.join(__dirname, service))) {
772
- result = service;
773
- sep = '/';
770
+ switch (service) {
771
+ case 'atlas':
772
+ case 'aws':
773
+ case 'aws-v3':
774
+ case 'azure':
775
+ case 'gcp':
776
+ case 'ibm':
777
+ case 'minio':
778
+ case 'oci':
779
+ result = '@e-mc2/' + service;
780
+ break;
781
+ default:
782
+ result = service;
783
+ break;
774
784
  }
775
785
  }
776
- return result + (folder ? sep + folder : '');
786
+ return result + (folder ? '/' + folder : '');
777
787
  }
778
788
  settingsOf(service, name, component) {
779
789
  const settings = this.settings;
@@ -806,7 +816,7 @@ class Cloud extends core_1.ClientDb {
806
816
  data.ignoreCache ?? (data.ignoreCache = true);
807
817
  return this.getDatabaseRows(data, true);
808
818
  });
809
- return items.length === 0 ? Promise.resolve(false) : this.allSettled(items, ["Execute unassigned queries" /* CMD_DB.EXEC_QUERYUNASSIGNED */, this.moduleName]).then(result => result.length > 0).catch(() => false);
819
+ return items.length === 0 ? Promise.resolve(false) : this.allSettled(items, ["Execute unassigned queries" /* VAL_DB.EXEC_QUERYUNASSIGNED */, this.moduleName]).then(result => result.length > 0).catch(() => false);
810
820
  }
811
821
  getClient(service) {
812
822
  try {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@e-mc/cloud",
3
- "version": "0.0.1",
4
- "description": "Cloud constructor for e-mc.",
3
+ "version": "0.0.3",
4
+ "description": "Cloud constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
7
7
  "publishConfig": {
@@ -20,7 +20,7 @@
20
20
  "license": "BSD 3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/core": "0.0.1",
24
- "@e-mc/db": "0.0.1"
23
+ "@e-mc/core": "0.0.3",
24
+ "@e-mc/db": "0.0.3"
25
25
  }
26
26
  }
package/atlas/index.js DELETED
@@ -1,193 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getObjectId = exports.executeBatchQuery = exports.executeQuery = exports.createDatabaseClient = exports.validateDatabase = void 0;
4
- const types_1 = require("../../types");
5
- const mongodb_1 = require("../../db/mongodb");
6
- const util_1 = require("../util");
7
- const module_1 = require("../../module");
8
- const MONGODB_V3 = module_1.default.checkSemVer('mongodb', 1, 4);
9
- function sanitizeCredentials(item, credential) {
10
- const { username, password } = credential;
11
- if (username) {
12
- const auth = password ? { username, password } : { username };
13
- if (MONGODB_V3) {
14
- credential.auth = password || !credential.auth || !credential.auth.password ? auth : credential.auth;
15
- }
16
- else {
17
- credential.auth || (credential.auth = auth);
18
- }
19
- delete credential.username;
20
- }
21
- if (password) {
22
- delete credential.password;
23
- }
24
- if (MONGODB_V3 && credential.auth) {
25
- if (!(0, util_1.hasBasicAuth)(item.uri)) {
26
- const auth = (0, util_1.getBasicAuth)(credential.auth);
27
- if (auth) {
28
- const url = item.uri.split('://');
29
- item.uri = url[0] + '://' + auth + url[1];
30
- }
31
- }
32
- delete credential.auth;
33
- }
34
- }
35
- function validateDatabase(credential, data) {
36
- const auth = credential.auth;
37
- return !!(data.table && (auth?.username || credential.username || module_1.default.isURL(data.uri) && (0, util_1.hasBasicAuth)(data.uri)));
38
- }
39
- exports.validateDatabase = validateDatabase;
40
- function createDatabaseClient(credential, data) {
41
- try {
42
- sanitizeCredentials(data, credential);
43
- const { MongoClient } = require('mongodb');
44
- return new MongoClient(data.uri, credential);
45
- }
46
- catch (err) {
47
- this.checkPackage(err, 'mongodb', { passThrough: true });
48
- throw err;
49
- }
50
- }
51
- exports.createDatabaseClient = createDatabaseClient;
52
- async function executeQuery(credential, data, sessionKey) {
53
- return (await executeBatchQuery.call(this, credential, [data], sessionKey))[0] || [];
54
- }
55
- exports.executeQuery = executeQuery;
56
- async function executeBatchQuery(credential, batch, sessionKey) {
57
- const length = batch.length;
58
- const result = new Array(length);
59
- const clients = {};
60
- const caching = length > 0 && this.hasCache(batch[0].service, sessionKey);
61
- const coercing = this.hasCoerce('atlas', 'options');
62
- const cacheValue = { value: this.valueOfKey(credential, 'cache'), sessionKey };
63
- const createClient = async (item, options) => {
64
- var _a;
65
- return clients[_a = item.uri + module_1.default.asString(options, true)] || (clients[_a] = await createDatabaseClient.call(this, options, item).connect());
66
- };
67
- const closeClient = () => {
68
- for (const key in clients) {
69
- clients[key].close();
70
- }
71
- };
72
- for (let i = 0; i < length; ++i) {
73
- const item = batch[i];
74
- const { service, name, table, id, aggregate, sort, client, ignoreCache } = item;
75
- if (!table) {
76
- closeClient();
77
- throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
78
- }
79
- let { query, update, limit = 0 } = item;
80
- if (id && (!query || limit === 1)) {
81
- query = getObjectId(id);
82
- limit = 1;
83
- }
84
- const renewCache = ignoreCache === 0;
85
- const options = item.options ? length === 1 && !MONGODB_V3 ? Object.assign(item.options, credential) : { ...item.options, ...credential } : length === 1 && !MONGODB_V3 ? credential : { ...credential };
86
- let rows, queryString = caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache ? (name || '') + '_' + table + '_' + limit + module_1.default.asString(options, true) + (sort ? module_1.default.asString(sort, true) : '') + (client ? module_1.default.asString(client, true) : '') : '', auth, pipeline;
87
- if (queryString) {
88
- sanitizeCredentials(item, options);
89
- auth = { uri: item.uri, options };
90
- }
91
- const getCache = () => {
92
- if (ignoreCache === 1) {
93
- return;
94
- }
95
- cacheValue.renewCache = renewCache;
96
- return this.getQueryResult(service, auth, queryString, cacheValue);
97
- };
98
- const getCollection = (mongoClient) => mongoClient.db(name, client?.db).collection(table, client?.collection);
99
- if (aggregate) {
100
- pipeline = Array.isArray(aggregate) ? aggregate : aggregate.pipeline;
101
- }
102
- if (pipeline || query) {
103
- if (queryString) {
104
- queryString += module_1.default.asString(aggregate || query, true);
105
- if ((aggregate || !update) && (rows = getCache())) {
106
- result[i] = rows;
107
- continue;
108
- }
109
- }
110
- const collection = getCollection(await createClient(item, options));
111
- if (pipeline) {
112
- const aggregateOptions = aggregate?.options;
113
- if (coercing) {
114
- pipeline.forEach(doc => (0, types_1.coerceObject)(doc));
115
- if (aggregateOptions) {
116
- (0, types_1.coerceObject)(aggregateOptions);
117
- }
118
- }
119
- const items = collection.aggregate(pipeline, aggregateOptions);
120
- if (sort) {
121
- items.sort((0, mongodb_1.getSortValue)(sort, coercing)[0]);
122
- }
123
- if (limit > 0) {
124
- items.limit(limit);
125
- }
126
- rows = await items.toArray();
127
- }
128
- else {
129
- const updateType = item.updateType;
130
- const [filter, command] = (0, mongodb_1.getFilterValue)(query, coercing);
131
- if (update && coercing) {
132
- (0, types_1.coerceObject)(update);
133
- }
134
- if ((0, types_1.isArray)(update)) {
135
- const insertOptions = item.execute?.insert;
136
- if (insertOptions) {
137
- await collection.insertMany(update, coercing ? (0, types_1.coerceObject)(insertOptions) : insertOptions);
138
- }
139
- else {
140
- await collection.insertMany(update);
141
- }
142
- update = undefined;
143
- }
144
- else if (updateType === 1) {
145
- update = undefined;
146
- }
147
- if (limit === 1) {
148
- const document = update ? await collection[updateType === 2 ? 'findOneAndReplace' : updateType === 3 ? 'findOneAndDelete' : 'findOneAndUpdate'](filter, update, command) : await collection.findOne(filter, command);
149
- rows = document ? [document] : [];
150
- }
151
- else {
152
- if (update) {
153
- await collection.updateMany(filter, update);
154
- }
155
- const items = collection.find(filter, command);
156
- if (sort) {
157
- items.sort(...(0, mongodb_1.getSortValue)(sort, coercing));
158
- }
159
- if (limit > 1) {
160
- items.limit(limit);
161
- }
162
- rows = await items.toArray();
163
- }
164
- }
165
- }
166
- else if (!aggregate) {
167
- if (queryString && (rows = getCache())) {
168
- result[i] = rows;
169
- continue;
170
- }
171
- const items = getCollection(await createClient(item, options)).find();
172
- if (sort) {
173
- items.sort(...(0, mongodb_1.getSortValue)(sort, coercing));
174
- }
175
- if (limit > 0) {
176
- items.limit(limit);
177
- }
178
- rows = await items.toArray();
179
- }
180
- else {
181
- queryString = '';
182
- }
183
- result[i] = this.setQueryResult(service, auth, queryString, rows, cacheValue);
184
- }
185
- closeClient();
186
- return result;
187
- }
188
- exports.executeBatchQuery = executeBatchQuery;
189
- function getObjectId(value) {
190
- const { ObjectId } = require('mongodb');
191
- return { '_id': new ObjectId(value) };
192
- }
193
- exports.getObjectId = getObjectId;
@@ -1,44 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const index_1 = require("../index");
4
- const types_1 = require("../../../types");
5
- const module_1 = require("../../../module");
6
- const index_2 = require("../../index");
7
- function download(credential, service = 'aws', sdk = 'aws-sdk/clients/s3') {
8
- const s3 = index_1.createStorageClient.call(this, credential, service, sdk);
9
- return (data, callback) => {
10
- const { bucket: Bucket, download: target } = data;
11
- const Key = target.filename;
12
- if (!Bucket || !Key) {
13
- callback((0, types_1.errorValue)('Missing property', !Bucket ? 'Bucket' : 'Key'));
14
- return;
15
- }
16
- const location = module_1.default.joinPath(Bucket, Key);
17
- const params = { Bucket, Key, VersionId: target.versionId };
18
- s3.getObject(params, (err, result) => {
19
- if (!err) {
20
- callback(null, result.Body);
21
- const deleteObject = target.deleteObject;
22
- if (deleteObject) {
23
- s3.deleteObject((0, types_1.isPlainObject)(deleteObject) ? Object.assign(deleteObject, params) : params, error => {
24
- if (!error) {
25
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Delete success" /* CMD_CLOUD.DELETE_FILE */, location, { ...index_2.default.LOG_CLOUD_DELETE });
26
- }
27
- else {
28
- this.formatFail(64 /* LOG_TYPE.CLOUD */, service, ["Delete failed" /* ERR_CLOUD.DELETE_FAIL */, location], error, { ...index_2.default.LOG_CLOUD_FAIL, fatal: !!target.active });
29
- }
30
- });
31
- }
32
- }
33
- else {
34
- callback(err);
35
- }
36
- });
37
- };
38
- }
39
- exports.default = download;
40
-
41
- if (exports.default) {
42
- module.exports = exports.default;
43
- module.exports.default = exports.default;
44
- }