@pi-r/gcp 0.6.1 → 0.6.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/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  ### @pi-r/gcp
2
2
 
3
+ https://e-mc.readthedocs.io/en/latest/cloud/gcp.html
4
+
3
5
  ### LICENSE
4
6
 
5
7
  MIT
package/client/index.js CHANGED
@@ -106,18 +106,18 @@ async function setUniformBucketLevel(bucket, enabled) {
106
106
  return bucket.setMetadata({ iamConfiguration: { uniformBucketLevelAccess: { enabled } } });
107
107
  }
108
108
  const getFirebasePackageName = (credential) => credential.apiKey && credential.authDomain ? 'firebase' : 'firebase-admin';
109
- const hasKeyFile = (credential) => !!(credential.keyFile || credential.keyFilename || process.env.GOOGLE_APPLICATION_CREDENTIALS);
109
+ const hasKeyFile = (credential) => (0, types_1.isString)(credential.keyFilename || credential.keyFile || process.env.GOOGLE_APPLICATION_CREDENTIALS) || (0, types_1.isObject)(credential.credentials);
110
110
  const isErrorConflict = (err) => (err instanceof Error) && err.code === 409;
111
111
  function isFirebaseApp(credential, store, admin) {
112
112
  return !!(credential.apiKey && credential.authDomain && (store || credential.databaseURL) || credential.product === 'firebase' && process.env.GOOGLE_APPLICATION_CREDENTIALS) && (!admin || !credential.admin);
113
113
  }
114
114
  exports.isFirebaseApp = isFirebaseApp;
115
115
  function validateStorage(credential) {
116
- return hasKeyFile(credential) || isFirebaseApp(credential, true);
116
+ return hasKeyFile(credential) || !!credential.authClient || isFirebaseApp(credential, true);
117
117
  }
118
118
  exports.validateStorage = validateStorage;
119
119
  function validateDatabase(credential, data) {
120
- return hasKeyFile(credential) && !!(data.product || data.database || data.table || data.query || data.id || data.keys || data.kind || data.options?.query) || isFirebaseApp(credential);
120
+ return (hasKeyFile(credential) || !!credential.authClient) && !!(data.product || data.database || data.table || data.query || data.id || data.keys || data.kind || data.options?.query) || isFirebaseApp(credential);
121
121
  }
122
122
  exports.validateDatabase = validateDatabase;
123
123
  function createStorageClient(credential, bucket) {
@@ -407,7 +407,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
407
407
  };
408
408
  for (let i = 0; i < length; ++i) {
409
409
  const item = batch[i];
410
- const { service, product, name, database, table, id, query, orderBy, limit = 0, update, ignoreCache } = item;
410
+ const { service, product, name, database, table, id, query, limit = 0, update, ignoreCache } = item;
411
411
  const useCache = caching && ignoreCache !== true;
412
412
  const getCache = (value) => {
413
413
  if (ignoreCache !== 1) {
@@ -422,6 +422,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
422
422
  closeClient();
423
423
  throw (0, util_1.formatError)(item, "Missing database query" /* ERR_DB.QUERY */);
424
424
  }
425
+ const orderBy = item.orderBy;
425
426
  if (useCache) {
426
427
  queryString = query + Module.asString(orderBy, true);
427
428
  if (!update && (rows = getCache(queryString))) {
@@ -435,30 +436,29 @@ async function executeBatchQuery(credential, batch, sessionKey) {
435
436
  if ((0, types_1.isArray)(orderBy)) {
436
437
  const sort = require('@firebase/database');
437
438
  for (const items of orderBy) {
438
- if (!(0, types_1.isArray)(items)) {
439
- continue;
440
- }
441
- const method = items[0];
442
- switch (method) {
443
- case 'orderByChild':
444
- constraints.push(sort.orderByChild(items[1]));
445
- break;
446
- case 'orderByKey':
447
- case 'orderByValue':
448
- case 'orderByPriority':
449
- constraints.push(sort[method]());
450
- break;
451
- case 'limitToFirst':
452
- case 'limitToLast':
453
- constraints.push(sort[method](limiter = +items[1]));
454
- break;
455
- case 'startAt':
456
- case 'startAfter':
457
- case 'endAt':
458
- case 'endBefore':
459
- case 'equalTo':
460
- constraints.push(sort[method](items[1], items[2]));
461
- break;
439
+ if ((0, types_1.isArray)(items)) {
440
+ const method = items[0];
441
+ switch (method) {
442
+ case 'orderByChild':
443
+ constraints.push(sort.orderByChild(items[1]));
444
+ break;
445
+ case 'orderByKey':
446
+ case 'orderByValue':
447
+ case 'orderByPriority':
448
+ constraints.push(sort[method]());
449
+ break;
450
+ case 'limitToFirst':
451
+ case 'limitToLast':
452
+ constraints.push(sort[method](limiter = +items[1]));
453
+ break;
454
+ case 'startAt':
455
+ case 'startAfter':
456
+ case 'endAt':
457
+ case 'endBefore':
458
+ case 'equalTo':
459
+ constraints.push(sort[method](items[1], items[2]));
460
+ break;
461
+ }
462
462
  }
463
463
  }
464
464
  }
@@ -496,10 +496,10 @@ async function executeBatchQuery(credential, batch, sessionKey) {
496
496
  if ((0, types_1.isPlainObject)(update)) {
497
497
  await db.table(table)[updateType === 1 ? 'insert' : updateType === 2 ? 'replace' : 'update'](update, options.tableUpdate);
498
498
  }
499
- const request = (0, types_1.isPlainObject)(query) ? query : { columns };
499
+ const request = (0, types_1.isPlainObject)(query) ? query : {};
500
500
  request.json = true;
501
- if ((0, types_1.isObject)(item.columns)) {
502
- request.columns = item.columns;
501
+ if ((0, types_1.isObject)(columns)) {
502
+ request.columns = columns;
503
503
  }
504
504
  request.keySet = { all: true };
505
505
  if (limit > 0) {
@@ -543,14 +543,14 @@ async function executeBatchQuery(credential, batch, sessionKey) {
543
543
  (options.gaxOptions || (options.gaxOptions = {})).maxResults = limit;
544
544
  }
545
545
  if (useCache) {
546
- queryString = name + '_' + table + '_' + (id ? id + ((0, types_1.isArray)(columns) ? '_' + Module.asString(columns, true) : '') : '') + Module.asString(options, true);
546
+ queryString = name + '_' + table + '_' + ((0, types_1.isString)(id) ? id + ((0, types_1.isArray)(columns) ? '_' + Module.asString(columns, true) : '') : '') + Module.asString(options, true);
547
547
  if (!(update && id) && (rows = getCache(queryString))) {
548
548
  result[i] = rows;
549
549
  continue;
550
550
  }
551
551
  }
552
552
  const db = createClient(item, 4 /* DB.BIGTABLE */).instance(name).table(table);
553
- if (id) {
553
+ if ((0, types_1.isString)(id)) {
554
554
  const row = db.row(id);
555
555
  if (update) {
556
556
  await row.save(update);
@@ -615,12 +615,12 @@ async function executeBatchQuery(credential, batch, sessionKey) {
615
615
  }
616
616
  else if (product === 'datastore' || !product && (item.keys || item.kind || query && !table && !id)) {
617
617
  const { keys, kind } = item;
618
- if (!keys && !Array.isArray(query)) {
618
+ if (!keys && !kind && !Array.isArray(query)) {
619
619
  closeClient();
620
620
  throw (0, util_1.formatError)(item, "Missing database query" /* ERR_DB.QUERY */);
621
621
  }
622
622
  if (useCache) {
623
- queryString = (keys ? Module.asString(keys, true) : Module.asString(query, true) + (kind || '')) + Module.asString(item.options, true) + (limit > 0 ? limit : 0);
623
+ queryString = (keys ? Cloud.asString(keys, true) : (kind ? (name ? name + '_' : '') + Cloud.asString(kind, true) : '') + (limit > 0 ? '_' + limit : 0) + Cloud.asString(query, true)) + Cloud.asString(item.options, true);
624
624
  if (!update && (rows = getCache(queryString))) {
625
625
  result[i] = rows;
626
626
  continue;
@@ -638,22 +638,26 @@ async function executeBatchQuery(credential, batch, sessionKey) {
638
638
  }
639
639
  }
640
640
  else {
641
- const task = db.createQuery(kind);
642
- for (const args of query) {
643
- if ((0, types_1.isArray)(args)) {
644
- const method = args[0];
645
- switch (method) {
646
- case 'end':
647
- case 'filter':
648
- case 'groupBy':
649
- case 'hasAncestor':
650
- case 'limit':
651
- case 'offset':
652
- case 'order':
653
- case 'select':
654
- case 'start':
655
- task[method].apply(task, args.slice(1)); // eslint-disable-line prefer-spread
656
- break;
641
+ const task = name && kind ? db.createQuery(name, kind) : db.createQuery(kind);
642
+ if ((0, types_1.isArray)(query)) {
643
+ for (const args of query) {
644
+ if ((0, types_1.isArray)(args)) {
645
+ const method = args[0];
646
+ switch (method) {
647
+ case 'end':
648
+ case 'filter':
649
+ case 'groupBy':
650
+ case 'limit':
651
+ case 'offset':
652
+ case 'order':
653
+ case 'select':
654
+ case 'start':
655
+ task[method].apply(task, args.slice(1)); // eslint-disable-line prefer-spread
656
+ break;
657
+ case 'hasAncestor':
658
+ task.hasAncestor(db.key(args[1]));
659
+ break;
660
+ }
657
661
  }
658
662
  }
659
663
  }
@@ -685,21 +689,32 @@ async function executeBatchQuery(credential, batch, sessionKey) {
685
689
  if (id) {
686
690
  if (useCache) {
687
691
  queryString = table + '_' + id;
688
- if (!update && (rows = getCache(queryString))) {
692
+ if ((!update || Array.isArray(id)) && (rows = getCache(queryString))) {
689
693
  result[i] = rows;
690
694
  continue;
691
695
  }
692
696
  }
693
- const db = createClient(item, 3 /* DB.FIRESTORE */).collection(table).doc(id);
694
- if ((0, types_1.isPlainObject)(update)) {
695
- await db.update(update, { exists: true });
697
+ const db = createClient(item, 3 /* DB.FIRESTORE */);
698
+ if (Array.isArray(id)) {
699
+ const items = id.map(ref => db.doc(table + '/' + ref));
700
+ if ((0, types_1.isPlainObject)(item.options)) {
701
+ items.push(item.options);
702
+ }
703
+ rows = await db.getAll(...items);
696
704
  }
697
- const data = (await db.get()).data();
698
- if (data) {
699
- rows = [data];
705
+ else {
706
+ const doc = db.collection(table).doc(id);
707
+ if ((0, types_1.isPlainObject)(update)) {
708
+ await doc.update(update, { exists: true });
709
+ }
710
+ const data = (await doc.get()).data();
711
+ if (data) {
712
+ rows = [data];
713
+ }
700
714
  }
701
715
  }
702
716
  else if (Array.isArray(query)) {
717
+ const orderBy = item.orderBy;
703
718
  if (useCache && (rows = getCache(queryString = table + '_' + Module.asString(query, true) + Module.asString(orderBy, true) + (limit > 0 ? limit : '')))) {
704
719
  result[i] = rows;
705
720
  continue;
@@ -893,5 +908,6 @@ function getProjectId(credential) {
893
908
  }
894
909
  }
895
910
  }
911
+ return credential.projectId;
896
912
  }
897
913
  exports.getProjectId = getProjectId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/gcp",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "description": "Google (GCP) cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "publishConfig": {
@@ -20,10 +20,10 @@
20
20
  "license": "MIT",
21
21
  "homepage": "https://github.com/anpham6/pi-r#readme",
22
22
  "dependencies": {
23
- "@e-mc/cloud": "^0.8.1",
24
- "@e-mc/module": "^0.8.1",
25
- "@e-mc/types": "^0.8.1",
26
- "@google-cloud/firestore": "^7.1.0",
23
+ "@e-mc/cloud": "^0.8.2",
24
+ "@e-mc/module": "^0.8.2",
25
+ "@e-mc/types": "^0.8.2",
26
+ "@google-cloud/firestore": "^7.2.0",
27
27
  "@google-cloud/storage": "^7.7.0"
28
28
  }
29
29
  }
package/upload/index.js CHANGED
@@ -184,6 +184,9 @@ function upload(credential, service = 'gcp') {
184
184
  if (first) {
185
185
  uploadResponse(err);
186
186
  }
187
+ else {
188
+ addLog(err);
189
+ }
187
190
  });
188
191
  }
189
192
  }