opticedge-cloud-utils 1.1.12 → 1.1.13

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.
@@ -1,4 +1,5 @@
1
- import { Collection, Document } from 'mongodb';
1
+ import { Db, Collection, Document } from 'mongodb';
2
2
  export declare function connectToMongo(projectId: string, uriSecret: string, dbName: string): Promise<void>;
3
3
  export declare function connectToMongoWithUri(uri: string, dbName: string): Promise<void>;
4
+ export declare function getDb(): Db;
4
5
  export declare function getCollection<T extends Document = Document>(name: string): Collection<T>;
package/dist/db/mongo.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.connectToMongo = connectToMongo;
4
4
  exports.connectToMongoWithUri = connectToMongoWithUri;
5
+ exports.getDb = getDb;
5
6
  exports.getCollection = getCollection;
6
7
  const mongodb_1 = require("mongodb");
7
8
  const secrets_1 = require("../secrets");
@@ -33,6 +34,11 @@ async function connectToMongoWithUri(uri, dbName) {
33
34
  }
34
35
  await connectPromise;
35
36
  }
37
+ function getDb() {
38
+ if (!db)
39
+ throw new Error('Mongo not initialized. Call connectToMongo(...) first.');
40
+ return db;
41
+ }
36
42
  function getCollection(name) {
37
43
  if (!db)
38
44
  throw new Error('Mongo not initialized');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opticedge-cloud-utils",
3
- "version": "1.1.12",
3
+ "version": "1.1.13",
4
4
  "description": "Common utilities for cloud functions",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/db/mongo.ts CHANGED
@@ -34,6 +34,11 @@ export async function connectToMongoWithUri(uri: string, dbName: string) {
34
34
  await connectPromise
35
35
  }
36
36
 
37
+ export function getDb(): Db {
38
+ if (!db) throw new Error('Mongo not initialized. Call connectToMongo(...) first.')
39
+ return db
40
+ }
41
+
37
42
  export function getCollection<T extends Document = Document>(name: string): Collection<T> {
38
43
  if (!db) throw new Error('Mongo not initialized')
39
44
  return db.collection<T>(name)
@@ -1,4 +1,4 @@
1
- import { connectToMongo, connectToMongoWithUri, getCollection } from '../../src/db/mongo'
1
+ import { connectToMongo, connectToMongoWithUri, getCollection, getDb } from '../../src/db/mongo'
2
2
  import { MongoClient, Db } from 'mongodb'
3
3
  import * as secretsModule from '../../src/secrets'
4
4
 
@@ -37,18 +37,27 @@ describe('Mongo Utils', () => {
37
37
  expect(mockDb.collection).toHaveBeenCalledWith('users')
38
38
  })
39
39
 
40
- it('should throw error if db is not initialized', () => {
40
+ it('getDb should return the initialized Db instance', async () => {
41
+ await connectToMongo('test-project', 'mongo-uri-secret', 'test-db')
42
+ const got = getDb()
43
+ expect(got).toBe(mockDb)
44
+ })
45
+
46
+ it('should throw error if db is not initialized (getCollection/getDb)', () => {
41
47
  jest.isolateModules(() => {
48
+ // require a fresh copy of the module so module-level state is uninitialized
42
49
  // eslint-disable-next-line @typescript-eslint/no-require-imports
43
- const { getCollection } = require('../../src/db/mongo')
44
- expect(() => getCollection('users')).toThrow('Mongo not initialized')
50
+ const mod = require('../../src/db/mongo')
51
+ expect(() => mod.getCollection('users')).toThrow(/Mongo not initialized/)
52
+ expect(() => mod.getDb()).toThrow(/Mongo not initialized/)
45
53
  })
46
54
  })
47
55
  })
48
56
 
49
57
  describe('Mongo Utils 2', () => {
50
- it('should connect to MongoDB and store client/db', async () => {
51
- jest.isolateModules(async () => {
58
+ it('should connect to MongoDB and store client/db with URI helper', async () => {
59
+ // isolate modules so connect state is fresh for this test
60
+ await jest.isolateModulesAsync(async () => {
52
61
  // eslint-disable-next-line @typescript-eslint/no-require-imports
53
62
  const { connectToMongoWithUri } = require('../../src/db/mongo')
54
63
  await connectToMongoWithUri('mongo-uri', 'test-db')
@@ -58,10 +67,16 @@ describe('Mongo Utils 2', () => {
58
67
  })
59
68
  })
60
69
 
61
- it('should return a collection when db is initialized', async () => {
70
+ it('should return a collection when db is initialized (connectToMongoWithUri)', async () => {
62
71
  await connectToMongoWithUri('mongo-uri', 'test-db')
63
72
  getCollection('users')
64
73
 
65
74
  expect(mockDb.collection).toHaveBeenCalledWith('users')
66
75
  })
76
+
77
+ it('getDb should return the initialized Db instance after connectToMongoWithUri', async () => {
78
+ await connectToMongoWithUri('mongo-uri', 'test-db')
79
+ const got = getDb()
80
+ expect(got).toBe(mockDb)
81
+ })
67
82
  })