@shopify/shopify-app-session-storage-redis
Version:
Shopify App Session Storage for Redis
56 lines (53 loc) • 1.83 kB
JavaScript
import { createClient } from 'redis';
class RedisConnection {
sessionStorageIdentifier;
client;
constructor(urlOrClient, options, keyPrefix) {
this.init(urlOrClient, options);
this.sessionStorageIdentifier = keyPrefix;
}
query(_query, _params) {
throw new Error('Method not implemented. Use get(string, boolean) instead');
}
async connect() {
if (!this.client.isReady) {
await this.client.connect();
}
}
async disconnect() {
await this.client.quit();
}
async keys(name) {
return this.client.keys(name);
}
async set(baseKey, value, addKeyPrefix = true) {
await this.client.set(this.buildKey(baseKey, addKeyPrefix), value);
}
async del(baseKey, addKeyPrefix = true) {
return this.client.del(this.buildKey(baseKey, addKeyPrefix));
}
async get(baseKey, addKeyPrefix = true) {
return this.client.get(this.buildKey(baseKey, addKeyPrefix));
}
generateFullKey(name) {
return `${this.sessionStorageIdentifier}_${name}`;
}
buildKey(baseKey, addKeyPrefix) {
return addKeyPrefix ? this.generateFullKey(baseKey) : baseKey;
}
init(urlOrClient, options) {
if (typeof urlOrClient === 'string' || urlOrClient instanceof URL) {
this.client = createClient({ ...options, url: urlOrClient.toString() });
this.client.on('error', this.eventHandler);
this.client.on('connect', this.eventHandler);
this.client.on('reconnecting', this.eventHandler);
this.client.on('ready', this.eventHandler);
}
else {
this.client = urlOrClient;
}
}
eventHandler = (..._args) => { };
}
export { RedisConnection };
//# sourceMappingURL=redis-connection.mjs.map