diff --git a/express/storage/multi_level_storage/README.md b/express/storage/multi_level_storage/README.md index a7e0350b..5befd6b8 100644 --- a/express/storage/multi_level_storage/README.md +++ b/express/storage/multi_level_storage/README.md @@ -12,7 +12,7 @@ MultiLevelStorage is a Node.js library that provides a multi-level caching mecha ## Prerequisites - A running Redis instance. -- A connected Mongoose instance for MongoDB. +- A connected **Mongoose connection object** (recommended) for MongoDB. (You can obtain this from `mongoose.connection` after connecting.) ## Usage @@ -25,7 +25,8 @@ const { MultiLevelStorage } = require('@gofynd/fdk-extension-javascript/express/ ``` ## Notes -> - 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). +> - **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. +> - Ensure your MongoDB collection has a TTL index on the `expireAt` field (see Index Creation below). ### Initialize Connections @@ -36,8 +37,8 @@ const redisClient = new Redis(); // Connect to MongoDB await mongoose.connect('mongodb://localhost:27017/yourdb'); -// Initialize MultiLevelStorage -const storage = new MultiLevelStorage('app_prefix_', redisClient, mongoose, { collectionName: 'collection_name', autoIndex: true }); +// Use the mongoose connection object (recommended) +const storage = new MultiLevelStorage('app_prefix_', redisClient, mongoose.connection, { collectionName: 'collection_name', autoIndex: true }); ``` ### Options @@ -70,7 +71,7 @@ await storage.del('user:123'); ## Index Creation -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. +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. ### Manual Index Creation diff --git a/express/storage/multi_level_storage/multi_level_storage.js b/express/storage/multi_level_storage/multi_level_storage.js index 1fbee059..71ceedf7 100644 --- a/express/storage/multi_level_storage/multi_level_storage.js +++ b/express/storage/multi_level_storage/multi_level_storage.js @@ -39,16 +39,26 @@ class MultiLevelStorage extends BaseStorage { throw new StorageConnectionError('Invalid ioredis instance provided.'); } - if (typeof mongooseInstance.model !== 'function') { - throw new StorageConnectionError('Invalid Mongoose instance provided.'); - } - this.redis = redisInstance; this.mongoose = mongooseInstance; const collectionName = options.collectionName || 'fdk_ext_acc_tokens'; const autoIndex = options.autoIndex !== undefined ? options.autoIndex : true; - const schema = new this.mongoose.Schema({ + // Support both mongoose main object and connection object + let SchemaCtor, modelFn; + if (this.mongoose.base && typeof this.mongoose.base.Schema === 'function') { + // It's a connection object + SchemaCtor = this.mongoose.base.Schema; + modelFn = this.mongoose.model.bind(this.mongoose); + } else if (typeof this.mongoose.Schema === 'function') { + // It's the main mongoose object + SchemaCtor = this.mongoose.Schema; + modelFn = this.mongoose.model.bind(this.mongoose); + } else { + throw new StorageConnectionError('Invalid Mongoose instance provided.'); + } + + const schema = new SchemaCtor({ key: { type: String, required: true, unique: true }, value: { type: Object, required: true }, updatedAt: { type: Date, default: Date.now }, @@ -59,7 +69,7 @@ class MultiLevelStorage extends BaseStorage { schema.index({ expireAt: 1 }, { expireAfterSeconds: 0 }); } - this.model = this.mongoose.model(collectionName, schema); + this.model = modelFn(collectionName, schema); if (autoIndex) { this.model.createIndexes().catch(err => { diff --git a/package-lock.json b/package-lock.json index 6d07fcde..28423167 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@gofynd/fdk-extension-javascript", - "version": "1.1.4", + "version": "1.1.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@gofynd/fdk-extension-javascript", - "version": "1.1.4", + "version": "1.1.5", "license": "ISC", "dependencies": { "axios": "^1.6.4", diff --git a/package.json b/package.json index bd08daec..29fb26d6 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ }, "name": "@gofynd/fdk-extension-javascript", "description": "FDK Extension Helper Library", - "version": "1.1.4", + "version": "1.1.5", "main": "index.js", "directories": { "example": "examples"