@javien/mikro-orm-redis-cache-adapter
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -1,1 +0,151 @@ | ||
"use strict";var d=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var u=(s,e)=>{for(var t in e)d(s,t,{get:e[t],enumerable:!0})},f=(s,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of p(e))!h.call(s,i)&&i!==t&&d(s,i,{get:()=>e[i],enumerable:!(r=g(e,i))||r.enumerable});return s};var m=s=>f(d({},"__esModule",{value:!0}),s);var y={};u(y,{RedisCacheAdapter:()=>c});module.exports=m(y);var a=class{debug=!1;prefix="mikro";prefixDelimiter=":";logger=console.log;client;gracefulShutdown=!0;constructor(e){Object.assign(this,e)}};var l=require("node:v8"),c=class{options;serializer=l.serialize;deserializer=l.deserialize;constructor(e){this.options=e instanceof a?e:new a(e)}async get(e){try{let t=this.generateKey(e),r=await this.options.client.getBuffer(t);if(!r)return;let i=this.deserializer(r);return this.debug("Get",t,i),i}catch(t){this.options.logger("Failed to fetch data."),this.options.logger(t);return}}async set(e,t,r,i){try{let o=this.generateKey(e),n=this.serializer(t);i?this.options.client.set(o,n,"PX",i):this.options.client.set(o,n),this.debug("Set",o,n)}catch(o){this.options.logger("Failed to store data."),this.options.logger(o)}}async remove(e){try{let t=this.generateKey(e);await this.options.client.del(t),this.debug("Remove",t,void 0)}catch(t){this.options.logger("Failed to remove data."),this.options.logger(t)}}clear(){return new Promise((e,t)=>{let r=this.options.client.scanStream({match:`${this.options.prefix}${this.options.prefixDelimiter}*`}),i=this.options.client.pipeline();r.on("data",o=>{if(o.length!==0)for(let n of o)i.del(n)}),r.on("end",()=>{i.exec(o=>{if(o)return this.options.logger("Failed to clear data."),this.options.logger(o),t(o);this.options.logger("Cache has been successfully cleared."),e()})})})}close(){}debug(e,t,r){this.options.debug&&this.options.logger(`${e} ${t}: ${JSON.stringify(r)}`)}generateKey(e){return`${this.options.prefix}:${e}`}};0&&(module.exports={RedisCacheAdapter}); | ||
"use strict"; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
// src/redisCacheAdapter.ts | ||
var redisCacheAdapter_exports = {}; | ||
__export(redisCacheAdapter_exports, { | ||
RedisCacheAdapter: () => RedisCacheAdapter | ||
}); | ||
module.exports = __toCommonJS(redisCacheAdapter_exports); | ||
// src/options.ts | ||
var Options = class { | ||
/** | ||
* Debug mode. Defaults to `false`. | ||
*/ | ||
debug = false; | ||
/** | ||
* The prefix for the cache keys. Defaults to `mikro`. | ||
*/ | ||
prefix = "mikro"; | ||
/** | ||
* The delimiter between the prefix and the cache key. Defaults to `:`. | ||
*/ | ||
prefixDelimiter = ":"; | ||
/** | ||
* Logger. Defaults to `console.log`. | ||
*/ | ||
logger = console.log; | ||
/** | ||
* Redis client. | ||
*/ | ||
client; | ||
/** | ||
* If you want to close the Redis connection by yourself, set it to `false`. | ||
* Defaults to `true`. | ||
*/ | ||
gracefulShutdown = true; | ||
constructor(options) { | ||
Object.assign(this, options); | ||
} | ||
}; | ||
// src/redisCacheAdapter.ts | ||
var import_node_v8 = require("node:v8"); | ||
var RedisCacheAdapter = class { | ||
options; | ||
serializer = import_node_v8.serialize; | ||
deserializer = import_node_v8.deserialize; | ||
constructor(options) { | ||
this.options = options instanceof Options ? options : new Options(options); | ||
} | ||
async get(name) { | ||
try { | ||
const key = this.generateKey(name); | ||
const data = await this.options.client.getBuffer(key); | ||
if (!data) { | ||
return void 0; | ||
} | ||
const deserializedData = this.deserializer(data); | ||
this.debug("Get", key, deserializedData); | ||
return deserializedData; | ||
} catch (error) { | ||
this.options.logger("Failed to fetch data."); | ||
this.options.logger(error); | ||
return void 0; | ||
} | ||
} | ||
async set(name, data, _origin, expiration) { | ||
try { | ||
const key = this.generateKey(name); | ||
const serializedData = this.serializer(data); | ||
if (expiration) { | ||
this.options.client.set(key, serializedData, "PX", expiration); | ||
} else { | ||
this.options.client.set(key, serializedData); | ||
} | ||
this.debug("Set", key, serializedData); | ||
} catch (error) { | ||
this.options.logger("Failed to store data."); | ||
this.options.logger(error); | ||
} | ||
} | ||
async remove(name) { | ||
try { | ||
const key = this.generateKey(name); | ||
await this.options.client.del(key); | ||
this.debug("Remove", key, void 0); | ||
} catch (error) { | ||
this.options.logger("Failed to remove data."); | ||
this.options.logger(error); | ||
} | ||
} | ||
clear() { | ||
return new Promise((resolve, reject) => { | ||
const stream = this.options.client.scanStream({ | ||
match: `${this.options.prefix}${this.options.prefixDelimiter}*` | ||
}); | ||
const pipeline = this.options.client.pipeline(); | ||
stream.on("data", (keys) => { | ||
if (keys.length === 0) { | ||
return; | ||
} | ||
for (const key of keys) { | ||
pipeline.del(key); | ||
} | ||
}); | ||
stream.on("end", () => { | ||
pipeline.exec((err) => { | ||
if (err) { | ||
this.options.logger("Failed to clear data."); | ||
this.options.logger(err); | ||
return reject(err); | ||
} | ||
this.options.logger("Cache has been successfully cleared."); | ||
resolve(); | ||
}); | ||
}); | ||
}); | ||
} | ||
close() { | ||
} | ||
debug(method, key, data) { | ||
if (!this.options.debug) { | ||
return; | ||
} | ||
this.options.logger(`${method} ${key}: ${JSON.stringify(data)}`); | ||
} | ||
generateKey(name) { | ||
return `${this.options.prefix}:${name}`; | ||
} | ||
}; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
RedisCacheAdapter | ||
}); |
{ | ||
"name": "@javien/mikro-orm-redis-cache-adapter", | ||
"license": "ISC", | ||
"license": "MIT", | ||
"description": "", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"author": "Javien Lee <kraccoon@dimipay.io> (https://github.com/SnowMarble/)", | ||
@@ -13,3 +13,3 @@ "keywords": ["mikro-orm", "redis", "cache", "adapter"], | ||
"check:apply": "biome check --apply src", | ||
"build": "esbuild src/redisCacheAdapter.ts --bundle --platform=node --outdir=dist --minify" | ||
"build": "rm -rf dist && esbuild src/redisCacheAdapter.ts --bundle --platform=node --outdir=dist& tsc --emitDeclarationOnly --outdir dist" | ||
}, | ||
@@ -16,0 +16,0 @@ "devDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
7809
6
193
2
1
8