diff --git a/README.md b/README.md index fe336d70..e535f526 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/express/storage/README.md b/express/storage/README.md new file mode 100644 index 00000000..97f2e557 --- /dev/null +++ b/express/storage/README.md @@ -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; +``` \ No newline at end of file