@pi-r/gcp 0.7.0 → 0.7.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.
Files changed (2) hide show
  1. package/client/index.js +32 -22
  2. package/package.json +6 -6
package/client/index.js CHANGED
@@ -102,6 +102,7 @@ async function updatePredefinedAcl(acl, values, metadata = {}) {
102
102
  async function setUniformBucketLevel(bucket, enabled) {
103
103
  return bucket.setMetadata({ iamConfiguration: { uniformBucketLevelAccess: { enabled } } });
104
104
  }
105
+ const asFieldPath = (params) => Array.isArray(params) && params.every(item => typeof item === 'string') ? new firestore_1.FieldPath(...params) : params;
105
106
  const getFirebasePackageName = (credential) => credential.apiKey && credential.authDomain ? 'firebase' : 'firebase-admin';
106
107
  const isErrorConflict = (err) => (err instanceof Error) && err.code === 409;
107
108
  const hasGoogleAuth = (credential) => (0, types_1.isString)(credential.keyFilename || credential.keyFile || process.env.GOOGLE_APPLICATION_CREDENTIALS) || (0, types_1.isObject)(credential.credentials) || !!credential.authClient;
@@ -513,7 +514,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
513
514
  }
514
515
  const { updateType, options = {} } = item;
515
516
  if (useCache) {
516
- queryString = name + '_' + database + '_' + (table ? table + (columns ? Cloud.asString(columns, true) : '') : '') + Cloud.asString(query, true) + (limit > 0 ? limit : '');
517
+ queryString = name + '_' + database + (table ? '_' + table + (columns ? Cloud.asString(columns, true) : '') : '') + '_' + Cloud.asString(query, true) + (limit > 0 ? limit : '');
517
518
  if (!update && (rows = getCache(queryString))) {
518
519
  result[i] = rows;
519
520
  continue;
@@ -547,9 +548,9 @@ async function executeBatchQuery(credential, batch, sessionKey) {
547
548
  .then(async () => {
548
549
  transaction.commit()
549
550
  .then(() => resolve())
550
- .catch(reject);
551
+ .catch((error) => reject(error));
551
552
  })
552
- .catch(reject);
553
+ .catch((error) => reject(error));
553
554
  }
554
555
  });
555
556
  });
@@ -575,7 +576,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
575
576
  (options.gaxOptions ||= {}).maxResults = limit;
576
577
  }
577
578
  if (useCache) {
578
- queryString = name + '_' + table + '_' + ((0, types_1.isString)(id) ? id + ((0, types_1.isArray)(columns) ? '_' + Cloud.asString(columns, true) : '') : '') + Cloud.asString(options, true);
579
+ queryString = name + '_' + table + ((0, types_1.isString)(id) ? '_' + id + ((0, types_1.isArray)(columns) ? '_' + Cloud.asString(columns, true) : '') : '') + Cloud.asString(options, true);
579
580
  if (!(update && id) && (rows = getCache(queryString))) {
580
581
  result[i] = rows;
581
582
  continue;
@@ -646,13 +647,13 @@ async function executeBatchQuery(credential, batch, sessionKey) {
646
647
  [rows] = await job.getQueryResults();
647
648
  }
648
649
  else if (product === "datastore" || !product && (item.keys || item.kind || query && !table && !id)) {
649
- const { keys, kind } = item;
650
+ const { keys, kind, options } = item;
650
651
  if (!keys && !kind && !Array.isArray(query)) {
651
652
  closeClient();
652
653
  throw (0, util_1.formatError)(item, "Missing database query");
653
654
  }
654
655
  if (useCache) {
655
- 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);
656
+ queryString = (keys ? Cloud.asString(keys, true) : (kind ? (name ? '_' + name : '') + '_' + Cloud.asString(kind, true) : '') + '_' + Cloud.asString(query, true)) + (limit > 0 ? '_' + limit : 0) + (options ? Cloud.asString(options, true) : '');
656
657
  if (!update && (rows = getCache(queryString))) {
657
658
  result[i] = rows;
658
659
  continue;
@@ -664,7 +665,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
664
665
  if ((0, types_1.isPlainObject)(update)) {
665
666
  await db.save({ key: items, data: update });
666
667
  }
667
- rows = await db.get(items, item.options);
668
+ rows = await db.get(items, options);
668
669
  if (limit > 0 && rows.length > limit) {
669
670
  rows = rows.slice(0, limit);
670
671
  }
@@ -698,7 +699,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
698
699
  }
699
700
  if ((0, types_1.isPlainObject)(update)) {
700
701
  await new Promise((resolve, reject) => {
701
- db.runQuery(task.select('__key__'), item.options).then(entities => {
702
+ db.runQuery(task.select('__key__'), options).then(entities => {
702
703
  if ((0, types_1.isArray)(entities)) {
703
704
  const items = entities.map((entity) => db.key(entity[db.KEY]));
704
705
  db.save({ key: items, data: update }).then(() => resolve()).catch((err) => reject(err));
@@ -710,7 +711,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
710
711
  .catch((err) => reject(err));
711
712
  });
712
713
  }
713
- [rows] = await db.runQuery(task, item.options);
714
+ [rows] = await db.runQuery(task, options);
714
715
  }
715
716
  }
716
717
  else if (product === "firestore" || !product && table) {
@@ -719,8 +720,9 @@ async function executeBatchQuery(credential, batch, sessionKey) {
719
720
  throw (0, util_1.formatError)(item, "Missing database table");
720
721
  }
721
722
  if (id) {
723
+ const options = Array.isArray(id) ? item.options : null;
722
724
  if (useCache) {
723
- queryString = table + '_' + id;
725
+ queryString = table + '_' + id + (options ? Cloud.asString(options, true) : '');
724
726
  if ((!update || Array.isArray(id)) && (rows = getCache(queryString))) {
725
727
  result[i] = rows;
726
728
  continue;
@@ -729,8 +731,8 @@ async function executeBatchQuery(credential, batch, sessionKey) {
729
731
  const db = createClient(item, 3);
730
732
  if (Array.isArray(id)) {
731
733
  const items = id.map(ref => db.doc(table + '/' + ref));
732
- if ((0, types_1.isPlainObject)(item.options)) {
733
- items.push(item.options);
734
+ if (options) {
735
+ items.push(options);
734
736
  }
735
737
  rows = (await db.getAll(...items)).filter(value => value.exists).map(value => value.data());
736
738
  }
@@ -774,7 +776,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
774
776
  }
775
777
  else if (Array.isArray(query)) {
776
778
  const orderBy = item.orderBy;
777
- if (useCache && (rows = getCache(queryString = table + '_' + Cloud.asString(query, true) + Cloud.asString(orderBy, true) + (limit > 0 ? limit : '')))) {
779
+ if (useCache && (rows = getCache(queryString = table + '_' + Cloud.asString(query, true) + '_' + Cloud.asString(orderBy, true) + (limit > 0 ? '_' + limit : '')))) {
778
780
  result[i] = rows;
779
781
  continue;
780
782
  }
@@ -786,33 +788,41 @@ async function executeBatchQuery(credential, batch, sessionKey) {
786
788
  switch (method) {
787
789
  case 'whereAnd':
788
790
  case 'whereOr':
789
- if (params.every(p => (0, types_1.isArray)(p))) {
790
- db = db.where(firestore_1.Filter[method === 'whereAnd' ? 'and' : 'or'](...params.map((p) => firestore_1.Filter.where(p[0], p[1], p[2]))));
791
- }
792
- else {
791
+ if (!params.every(p => (0, types_1.isArray)(p))) {
793
792
  throw (0, types_1.errorMessage)(method, 'Conditionals are not in array format', Cloud.asString(params));
794
793
  }
794
+ db = db.where(firestore_1.Filter[method === 'whereAnd' ? 'and' : 'or'](...params.map((p) => firestore_1.Filter.where(p[0], p[1], p[2]))));
795
+ break;
796
+ case 'where':
797
+ case 'orderBy':
798
+ params[0] = asFieldPath(params[0]);
799
+ db = db[method].apply(db, params);
800
+ case 'select':
801
+ params.forEach((field, index) => {
802
+ params[index] = asFieldPath(field);
803
+ });
804
+ db = db.select.apply(db, params);
795
805
  break;
796
806
  case 'endAt':
797
807
  case 'endBefore':
798
808
  case 'limit':
799
809
  case 'limitToLast':
800
810
  case 'offset':
801
- case 'orderBy':
802
- case 'select':
803
811
  case 'startAfter':
804
812
  case 'startAt':
805
- case 'where':
806
813
  case 'withConverter':
807
814
  db = db[method].apply(db, params);
808
815
  break;
809
816
  }
810
817
  }
811
818
  }
812
- if ((0, types_1.isArray)(orderBy)) {
819
+ if (Array.isArray(orderBy)) {
813
820
  for (const order of orderBy) {
814
821
  if ((0, types_1.isArray)(order)) {
815
- db = db.orderBy(order[0], order[1]);
822
+ db = db.orderBy(asFieldPath(order[0]), order[1]);
823
+ }
824
+ else if ((0, types_1.isString)(order)) {
825
+ db = db.orderBy(order);
816
826
  }
817
827
  }
818
828
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/gcp",
3
- "version": "0.7.0",
3
+ "version": "0.7.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.9.0",
24
- "@e-mc/module": "^0.9.0",
25
- "@e-mc/types": "^0.9.0",
26
- "@google-cloud/firestore": "^7.6.0",
27
- "@google-cloud/storage": "^7.10.1"
23
+ "@e-mc/cloud": "^0.9.4",
24
+ "@e-mc/module": "^0.9.4",
25
+ "@e-mc/types": "^0.9.4",
26
+ "@google-cloud/firestore": "^7.8.0",
27
+ "@google-cloud/storage": "^7.11.2"
28
28
  }
29
29
  }