@crvouga/mockingbird-service-postgres 1.3.1 → 1.5.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.
@@ -2,10 +2,27 @@ import { type DatabaseOptions, type Int8Mode, type RandomMode } from "../runtime
2
2
  import type { BindValue, JsValue, QueryRow } from "./bind.js";
3
3
  import { type Snapshot } from "./snapshot.js";
4
4
  import { Statement } from "./statement.js";
5
+ /** A timeline checkpoint without its retained snapshot value. */
6
+ export interface HistoryCheckpoint {
7
+ id: string;
8
+ branch: string;
9
+ parent: string | null;
10
+ at: number;
11
+ }
12
+ /** Branch heads and checkpoints owned by the shared Timeline. */
13
+ export interface DatabaseHistory {
14
+ readonly size: number;
15
+ head(branch?: string): HistoryCheckpoint | undefined;
16
+ branches(): Readonly<Record<string, string>>;
17
+ checkpoints(): readonly HistoryCheckpoint[];
18
+ }
5
19
  /** Additive time-travel controls for read APIs. */
6
20
  export interface QueryOptions {
7
- /** Execute against this immutable checkpoint instead of the live database. */
8
- at?: Snapshot;
21
+ /**
22
+ * Execute against this immutable snapshot, or a timeline checkpoint id,
23
+ * instead of the live database.
24
+ */
25
+ at?: Snapshot | string;
9
26
  }
10
27
  /**
11
28
  * Pure TypeScript in-memory PostgreSQL database.
@@ -35,6 +52,8 @@ export declare class Database {
35
52
  private closed;
36
53
  private transactionSequence;
37
54
  private apiTransactionDepth;
55
+ private historyBranch;
56
+ private originId;
38
57
  constructor(options?: DatabaseOptions);
39
58
  /**
40
59
  * Execute SQL for its side effects. Multiple semicolon-separated statements
@@ -65,6 +84,19 @@ export declare class Database {
65
84
  snapshot(): Snapshot;
66
85
  /** Alias for {@link snapshot}, naming the value as a timeline checkpoint. */
67
86
  checkpoint(): Snapshot;
87
+ /**
88
+ * Commit the live state onto `branch` (default: the branch this database follows).
89
+ * Does not switch the live database. Illegal inside a transaction.
90
+ */
91
+ record(branch?: string): HistoryCheckpoint;
92
+ /** Point `name` at a checkpoint without copying rows or switching the live database. */
93
+ fork(name: string, at?: string): HistoryCheckpoint;
94
+ /** Move `branch` to `id` and install that checkpoint as the live database. */
95
+ checkout(id: string, branch?: string): HistoryCheckpoint;
96
+ /** Return the live database to the origin checkpoint on `main`. */
97
+ reset(): HistoryCheckpoint;
98
+ /** Checkpoint ids and branch heads. Values stay inside the timeline. */
99
+ get history(): DatabaseHistory;
68
100
  /** Open a copy-on-write branch from `at`, or from the current state when omitted. */
69
101
  branch(at?: Snapshot): Database;
70
102
  /** Close the database. Further SQL throws. Idempotent. */
@@ -82,6 +114,9 @@ export declare class Database {
82
114
  copyFrom(sql: string, data: string): number;
83
115
  /** Rows affected by the most recent INSERT / UPDATE / DELETE. */
84
116
  get changes(): number;
117
+ private historyError;
118
+ private captureLive;
119
+ private install;
85
120
  private prepareSingle;
86
121
  }
87
122
  export { Snapshot } from "./snapshot.js";
@@ -501,7 +501,8 @@ export interface CreateIndexStmt {
501
501
  expr: Expr;
502
502
  dir: "asc" | "desc" | null;
503
503
  nulls: "first" | "last" | null;
504
- opclass: string | null;
504
+ /** Operator class name, possibly schema-qualified (`public.gin_trgm_ops`). */
505
+ opclass: string[] | null;
505
506
  }>;
506
507
  readonly include: string[];
507
508
  readonly where: Expr | null;
@@ -547,6 +548,14 @@ export interface CreateSchemaStmt {
547
548
  readonly name: string;
548
549
  readonly ifNotExists: boolean;
549
550
  }
551
+ export interface CreateExtensionStmt {
552
+ readonly type: "create_extension";
553
+ readonly name: string;
554
+ readonly ifNotExists: boolean;
555
+ readonly schema: string | null;
556
+ readonly version: string | null;
557
+ readonly cascade: boolean;
558
+ }
550
559
  export interface CreateEnumStmt {
551
560
  readonly type: "create_enum";
552
561
  readonly name: string[];
@@ -809,4 +818,4 @@ export interface DoStmt {
809
818
  readonly language: string;
810
819
  readonly body: string;
811
820
  }
812
- export type Statement = SelectStmt | InsertStmt | UpdateStmt | DeleteStmt | CreateTableStmt | CreateTableAsStmt | CreateIndexStmt | CreateViewStmt | CreateSequenceStmt | AlterSequenceStmt | CreateSchemaStmt | CreateEnumStmt | AlterEnumStmt | CreateDomainStmt | CreateFunctionStmt | CreateTriggerStmt | AlterTableStmt | AlterViewStmt | AlterIndexStmt | AlterSchemaStmt | DropStmt | TruncateStmt | RefreshMaterializedViewStmt | TransactionStmt | SetStmt | ShowStmt | ResetStmt | PrepareStmt | ExecuteStmt | DeallocateStmt | ExplainStmt | CopyStmt | CommentStmt | NoOpStmt | DoStmt;
821
+ export type Statement = SelectStmt | InsertStmt | UpdateStmt | DeleteStmt | CreateTableStmt | CreateTableAsStmt | CreateIndexStmt | CreateViewStmt | CreateSequenceStmt | AlterSequenceStmt | CreateSchemaStmt | CreateExtensionStmt | CreateEnumStmt | AlterEnumStmt | CreateDomainStmt | CreateFunctionStmt | CreateTriggerStmt | AlterTableStmt | AlterViewStmt | AlterIndexStmt | AlterSchemaStmt | DropStmt | TruncateStmt | RefreshMaterializedViewStmt | TransactionStmt | SetStmt | ShowStmt | ResetStmt | PrepareStmt | ExecuteStmt | DeallocateStmt | ExplainStmt | CopyStmt | CommentStmt | NoOpStmt | DoStmt;
@@ -1,4 +1,4 @@
1
- import type { AlterEnumStmt, AlterIndexStmt, AlterSchemaStmt, AlterSequenceStmt, AlterTableStmt, AlterViewStmt, CreateDomainStmt, CreateEnumStmt, CreateFunctionStmt, CreateIndexStmt, CreateSchemaStmt, CreateSequenceStmt, CreateTableAsStmt, CreateTableStmt, CreateTriggerStmt, CreateViewStmt, DropStmt, RefreshMaterializedViewStmt, SequenceOptions, TruncateStmt } from "../ast/nodes.js";
1
+ import type { AlterEnumStmt, AlterIndexStmt, AlterSchemaStmt, AlterSequenceStmt, AlterTableStmt, AlterViewStmt, CreateDomainStmt, CreateEnumStmt, CreateExtensionStmt, CreateFunctionStmt, CreateIndexStmt, CreateSchemaStmt, CreateSequenceStmt, CreateTableAsStmt, CreateTableStmt, CreateTriggerStmt, CreateViewStmt, DropStmt, RefreshMaterializedViewStmt, SequenceOptions, TruncateStmt } from "../ast/nodes.js";
2
2
  import { SchemaData, type SequenceData } from "../storage/database-state.js";
3
3
  import { type ExecEnv, type ExecResult } from "./relation.js";
4
4
  export declare function buildSequence(env: ExecEnv, schema: SchemaData, name: string, options: SequenceOptions, temp: boolean): SequenceData;
@@ -9,6 +9,7 @@ export declare function executeCreateTableAs(env: ExecEnv, stmt: CreateTableAsSt
9
9
  export declare function executeCreateIndex(env: ExecEnv, stmt: CreateIndexStmt): ExecResult;
10
10
  export declare function executeCreateView(env: ExecEnv, stmt: CreateViewStmt): ExecResult;
11
11
  export declare function executeRefreshMatView(env: ExecEnv, stmt: RefreshMaterializedViewStmt): ExecResult;
12
+ export declare function executeCreateExtension(env: ExecEnv, stmt: CreateExtensionStmt): ExecResult;
12
13
  export declare function executeCreateSchema(env: ExecEnv, stmt: CreateSchemaStmt): ExecResult;
13
14
  export declare function executeCreateEnum(env: ExecEnv, stmt: CreateEnumStmt): ExecResult;
14
15
  export declare function executeAlterEnum(env: ExecEnv, stmt: AlterEnumStmt): ExecResult;
@@ -40,6 +40,11 @@ export interface ExecEnv {
40
40
  /** CTE results by name (WITH clause); shadowed inner-to-outer */
41
41
  ctes: Map<string, Relation>;
42
42
  outer: RowScope | null;
43
+ /**
44
+ * CREATE/REFRESH MATERIALIZED VIEW … WITH NO DATA. An unpopulated matview
45
+ * contributes its column shape and no rows, matching PostgreSQL planning.
46
+ */
47
+ allowUnpopulatedMatviews?: boolean;
43
48
  }
44
49
  export declare function childEnv(env: ExecEnv, outer: RowScope | null): ExecEnv;
45
50
  export declare function withCtes(env: ExecEnv, ctes: Map<string, Relation>): ExecEnv;
@@ -0,0 +1,9 @@
1
+ import type { EngineCtx } from "../expressions/context.js";
2
+ import type { DatabaseState, FunctionData } from "../storage/database-state.js";
3
+ import type { TypedValue } from "../types/value.js";
4
+ /** pgcrypto `digest(data, type)` for the algorithms PostgreSQL documents as standard. */
5
+ export declare function digestBytes(data: Uint8Array, algorithm: string): Uint8Array;
6
+ export declare function pgcryptoInstalled(state: DatabaseState): boolean;
7
+ export declare function installPgcrypto(state: DatabaseState, schemaName: string): void;
8
+ export declare function uninstallPgcrypto(state: DatabaseState): boolean;
9
+ export declare function evalPgcryptoFunction(ctx: EngineCtx, fn: FunctionData, args: TypedValue[]): TypedValue;
@@ -0,0 +1,24 @@
1
+ import type { CreateIndexStmt } from "../ast/nodes.js";
2
+ import type { EngineCtx } from "../expressions/context.js";
3
+ import type { DatabaseState, FunctionData, TableData } from "../storage/database-state.js";
4
+ import { type TypedValue } from "../types/value.js";
5
+ export declare function pgTrgmSchema(state: DatabaseState): string | null;
6
+ export declare function pgTrgmInstalled(state: DatabaseState): boolean;
7
+ export declare function installPgTrgm(state: DatabaseState, schemaName: string): void;
8
+ export declare function uninstallPgTrgm(state: DatabaseState): boolean;
9
+ export declare function evalPgTrgmFunction(ctx: EngineCtx, fn: FunctionData, args: TypedValue[]): TypedValue;
10
+ /** `text <% text` — word similarity at the default threshold. Absent until pg_trgm is installed. */
11
+ export declare function evalTrgmWordSimilar(ctx: EngineCtx, left: TypedValue, right: TypedValue): TypedValue;
12
+ /**
13
+ * Reject `gin_trgm_ops` unless pg_trgm is installed for access method gin.
14
+ * A single text/varchar column is stored as an ordinary non-unique index:
15
+ * lookup speed is not modelled.
16
+ */
17
+ export declare function assertGinTrgmIndex(state: DatabaseState, table: TableData, stmt: CreateIndexStmt): void;
18
+ /** `|intersection| / |union|` as float4. Either empty trigram set yields 0. */
19
+ export declare function trigramSimilarity(left: string, right: string): number;
20
+ /**
21
+ * Greatest similarity between the trigram set of `pattern` and any continuous
22
+ * extent of the ordered trigrams of `haystack` (PostgreSQL `word_similarity`).
23
+ */
24
+ export declare function wordSimilarity(pattern: string, haystack: string): number;
package/dist/index.d.ts CHANGED
@@ -23,6 +23,6 @@
23
23
  */
24
24
  import "./serialization/codec.js";
25
25
  export type { BindValue, JsValue, QueryRow } from "./api/bind.js";
26
- export { Database, type DatabaseOptions, type QueryOptions, type RegisterFunctionOptions, Snapshot, } from "./api/database.js";
26
+ export { Database, type DatabaseHistory, type DatabaseOptions, type HistoryCheckpoint, type QueryOptions, type RegisterFunctionOptions, Snapshot, } from "./api/database.js";
27
27
  export { type ResultSet, type RunResult, Statement } from "./api/statement.js";
28
28
  export { type ErrorCategory, PostgresError } from "./errors/error.js";