@pi-r/gcp 0.10.0 → 0.10.2

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
@@ -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;
@@ -673,14 +741,14 @@ async function executeBatchQuery(credential, batch, sessionKey) {
673
741
  metadata.view = update;
674
742
  await target.setMetadata(metadata);
675
743
  }
676
- const job = (await dataset.createQueryJob(options))[0];
744
+ const [job] = await dataset.createQueryJob(options);
677
745
  [rows] = await job.getQueryResults();
678
746
  }
679
747
  else {
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/download/index.js CHANGED
@@ -11,23 +11,11 @@ function download(credential, service = 'gcp') {
11
11
  return (data, callback) => {
12
12
  const { bucket: Bucket, download: target } = data;
13
13
  const Key = target.keyname || target.filename;
14
- let tempDir;
15
- const downloadResponse = (err, url = null) => {
16
- callback(err, url);
17
- if (tempDir) {
18
- setImmediate(() => {
19
- Cloud.removeDir(tempDir);
20
- });
21
- }
22
- };
23
14
  if (!Bucket || !Key) {
24
- downloadResponse((0, types_1.errorValue)('Missing property', !Bucket ? 'Bucket' : 'Key'));
25
- return;
26
- }
27
- if (!(tempDir = this.getTempDir({ uuidDir: true }))) {
28
- downloadResponse((0, types_1.errorValue)("Unable to create temp directory", service));
15
+ callback((0, types_1.errorValue)('Missing property', !Bucket ? 'Bucket' : 'Key'));
29
16
  return;
30
17
  }
18
+ const tempDir = (0, types_1.getTempDir)(true, service);
31
19
  const location = Cloud.joinPath(Bucket, Key);
32
20
  const destination = path.join(tempDir, path.basename(target.filename || Key));
33
21
  const deleteMessage = () => {
@@ -40,10 +28,10 @@ function download(credential, service = 'gcp') {
40
28
  https.get(url, res => {
41
29
  const statusCode = res.statusCode;
42
30
  if (statusCode >= 200 && statusCode < 300) {
43
- res.on('error', downloadResponse);
31
+ res.on('error', callback);
44
32
  res.pipe(fs.createWriteStream(destination)
45
33
  .on('finish', () => {
46
- downloadResponse(null, destination);
34
+ callback(null, destination);
47
35
  if (target.deleteObject) {
48
36
  deleteObject(objectRef)
49
37
  .then(deleteMessage)
@@ -52,10 +40,10 @@ function download(credential, service = 'gcp') {
52
40
  });
53
41
  }
54
42
  })
55
- .on('error', downloadResponse));
43
+ .on('error', callback));
56
44
  }
57
45
  else {
58
- downloadResponse((0, types_1.errorMessage)(statusCode, 'Invalid HTTP request', location));
46
+ callback((0, types_1.errorMessage)(statusCode, 'Invalid HTTP request', location));
59
47
  }
60
48
  });
61
49
  });
@@ -66,7 +54,7 @@ function download(credential, service = 'gcp') {
66
54
  const file = bucket.file(Key, { generation: target.versionId });
67
55
  const chunkSizeBytes = flags & 4 ? (0, types_1.alignSize)(target.chunkSize, 1024) : 0;
68
56
  const fileDownload = () => {
69
- downloadResponse(null, destination);
57
+ callback(null, destination);
70
58
  const deleteObject = target.deleteObject;
71
59
  if (deleteObject) {
72
60
  file.delete((0, types_1.isObject)(deleteObject) ? deleteObject : { ignoreNotFound: true }, err => {
@@ -86,18 +74,18 @@ function download(credential, service = 'gcp') {
86
74
  concurrencyLimit: target.chunkLimit || target.options?.concurrencyLimit
87
75
  })
88
76
  .then(fileDownload)
89
- .catch(downloadResponse);
77
+ .catch(callback);
90
78
  }
91
79
  else if (target.chunkLimit === 1 && !target.chunkSize) {
92
80
  file.createReadStream()
93
81
  .pipe(fs.createWriteStream(destination))
94
82
  .on('finish', fileDownload)
95
- .on('error', downloadResponse);
83
+ .on('error', callback);
96
84
  }
97
85
  else {
98
86
  file.download({ destination })
99
87
  .then(fileDownload)
100
- .catch(downloadResponse);
88
+ .catch(callback);
101
89
  }
102
90
  };
103
91
  }
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.2",
4
4
  "description": "Google (GCP) cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "publishConfig": {
@@ -19,10 +19,10 @@
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",
25
- "@google-cloud/firestore": "^7.11.0",
22
+ "@e-mc/cloud": "^0.12.5",
23
+ "@e-mc/module": "^0.12.5",
24
+ "@e-mc/types": "^0.12.5",
25
+ "@google-cloud/firestore": "^7.11.3",
26
26
  "@google-cloud/storage": "^7.16.0"
27
27
  }
28
28
  }