gcf-common-lib 0.23.8 → 0.23.10

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gcf-common-lib",
3
3
  "description": "",
4
- "version": "0.23.8",
4
+ "version": "0.23.10",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "branches": [
@@ -21,7 +21,6 @@
21
21
  "@google-cloud/pubsub": "^3.2.1",
22
22
  "@google-cloud/secret-manager": "^4.2.0",
23
23
  "@google-cloud/storage": "^6.9.0",
24
- "bluebird": "^3.7.2",
25
24
  "lodash": "^4.17.21",
26
25
  "moment": "^2.29.4",
27
26
  "mongodb": "^4.14.0",
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MongoHelper = void 0;
13
+ class MongoHelper {
14
+ static collectionExists(client, name) {
15
+ return __awaiter(this, void 0, void 0, function* () {
16
+ const db = client.db();
17
+ const collections = yield db.listCollections({ name }, { nameOnly: true }).toArray();
18
+ return collections.length > 0;
19
+ });
20
+ }
21
+ static collectionSafeGet(client, name, options, afterCreate) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ const db = client.db();
24
+ let coll;
25
+ if (yield this.collectionExists(client, name)) {
26
+ coll = db.collection(name);
27
+ }
28
+ else {
29
+ coll = yield db.createCollection(name, options);
30
+ afterCreate && (yield afterCreate(coll));
31
+ }
32
+ return coll;
33
+ });
34
+ }
35
+ }
36
+ exports.MongoHelper = MongoHelper;
@@ -0,0 +1,30 @@
1
+ import {Collection, CreateCollectionOptions, MongoClient} from "mongodb";
2
+ import {Document} from "bson";
3
+
4
+ export class MongoHelper {
5
+ static async collectionExists(client: MongoClient, name: string) {
6
+ const db = client.db();
7
+ const collections = await db.listCollections({name}, {nameOnly: true}).toArray();
8
+ return collections.length > 0;
9
+ }
10
+
11
+ static async collectionSafeGet<TSchema extends Document = Document>(
12
+ client: MongoClient,
13
+ name: string,
14
+ options?: CreateCollectionOptions,
15
+ afterCreate?: (col: Collection<TSchema>) => Promise<void>,
16
+ ) {
17
+ const db = client.db();
18
+ let coll: Collection<TSchema>;
19
+
20
+ if (await this.collectionExists(client, name)) {
21
+ coll = db.collection<TSchema>(name);
22
+ } else {
23
+ coll = await db.createCollection<TSchema>(name, options);
24
+ afterCreate && (await afterCreate(coll));
25
+ }
26
+
27
+ return coll;
28
+ }
29
+
30
+ }
package/src/mongo-lock.js CHANGED
@@ -16,13 +16,14 @@ exports.MongoLock = void 0;
16
16
  const moment_1 = __importDefault(require("moment"));
17
17
  const noop_1 = __importDefault(require("lodash/noop"));
18
18
  const rxjs_1 = require("rxjs");
19
+ const mongo_helper_1 = require("./mongo-helper");
19
20
  class MongoLock {
20
21
  constructor(client) {
21
22
  this.client = client;
22
23
  }
23
24
  getCollection() {
24
25
  return __awaiter(this, void 0, void 0, function* () {
25
- const locksColl = this.client.db().collection('locks');
26
+ const locksColl = yield mongo_helper_1.MongoHelper.collectionSafeGet(this.client, 'locks');
26
27
  yield locksColl.indexExists('expiresAt')
27
28
  .then((exists) => __awaiter(this, void 0, void 0, function* () {
28
29
  if (!exists)
package/src/mongo-lock.ts CHANGED
@@ -2,6 +2,7 @@ import {MongoClient, MongoError} from "mongodb";
2
2
  import moment from "moment";
3
3
  import noop from "lodash/noop";
4
4
  import {defer, from, identity, lastValueFrom, map, retry, switchMap} from "rxjs";
5
+ import {MongoHelper} from "./mongo-helper";
5
6
 
6
7
  export class MongoLock {
7
8
 
@@ -9,7 +10,7 @@ export class MongoLock {
9
10
  }
10
11
 
11
12
  async getCollection() {
12
- const locksColl = this.client.db().collection('locks');
13
+ const locksColl = await MongoHelper.collectionSafeGet(this.client, 'locks' );
13
14
  await locksColl.indexExists('expiresAt')
14
15
  .then(async exists => {
15
16
  if (!exists) await locksColl.createIndex({"expiresAt": 1}, {expireAfterSeconds: 0});