@nabasolution/database-postgres 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/dist/src/index.d.ts +130 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +1077 -0
- package/dist/src/index.js.map +1 -0
- package/dist/test/audit-chain.test.d.ts +2 -0
- package/dist/test/audit-chain.test.d.ts.map +1 -0
- package/dist/test/audit-chain.test.js +398 -0
- package/dist/test/audit-chain.test.js.map +1 -0
- package/dist/test/database-postgres.test.d.ts +2 -0
- package/dist/test/database-postgres.test.d.ts.map +1 -0
- package/dist/test/database-postgres.test.js +202 -0
- package/dist/test/database-postgres.test.js.map +1 -0
- package/dist/test/runtime.test.d.ts +2 -0
- package/dist/test/runtime.test.d.ts.map +1 -0
- package/dist/test/runtime.test.js +294 -0
- package/dist/test/runtime.test.js.map +1 -0
- package/naba.package.json +23 -0
- package/package.json +53 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 NABA Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { StateStore } from "@nabasolution/core";
|
|
2
|
+
/**
|
|
3
|
+
* PostgreSQL-backed persistence and runtime services for NABA.
|
|
4
|
+
*
|
|
5
|
+
* Two engines are supported behind one driver:
|
|
6
|
+
* - embedded PGlite (`@electric-sql/pglite`) by default, stored in `dataDir`;
|
|
7
|
+
* - a real PostgreSQL server via `pg` when `connectionString` is provided.
|
|
8
|
+
*
|
|
9
|
+
* `createPostgresStateStore` exposes the versioned StateStore used by the
|
|
10
|
+
* business modules. `createPostgresRuntime` additionally builds a shared
|
|
11
|
+
* fixed-window rate limiter, an audit sink and a single-writer lease on the same
|
|
12
|
+
* connection, so replicas coordinate through the database instead of process
|
|
13
|
+
* memory.
|
|
14
|
+
*
|
|
15
|
+
* Schema (created by `init`, versioned by `naba_migrations`):
|
|
16
|
+
* v1 naba_state(key text primary key, value jsonb not null, updated_at timestamptz)
|
|
17
|
+
* v2 naba_rate_limits(key text primary key, window_start bigint, count int)
|
|
18
|
+
* v3 naba_audit(id bigserial, timestamp, request_id, method, path, status,
|
|
19
|
+
* duration_ms, client_key, tenant_id, entry jsonb)
|
|
20
|
+
* v4 naba_writer_leases(name text primary key, owner_id text, expires_at timestamptz)
|
|
21
|
+
* v5 naba_audit.prev_hash/hash, naba_audit_archive, naba_audit_anchors
|
|
22
|
+
*
|
|
23
|
+
* Audit rows form a SHA-256 hash chain (`hash = sha256(canonical entry + prev_hash)`)
|
|
24
|
+
* appended under a database advisory transaction lock, so replicas cannot fork
|
|
25
|
+
* it. Retention archives an all-older prefix into `naba_audit_archive` and
|
|
26
|
+
* advances `naba_audit_anchors.last_hash`, keeping the live chain verifiable.
|
|
27
|
+
*/
|
|
28
|
+
export interface PostgresStateStoreOptions {
|
|
29
|
+
/** PGlite directory. Defaults to `.naba/data`. */
|
|
30
|
+
dataDir?: string;
|
|
31
|
+
/** When set, connect to a real server instead of PGlite. */
|
|
32
|
+
connectionString?: string;
|
|
33
|
+
/** Prepended to every key (e.g. `"tenant-a"` stores `tenant-a:inventory`). */
|
|
34
|
+
keyPrefix?: string;
|
|
35
|
+
}
|
|
36
|
+
export declare const POSTGRES_SCHEMA_VERSION = 1;
|
|
37
|
+
export declare const POSTGRES_RUNTIME_SCHEMA_VERSION = 5;
|
|
38
|
+
export declare const NABA_WRITER_LEASE_NAME = "naba-writer";
|
|
39
|
+
/** Stable advisory-lock key serializing audit-chain appends and purges. */
|
|
40
|
+
export declare const AUDIT_CHAIN_LOCK_KEY = 7314005001;
|
|
41
|
+
export interface RateLimitOptions {
|
|
42
|
+
windowMs: number;
|
|
43
|
+
max: number;
|
|
44
|
+
}
|
|
45
|
+
export interface RateLimitDecision {
|
|
46
|
+
allowed: boolean;
|
|
47
|
+
/** Seconds until the current window resets. Always >= 1 for a rejection. */
|
|
48
|
+
retryAfter: number;
|
|
49
|
+
remaining: number;
|
|
50
|
+
limit: number;
|
|
51
|
+
}
|
|
52
|
+
export interface PostgresRateLimiter {
|
|
53
|
+
consume(key: string, now?: number): Promise<RateLimitDecision>;
|
|
54
|
+
}
|
|
55
|
+
export interface AuditEntry {
|
|
56
|
+
timestamp: string;
|
|
57
|
+
requestId: string;
|
|
58
|
+
method: string;
|
|
59
|
+
path: string;
|
|
60
|
+
status: number;
|
|
61
|
+
durationMs: number;
|
|
62
|
+
clientKey: string;
|
|
63
|
+
tenantId: string;
|
|
64
|
+
}
|
|
65
|
+
export interface PostgresAuditSink {
|
|
66
|
+
record(entry: AuditEntry): void;
|
|
67
|
+
flush(): Promise<void>;
|
|
68
|
+
close(): Promise<void>;
|
|
69
|
+
}
|
|
70
|
+
export interface PostgresRuntimeOptions {
|
|
71
|
+
dataDir?: string;
|
|
72
|
+
connectionString?: string;
|
|
73
|
+
rateLimit?: RateLimitOptions;
|
|
74
|
+
/** Default true. false explicitly opts out of the single-writer guard. */
|
|
75
|
+
singleWriter?: boolean;
|
|
76
|
+
/** Lease duration in milliseconds. Default 30_000; heartbeat every ttl/3. */
|
|
77
|
+
leaseTtlMs?: number;
|
|
78
|
+
/** Default `hostname:pid:random`. */
|
|
79
|
+
ownerId?: string;
|
|
80
|
+
/** Audit retention in days. Default 90; 0 keeps rows forever. */
|
|
81
|
+
auditRetentionDays?: number;
|
|
82
|
+
}
|
|
83
|
+
export interface AuditChainVerification {
|
|
84
|
+
valid: boolean;
|
|
85
|
+
checked: number;
|
|
86
|
+
/** Database id of the first row whose prev_hash/hash does not match. */
|
|
87
|
+
brokenAt?: number;
|
|
88
|
+
}
|
|
89
|
+
export interface AuditPurgeResult {
|
|
90
|
+
archived: number;
|
|
91
|
+
deleted: number;
|
|
92
|
+
/** ISO cutoff used for the purge, or null when retention is disabled/no-op. */
|
|
93
|
+
cutoff: string | null;
|
|
94
|
+
/** Hash of the last archived row (the new chain anchor), or null. */
|
|
95
|
+
lastHash: string | null;
|
|
96
|
+
}
|
|
97
|
+
export interface PostgresLeaseInfo {
|
|
98
|
+
name: string;
|
|
99
|
+
ownerId: string;
|
|
100
|
+
/** Lease expiry as epoch milliseconds, or null when not held. */
|
|
101
|
+
expiresAt(): Promise<number | null>;
|
|
102
|
+
/** Manually extend the lease (the runtime also does this on a timer). */
|
|
103
|
+
heartbeat(): Promise<void>;
|
|
104
|
+
}
|
|
105
|
+
export interface PostgresRuntime {
|
|
106
|
+
store: StateStore;
|
|
107
|
+
rateLimiter?: PostgresRateLimiter;
|
|
108
|
+
auditSink: PostgresAuditSink;
|
|
109
|
+
ownerId: string;
|
|
110
|
+
/** Present only when `singleWriter` is enabled (the default). */
|
|
111
|
+
lease?: PostgresLeaseInfo;
|
|
112
|
+
/** Configured audit retention (0 = keep forever). */
|
|
113
|
+
auditRetentionDays: number;
|
|
114
|
+
/** Verify the live audit hash chain against the anchor. */
|
|
115
|
+
verifyAuditChain(): Promise<AuditChainVerification>;
|
|
116
|
+
/** Archive + delete rows older than the cutoff (0 disables retention). */
|
|
117
|
+
purgeAudit(retentionDays?: number): Promise<AuditPurgeResult>;
|
|
118
|
+
close(): Promise<void>;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Canonical SHA-256 link: hash = sha256(JSON({prevHash, entry})). Field order is
|
|
122
|
+
* explicit, so JSONB key ordering and property insertion order cannot change
|
|
123
|
+
* the digest. Input must be a sanitized entry.
|
|
124
|
+
*/
|
|
125
|
+
export declare function hashAuditEntry(entry: AuditEntry, prevHash: string): string;
|
|
126
|
+
export declare function createPostgresStateStore(options?: PostgresStateStoreOptions): Promise<StateStore>;
|
|
127
|
+
/** Offline verification against a database/data directory (opens its own driver). */
|
|
128
|
+
export declare function verifyAuditChain(options?: PostgresStateStoreOptions): Promise<AuditChainVerification>;
|
|
129
|
+
export declare function createPostgresRuntime(options?: PostgresRuntimeOptions): Promise<PostgresRuntime>;
|
|
130
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,MAAM,WAAW,yBAAyB;IACxC,kDAAkD;IAClD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,4DAA4D;IAC5D,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,8EAA8E;IAC9E,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,eAAO,MAAM,uB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