@pi-r/gcp 0.6.1 → 0.6.3

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
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getProjectId = exports.createBucketRequest = exports.setPredefinedAcl = exports.setPublicRead = exports.executeBatchQuery = exports.executeQuery = exports.deleteObjectsV2 = exports.deleteObjects = exports.setBucketWebsite = exports.setBucketPolicy = exports.createBucketV2 = exports.createBucket = exports.createDatabaseClient = exports.createStorageClient = exports.validateDatabase = exports.validateStorage = exports.isFirebaseApp = void 0;
4
4
  const path = require("path");
5
5
  const storage = require("@google-cloud/storage");
6
- const firestore = require("@google-cloud/firestore");
6
+ const firestore_1 = require("@google-cloud/firestore");
7
7
  const util_1 = require("@e-mc/cloud/util");
8
8
  const types_1 = require("@e-mc/types");
9
9
  const Module = require("@e-mc/module");
@@ -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) {
@@ -135,7 +135,7 @@ function createStorageClient(credential, bucket) {
135
135
  throw err;
136
136
  }
137
137
  }
138
- getProjectId(credential);
138
+ getProjectId(credential, false);
139
139
  return new storage.Storage(credential);
140
140
  }
141
141
  exports.createStorageClient = createStorageClient;
@@ -144,13 +144,13 @@ function createDatabaseClient(credential, data) {
144
144
  try {
145
145
  let product, name, database, table, query;
146
146
  if (data) {
147
- ({ product, name, table, query } = data);
147
+ ({ product, name, table, query, database } = data);
148
148
  }
149
149
  if (product === 'firebase' || isFirebaseApp(credential)) {
150
150
  packageName = getFirebasePackageName(credential);
151
151
  return getApp(credential, true);
152
152
  }
153
- getProjectId(credential);
153
+ getProjectId(credential, true);
154
154
  if (data) {
155
155
  if (product === 'spanner' || !product && name && database && (query || table)) {
156
156
  const { Spanner } = require(packageName = '@google-cloud/spanner');
@@ -174,7 +174,7 @@ function createDatabaseClient(credential, data) {
174
174
  this.checkPackage(err, packageName, { passThrough: true });
175
175
  throw err;
176
176
  }
177
- return new firestore.Firestore(credential);
177
+ return new firestore_1.Firestore(credential);
178
178
  }
179
179
  exports.createDatabaseClient = createDatabaseClient;
180
180
  async function createBucket(credential, bucket, publicRead, service = 'gcp') {
@@ -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)).filter(value => value.exists).map(value => value.data());
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;
@@ -708,7 +723,17 @@ async function executeBatchQuery(credential, batch, sessionKey) {
708
723
  for (const args of query) {
709
724
  if ((0, types_1.isArray)(args)) {
710
725
  const method = args[0];
726
+ const params = args.slice(1);
711
727
  switch (method) {
728
+ case 'whereAnd':
729
+ case 'whereOr':
730
+ if (params.every(p => Array.isArray(p))) {
731
+ db = db.where(firestore_1.Filter[method === 'whereAnd' ? 'and' : 'or'](...params.map((p) => firestore_1.Filter.where(p[0], p[1], p[2]))));
732
+ }
733
+ else {
734
+ throw (0, types_1.errorMessage)(method, 'Conditionals are not in array format', Cloud.asString(params));
735
+ }
736
+ break;
712
737
  case 'endAt':
713
738
  case 'endBefore':
714
739
  case 'limit':
@@ -720,7 +745,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
720
745
  case 'startAt':
721
746
  case 'where':
722
747
  case 'withConverter':
723
- db = db[method].apply(db, args.slice(1)); // eslint-disable-line prefer-spread
748
+ db = db[method].apply(db, params); // eslint-disable-line prefer-spread
724
749
  break;
725
750
  }
726
751
  }
@@ -873,19 +898,24 @@ function createBucketRequest(credential) {
873
898
  return options;
874
899
  }
875
900
  exports.createBucketRequest = createBucketRequest;
876
- function getProjectId(credential) {
901
+ function getProjectId(credential, cache) {
902
+ let projectId = credential.projectId;
903
+ if (!cache && projectId) {
904
+ return projectId;
905
+ }
877
906
  let pathname = credential.keyFilename || credential.keyFile;
878
- if (pathname && Module.isPath(pathname = path.resolve(pathname)) || (pathname = process.env.GOOGLE_APPLICATION_CREDENTIALS) && Module.isPath(pathname = path.resolve(pathname))) {
907
+ if (pathname && Cloud.isPath(pathname = path.resolve(pathname)) || (pathname = process.env.GOOGLE_APPLICATION_CREDENTIALS) && Cloud.isPath(pathname = path.resolve(pathname))) {
879
908
  if (credential.keyFilename) {
880
909
  credential.keyFilename = pathname;
881
910
  }
882
911
  else {
883
912
  credential.keyFile = pathname;
884
913
  }
885
- if (!credential.projectId) {
914
+ if (!projectId) {
886
915
  try {
887
- const projectId = require(pathname).project_id;
888
- if ((0, types_1.isString)(projectId)) {
916
+ const id = require(pathname).project_id;
917
+ if ((0, types_1.isString)(id)) {
918
+ projectId = id;
889
919
  credential.projectId = projectId;
890
920
  }
891
921
  }
@@ -893,5 +923,6 @@ function getProjectId(credential) {
893
923
  }
894
924
  }
895
925
  }
926
+ return projectId;
896
927
  }
897
928
  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.3",
4
4
  "description": "Google (GCP) cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "publishConfig": {
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/anpham6/pi-r.git",
11
+ "url": "git+https://github.com/anpham6/pi-r.git",
12
12
  "directory": "src/cloud/gcp"
13
13
  },
14
14
  "keywords": [
@@ -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.3",
24
+ "@e-mc/module": "^0.8.3",
25
+ "@e-mc/types": "^0.8.3",
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
  }