@pi-r/oci 0.9.3 → 0.10.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.
package/client/index.js CHANGED
@@ -72,9 +72,12 @@ async function executeBatchQuery(credential, batch, sessionKey) {
72
72
  const result = new Array(length);
73
73
  const caching = length > 0 && this.hasCache(batch[0].service, sessionKey);
74
74
  const cacheValue = { value: this.valueOfKey(credential, 'cache'), sessionKey };
75
- let client;
75
+ let db, client;
76
+ const createSoda = async () => {
77
+ return (client || await createClient()).getSodaDatabase();
78
+ };
76
79
  const createClient = async () => {
77
- return createDatabaseClient.call(this, credential);
80
+ return client ||= await createDatabaseClient.call(this, credential);
78
81
  };
79
82
  const closeClient = () => {
80
83
  if (client) {
@@ -85,14 +88,24 @@ async function executeBatchQuery(credential, batch, sessionKey) {
85
88
  const item = batch[i];
86
89
  const { service, table = '', id, query, updateType = 0, ignoreCache } = item;
87
90
  cacheValue.exclusiveOf = Array.isArray(ignoreCache) ? ignoreCache : undefined;
88
- let rows, queryString = caching && ignoreCache !== true ? table + '_' : '';
91
+ let rows, update = item.update, queryString = caching && ignoreCache !== true ? table + '_' : '';
92
+ if (Array.isArray(update) || update && updateType === 3) {
93
+ db ||= await createSoda();
94
+ const col = await db.createCollection(table);
95
+ if (Array.isArray(update)) {
96
+ await col.insertMany(update);
97
+ }
98
+ else {
99
+ await col.insertOne(update);
100
+ }
101
+ update = undefined;
102
+ }
89
103
  invalid: {
90
104
  if (id) {
91
105
  if (!table) {
92
106
  closeClient();
93
107
  throw (0, util_1.formatError)(item, "Missing database table");
94
108
  }
95
- const update = item.update;
96
109
  if (queryString) {
97
110
  queryString += id;
98
111
  if (!update && (rows = this.getCacheResult(service, credential, queryString, cacheValue, ignoreCache))) {
@@ -100,24 +113,24 @@ async function executeBatchQuery(credential, batch, sessionKey) {
100
113
  continue;
101
114
  }
102
115
  }
103
- const soda = (client ||= await createClient()).getSodaDatabase();
104
- const db = await soda.openCollection(table);
105
- if (!db) {
116
+ db ||= await createSoda();
117
+ const col = await db.openCollection(table);
118
+ if (!col) {
106
119
  closeClient();
107
120
  throw (0, util_1.formatError)(item, "Missing database table");
108
121
  }
109
122
  if (update && updateType === 1) {
110
123
  if (update._id) {
111
- const document = soda.createDocument(update);
112
- await db.insertOne(document);
124
+ const document = db.createDocument(update);
125
+ await col.insertOne(document);
113
126
  }
114
127
  else {
115
- void await db.insertOneAndGet(update);
128
+ void await col.insertOneAndGet(update);
116
129
  }
117
130
  }
118
131
  let target, document;
119
132
  if (typeof id === 'string') {
120
- target = db.find().key(id);
133
+ target = col.find().key(id);
121
134
  document = await target.getOne();
122
135
  if (document) {
123
136
  rows = [document.getContent()];
@@ -125,7 +138,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
125
138
  }
126
139
  else {
127
140
  rows = [];
128
- const row = await db.find().keys(id).getCursor();
141
+ const row = await col.find().keys(id).getCursor();
129
142
  while (document = await row.getNext()) {
130
143
  rows.push(document.getContent());
131
144
  }
@@ -142,7 +155,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
142
155
  this.addLog(types_1.STATUS_TYPE.WARN, message + ` (_id=${id.toString()})`, service, 'replaceOne');
143
156
  };
144
157
  if ((0, types_1.isArray)(id)) {
145
- target = db.find().key(id[0]);
158
+ target = col.find().key(id[0]);
146
159
  }
147
160
  if (rows && target) {
148
161
  document = await target.replaceOneAndGet(updateType === 0 ? { ...rows[0], ...update } : update);
@@ -161,21 +174,21 @@ async function executeBatchQuery(credential, batch, sessionKey) {
161
174
  closeClient();
162
175
  throw (0, util_1.formatError)(item, "Missing database table");
163
176
  }
164
- if (queryString && (rows = this.getCacheResult(service, credential, queryString += Cloud.asString(query, true) + (maxRows > 0 ? maxRows : ''), cacheValue, ignoreCache))) {
177
+ if (queryString && (rows = this.getCacheResult(service, credential, queryString += Cloud.asString(query, true) + '_' + (maxRows > 0 ? maxRows.toString() : '0'), cacheValue, ignoreCache))) {
165
178
  result[i] = rows;
166
179
  continue;
167
180
  }
168
- const db = await (client ||= await createClient()).getSodaDatabase().openCollection(table);
169
- if (!db) {
181
+ const col = await (db ||= await createSoda()).openCollection(table);
182
+ if (!col) {
170
183
  closeClient();
171
184
  throw (0, util_1.formatError)(item, "Missing database table");
172
185
  }
173
186
  const tryQuery = async () => {
174
- let operation = db.find().filter(query);
187
+ let oper = col.find().filter(query);
175
188
  if (maxRows > 0) {
176
- operation = operation.limit(maxRows);
189
+ oper = oper.limit(maxRows);
177
190
  }
178
- return (await operation.getDocuments()).map(doc => doc.getContent());
191
+ return (await oper.getDocuments()).map(doc => doc.getContent());
179
192
  };
180
193
  try {
181
194
  rows = await tryQuery();
@@ -210,31 +223,27 @@ async function executeBatchQuery(credential, batch, sessionKey) {
210
223
  client ||= await createClient();
211
224
  const executeOptions = { ...options, outFormat: 4002, maxRows, resultSet: false, autoCommit: true };
212
225
  if (item.streamRow) {
213
- rows = [];
214
- await new Promise((resolve, reject) => {
215
- let error = false;
216
- const stream = client.queryStream(query, params || [], executeOptions);
217
- stream
218
- .on('data', row => {
219
- rows.push(row);
220
- })
221
- .on('error', err => {
222
- error = true;
223
- reject(err);
224
- })
225
- .on('close', () => {
226
- if (!error) {
227
- resolve(rows);
228
- }
229
- })
230
- .on('end', () => {
231
- stream.destroy();
226
+ try {
227
+ rows = await new Promise((resolve, reject) => {
228
+ const data = [];
229
+ const stream = client.queryStream(query, params || [], executeOptions);
230
+ stream
231
+ .on('data', row => {
232
+ data.push(row);
233
+ })
234
+ .on('close', () => {
235
+ resolve(data);
236
+ })
237
+ .on('end', () => {
238
+ stream.destroy();
239
+ })
240
+ .on('error', reject);
232
241
  });
233
- })
234
- .catch((err) => {
242
+ }
243
+ catch (err) {
235
244
  closeClient();
236
- throw (0, util_1.formatError)(item, err instanceof Error ? err.message : "Missing database query");
237
- });
245
+ throw err;
246
+ }
238
247
  }
239
248
  else {
240
249
  ({ rows } = await client.execute(query, params || [], executeOptions));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/oci",
3
- "version": "0.9.3",
3
+ "version": "0.10.0",
4
4
  "description": "Oracle (OCI) cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "publishConfig": {
@@ -19,11 +19,11 @@
19
19
  "license": "MIT",
20
20
  "homepage": "https://github.com/anpham6/pi-r#readme",
21
21
  "dependencies": {
22
- "@e-mc/cloud": "^0.11.4",
23
- "@e-mc/module": "^0.11.4",
24
- "@e-mc/types": "^0.11.4",
25
- "@pi-r/aws": "^0.9.3",
26
- "@pi-r/oracle": "^0.9.3",
22
+ "@e-mc/cloud": "^0.12.0",
23
+ "@e-mc/module": "^0.12.0",
24
+ "@e-mc/types": "^0.12.0",
25
+ "@pi-r/aws": "^0.10.0",
26
+ "@pi-r/oracle": "^0.10.0",
27
27
  "aws-sdk": "^2.1692.0",
28
28
  "oracledb": "^6.8.0"
29
29
  }
package/types/index.d.ts CHANGED
@@ -10,11 +10,11 @@ export interface OCIStorageCredential extends ConfigurationOptions {
10
10
  endpoint?: string;
11
11
  }
12
12
 
13
- export interface OCIDatabaseQuery extends Omit<CloudDatabase<PlainObject | string, ExecuteOptions, StandardMap, BindParameters, OCIDatabaseCredential>, "id"> {
13
+ export interface OCIDatabaseQuery extends Omit<CloudDatabase<PlainObject | string, ExecuteOptions, StandardMap | StandardMap[], BindParameters, OCIDatabaseCredential>, "id"> {
14
14
  source: "cloud";
15
15
  service: "oci";
16
16
  id?: string | string[];
17
- updateType?: 0 | 1 | 2;
17
+ updateType?: 0 | 1 | 2 | 3;
18
18
  streamRow?: boolean;
19
19
  }
20
20