palgres 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.
Files changed (50) hide show
  1. package/LICENSE +27 -0
  2. package/README.md +2 -2
  3. package/dist/chunks/5DSZT7RI.js +5 -0
  4. package/dist/chunks/7676QADW.js +2 -0
  5. package/dist/chunks/W7VRQDZA.js +2 -0
  6. package/dist/dsl.js +2 -0
  7. package/dist/index.js +372 -0
  8. package/dist/testing.js +2 -0
  9. package/package.json +32 -4
  10. package/types/internal/src/data.d.ts +15 -0
  11. package/types/palgres/src/adopt.d.ts +17 -0
  12. package/types/palgres/src/connect.d.ts +37 -0
  13. package/types/palgres/src/dsl/columns.d.ts +388 -0
  14. package/types/palgres/src/dsl/foreign-key.d.ts +33 -0
  15. package/types/palgres/src/dsl/index.d.ts +11 -0
  16. package/types/palgres/src/dsl/policy.d.ts +181 -0
  17. package/types/palgres/src/dsl/raw.d.ts +43 -0
  18. package/types/palgres/src/dsl/schema.d.ts +288 -0
  19. package/types/palgres/src/dsl/to-model.d.ts +6 -0
  20. package/types/palgres/src/forget.d.ts +12 -0
  21. package/types/palgres/src/index.d.ts +15 -0
  22. package/types/palgres/src/introspect.d.ts +17 -0
  23. package/types/palgres/src/migrate.d.ts +189 -0
  24. package/types/palgres/src/model.d.ts +181 -0
  25. package/types/palgres/src/plan.d.ts +107 -0
  26. package/types/palgres/src/pull.d.ts +13 -0
  27. package/types/palgres/src/query/admission.d.ts +80 -0
  28. package/types/palgres/src/query/catalog.d.ts +50 -0
  29. package/types/palgres/src/query/client.d.ts +154 -0
  30. package/types/palgres/src/query/data-mark.d.ts +7 -0
  31. package/types/palgres/src/query/diagnostics.d.ts +76 -0
  32. package/types/palgres/src/query/driver.d.ts +299 -0
  33. package/types/palgres/src/query/env-gen.d.ts +14 -0
  34. package/types/palgres/src/query/errors.d.ts +168 -0
  35. package/types/palgres/src/query/guards.d.ts +62 -0
  36. package/types/palgres/src/query/index.d.ts +76 -0
  37. package/types/palgres/src/query/options.d.ts +75 -0
  38. package/types/palgres/src/query/relations.d.ts +15 -0
  39. package/types/palgres/src/query/repository.d.ts +42 -0
  40. package/types/palgres/src/query/request.d.ts +54 -0
  41. package/types/palgres/src/query/search.d.ts +55 -0
  42. package/types/palgres/src/query/surface.d.ts +9 -0
  43. package/types/palgres/src/query/tx-plan.d.ts +229 -0
  44. package/types/palgres/src/query/typed.d.ts +537 -0
  45. package/types/palgres/src/query/unique.d.ts +16 -0
  46. package/types/palgres/src/search-worker.d.ts +36 -0
  47. package/types/palgres/src/search.d.ts +107 -0
  48. package/types/palgres/src/testing/fake.d.ts +22 -0
  49. package/types/palgres/src/testing/index.d.ts +2 -0
  50. package/types/palgres/src/units.d.ts +24 -0
@@ -0,0 +1,229 @@
1
+ import type { ColRefOf, Env, HasOnly, NoEnv, SqlFragment, WhereOpWith } from './typed.js';
2
+ /** A plan handle was used as a value — awaited, coerced, serialised. Thrown while the plan is being built: nothing was sent. */
3
+ export declare class TxRefError extends Error {
4
+ constructor(message: string);
5
+ }
6
+ /** The plan breaks a rule the executor would reject; named here, on the line that wrote it. */
7
+ export declare class TxPlanError extends Error {
8
+ constructor(message: string);
9
+ }
10
+ /** A backwards reference to an earlier operation's single row. */
11
+ export interface TxWireRef {
12
+ $ref: {
13
+ op: number;
14
+ field: string;
15
+ };
16
+ }
17
+ /** A call from the closed set: inc/dec read the column's current value, now() is the server's clock — less ago()'s span, when it has one. */
18
+ export interface TxWireExpr {
19
+ $expr: {
20
+ fn: 'inc' | 'dec';
21
+ by: number | string;
22
+ } | {
23
+ fn: 'now';
24
+ minus?: TxSpan;
25
+ };
26
+ }
27
+ export type TxWireValue = TxWireRef | TxWireExpr | unknown;
28
+ /** The author's expectation about an operation's row count; `slot` indexes the client-side error table — the Error object itself never travels. */
29
+ export interface TxWireGuard {
30
+ kind: 'one' | 'none' | 'atLeast' | 'atMost';
31
+ n: number;
32
+ slot: number;
33
+ }
34
+ /** One operation. A field is omitted, never null: each kind carries exactly its own. */
35
+ export interface TxWireOp {
36
+ /** upsert and insertMany: the columns a collision is matched on. An insertMany that ignores without them skips a collision with any unique key. */
37
+ onConflict?: readonly string[];
38
+ /** insertMany only: what a collision does. Absent: no ON CONFLICT at all. */
39
+ action?: 'ignore' | 'update';
40
+ op: 'insert' | 'insertMany' | 'upsert' | 'update' | 'delete' | 'select';
41
+ table: string;
42
+ values?: Record<string, TxWireValue>;
43
+ rows?: Record<string, TxWireValue>[];
44
+ set?: Record<string, TxWireValue>;
45
+ where?: Record<string, TxWireValue>;
46
+ limit?: number;
47
+ lock?: 'update';
48
+ guard?: TxWireGuard;
49
+ }
50
+ export interface TxPlanBody {
51
+ ops: TxWireOp[];
52
+ }
53
+ export interface TxPlanOpResult {
54
+ rows: Record<string, unknown>[];
55
+ rows_affected: number;
56
+ }
57
+ export interface TxPlanResponse {
58
+ results: TxPlanOpResult[];
59
+ }
60
+ /** What the executor's rejection carries: on a failed guard, the slot of the expectation, which maps back to the author's Error. */
61
+ export interface TxPlanRejection {
62
+ error_code?: string;
63
+ slot?: number;
64
+ }
65
+ declare const refBrand: unique symbol;
66
+ declare const rowBrand: unique symbol;
67
+ declare const rowsBrand: unique symbol;
68
+ /**
69
+ * Makes a handle a compile error to `await`: a callable `then` whose
70
+ * signature is not PromiseLike is exactly what TypeScript rejects (TS1320,
71
+ * and TS1058 for `async () => handle`).
72
+ */
73
+ export interface NotAwaitable {
74
+ /** Not a promise. Nothing here has run yet; there is nothing to await. */
75
+ then(doNotAwaitAPlanHandle: 'a transaction plan is built synchronously'): never;
76
+ }
77
+ /** A value the server will produce for a column of a row this plan writes or reads. */
78
+ export interface Ref<T> extends NotAwaitable {
79
+ readonly [refBrand]: T;
80
+ }
81
+ /** The brand of a single-row handle; `Materialized` turns `return row` into the whole row. */
82
+ export interface TxRowHandle<Row> extends NotAwaitable {
83
+ readonly [rowBrand]: Row;
84
+ }
85
+ /** A row the plan is known to produce exactly one of; every property is a Ref. Only .expectOne(err) makes one. */
86
+ export type TxRow<Row> = {
87
+ readonly [K in keyof Row]: Ref<Row[K]>;
88
+ } & TxRowHandle<Row>;
89
+ /** An operation's result before an expectation is declared about it — at most one expectation per operation. */
90
+ export interface TxRows<Row> extends NotAwaitable {
91
+ readonly [rowsBrand]: Row;
92
+ /** Exactly one row, and read it; on any other count the plan rolls back and `error` is thrown. */
93
+ expectOne(error: Error): TxRow<Row>;
94
+ /** No row (e.g. "this membership must not exist yet"). */
95
+ expectNone(error: Error): void;
96
+ expectAtLeast(n: number, error: Error): void;
97
+ expectAtMost(n: number, error: Error): void;
98
+ }
99
+ /** now() — the server's clock, wherever a value goes; ago(span) is it less a span. */
100
+ export interface TxNow extends NotAwaitable {
101
+ readonly $expr: {
102
+ fn: 'now';
103
+ minus?: TxSpan;
104
+ };
105
+ }
106
+ /** A span of the clock, in whole units: what ago() takes off now(). */
107
+ export interface TxSpan {
108
+ readonly days?: number;
109
+ readonly hours?: number;
110
+ readonly minutes?: number;
111
+ readonly seconds?: number;
112
+ }
113
+ /** increment(n) / decrement(n): the column's current value, changed. Only in an update's set. */
114
+ export interface TxColumnExpr extends NotAwaitable {
115
+ readonly $expr: {
116
+ fn: 'inc' | 'dec';
117
+ by: number | string;
118
+ };
119
+ }
120
+ /** The callback's return type as it comes back: a Ref is its value, a TxRow its row. */
121
+ export type Materialized<T> = T extends Ref<infer U> ? U : T extends TxRowHandle<infer R> ? R : T extends TxRows<unknown> ? 'a TxRows cannot leave the transaction callback — read a row with .expectOne(err) first' : T extends Date ? T : T extends object ? {
122
+ [K in keyof T]: Materialized<T[K]>;
123
+ } : T;
124
+ /** An INSERT's value: a literal, an earlier row's field, or now(). inc/dec read a current value, which an inserted row has none of. */
125
+ export type TxInsertValue<V> = V | Ref<V> | TxNow;
126
+ /** An update's value: an insert's, plus increment()/decrement() on a numeric-like column. */
127
+ export type TxSetValue<V> = V | Ref<V> | TxNow | (NonNullable<V> extends number | string ? TxColumnExpr : never);
128
+ export type TxInsertShape<Insert> = {
129
+ [K in keyof Insert]: TxInsertValue<Insert[K]>;
130
+ };
131
+ export type TxSetShape<Insert> = {
132
+ [K in keyof Insert]?: TxSetValue<Insert[K]>;
133
+ };
134
+ type TxWhereField<Row, K extends keyof Row> = WhereOpWith<Row[K], ColRefOf<Row, Row[K]> | Ref<Row[K]>>;
135
+ export type TxWhere<Row, Rels = unknown, E extends Env = NoEnv> = {
136
+ [K in keyof Row]?: TxWhereField<Row, K>;
137
+ } & {
138
+ OR?: (TxWhere<Row, Rels, E> | SqlFragment)[];
139
+ AND?: (TxWhere<Row, Rels, E> | SqlFragment)[];
140
+ NOT?: TxWhere<Row, Rels, E> | SqlFragment;
141
+ } & HasOnly<Rels, E>;
142
+ /**
143
+ * What a collision of an insertMany does: matched on `onConflict`, it is
144
+ * ignored or overwritten ("ignore" unless said); `action: "ignore"` alone
145
+ * skips a collision with ANY unique key — an overwrite needs the columns it
146
+ * is matched on.
147
+ */
148
+ export type TxInsertConflict<Key extends string = string> = {
149
+ onConflict: readonly Key[];
150
+ action?: 'ignore' | 'update';
151
+ } | {
152
+ onConflict?: undefined;
153
+ action: 'ignore';
154
+ };
155
+ export interface TxSelectOptions {
156
+ limit?: number;
157
+ /** A real FOR UPDATE row lock for the rest of the transaction. */
158
+ lock?: 'update';
159
+ }
160
+ /** One table, as the plan sees it. */
161
+ export interface TxTable<Row, Insert, Rels = unknown, E extends Env = NoEnv> {
162
+ /** Insert one row; .expectOne(err) to read its fields. */
163
+ insert(values: TxInsertShape<Insert>): TxRows<Row>;
164
+ /**
165
+ * Many rows in ONE statement, every row setting the same columns. Without
166
+ * opts a collision aborts the plan; `action: "ignore"` is ON CONFLICT DO
167
+ * NOTHING — the rows returned are the ones INSERTED, a collided row is
168
+ * absent — on the onConflict columns, or on any unique key without them;
169
+ * `action: "update"` sets every non-conflict column from the row.
170
+ * An empty list writes and sends nothing.
171
+ */
172
+ insertMany(rows: readonly TxInsertShape<Insert>[], opts?: TxInsertConflict<Extract<keyof Row, string>>): TxRows<Row>;
173
+ /** Insert, or overwrite on a collision of `onConflict` — the same as Database.<schema>.<table>.put(). */
174
+ put(values: TxInsertShape<Insert>, options: {
175
+ onConflict: readonly Extract<keyof Row, string>[];
176
+ }): TxRows<Row>;
177
+ /** Update every row matching `where` — the filter first, because it is the dangerous half. An unfiltered update is refused. */
178
+ updateWhere(where: TxWhere<Row, Rels, E>, set: TxSetShape<Insert>): TxRows<Row>;
179
+ /** Delete every row matching `where`; an unfiltered delete is refused. */
180
+ deleteWhere(where: TxWhere<Row, Rels, E>): TxRows<Row>;
181
+ /** Read rows, optionally locking them for the rest of the transaction. */
182
+ select(where?: TxWhere<Row, Rels, E>, options?: TxSelectOptions): TxRows<Row>;
183
+ }
184
+ /** The handle a plan callback gets: tables, nothing else — a read the plan does not write belongs outside it, as an ordinary value. */
185
+ export interface TxPlanHandle<TTables> {
186
+ /** @deprecated public's alias; write `tx.public`. */
187
+ tables: TTables;
188
+ }
189
+ /** The server's now(). */
190
+ export declare function now(): TxNow;
191
+ /**
192
+ * The server's clock less a span — `{ at: { gt: ago({ hours: 1 }) } }` is
193
+ * `at > now() - interval '1 hour'` — wherever now() goes. Both sides are read
194
+ * where the data is: a window counted on the app's clock moves with every
195
+ * machine's drift from the database's, and a row stamped `defaultNow()` is
196
+ * stamped by the database. Whole units, none negative: a sign kept in the span
197
+ * is seen by no code that reads the word "ago".
198
+ */
199
+ export declare function ago(span: TxSpan): TxNow;
200
+ /** Add `by` to the column's current value. Only in an update's set. */
201
+ export declare function increment(by: number | string): TxColumnExpr;
202
+ /** Subtract `by` from the column's current value. Only in an update's set. */
203
+ export declare function decrement(by: number | string): TxColumnExpr;
204
+ /** The wire form of an increment()/decrement()/now() handle, or null. The direct path (updateMany) reads it too: one spelling of "add to the column". */
205
+ export declare function columnExprOf(v: unknown): TxWireExpr['$expr'] | null;
206
+ /** The operations and the error table of one plan, while its callback runs. */
207
+ export declare class TxPlanBuilder {
208
+ private readonly ops;
209
+ /** Errors handed to expectations, by the slot a failed guard names. */
210
+ private readonly slots;
211
+ /** One table's operations, untyped; the public signatures put the schema's types on top. */
212
+ table(name: string): TxTable<Record<string, unknown>, Record<string, unknown>>;
213
+ private push;
214
+ attachGuard(opIndex: number, kind: TxWireGuard['kind'], n: number, error: Error): void;
215
+ body(): TxPlanBody;
216
+ errorForSlot(slot: number): Error | null;
217
+ }
218
+ /** The callback's return value with every handle replaced by what the server produced. Arrays and plain objects only: a class instance cannot hold a handle. */
219
+ export declare function materializeResult(value: unknown, results: TxPlanOpResult[]): unknown;
220
+ /** What runTxPlan needs: one call, one savepoint. */
221
+ export interface TxPlanTransport {
222
+ txPlan(plan: TxPlanBody): Promise<TxPlanResponse>;
223
+ }
224
+ /**
225
+ * Builds the plan, runs it and resolves the callback's return value. A
226
+ * callback that throws, or describes nothing, sends nothing.
227
+ */
228
+ export declare function runTxPlan<THandle>(transport: TxPlanTransport, handle: THandle, builder: TxPlanBuilder, fn: (tx: THandle) => unknown): Promise<unknown>;
229
+ export {};