Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions express/storage/multi_level_storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
22 changes: 16 additions & 6 deletions express/storage/multi_level_storage/multi_level_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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 => {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading