@pi-r/gcp 0.10.0 → 0.10.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 +87 -19
  2. package/package.json +4 -4
package/client/index.js CHANGED
@@ -143,6 +143,10 @@ const asFieldPath = (params) => Array.isArray(params) && params.every(item => ty
143
143
  const getFirebasePackageName = (credential) => credential.apiKey && credential.authDomain && credential ? 'firebase' : 'firebase-admin';
144
144
  const isErrorConflict = (err) => err instanceof Error && err.code === 409;
145
145
  const hasGoogleAuth = (credential) => (0, types_1.isString)(credential.keyFilename || credential.keyFile || process.env.GOOGLE_APPLICATION_CREDENTIALS) || (0, types_1.isObject)(credential.credentials) || !!credential.authClient;
146
+ const isSpanner = (data) => data.product === "spanner" || !data.product && !!(data.name && data.database && (data.query || data.table));
147
+ const isBigtable = (data) => data.product === "bigtable" || !data.product && !!(data.name && data.table && !data.database);
148
+ const isBigQuery = (data) => data.product === "bigquery" || !data.product && !!(!data.name && data.table && data.database || (0, types_1.isString)(data.query) || (0, types_1.isString)(data.options?.query));
149
+ const isDatastore = (data) => data.product === "datastore" || !data.product && !!(data.keys || data.kind || data.query && !data.table && !data.id);
146
150
  function isFirebaseApp(credential, store, admin) {
147
151
  return !!(credential.apiKey && credential.authDomain && (store || credential.databaseURL) || credential.product === "firebase" && process.env.GOOGLE_APPLICATION_CREDENTIALS) && (!admin || !credential.admin);
148
152
  }
@@ -178,20 +182,19 @@ function createDatabaseClient(credential, data) {
178
182
  }
179
183
  getProjectId(credential, true);
180
184
  if (data) {
181
- const { product, name, table, query, database } = data;
182
- if (product === "spanner" || !product && name && database && (query || table)) {
185
+ if (isSpanner(data)) {
183
186
  const { Spanner } = require(packageName = '@google-cloud/spanner');
184
187
  return new Spanner(credential);
185
188
  }
186
- if (product === "bigquery" || !product && (!name && table && database || (0, types_1.isString)(query))) {
187
- const { BigQuery } = require(packageName = '@google-cloud/bigquery');
188
- return new BigQuery(credential);
189
- }
190
- if (product === "bigtable" || !product && name && table && !database) {
189
+ if (isBigtable(data)) {
191
190
  const { Bigtable } = require(packageName = '@google-cloud/bigtable');
192
191
  return new Bigtable(credential);
193
192
  }
194
- if (product === "datastore" || !product && (data.keys || data.kind || query && !table && !data.id)) {
193
+ if (isBigQuery(data)) {
194
+ const { BigQuery } = require(packageName = '@google-cloud/bigquery');
195
+ return new BigQuery(credential);
196
+ }
197
+ if (isDatastore(data)) {
195
198
  const { Datastore } = require(packageName = '@google-cloud/datastore');
196
199
  return new Datastore(credential);
197
200
  }
@@ -529,7 +532,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
529
532
  rows = [group && (0, types_1.isObject)(val) && (0, types_1.isObject)(val[group[0]]) ? val[group[0]] : val];
530
533
  }
531
534
  }
532
- else if (product === "spanner" || !product && name && database && (query || table)) {
535
+ else if (isSpanner(item)) {
533
536
  const columns = (0, types_1.isPlainObject)(query) && query.columns || item.columns;
534
537
  if (!(query || table && columns)) {
535
538
  closeClient();
@@ -554,7 +557,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
554
557
  }
555
558
  const request = (0, types_1.isPlainObject)(query) ? query : {};
556
559
  request.json = true;
557
- if ((0, types_1.isObject)(columns)) {
560
+ if (columns) {
558
561
  request.columns = columns;
559
562
  }
560
563
  request.keySet = { all: true };
@@ -573,11 +576,8 @@ async function executeBatchQuery(credential, batch, sessionKey) {
573
576
  else {
574
577
  transaction.runUpdate(update)
575
578
  .then(async () => {
576
- transaction.commit()
577
- .then(() => {
578
- resolve();
579
- })
580
- .catch(reject);
579
+ await transaction.commit();
580
+ resolve();
581
581
  })
582
582
  .catch(reject);
583
583
  }
@@ -586,13 +586,81 @@ async function executeBatchQuery(credential, batch, sessionKey) {
586
586
  }
587
587
  const request = (0, types_1.isPlainObject)(query) ? query : { sql: query };
588
588
  request.json = true;
589
+ if (request.params) {
590
+ const types = request.types;
591
+ if ((0, types_1.isPlainObject)(types)) {
592
+ const { Spanner } = require('@google-cloud/spanner');
593
+ for (const attr in types) {
594
+ let value = request.params[attr];
595
+ if (value === undefined) {
596
+ continue;
597
+ }
598
+ const method = types[attr];
599
+ switch (method) {
600
+ case 'date':
601
+ if ((0, types_1.isString)(value)) {
602
+ value = Spanner.date(value);
603
+ }
604
+ else if (Array.isArray(value) && value.length === 3) {
605
+ value = Spanner.date(...value);
606
+ }
607
+ break;
608
+ case 'interval':
609
+ if (Array.isArray(value) && value.length === 3) {
610
+ value = Spanner.interval(value[0], value[1], BigInt(value[2]));
611
+ }
612
+ break;
613
+ case 'float':
614
+ case 'float32':
615
+ case 'int':
616
+ case 'numeric':
617
+ value = Spanner[method](value);
618
+ break;
619
+ case 'protoEnum':
620
+ case 'protoMessage':
621
+ case 'struct':
622
+ if ((0, types_1.isPlainObject)(value)) {
623
+ value = Spanner[method](value);
624
+ }
625
+ break;
626
+ case 'pgJsonb':
627
+ if ((0, types_1.isString)(value) || (0, types_1.isPlainObject)(value)) {
628
+ value = Spanner.pgJsonb(value);
629
+ }
630
+ break;
631
+ case 'pgNumeric':
632
+ if ((0, types_1.isString)(value)) {
633
+ value = Spanner.pgNumeric(value);
634
+ }
635
+ break;
636
+ case 'timestamp':
637
+ value = Spanner.timestamp(value);
638
+ break;
639
+ }
640
+ request.params[attr] = value;
641
+ }
642
+ }
643
+ }
589
644
  if (limit > 0) {
590
645
  (request.gaxOptions ||= {}).maxResults = limit;
591
646
  }
592
- [rows] = await db.run(request);
647
+ if ((flags & 2) === 2) {
648
+ rows = [];
649
+ await new Promise((resolve, reject) => {
650
+ db.runStream(request)
651
+ .on('data', row => {
652
+ rows.push(row);
653
+ })
654
+ .on('end', resolve)
655
+ .on('error', reject);
656
+ });
657
+ }
658
+ else {
659
+ [rows] = await db.run(request);
660
+ }
593
661
  }
594
662
  }
595
- else if (product === "bigtable" || !product && name && table && !database) {
663
+ else if (isBigtable(item)) {
596
664
  if (!name || !table) {
597
665
  closeClient();
598
666
  throw (0, util_1.formatError)(item, !name ? "Missing database name" : "Missing database table");
@@ -638,7 +706,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
638
706
  });
639
707
  }
640
708
  }
641
- else if (product === "bigquery" || !product && (!name && database && table || (0, types_1.isString)(query) || (0, types_1.isString)(item.options?.query))) {
709
+ else if (isBigQuery(item)) {
642
710
  const { params, options = {} } = item;
643
711
  if ((0, types_1.isString)(query)) {
644
712
  options.query = query;
@@ -680,7 +748,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
680
748
  [rows] = await instance.query(options);
681
749
  }
682
750
  }
683
- else if (product === "datastore" || !product && (item.keys || item.kind || query && !table && !id)) {
751
+ else if (isDatastore(item)) {
684
752
  const { keys, kind, options } = item;
685
753
  if (!keys && !kind && !Array.isArray(query)) {
686
754
  closeClient();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/gcp",
3
- "version": "0.10.0",
3
+ "version": "0.10.1",
4
4
  "description": "Google (GCP) cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "publishConfig": {
@@ -19,9 +19,9 @@
19
19
  "license": "MIT",
20
20
  "homepage": "https://github.com/anpham6/pi-r#readme",
21
21
  "dependencies": {
22
- "@e-mc/cloud": "^0.12.0",
23
- "@e-mc/module": "^0.12.0",
24
- "@e-mc/types": "^0.12.0",
22
+ "@e-mc/cloud": "^0.12.3",
23
+ "@e-mc/module": "^0.12.3",
24
+ "@e-mc/types": "^0.12.3",
25
25
  "@google-cloud/firestore": "^7.11.0",
26
26
  "@google-cloud/storage": "^7.16.0"
27
27
  }