@postgresx/noredis 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,84 @@
1
+ interface BunSqlLike {
2
+ unsafe<T = Record<string, unknown>>(query: string, params?: readonly unknown[]): Promise<T[]>;
3
+ }
4
+ interface PgKvCacheL1Options {
5
+ max?: number;
6
+ ttlMs?: number;
7
+ }
8
+ interface PgKvCacheNotifyOptions {
9
+ channel?: string;
10
+ enabled?: boolean;
11
+ }
12
+ interface PgKvCacheOptions {
13
+ sql: BunSqlLike;
14
+ namespace?: string;
15
+ tableName?: string;
16
+ l1?: false | PgKvCacheL1Options;
17
+ notify?: false | PgKvCacheNotifyOptions;
18
+ instanceId?: string;
19
+ now?: () => number;
20
+ }
21
+ interface PgKvSetOptions {
22
+ ttlMs?: number | null;
23
+ notify?: boolean;
24
+ }
25
+ interface PgKvSchemaOptions {
26
+ unlogged?: boolean;
27
+ }
28
+ interface PgKvNotification {
29
+ namespace: string;
30
+ key?: string;
31
+ prefix?: string;
32
+ op: "set" | "delete" | "clearPrefix" | "clearNamespace";
33
+ senderId?: string;
34
+ }
35
+ interface PgKvCacheStats {
36
+ namespace: string;
37
+ tableName: string;
38
+ l1Size: number;
39
+ l1Max: number;
40
+ }
41
+ declare class PgKvCache {
42
+ readonly namespace: string;
43
+ readonly tableName: string;
44
+ readonly quotedTableName: string;
45
+ readonly notifyChannel: string;
46
+ readonly instanceId: string;
47
+ private readonly sql;
48
+ private readonly now;
49
+ private readonly l1Enabled;
50
+ private readonly l1Max;
51
+ private readonly l1TtlMs;
52
+ private readonly notifyEnabled;
53
+ private readonly l1;
54
+ constructor(options: PgKvCacheOptions);
55
+ ensureSchema(options?: PgKvSchemaOptions): Promise<void>;
56
+ get<T = unknown>(key: string): Promise<T | null>;
57
+ mget<T = unknown>(keys: readonly string[]): Promise<Map<string, T>>;
58
+ set<T = unknown>(key: string, value: T, options?: PgKvSetOptions): Promise<void>;
59
+ mset<T = unknown>(entries: Iterable<readonly [string, T]>, options?: PgKvSetOptions): Promise<void>;
60
+ delete(key: string, options?: {
61
+ notify?: boolean;
62
+ }): Promise<boolean>;
63
+ clearPrefix(prefix: string, options?: {
64
+ notify?: boolean;
65
+ }): Promise<number>;
66
+ clearNamespace(options?: {
67
+ notify?: boolean;
68
+ }): Promise<number>;
69
+ cleanupExpired(limit?: number): Promise<number>;
70
+ invalidate(key: string): void;
71
+ handleNotification(payload: string | PgKvNotification): boolean;
72
+ stats(): PgKvCacheStats;
73
+ private publish;
74
+ private parseNotification;
75
+ private getL1;
76
+ private setL1;
77
+ private computeL1ExpiresAt;
78
+ private deleteL1;
79
+ private cleanupL1;
80
+ private enforceL1Max;
81
+ }
82
+ declare function createPgKvCache(options: PgKvCacheOptions): PgKvCache;
83
+
84
+ export { type BunSqlLike, PgKvCache, type PgKvCacheL1Options, type PgKvCacheNotifyOptions, type PgKvCacheOptions, type PgKvCacheStats, type PgKvNotification, type PgKvSchemaOptions, type PgKvSetOptions, createPgKvCache };
@@ -0,0 +1,8 @@
1
+ import {
2
+ PgKvCache,
3
+ createPgKvCache
4
+ } from "./chunk-GBFG2TOL.js";
5
+ export {
6
+ PgKvCache,
7
+ createPgKvCache
8
+ };
@@ -0,0 +1,60 @@
1
+ import { P as PgSqlLike } from './sql-DKf2TDlu.js';
2
+
3
+ type PgNotifyPayload = string | number | boolean | null | Record<string, unknown> | unknown[];
4
+ interface PgPublisher {
5
+ publish(channel: string, payload: PgNotifyPayload): Promise<void>;
6
+ }
7
+ type NotifyHandler = (channel: string, payload: string) => void;
8
+ interface PgListenerHealth {
9
+ status: "connecting" | "connected" | "reconnecting" | "closed" | "error";
10
+ connected: boolean;
11
+ listeningChannels: string[];
12
+ queuedQueries: number;
13
+ activeQuery: boolean;
14
+ reconnectAttempts: number;
15
+ lastConnectedAt: number | null;
16
+ lastMessageAt: number | null;
17
+ lastNotificationAt: number | null;
18
+ lastError: string | null;
19
+ }
20
+ interface PgListenerEvents {
21
+ connected: PgListenerHealth;
22
+ close: {
23
+ willReconnect: boolean;
24
+ error?: Error;
25
+ };
26
+ error: Error;
27
+ health: PgListenerHealth;
28
+ notification: {
29
+ channel: string;
30
+ payload: string;
31
+ };
32
+ reconnect: {
33
+ attempt: number;
34
+ delayMs: number;
35
+ };
36
+ }
37
+ interface PgListenerOptions {
38
+ channels?: string[];
39
+ onNotify?: NotifyHandler;
40
+ tls?: boolean | Record<string, unknown>;
41
+ ssl?: boolean | Record<string, unknown>;
42
+ sslMode?: "disable" | "allow" | "prefer" | "require" | "verify-ca" | "verify-full" | "no-verify";
43
+ reconnectDelayMs?: number | ((attempt: number) => number);
44
+ healthCheckIntervalMs?: number;
45
+ logger?: Partial<Pick<typeof console, "debug" | "info" | "warn" | "error">> | false;
46
+ }
47
+ interface PgListenerHandle {
48
+ close(): void;
49
+ notify(channel: string, payload?: string): Promise<void>;
50
+ getHealth(): PgListenerHealth;
51
+ on<K extends keyof PgListenerEvents>(event: K, handler: (payload: PgListenerEvents[K]) => void): () => void;
52
+ off<K extends keyof PgListenerEvents>(event: K, handler: (payload: PgListenerEvents[K]) => void): void;
53
+ }
54
+ declare function createBunPgListener(databaseUrl: string, channels: string[], onNotify: NotifyHandler, options?: PgListenerOptions): PgListenerHandle;
55
+ declare function createBunPgListener(databaseUrl: string, options?: PgListenerOptions): PgListenerHandle;
56
+ declare const createPgListener: typeof createBunPgListener;
57
+ declare function publishPgNotify(sql: PgSqlLike, channel: string, payload: PgNotifyPayload): Promise<void>;
58
+ declare function createPgPublisher(sql: PgSqlLike): PgPublisher;
59
+
60
+ export { type NotifyHandler as N, type PgListenerEvents as P, type PgListenerHandle as a, type PgListenerHealth as b, type PgListenerOptions as c, type PgNotifyPayload as d, type PgPublisher as e, createBunPgListener as f, createPgListener as g, createPgPublisher as h, publishPgNotify as p };
@@ -0,0 +1,10 @@
1
+ interface PgSqlLike {
2
+ unsafe<T = Record<string, unknown>>(query: string, params?: readonly unknown[]): Promise<T[]>;
3
+ begin?<T>(callback: (tx: PgSqlLike) => Promise<T>): Promise<T>;
4
+ }
5
+ declare function quoteIdentifier(identifier: string): string;
6
+ declare function quoteQualifiedName(name: string): string;
7
+ declare function indexName(tableName: string, suffix: string): string;
8
+ declare function normalizePositiveInteger(value: number, name: string): number;
9
+
10
+ export { type PgSqlLike as P, quoteQualifiedName as a, indexName as i, normalizePositiveInteger as n, quoteIdentifier as q };
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@postgresx/noredis",
3
+ "version": "0.1.0",
4
+ "description": "PostgreSQL-backed Redis feature replacement toolkit: KV/TTL cache, pub/sub, advisory locks, rate limiting, and pg-boss queues",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "sideEffects": false,
8
+ "files": [
9
+ "dist/**/*",
10
+ "README.md"
11
+ ],
12
+ "main": "dist/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "import": "./dist/index.js",
18
+ "default": "./dist/index.js"
19
+ },
20
+ "./adapters/bun": {
21
+ "types": "./dist/adapters/bun.d.ts",
22
+ "import": "./dist/adapters/bun.js",
23
+ "default": "./dist/adapters/bun.js"
24
+ },
25
+ "./adapters/node": {
26
+ "types": "./dist/adapters/node.d.ts",
27
+ "import": "./dist/adapters/node.js",
28
+ "default": "./dist/adapters/node.js"
29
+ },
30
+ "./kv-cache": {
31
+ "types": "./dist/kv-cache.d.ts",
32
+ "import": "./dist/kv-cache.js",
33
+ "default": "./dist/kv-cache.js"
34
+ }
35
+ },
36
+ "scripts": {
37
+ "build": "tsup src/index.ts src/adapters/bun.ts src/adapters/node.ts src/kv-cache.ts --format esm --dts --clean --out-dir dist --external pg --external pg-boss --external @postgresx/bun-listen",
38
+ "test": "bun test src/*.test.ts",
39
+ "check": "tsc --noEmit -p tsconfig.json"
40
+ },
41
+ "devDependencies": {
42
+ "@types/bun": "^1.3.14",
43
+ "@types/node": "^25.0.0",
44
+ "@types/pg": "^8.16.0",
45
+ "@postgresx/bun-listen": "^0.1.0",
46
+ "tsup": "^8.5.0",
47
+ "typescript": "^6.0.3"
48
+ }
49
+ }