@pi-r/oci 0.0.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.
package/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2023 An Pham
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ ### @pi-r/oci
2
+
3
+ ### LICENSE
4
+
5
+ MIT
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.executeBatchQuery = exports.executeQuery = exports.deleteObjectsV2 = exports.deleteObjects = exports.setBucketWebsite = exports.setBucketPolicy = exports.createBucketV2 = exports.createBucket = exports.createDatabaseClient = exports.setStorageCredential = exports.validateDatabase = exports.validateStorage = void 0;
4
+ const types_1 = require("@e-mc/types");
5
+ const aws_1 = require("@pi-r/aws");
6
+ const util_1 = require("@e-mc/cloud/util");
7
+ const Module = require("@e-mc/module");
8
+ function validateStorage(credential) {
9
+ return !!(credential.accessKeyId && credential.secretAccessKey && (credential.region && credential.namespace || credential.endpoint));
10
+ }
11
+ exports.validateStorage = validateStorage;
12
+ function validateDatabase(credential, data) {
13
+ return !!(data.table && (credential.password && (credential.user || credential.username) && (credential.connectString || credential.connectionString) || credential.poolAlias));
14
+ }
15
+ exports.validateDatabase = validateDatabase;
16
+ function setStorageCredential(credential) {
17
+ if (credential.endpoint) {
18
+ credential.region || (credential.region = /([^.]+)\.oraclecloud\.com$/.exec(credential.endpoint)?.[1]);
19
+ }
20
+ else {
21
+ credential.endpoint || (credential.endpoint = `https://${credential.namespace}.compat.objectstorage.${credential.region}.oraclecloud.com`);
22
+ }
23
+ credential.s3ForcePathStyle = true;
24
+ credential.signatureVersion = 'v4';
25
+ }
26
+ exports.setStorageCredential = setStorageCredential;
27
+ function createDatabaseClient(credential) {
28
+ try {
29
+ if (credential.username) {
30
+ credential.user || (credential.user = credential.username);
31
+ delete credential.username;
32
+ }
33
+ const { getConnection } = require('oracledb');
34
+ return getConnection(credential);
35
+ }
36
+ catch (err) {
37
+ this.checkPackage(err, 'oracledb', { passThrough: true });
38
+ throw err;
39
+ }
40
+ }
41
+ exports.createDatabaseClient = createDatabaseClient;
42
+ function createBucket(credential, bucket, publicRead) {
43
+ return aws_1.createBucketV2.call(this, credential, bucket, publicRead ? 'public-read' : undefined, undefined, 'oci');
44
+ }
45
+ exports.createBucket = createBucket;
46
+ function createBucketV2(credential, bucket, ACL, options) {
47
+ return aws_1.createBucketV2.call(this, credential, bucket, ACL, options, 'oci');
48
+ }
49
+ exports.createBucketV2 = createBucketV2;
50
+ function setBucketPolicy(credential, bucket, options) {
51
+ return aws_1.setBucketPolicy.call(this, credential, bucket, options, 'oci');
52
+ }
53
+ exports.setBucketPolicy = setBucketPolicy;
54
+ function setBucketWebsite(credential, bucket, options) {
55
+ return aws_1.setBucketWebsite.call(this, credential, bucket, options, 'oci');
56
+ }
57
+ exports.setBucketWebsite = setBucketWebsite;
58
+ function deleteObjects(credential, bucket) {
59
+ return deleteObjectsV2.call(this, credential, bucket, true);
60
+ }
61
+ exports.deleteObjects = deleteObjects;
62
+ function deleteObjectsV2(credential, bucket, recursive = true) {
63
+ setStorageCredential(credential);
64
+ return aws_1.deleteObjectsV2.call(this, credential, bucket, recursive, 'oci');
65
+ }
66
+ exports.deleteObjectsV2 = deleteObjectsV2;
67
+ async function executeQuery(credential, data, sessionKey) {
68
+ return (await executeBatchQuery.call(this, credential, [data], sessionKey))[0] || [];
69
+ }
70
+ exports.executeQuery = executeQuery;
71
+ async function executeBatchQuery(credential, batch, sessionKey) {
72
+ const length = batch.length;
73
+ const result = new Array(length);
74
+ const caching = length > 0 && this.hasCache(batch[0].service, sessionKey);
75
+ const cacheValue = { value: this.valueOfKey(credential, 'cache'), sessionKey };
76
+ let client;
77
+ const createClient = () => createDatabaseClient.call(this, length === 1 ? credential : { ...credential });
78
+ const closeClient = () => client?.close();
79
+ for (let i = 0; i < length; ++i) {
80
+ const item = batch[i];
81
+ const { service, table, id, query, ignoreCache } = item;
82
+ if (!table) {
83
+ closeClient();
84
+ throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
85
+ }
86
+ const renewCache = ignoreCache === 0;
87
+ const getCache = (value) => {
88
+ if (ignoreCache === 1) {
89
+ return;
90
+ }
91
+ cacheValue.renewCache = renewCache;
92
+ return this.getQueryResult(service, credential, value, cacheValue);
93
+ };
94
+ let rows, queryString = caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache ? table + '_' : '';
95
+ invalid: {
96
+ if (id) {
97
+ const update = item.update;
98
+ if (queryString) {
99
+ queryString += id;
100
+ if (!update && (rows = getCache(queryString))) {
101
+ result[i] = rows;
102
+ continue;
103
+ }
104
+ }
105
+ const db = await (client || (client = await createClient())).getSodaDatabase().openCollection(table);
106
+ if (!db) {
107
+ closeClient();
108
+ throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
109
+ }
110
+ const row = db.find().key(id);
111
+ let document = await row.getOne();
112
+ if (document) {
113
+ rows = [document.getContent()];
114
+ }
115
+ if (update) {
116
+ const failed = (message, fatal) => {
117
+ if (fatal) {
118
+ item.transactionFail = true;
119
+ }
120
+ else {
121
+ delete item.update;
122
+ }
123
+ this.addLog(types_1.STATUS_TYPE.WARN, service + `-> ${message} (_id=${id})`);
124
+ };
125
+ if (rows) {
126
+ const status = await row.replaceOne({ ...rows[0], ...update });
127
+ if (status.replaced) {
128
+ rows = undefined;
129
+ if (document = await row.getOne()) {
130
+ rows = [document.getContent()];
131
+ }
132
+ else {
133
+ failed('Update success (GET failed)');
134
+ }
135
+ }
136
+ else {
137
+ failed('Update failed');
138
+ }
139
+ }
140
+ else {
141
+ failed('Row does not exist', true);
142
+ break invalid;
143
+ }
144
+ }
145
+ }
146
+ else if (query) {
147
+ const maxRows = typeof item.limit === 'number' ? Math.max(item.limit, 0) : undefined;
148
+ if ((0, types_1.isPlainObject)(query)) {
149
+ if (queryString && (rows = getCache(queryString += Module.asString(query, true) + (maxRows ? maxRows : '')))) {
150
+ result[i] = rows;
151
+ continue;
152
+ }
153
+ const db = await (client || (client = await createClient())).getSodaDatabase().openCollection(table);
154
+ if (!db) {
155
+ closeClient();
156
+ throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
157
+ }
158
+ let operation = db.find().filter(query);
159
+ if (maxRows) {
160
+ operation = operation.limit(maxRows);
161
+ }
162
+ rows = (await operation.getDocuments()).map(doc => doc.getContent());
163
+ }
164
+ else {
165
+ const { params, options } = item;
166
+ if (queryString && (rows = getCache(queryString += query + Module.asString(params, true) + Module.asString(options, true) + (maxRows ? maxRows : '')))) {
167
+ result[i] = rows;
168
+ continue;
169
+ }
170
+ ({ rows } = await (client || (client = await createClient())).execute(query, params || [], { ...options, outFormat: 4002 /* OUT_FORMAT.OBJECT */, maxRows, resultSet: false, autoCommit: true }));
171
+ }
172
+ }
173
+ else {
174
+ closeClient();
175
+ throw (0, util_1.formatError)(item, "Missing database query" /* ERR_DB.QUERY */);
176
+ }
177
+ }
178
+ result[i] = this.setQueryResult(service, credential, queryString, rows, cacheValue);
179
+ }
180
+ closeClient();
181
+ return result;
182
+ }
183
+ exports.executeBatchQuery = executeBatchQuery;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const client_1 = require("../client");
4
+ function download(credential, service = 'oci') {
5
+ (0, client_1.setStorageCredential)(credential);
6
+ return require('../../aws/download').call(this, credential, service);
7
+ }
8
+
9
+ module.exports = download;
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@pi-r/oci",
3
+ "version": "0.0.1",
4
+ "description": "Oracle (OCI) cloud functions for E-mc.",
5
+ "main": "client/index.js",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/anpham6/pi-r.git",
12
+ "directory": "src/cloud/oci"
13
+ },
14
+ "keywords": [
15
+ "squared",
16
+ "e-mc",
17
+ "squared-functions"
18
+ ],
19
+ "author": "An Pham <anpham6@gmail.com>",
20
+ "license": "MIT",
21
+ "homepage": "https://github.com/anpham6/pi-r#readme",
22
+ "dependencies": {
23
+ "@e-mc/cloud": "^0.4.0",
24
+ "@e-mc/module": "^0.4.0",
25
+ "@e-mc/types": "^0.4.0",
26
+ "@pi-r/aws": "^0.0.1",
27
+ "aws-sdk": "^2.1337.0",
28
+ "oracledb": "^5.5.0"
29
+ }
30
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const client_1 = require("../client");
4
+ function upload(credential, service = 'oci') {
5
+ (0, client_1.setStorageCredential)(credential);
6
+ return require('../../aws/upload').call(this, credential, service);
7
+ }
8
+
9
+ module.exports = upload;