@crvouga/mockingbird-service-postgres 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/AGENTS.md +174 -0
- package/COMPATIBILITY-AUDIT.md +156 -0
- package/COMPATIBILITY.md +87 -0
- package/LICENSE +21 -0
- package/README.md +416 -0
- package/compat/coverage.json +1081 -0
- package/compat/divergences.json +195 -0
- package/compat/requirements.json +1105 -0
- package/compat/scenario-types.ts +98 -0
- package/compat/scenarios.ts +98 -0
- package/compat/sections/agg.ts +45 -0
- package/compat/sections/api.ts +34 -0
- package/compat/sections/arr.ts +45 -0
- package/compat/sections/cat.ts +47 -0
- package/compat/sections/con.ts +32 -0
- package/compat/sections/cpy.ts +24 -0
- package/compat/sections/cte.ts +29 -0
- package/compat/sections/dat.ts +60 -0
- package/compat/sections/ddl.ts +55 -0
- package/compat/sections/det.ts +20 -0
- package/compat/sections/dml.ts +37 -0
- package/compat/sections/eco.ts +16 -0
- package/compat/sections/err.ts +16 -0
- package/compat/sections/exp.ts +53 -0
- package/compat/sections/fun.ts +58 -0
- package/compat/sections/fzz.ts +41 -0
- package/compat/sections/guc.ts +27 -0
- package/compat/sections/joi.ts +33 -0
- package/compat/sections/jsn.ts +46 -0
- package/compat/sections/lim.ts +13 -0
- package/compat/sections/par.ts +54 -0
- package/compat/sections/pre.ts +26 -0
- package/compat/sections/sch.ts +31 -0
- package/compat/sections/sel.ts +43 -0
- package/compat/sections/seq.ts +31 -0
- package/compat/sections/snp.ts +22 -0
- package/compat/sections/tok.ts +53 -0
- package/compat/sections/trg.ts +57 -0
- package/compat/sections/tsr.ts +37 -0
- package/compat/sections/txn.ts +39 -0
- package/compat/sections/typ.ts +55 -0
- package/compat/sections/uni.ts +13 -0
- package/compat/sections/win.ts +43 -0
- package/compat/smoke-baseline.json +3 -0
- package/compat/unsupported-register.json +2524 -0
- package/dist/api/bind.d.ts +11 -0
- package/dist/api/database.d.ts +87 -0
- package/dist/api/snapshot.d.ts +21 -0
- package/dist/api/statement.d.ts +51 -0
- package/dist/ast/nodes.d.ts +812 -0
- package/dist/constraints/enforce.d.ts +40 -0
- package/dist/errors/error.d.ts +24 -0
- package/dist/executor/ddl.d.ts +23 -0
- package/dist/executor/dml.d.ts +10 -0
- package/dist/executor/execute.d.ts +11 -0
- package/dist/executor/plpgsql.d.ts +72 -0
- package/dist/executor/relation.d.ts +60 -0
- package/dist/executor/select.d.ts +45 -0
- package/dist/executor/session.d.ts +21 -0
- package/dist/executor/triggers-exec.d.ts +1 -0
- package/dist/executor/triggers.d.ts +16 -0
- package/dist/executor/window.d.ts +13 -0
- package/dist/expressions/context.d.ts +23 -0
- package/dist/expressions/eval.d.ts +42 -0
- package/dist/expressions/operators.d.ts +8 -0
- package/dist/expressions/pattern.d.ts +20 -0
- package/dist/functions/aggregates.d.ts +17 -0
- package/dist/functions/array-fns.d.ts +2 -0
- package/dist/functions/datetime-fns.d.ts +26 -0
- package/dist/functions/datetime-registry.d.ts +2 -0
- package/dist/functions/json-fns.d.ts +7 -0
- package/dist/functions/math-fns.d.ts +2 -0
- package/dist/functions/misc-fns.d.ts +8 -0
- package/dist/functions/scalar.d.ts +10 -0
- package/dist/functions/srf.d.ts +12 -0
- package/dist/functions/string-fns.d.ts +4 -0
- package/dist/functions/tsearch-fns.d.ts +2 -0
- package/dist/functions/util.d.ts +14 -0
- package/dist/functions/window.d.ts +7 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +23581 -0
- package/dist/index.js.map +7 -0
- package/dist/indexes/index.d.ts +14 -0
- package/dist/indexes/maintain.d.ts +11 -0
- package/dist/lexer/tokenize.d.ts +8 -0
- package/dist/parser/index.d.ts +1 -0
- package/dist/parser/parser.d.ts +98 -0
- package/dist/planner/access.d.ts +15 -0
- package/dist/runtime/assert.d.ts +5 -0
- package/dist/runtime/clock.d.ts +14 -0
- package/dist/runtime/index.d.ts +3 -0
- package/dist/runtime/options.d.ts +34 -0
- package/dist/runtime/prng.d.ts +48 -0
- package/dist/schema/catalog-tables.d.ts +1 -0
- package/dist/schema/catalog.d.ts +12 -0
- package/dist/serialization/codec.d.ts +22 -0
- package/dist/serialization/index.d.ts +1 -0
- package/dist/serialization/wire.d.ts +63 -0
- package/dist/sql/deparse.d.ts +3 -0
- package/dist/storage/columnar-slab.d.ts +37 -0
- package/dist/storage/database-state.d.ts +253 -0
- package/dist/transactions/manager.d.ts +25 -0
- package/dist/tsearch/stem.d.ts +5 -0
- package/dist/tsearch/tsearch.d.ts +59 -0
- package/dist/types/cast.d.ts +26 -0
- package/dist/types/compare.d.ts +18 -0
- package/dist/types/datetime.d.ts +68 -0
- package/dist/types/jsonb.d.ts +47 -0
- package/dist/types/jsonpath.d.ts +16 -0
- package/dist/types/numeric.d.ts +69 -0
- package/dist/types/range.d.ts +17 -0
- package/dist/types/resolve.d.ts +15 -0
- package/dist/types/timezone.d.ts +9 -0
- package/dist/types/value.d.ts +99 -0
- package/dist/unstable.d.ts +15 -0
- package/dist/unstable.js +21810 -0
- package/dist/unstable.js.map +7 -0
- package/package.json +131 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Int8Mode } from "../runtime/options.js";
|
|
2
|
+
import { type Datum, type OutputCtx, type TypedValue, type TypeId } from "../types/value.js";
|
|
3
|
+
/** JavaScript values accepted as query parameters (`$1..$n`). */
|
|
4
|
+
export type BindValue = null | undefined | boolean | number | bigint | string | Uint8Array | Date;
|
|
5
|
+
/** JavaScript values produced in query result rows. */
|
|
6
|
+
export type JsValue = null | boolean | number | bigint | string | Uint8Array;
|
|
7
|
+
/** A result row keyed by column name. */
|
|
8
|
+
export type QueryRow = Record<string, JsValue>;
|
|
9
|
+
export declare function bindValueToTyped(value: BindValue, index: number): TypedValue;
|
|
10
|
+
/** Convert an engine datum to the public JS value for result rows. */
|
|
11
|
+
export declare function datumToJs(t: TypeId, v: Datum, ctx: OutputCtx, int8?: Int8Mode): JsValue;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { type DatabaseOptions, type Int8Mode, type RandomMode } from "../runtime/index.js";
|
|
2
|
+
import type { BindValue, JsValue, QueryRow } from "./bind.js";
|
|
3
|
+
import { type Snapshot } from "./snapshot.js";
|
|
4
|
+
import { Statement } from "./statement.js";
|
|
5
|
+
/**
|
|
6
|
+
* Pure TypeScript in-memory PostgreSQL database.
|
|
7
|
+
*
|
|
8
|
+
* Deterministic by default: `random()` / `gen_random_uuid()` use a seeded PRNG
|
|
9
|
+
* (`seed` defaults to `1`) and `now()` / `current_timestamp` use a fixed clock
|
|
10
|
+
* (`2000-01-01T00:00:00.000Z`). Pass `{ random: "os" }` and `{ now: "system" }`
|
|
11
|
+
* for PostgreSQL-like entropy and wall-clock time. No filesystem, no WASM.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { Database } from "@crvouga/mockingbird-service-postgres";
|
|
16
|
+
*
|
|
17
|
+
* const db = new Database();
|
|
18
|
+
* db.exec("CREATE TABLE users (id serial PRIMARY KEY, name text NOT NULL)");
|
|
19
|
+
* db.prepare("INSERT INTO users (name) VALUES ($1)").run("Alice");
|
|
20
|
+
* const users = db.query<{ id: number; name: string }>("SELECT * FROM users");
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare class Database {
|
|
24
|
+
/** Seed used to construct the PRNG. Ignored when {@link randomMode} is `"os"`. */
|
|
25
|
+
readonly seed: number | bigint;
|
|
26
|
+
/** Entropy mode for `random()` / `gen_random_uuid()`. */
|
|
27
|
+
readonly randomMode: RandomMode;
|
|
28
|
+
/** How `int8` columns surface in query rows. */
|
|
29
|
+
readonly int8Mode: Int8Mode;
|
|
30
|
+
private closed;
|
|
31
|
+
private transactionSequence;
|
|
32
|
+
private apiTransactionDepth;
|
|
33
|
+
constructor(options?: DatabaseOptions);
|
|
34
|
+
/**
|
|
35
|
+
* Execute SQL for its side effects. Multiple semicolon-separated statements
|
|
36
|
+
* are allowed. Does not accept bind parameters — use {@link prepare} / {@link query}.
|
|
37
|
+
*/
|
|
38
|
+
exec(sql: string): void;
|
|
39
|
+
/**
|
|
40
|
+
* Register a JavaScript function callable from SQL. Not encoded in PGMM
|
|
41
|
+
* snapshots — re-register after {@link Snapshot.decode} / {@link Snapshot.open}.
|
|
42
|
+
* {@link Snapshot.open} copies the implementation by reference when opening
|
|
43
|
+
* a live in-memory snapshot.
|
|
44
|
+
*/
|
|
45
|
+
registerFunction(spec: RegisterFunctionOptions): void;
|
|
46
|
+
/** Execute a single-statement query and return all rows keyed by column name. */
|
|
47
|
+
query<T = QueryRow>(sql: string, params?: readonly BindValue[]): T[];
|
|
48
|
+
/** Compile a single SQL statement into a reusable {@link Statement}. */
|
|
49
|
+
prepare(sql: string): Statement;
|
|
50
|
+
/**
|
|
51
|
+
* Run `fn` inside a transaction. Commits on success; rolls back if `fn`
|
|
52
|
+
* throws. Nested calls use savepoints.
|
|
53
|
+
*/
|
|
54
|
+
transaction<T>(fn: () => T): T;
|
|
55
|
+
/**
|
|
56
|
+
* Freeze this database into a reusable {@link Snapshot} template.
|
|
57
|
+
* Does not encode PGMM bytes. Call {@link Snapshot.encode} to persist, or
|
|
58
|
+
* {@link Snapshot.open} for a copy-on-write fork.
|
|
59
|
+
*/
|
|
60
|
+
snapshot(): Snapshot;
|
|
61
|
+
/** Close the database. Further SQL throws. Idempotent. */
|
|
62
|
+
close(): void;
|
|
63
|
+
/**
|
|
64
|
+
* Execute `COPY table [(cols)] FROM STDIN` with the given text/csv payload
|
|
65
|
+
* (the API-level analog of psql's `\copy` / the wire-protocol copy-in stream).
|
|
66
|
+
* Returns the number of rows copied.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* db.copyFrom("COPY t (a, b) FROM STDIN", "1\tx\n2\ty\n");
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
copyFrom(sql: string, data: string): number;
|
|
74
|
+
/** Rows affected by the most recent INSERT / UPDATE / DELETE. */
|
|
75
|
+
get changes(): number;
|
|
76
|
+
private prepareSingle;
|
|
77
|
+
}
|
|
78
|
+
export { Snapshot } from "./snapshot.js";
|
|
79
|
+
/** Options for {@link Database.registerFunction}. */
|
|
80
|
+
export interface RegisterFunctionOptions {
|
|
81
|
+
name: string;
|
|
82
|
+
args: string[];
|
|
83
|
+
returns: string;
|
|
84
|
+
strict?: boolean;
|
|
85
|
+
fn: (...args: JsValue[]) => JsValue;
|
|
86
|
+
}
|
|
87
|
+
export type { DatabaseOptions };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type DatabaseOptions } from "../runtime/index.js";
|
|
2
|
+
import { type Database } from "./database.js";
|
|
3
|
+
/**
|
|
4
|
+
* Frozen in-memory database template. {@link open} is a copy-on-write fork.
|
|
5
|
+
* Encoded PGMM bytes are produced lazily via {@link encode}.
|
|
6
|
+
*/
|
|
7
|
+
export declare class Snapshot {
|
|
8
|
+
private cachedBytes;
|
|
9
|
+
/** Encoded PGMM blob. Computed once; never mutates a buffer passed to {@link decode}. */
|
|
10
|
+
encode(): Uint8Array;
|
|
11
|
+
/**
|
|
12
|
+
* Copy-on-write database from this frozen template. Does not re-encode or re-decode.
|
|
13
|
+
*/
|
|
14
|
+
open(options?: DatabaseOptions): Database;
|
|
15
|
+
/**
|
|
16
|
+
* Decode `bytes` once and freeze the result. The same `Uint8Array` object
|
|
17
|
+
* returns the same {@link Snapshot} (WeakMap), so later {@link open} calls
|
|
18
|
+
* are copy-on-write forks after the first hydrate.
|
|
19
|
+
*/
|
|
20
|
+
static decode(bytes: Uint8Array): Snapshot;
|
|
21
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type BindValue, type QueryRow } from "./bind.js";
|
|
2
|
+
/** Row-count summary returned by {@link Statement.run}. */
|
|
3
|
+
export interface RunResult {
|
|
4
|
+
/** Rows affected by the last statement (PostgreSQL command tag count). */
|
|
5
|
+
rowCount: number;
|
|
6
|
+
/** Command tag of the last statement, e.g. `"INSERT"`. */
|
|
7
|
+
command: string;
|
|
8
|
+
}
|
|
9
|
+
/** {@link Statement.textResult}: rows as canonical PostgreSQL text. */
|
|
10
|
+
export interface TextResultSet {
|
|
11
|
+
columns: string[];
|
|
12
|
+
columnTypes: string[];
|
|
13
|
+
rows: (string | null)[][];
|
|
14
|
+
rowCount: number;
|
|
15
|
+
command: string;
|
|
16
|
+
}
|
|
17
|
+
/** Full result including column metadata (useful for zero-row results). */
|
|
18
|
+
export interface ResultSet {
|
|
19
|
+
columns: string[];
|
|
20
|
+
/** canonical type names per column (PG internal names, e.g. "int4") */
|
|
21
|
+
columnTypes: string[];
|
|
22
|
+
rows: QueryRow[];
|
|
23
|
+
rowCount: number;
|
|
24
|
+
command: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Prepared SQL statement bound to a {@link Database}. Parameters are
|
|
28
|
+
* positional PostgreSQL placeholders `$1..$n`, passed per call (stateless).
|
|
29
|
+
*/
|
|
30
|
+
export declare class Statement {
|
|
31
|
+
private readonly database;
|
|
32
|
+
private statements;
|
|
33
|
+
/** The source SQL this statement was prepared from. */
|
|
34
|
+
readonly sql: string;
|
|
35
|
+
private constructor();
|
|
36
|
+
/** Execute for side effects; returns the affected row count. */
|
|
37
|
+
run(...params: BindValue[]): RunResult;
|
|
38
|
+
/** Execute and return all rows as objects keyed by column name. */
|
|
39
|
+
all<T = QueryRow>(...params: BindValue[]): T[];
|
|
40
|
+
/** Execute and return the first row, or `undefined`. */
|
|
41
|
+
get<T = QueryRow>(...params: BindValue[]): T | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Execute and return rows rendered as canonical PostgreSQL text (psql-style),
|
|
44
|
+
* plus column metadata. NULLs stay `null`.
|
|
45
|
+
*/
|
|
46
|
+
textResult(...params: BindValue[]): TextResultSet;
|
|
47
|
+
/** Execute and return the full {@link ResultSet} with column metadata. */
|
|
48
|
+
result(...params: BindValue[]): ResultSet;
|
|
49
|
+
private execute;
|
|
50
|
+
private toRows;
|
|
51
|
+
}
|