@crvouga/postgres-mem 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.
Files changed (109) hide show
  1. package/AGENTS.md +159 -0
  2. package/COMPATIBILITY-AUDIT.md +155 -0
  3. package/COMPATIBILITY.md +87 -0
  4. package/LICENSE +21 -0
  5. package/README.md +325 -0
  6. package/compat/coverage.json +1081 -0
  7. package/compat/divergences.json +191 -0
  8. package/compat/gate-report.json +80 -0
  9. package/compat/requirements.json +1105 -0
  10. package/compat/requirements.raw.html +484 -0
  11. package/compat/scenario-types.ts +98 -0
  12. package/compat/scenarios.ts +98 -0
  13. package/compat/sections/agg.ts +45 -0
  14. package/compat/sections/api.ts +28 -0
  15. package/compat/sections/arr.ts +45 -0
  16. package/compat/sections/cat.ts +47 -0
  17. package/compat/sections/con.ts +32 -0
  18. package/compat/sections/cpy.ts +24 -0
  19. package/compat/sections/cte.ts +29 -0
  20. package/compat/sections/dat.ts +60 -0
  21. package/compat/sections/ddl.ts +55 -0
  22. package/compat/sections/det.ts +20 -0
  23. package/compat/sections/dml.ts +37 -0
  24. package/compat/sections/eco.ts +16 -0
  25. package/compat/sections/err.ts +16 -0
  26. package/compat/sections/exp.ts +53 -0
  27. package/compat/sections/fun.ts +58 -0
  28. package/compat/sections/fzz.ts +36 -0
  29. package/compat/sections/guc.ts +27 -0
  30. package/compat/sections/joi.ts +33 -0
  31. package/compat/sections/jsn.ts +46 -0
  32. package/compat/sections/lim.ts +13 -0
  33. package/compat/sections/par.ts +61 -0
  34. package/compat/sections/pre.ts +26 -0
  35. package/compat/sections/sch.ts +31 -0
  36. package/compat/sections/sel.ts +43 -0
  37. package/compat/sections/seq.ts +31 -0
  38. package/compat/sections/snp.ts +21 -0
  39. package/compat/sections/tok.ts +53 -0
  40. package/compat/sections/trg.ts +57 -0
  41. package/compat/sections/tsr.ts +37 -0
  42. package/compat/sections/txn.ts +39 -0
  43. package/compat/sections/typ.ts +55 -0
  44. package/compat/sections/uni.ts +13 -0
  45. package/compat/sections/win.ts +43 -0
  46. package/compat/smoke-baseline.json +3 -0
  47. package/compat/unsupported-register.json +2526 -0
  48. package/dist/api/bind.d.ts +10 -0
  49. package/dist/api/database.d.ts +69 -0
  50. package/dist/api/statement.d.ts +51 -0
  51. package/dist/ast/nodes.d.ts +810 -0
  52. package/dist/constraints/enforce.d.ts +39 -0
  53. package/dist/errors/error.d.ts +24 -0
  54. package/dist/executor/ddl.d.ts +23 -0
  55. package/dist/executor/dml.d.ts +10 -0
  56. package/dist/executor/execute.d.ts +11 -0
  57. package/dist/executor/relation.d.ts +60 -0
  58. package/dist/executor/select.d.ts +41 -0
  59. package/dist/executor/session.d.ts +21 -0
  60. package/dist/executor/triggers-exec.d.ts +1 -0
  61. package/dist/executor/triggers.d.ts +16 -0
  62. package/dist/executor/window.d.ts +13 -0
  63. package/dist/expressions/context.d.ts +23 -0
  64. package/dist/expressions/eval.d.ts +38 -0
  65. package/dist/expressions/operators.d.ts +8 -0
  66. package/dist/expressions/pattern.d.ts +20 -0
  67. package/dist/functions/aggregates.d.ts +17 -0
  68. package/dist/functions/array-fns.d.ts +2 -0
  69. package/dist/functions/datetime-fns.d.ts +26 -0
  70. package/dist/functions/datetime-registry.d.ts +2 -0
  71. package/dist/functions/json-fns.d.ts +7 -0
  72. package/dist/functions/math-fns.d.ts +2 -0
  73. package/dist/functions/misc-fns.d.ts +8 -0
  74. package/dist/functions/scalar.d.ts +10 -0
  75. package/dist/functions/srf.d.ts +12 -0
  76. package/dist/functions/string-fns.d.ts +4 -0
  77. package/dist/functions/tsearch-fns.d.ts +2 -0
  78. package/dist/functions/util.d.ts +14 -0
  79. package/dist/functions/window.d.ts +7 -0
  80. package/dist/index.d.ts +28 -0
  81. package/dist/index.js +19591 -0
  82. package/dist/index.js.map +7 -0
  83. package/dist/lexer/tokenize.d.ts +8 -0
  84. package/dist/parser/index.d.ts +1 -0
  85. package/dist/parser/parser.d.ts +96 -0
  86. package/dist/runtime/clock.d.ts +14 -0
  87. package/dist/runtime/index.d.ts +3 -0
  88. package/dist/runtime/options.d.ts +25 -0
  89. package/dist/runtime/prng.d.ts +48 -0
  90. package/dist/schema/catalog-tables.d.ts +1 -0
  91. package/dist/schema/catalog.d.ts +12 -0
  92. package/dist/serialization/codec.d.ts +29 -0
  93. package/dist/serialization/index.d.ts +1 -0
  94. package/dist/storage/database-state.d.ts +216 -0
  95. package/dist/transactions/manager.d.ts +25 -0
  96. package/dist/tsearch/stem.d.ts +5 -0
  97. package/dist/tsearch/tsearch.d.ts +59 -0
  98. package/dist/types/cast.d.ts +26 -0
  99. package/dist/types/compare.d.ts +18 -0
  100. package/dist/types/datetime.d.ts +68 -0
  101. package/dist/types/jsonb.d.ts +45 -0
  102. package/dist/types/numeric.d.ts +69 -0
  103. package/dist/types/resolve.d.ts +15 -0
  104. package/dist/types/timezone.d.ts +9 -0
  105. package/dist/types/value.d.ts +99 -0
  106. package/dist/unstable.d.ts +15 -0
  107. package/dist/unstable.js +19153 -0
  108. package/dist/unstable.js.map +7 -0
  109. package/package.json +120 -0
@@ -0,0 +1,10 @@
1
+ import { type Datum, type OutputCtx, type TypedValue, type TypeId } from "../types/value.js";
2
+ /** JavaScript values accepted as query parameters (`$1..$n`). */
3
+ export type BindValue = null | undefined | boolean | number | bigint | string | Uint8Array | Date;
4
+ /** JavaScript values produced in query result rows. */
5
+ export type JsValue = null | boolean | number | bigint | string | Uint8Array;
6
+ /** A result row keyed by column name. */
7
+ export type QueryRow = Record<string, JsValue>;
8
+ export declare function bindValueToTyped(value: BindValue, index: number): TypedValue;
9
+ /** Convert an engine datum to the public JS value for result rows. */
10
+ export declare function datumToJs(t: TypeId, v: Datum, ctx: OutputCtx): JsValue;
@@ -0,0 +1,69 @@
1
+ import { type DatabaseOptions, type RandomMode } from "../runtime/index.js";
2
+ import type { BindValue, QueryRow } from "./bind.js";
3
+ import { Statement } from "./statement.js";
4
+ /**
5
+ * Pure TypeScript in-memory PostgreSQL database.
6
+ *
7
+ * Deterministic by default: `random()` / `gen_random_uuid()` use a seeded PRNG
8
+ * (`seed` defaults to `1`) and `now()` / `current_timestamp` use a fixed clock
9
+ * (`2000-01-01T00:00:00.000Z`). Pass `{ random: "os" }` and `{ now: "system" }`
10
+ * for PostgreSQL-like entropy and wall-clock time. No filesystem, no WASM.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { Database } from "@crvouga/postgres-mem";
15
+ *
16
+ * const db = new Database();
17
+ * db.exec("CREATE TABLE users (id serial PRIMARY KEY, name text NOT NULL)");
18
+ * db.prepare("INSERT INTO users (name) VALUES ($1)").run("Alice");
19
+ * const users = db.query<{ id: number; name: string }>("SELECT * FROM users");
20
+ * ```
21
+ */
22
+ export declare class Database {
23
+ /** Seed used to construct the PRNG. Ignored when {@link randomMode} is `"os"`. */
24
+ readonly seed: number | bigint;
25
+ /** Entropy mode for `random()` / `gen_random_uuid()`. */
26
+ readonly randomMode: RandomMode;
27
+ private closed;
28
+ private transactionSequence;
29
+ private apiTransactionDepth;
30
+ constructor(options?: DatabaseOptions);
31
+ /**
32
+ * Execute SQL for its side effects. Multiple semicolon-separated statements
33
+ * are allowed. Does not accept bind parameters — use {@link prepare} / {@link query}.
34
+ */
35
+ exec(sql: string): void;
36
+ /** Execute a single-statement query and return all rows keyed by column name. */
37
+ query<T = QueryRow>(sql: string, params?: readonly BindValue[]): T[];
38
+ /** Compile a single SQL statement into a reusable {@link Statement}. */
39
+ prepare(sql: string): Statement;
40
+ /**
41
+ * Run `fn` inside a transaction. Commits on success; rolls back if `fn`
42
+ * throws. Nested calls use savepoints.
43
+ */
44
+ transaction<T>(fn: () => T): T;
45
+ /**
46
+ * Serialize schema, rows, PRNG state, and clock into a `PGMM` snapshot blob.
47
+ * Restore with {@link restore}. Not an on-disk PostgreSQL format.
48
+ */
49
+ snapshot(): Uint8Array;
50
+ /** Replace this database's contents with a blob from {@link snapshot}. */
51
+ restore(snapshot: Uint8Array): void;
52
+ /** Close the database. Further SQL throws. Idempotent. */
53
+ close(): void;
54
+ /**
55
+ * Execute `COPY table [(cols)] FROM STDIN` with the given text/csv payload
56
+ * (the API-level analog of psql's `\copy` / the wire-protocol copy-in stream).
57
+ * Returns the number of rows copied.
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * db.copyFrom("COPY t (a, b) FROM STDIN", "1\tx\n2\ty\n");
62
+ * ```
63
+ */
64
+ copyFrom(sql: string, data: string): number;
65
+ /** Rows affected by the most recent INSERT / UPDATE / DELETE. */
66
+ get changes(): number;
67
+ private prepareSingle;
68
+ }
69
+ export type { DatabaseOptions };
@@ -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
+ }