You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@postgresql-typed/cache

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@postgresql-typed/cache - npm Package Compare versions

Comparing version

to
0.3.0

33

lib/index.d.ts

@@ -0,1 +1,2 @@

import { PostQueryHookData } from "@postgresql-typed/util";
declare const _default: import("@postgresql-typed/util").PgTExtension<PgTCacheOptions>;

@@ -29,1 +30,33 @@ export default _default;

}
export interface PgTCacheContext {
/**
* Whether the query should be cached.
*
* @default true
*/
enabled?: boolean;
/**
* The TTL for the cache.
*
* Set to `0` to disable TTL.
*
* @default "What was set in the config file"
*/
ttl?: number;
/**
* The key to use for the cache.
*
* If not provided, the key will be generated from the query.
*
* @default undefined
*/
key?: string;
/**
* Do NOT set this manually.
*
* This is set by the extension to the cached value.
*
* @default undefined
*/
cache?: PostQueryHookData["output"];
}

24

lib/index.js

@@ -25,14 +25,25 @@ import { definePgTExtension } from "@postgresql-typed/util";

return;
const context = defaultContext(data.context, ttl);
if (!context.cache?.enabled)
return;
if (!types.includes(data.input.query.text.trim().split(" ")[0].toLowerCase()))
return;
const hashed = hash(data.input, {}), foundCache = await keyv.get(hashed);
const key = context.cache?.key ?? hash(data.input, {}), foundCache = await keyv.get(key);
if (foundCache !== undefined)
data.output = foundCache;
context.cache.key = key;
context.cache.cache = foundCache;
});
manager.hook("pgt:post-query", async (data) => {
const context = defaultContext(data.context, ttl);
if (!context.cache?.enabled)
return;
if (!types.includes(data.input.query.text.trim().split(" ")[0].toLowerCase()))
return;
const hashed = hash(data.input, {}), isCached = await keyv.has(hashed);
/* c8 ignore next 1 */
const key = context.cache?.key ?? hash(data.input, {}), isCached = await keyv.has(key);
if (!isCached)
await keyv.set(hashed, data.output);
await keyv.set(key, data.output, context.cache?.ttl);
context.cache.key = key;
context.cache.cache = data.output;
});

@@ -72,2 +83,9 @@ },

});
function defaultContext(contextt, ttl) {
const context = contextt;
context.cache ??= {};
context.cache.enabled ??= true;
context.cache.ttl ??= ttl;
return context;
}
//# sourceMappingURL=index.js.map

10

package.json
{
"name": "@postgresql-typed/cache",
"version": "0.2.0",
"version": "0.3.0",
"description": "A PostgreSQL-Typed extension to cache queries using Keyv",

@@ -46,4 +46,4 @@ "type": "module",

"source-map-support": "^0.5.21",
"@postgresql-typed/parsers": "0.9.0",
"@postgresql-typed/util": "0.7.0"
"@postgresql-typed/parsers": "0.9.1",
"@postgresql-typed/util": "0.8.0"
},

@@ -54,4 +54,4 @@ "devDependencies": {

"typescript": "^4.9.5",
"@postgresql-typed/cli": "0.5.4",
"@postgresql-typed/core": "0.8.0"
"@postgresql-typed/cli": "0.6.0",
"@postgresql-typed/core": "0.8.2"
},

@@ -58,0 +58,0 @@ "publishConfig": {

@@ -1,2 +0,2 @@

import type { PgTCacheOptions } from "./lib/index.js";
import type { PgTCacheContext,PgTCacheOptions } from "./lib/index.js";

@@ -7,2 +7,6 @@ declare module "@postgresql-typed/util" {

}
interface PgTExtensionContext {
cache?: PgTCacheContext
}
}

Sorry, the diff of this file is not supported yet