Skip to content

Commit

Permalink
Readme updated for custom storage (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
divyanshusinghh authored Oct 16, 2024
1 parent 498fe20 commit 841dcf2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,6 @@ After webhook config is passed to setupFdk whenever extension is launched to any
> Any update to webhook config will not automatically update subscriber data on Fynd Platform for a company until extension is opened atleast once after the update.
Other way to update webhook config manually for a company is to call `syncEvents` function of webhookRegistery.

# [Custom storage class](/express/storage/README.md)
The FDK Extension JavaScript library provides built-in support for SQLite, Redis and in-memory storage options as default choices for session data storage. However, if you require a different storage option, this readme will guide you through the process of implementing a custom storage class.
88 changes: 88 additions & 0 deletions express/storage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#### How to create a custom storage class?
Custom storage classes allow you to implement extension storage in your preferred database. To achieve this, you are required to create a custom storage class by extending the base storage class provided by the fdk extension JavaScript library and implementing the mandatory methods according to your chosen database.

```javascript
const BaseStorage = require('fdk-extension-javascript');

class MyCustomStorage extends BaseStorage {
constructor(client, prefixKey) {
super(prefixKey);
this.client = client;
}

// All of the below methods must be implemented per your chosen database.

async get(key) {
// Implementation of a get method
}

async set(key, value) {
// Implementation of a set method
}

async del(key) {
// Implementation of a del method
}

async setex(key, value, ttl) {
// Implementation of a setex method
}

async hget(key, hashKey) {
// Implementation of a hget method
}

async hset(key, hashKey, value) {
// Implementation of a hset method
}

async hgetall(key) {
// Implementation of a hgetall method
}
}
```

Example implementation of Redis Storage class

```javascript
'use strict';

const BaseStorage = require('fdk-extension-javascript');

class RedisStorage extends BaseStorage {
constructor(client, prefixKey) {
super(prefixKey);
this.client = client;
}

async get(key) {
return await this.client.get(this.prefixKey + key);
}

async set(key, value) {
return await this.client.set(this.prefixKey + key, value);
}

async setex(key, value, ttl) {
return await this.client.setex(this.prefixKey + key, ttl, value);
}

async del(key) {
this.client.del(this.prefixKey + key);
}

async hget(key, hashKey) {
return await this.client.hget(this.prefixKey + key, hashKey);
}

async hset(key, hashKey, value) {
return await this.client.hset(this.prefixKey + key, hashKey, value);
}

async hgetall(key) {
return await this.client.hgetall(this.prefixKey + key);
}
}

module.exports = RedisStorage;
```

0 comments on commit 841dcf2

Please sign in to comment.