Skip to content

Commit 541fe36

Browse files
vivek-gofyndvivek-gofynd
andauthored
Mongoose connection object support (#82)
* Mongoose connection object support * Updated readme file * 1.1.5 --------- Co-authored-by: vivek-gofynd <[email protected]>
1 parent 3d54fcd commit 541fe36

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

express/storage/multi_level_storage/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ MultiLevelStorage is a Node.js library that provides a multi-level caching mecha
1212
## Prerequisites
1313

1414
- A running Redis instance.
15-
- A connected Mongoose instance for MongoDB.
15+
- A connected **Mongoose connection object** (recommended) for MongoDB. (You can obtain this from `mongoose.connection` after connecting.)
1616

1717
## Usage
1818

@@ -25,7 +25,8 @@ const { MultiLevelStorage } = require('@gofynd/fdk-extension-javascript/express/
2525
```
2626

2727
## Notes
28-
> - Ensure `mongoose` is initialized with `autoIndex: true` **or** manually create a TTL index on the `expireAt` field of the collection (default: `MultiLevelStorage` or your custom collection name).
28+
> - **Recommended:** Pass the `mongoose.connection` object (the Mongoose connection object) to MultiLevelStorage for best compatibility, especially in applications with multiple connections or advanced setups. Passing the main mongoose object is still supported for backward compatibility, but using the connection object is preferred.
29+
> - Ensure your MongoDB collection has a TTL index on the `expireAt` field (see Index Creation below).
2930
3031
### Initialize Connections
3132

@@ -36,8 +37,8 @@ const redisClient = new Redis();
3637
// Connect to MongoDB
3738
await mongoose.connect('mongodb://localhost:27017/yourdb');
3839

39-
// Initialize MultiLevelStorage
40-
const storage = new MultiLevelStorage('app_prefix_', redisClient, mongoose, { collectionName: 'collection_name', autoIndex: true });
40+
// Use the mongoose connection object (recommended)
41+
const storage = new MultiLevelStorage('app_prefix_', redisClient, mongoose.connection, { collectionName: 'collection_name', autoIndex: true });
4142
```
4243

4344
### Options
@@ -70,7 +71,7 @@ await storage.del('user:123');
7071

7172
## Index Creation
7273

73-
If `autoIndex` is set to `true` and the MongoDB instance is connected to a primary node, the TTL index will be created automatically on the `expireAt` field. If connected to a secondary node, you will need to create the index manually.
74+
If `autoIndex` is set to `true` and the MongoDB connection is to a primary node, the TTL index will be created automatically on the `expireAt` field. If connected to a secondary node, you will need to create the index manually.
7475

7576
### Manual Index Creation
7677

express/storage/multi_level_storage/multi_level_storage.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,26 @@ class MultiLevelStorage extends BaseStorage {
3939
throw new StorageConnectionError('Invalid ioredis instance provided.');
4040
}
4141

42-
if (typeof mongooseInstance.model !== 'function') {
43-
throw new StorageConnectionError('Invalid Mongoose instance provided.');
44-
}
45-
4642
this.redis = redisInstance;
4743
this.mongoose = mongooseInstance;
4844
const collectionName = options.collectionName || 'fdk_ext_acc_tokens';
4945
const autoIndex = options.autoIndex !== undefined ? options.autoIndex : true;
5046

51-
const schema = new this.mongoose.Schema({
47+
// Support both mongoose main object and connection object
48+
let SchemaCtor, modelFn;
49+
if (this.mongoose.base && typeof this.mongoose.base.Schema === 'function') {
50+
// It's a connection object
51+
SchemaCtor = this.mongoose.base.Schema;
52+
modelFn = this.mongoose.model.bind(this.mongoose);
53+
} else if (typeof this.mongoose.Schema === 'function') {
54+
// It's the main mongoose object
55+
SchemaCtor = this.mongoose.Schema;
56+
modelFn = this.mongoose.model.bind(this.mongoose);
57+
} else {
58+
throw new StorageConnectionError('Invalid Mongoose instance provided.');
59+
}
60+
61+
const schema = new SchemaCtor({
5262
key: { type: String, required: true, unique: true },
5363
value: { type: Object, required: true },
5464
updatedAt: { type: Date, default: Date.now },
@@ -59,7 +69,7 @@ class MultiLevelStorage extends BaseStorage {
5969
schema.index({ expireAt: 1 }, { expireAfterSeconds: 0 });
6070
}
6171

62-
this.model = this.mongoose.model(collectionName, schema);
72+
this.model = modelFn(collectionName, schema);
6373

6474
if (autoIndex) {
6575
this.model.createIndexes().catch(err => {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
},
5858
"name": "@gofynd/fdk-extension-javascript",
5959
"description": "FDK Extension Helper Library",
60-
"version": "1.1.4",
60+
"version": "1.1.5",
6161
"main": "index.js",
6262
"directories": {
6363
"example": "examples"

0 commit comments

Comments
 (0)