shutro 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 (145) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +89 -2
  3. package/dist/audit/index.d.ts +57 -0
  4. package/dist/audit/index.js +94 -0
  5. package/dist/cli/bin.d.ts +2 -0
  6. package/dist/cli/bin.js +11 -0
  7. package/dist/cli/cli.d.ts +10 -0
  8. package/dist/cli/cli.js +149 -0
  9. package/dist/cli/config.d.ts +18 -0
  10. package/dist/cli/config.js +47 -0
  11. package/dist/cli/errors.d.ts +16 -0
  12. package/dist/cli/errors.js +23 -0
  13. package/dist/cli/generate/generate.d.ts +33 -0
  14. package/dist/cli/generate/generate.js +82 -0
  15. package/dist/cli/generate/render.d.ts +23 -0
  16. package/dist/cli/generate/render.js +200 -0
  17. package/dist/cli/index.d.ts +12 -0
  18. package/dist/cli/index.js +12 -0
  19. package/dist/cli/init.d.ts +2 -0
  20. package/dist/cli/init.js +65 -0
  21. package/dist/cli/inspect.d.ts +10 -0
  22. package/dist/cli/inspect.js +82 -0
  23. package/dist/cli/packages.d.ts +6 -0
  24. package/dist/cli/packages.js +6 -0
  25. package/dist/cli/project.d.ts +18 -0
  26. package/dist/cli/project.js +39 -0
  27. package/dist/core/compute/graph.d.ts +52 -0
  28. package/dist/core/compute/graph.js +162 -0
  29. package/dist/core/env/env.d.ts +295 -0
  30. package/dist/core/env/env.js +631 -0
  31. package/dist/core/env/identity.d.ts +33 -0
  32. package/dist/core/env/identity.js +49 -0
  33. package/dist/core/env/inspector.d.ts +55 -0
  34. package/dist/core/env/inspector.js +85 -0
  35. package/dist/core/env/time.d.ts +12 -0
  36. package/dist/core/env/time.js +38 -0
  37. package/dist/core/errors.d.ts +242 -0
  38. package/dist/core/errors.js +339 -0
  39. package/dist/core/index.d.ts +43 -0
  40. package/dist/core/index.js +34 -0
  41. package/dist/core/model/company.d.ts +6 -0
  42. package/dist/core/model/company.js +31 -0
  43. package/dist/core/model/faces.d.ts +42 -0
  44. package/dist/core/model/faces.js +1 -0
  45. package/dist/core/model/field.d.ts +446 -0
  46. package/dist/core/model/field.js +398 -0
  47. package/dist/core/model/generated.d.ts +75 -0
  48. package/dist/core/model/generated.js +1 -0
  49. package/dist/core/model/id.d.ts +33 -0
  50. package/dist/core/model/id.js +4 -0
  51. package/dist/core/model/model.d.ts +347 -0
  52. package/dist/core/model/model.js +538 -0
  53. package/dist/core/model/partition.d.ts +31 -0
  54. package/dist/core/model/partition.js +71 -0
  55. package/dist/core/money/currency.d.ts +39 -0
  56. package/dist/core/money/currency.js +37 -0
  57. package/dist/core/money/decimal.d.ts +26 -0
  58. package/dist/core/money/decimal.js +51 -0
  59. package/dist/core/money/fx.d.ts +60 -0
  60. package/dist/core/money/fx.js +109 -0
  61. package/dist/core/money/money.d.ts +84 -0
  62. package/dist/core/money/money.js +173 -0
  63. package/dist/core/money/quantity.d.ts +67 -0
  64. package/dist/core/money/quantity.js +108 -0
  65. package/dist/core/money/rounding.d.ts +11 -0
  66. package/dist/core/money/rounding.js +12 -0
  67. package/dist/core/recordset/audit.d.ts +25 -0
  68. package/dist/core/recordset/audit.js +205 -0
  69. package/dist/core/recordset/compute.d.ts +34 -0
  70. package/dist/core/recordset/compute.js +405 -0
  71. package/dist/core/recordset/constraints.d.ts +9 -0
  72. package/dist/core/recordset/constraints.js +61 -0
  73. package/dist/core/recordset/domain.d.ts +27 -0
  74. package/dist/core/recordset/domain.js +228 -0
  75. package/dist/core/recordset/onchange.d.ts +43 -0
  76. package/dist/core/recordset/onchange.js +207 -0
  77. package/dist/core/recordset/recordset.d.ts +101 -0
  78. package/dist/core/recordset/recordset.js +723 -0
  79. package/dist/core/recordset/relations.d.ts +15 -0
  80. package/dist/core/recordset/relations.js +13 -0
  81. package/dist/core/recordset/rls.d.ts +15 -0
  82. package/dist/core/recordset/rls.js +108 -0
  83. package/dist/core/recordset/security.d.ts +42 -0
  84. package/dist/core/recordset/security.js +184 -0
  85. package/dist/core/recordset/sequences.d.ts +31 -0
  86. package/dist/core/recordset/sequences.js +154 -0
  87. package/dist/core/recordset/sqlErrors.d.ts +6 -0
  88. package/dist/core/recordset/sqlErrors.js +39 -0
  89. package/dist/core/recordset/transitions.d.ts +10 -0
  90. package/dist/core/recordset/transitions.js +77 -0
  91. package/dist/core/recordset/types.d.ts +296 -0
  92. package/dist/core/recordset/types.js +8 -0
  93. package/dist/core/recordset/values.d.ts +27 -0
  94. package/dist/core/recordset/values.js +147 -0
  95. package/dist/core/recordset/writes.d.ts +37 -0
  96. package/dist/core/recordset/writes.js +581 -0
  97. package/dist/core/registry/ddl.d.ts +22 -0
  98. package/dist/core/registry/ddl.js +127 -0
  99. package/dist/core/registry/extension.d.ts +94 -0
  100. package/dist/core/registry/extension.js +90 -0
  101. package/dist/core/registry/module.d.ts +80 -0
  102. package/dist/core/registry/module.js +96 -0
  103. package/dist/core/registry/registry.d.ts +105 -0
  104. package/dist/core/registry/registry.js +496 -0
  105. package/dist/driver-pg/driver.d.ts +86 -0
  106. package/dist/driver-pg/driver.js +176 -0
  107. package/dist/driver-pg/errors.d.ts +72 -0
  108. package/dist/driver-pg/errors.js +182 -0
  109. package/dist/driver-pg/index.d.ts +7 -0
  110. package/dist/driver-pg/index.js +7 -0
  111. package/dist/i18n/index.d.ts +25 -0
  112. package/dist/i18n/index.js +51 -0
  113. package/dist/migrate/apply.d.ts +78 -0
  114. package/dist/migrate/apply.js +167 -0
  115. package/dist/migrate/catalog.d.ts +10 -0
  116. package/dist/migrate/catalog.js +122 -0
  117. package/dist/migrate/diff.d.ts +98 -0
  118. package/dist/migrate/diff.js +160 -0
  119. package/dist/migrate/errors.d.ts +21 -0
  120. package/dist/migrate/errors.js +31 -0
  121. package/dist/migrate/index.d.ts +18 -0
  122. package/dist/migrate/index.js +18 -0
  123. package/dist/migrate/render.d.ts +5 -0
  124. package/dist/migrate/render.js +101 -0
  125. package/dist/migrate/schema.d.ts +61 -0
  126. package/dist/migrate/schema.js +195 -0
  127. package/dist/security/index.d.ts +123 -0
  128. package/dist/security/index.js +262 -0
  129. package/dist/sequence/index.d.ts +63 -0
  130. package/dist/sequence/index.js +106 -0
  131. package/dist/sql/ast.d.ts +49 -0
  132. package/dist/sql/ast.js +47 -0
  133. package/dist/sql/builder/builder.d.ts +215 -0
  134. package/dist/sql/builder/builder.js +308 -0
  135. package/dist/sql/builder/expression.d.ts +80 -0
  136. package/dist/sql/builder/expression.js +126 -0
  137. package/dist/sql/compile.d.ts +18 -0
  138. package/dist/sql/compile.js +41 -0
  139. package/dist/sql/errors.d.ts +13 -0
  140. package/dist/sql/errors.js +16 -0
  141. package/dist/sql/index.d.ts +19 -0
  142. package/dist/sql/index.js +19 -0
  143. package/dist/sql/tag.d.ts +42 -0
  144. package/dist/sql/tag.js +95 -0
  145. package/package.json +82 -4
@@ -0,0 +1,446 @@
1
+ /**
2
+ * Field builders (`f.*`) and descriptors, RFC 0001 §7.3. A descriptor is frozen runtime metadata (what Kosh and
3
+ * the migrator read) plus phantom types that carry the field's read / create / write faces.
4
+ */
5
+ import type { Translations } from '../../i18n/index.js';
6
+ import { type FieldGroups, type FieldGroupsInput } from '../../security/index.js';
7
+ import type { Env } from '../env/env.js';
8
+ import type { Decimal } from '../money/decimal.js';
9
+ import type { Money } from '../money/money.js';
10
+ import type { Quantity } from '../money/quantity.js';
11
+ import type { Id, X2ManyCommands } from './id.js';
12
+ /** Every field kind. */
13
+ export type FieldKind = 'char' | 'text' | 'html' | 'boolean' | 'integer' | 'bigint' | 'float' | 'decimal' | 'monetary' | 'quantity' | 'date' | 'datetime' | 'selection' | 'state' | 'json' | 'binary' | 'many2one' | 'one2many' | 'many2many';
14
+ /** Index kinds. `'trigram'` needs `pg_trgm`, `'brin'` suits append-only time series. */
15
+ export type IndexKind = true | 'unique' | 'trigram' | 'brin';
16
+ /** Batch compute: receives the recordset to fill (typed by the schema view, RFC 0002). */
17
+ export type ComputeFn = (records: never) => void | Promise<void>;
18
+ /** Makes a computed field writable: receives the records whose computed value was written. */
19
+ export type InverseFn = (records: never) => void | Promise<void>;
20
+ /** The normalized `sequence` option of a field (RFC 0012 §7.1). */
21
+ export interface SequenceFieldOptions {
22
+ readonly code: string;
23
+ /** As given on the field; the definition is authoritative. */
24
+ readonly gapless: boolean | undefined;
25
+ readonly on: 'create' | 'manual';
26
+ readonly dateField: string | undefined;
27
+ }
28
+ /** Phantom flags that decide a field's faces (RFC 0001 §7.3). */
29
+ export interface FieldFlags {
30
+ readonly required: boolean;
31
+ /** Excluded from create and write: explicit `readonly`, a sequence, a compute without inverse, or a state. */
32
+ readonly readonly: boolean;
33
+ readonly computed: boolean;
34
+ readonly inverse: boolean;
35
+ readonly hasDefault: boolean;
36
+ readonly sequence: boolean;
37
+ /** The method name when `compute` is a string (phantom; drives the methods check in `defineModel`). */
38
+ readonly computeName?: string | undefined;
39
+ /** Persisted as a column (phantom; `undefined` means not stored). */
40
+ readonly store?: boolean | undefined;
41
+ }
42
+ /** Phantom read/write types and flags of a field. */
43
+ export interface FieldTypes<R, W, Fl extends FieldFlags> {
44
+ readonly read: R;
45
+ readonly write: W;
46
+ readonly flags: Fl;
47
+ }
48
+ /** Runtime metadata common to every field. */
49
+ export interface FieldBase<K extends FieldKind, R, W, Fl extends FieldFlags> {
50
+ readonly kind: K;
51
+ /** PostgreSQL column type. `numeric` for named precisions until the registry resolves them. */
52
+ readonly pgType: string;
53
+ readonly label: string | undefined;
54
+ readonly help: string | undefined;
55
+ readonly required: boolean;
56
+ readonly readonly: boolean;
57
+ /** Persisted as a column (`false` for non-stored computes and x2many relations). */
58
+ readonly store: boolean;
59
+ readonly computed: boolean;
60
+ readonly compute: ComputeFn | string | undefined;
61
+ readonly inverse: InverseFn | undefined;
62
+ readonly depends: readonly string[];
63
+ /** A value, or `(env) => value`. */
64
+ readonly default: unknown;
65
+ readonly index: IndexKind | undefined;
66
+ readonly copy: boolean;
67
+ readonly tracking: boolean;
68
+ /** Column-name override. */
69
+ readonly column: string | undefined;
70
+ readonly groups: FieldGroups;
71
+ readonly deprecated: string | undefined;
72
+ readonly sequence: SequenceFieldOptions | undefined;
73
+ readonly translate: boolean;
74
+ /** Stored encrypted with the application keys (RFC 0014 §7.6); char / text only. */
75
+ readonly encrypted: boolean;
76
+ /** Phantom. Never present at runtime. */
77
+ readonly '~types'?: FieldTypes<R, W, Fl>;
78
+ }
79
+ /** Options accepted by every builder. `V` is the field's write type. */
80
+ export interface CommonOptions<V> {
81
+ readonly label?: string;
82
+ readonly help?: string;
83
+ readonly required?: boolean;
84
+ readonly readonly?: boolean;
85
+ readonly default?: V | ((env: Env) => V);
86
+ readonly index?: IndexKind;
87
+ readonly copy?: boolean;
88
+ readonly tracking?: boolean;
89
+ readonly column?: string;
90
+ /** Groups that may read / write the field (RFC 0014 §7.4): an array for both, or { read, write }. */
91
+ readonly groups?: FieldGroupsInput;
92
+ readonly deprecated?: string;
93
+ /** Persist a computed field (recomputed in batches before commit). */
94
+ readonly store?: boolean;
95
+ /** Dotted paths the compute reads. Checked against the schema view (RFC 0002). */
96
+ readonly depends?: readonly string[];
97
+ /** Inline batch function, or the key of a model method shared by several fields. */
98
+ readonly compute?: ComputeFn | string;
99
+ readonly inverse?: InverseFn;
100
+ }
101
+ /** Options of text-like fields. */
102
+ export interface TextOptions extends CommonOptions<string> {
103
+ /** Store per-language values as JSONB (RFC 0001 §7.3). */
104
+ readonly translate?: boolean;
105
+ /** Encrypt at rest with the application's keys (RFC 0014 §7.6). Not searchable, not translatable. */
106
+ readonly encrypted?: boolean;
107
+ }
108
+ /** Options of `f.char`. */
109
+ export interface CharOptions extends TextOptions {
110
+ /** `varchar(size)`; unlimited `text` when omitted. */
111
+ readonly size?: number;
112
+ }
113
+ /** Options of `f.html`. */
114
+ export interface HtmlOptions extends TextOptions {
115
+ /** Sanitize on write (default `true`). */
116
+ readonly sanitize?: boolean;
117
+ }
118
+ /** Options of `f.decimal`. */
119
+ export interface DecimalOptions extends CommonOptions<Decimal | string> {
120
+ /** Digits after the point, or a named precision such as `'product.price'` (RFC 0006 §7.5). */
121
+ readonly scale: number | string;
122
+ /** Total digits (default 32). Only with a numeric `scale`. */
123
+ readonly precision?: number;
124
+ }
125
+ /** Options of `f.monetary`. */
126
+ export interface MonetaryOptions extends CommonOptions<Decimal | string> {
127
+ /** The sibling `many2one('res.currency')` field. Checked by `defineModel`. */
128
+ readonly currency: string;
129
+ }
130
+ /** Options of `f.quantity`. */
131
+ export interface QuantityOptions extends CommonOptions<Decimal | string> {
132
+ /** The sibling `many2one('uom.uom')` field. */
133
+ readonly uom: string;
134
+ readonly scale?: number | string;
135
+ }
136
+ /** Options of `f.selection`. */
137
+ export interface SelectionOptions<V extends string> extends CommonOptions<V> {
138
+ /** Let other modules add values (RFC 0002 S5). Consumers must then handle unknown options. */
139
+ readonly extensible?: boolean;
140
+ }
141
+ /** Events of a state: run once per batch when records enter / leave it (RFC 0010 §7.2). */
142
+ export interface StateHooks {
143
+ readonly onEnter?: (records: never) => unknown;
144
+ readonly onExit?: (records: never) => unknown;
145
+ }
146
+ /** A state with a label and optional events. */
147
+ export interface StateSpec extends StateHooks {
148
+ readonly label: string;
149
+ }
150
+ /** A state transition: source states, target, optional guard and events (RFC 0010 §7.1). */
151
+ export interface Transition<S extends string> {
152
+ readonly from: readonly S[];
153
+ readonly to: S;
154
+ /** Returns the records that may not transition (a recordset, records or ids); empty means go. */
155
+ readonly guard?: (records: never) => unknown;
156
+ /** User-facing message when the guard rejects records. */
157
+ readonly message?: string;
158
+ readonly before?: (records: never) => unknown;
159
+ readonly after?: (records: never) => unknown;
160
+ }
161
+ /** Options of `f.state`. */
162
+ export interface StateOptions<S extends Record<string, string | StateSpec>, T extends Record<string, Transition<keyof S & string>>> {
163
+ readonly states: S;
164
+ readonly initial: keyof S & string;
165
+ readonly transitions: T;
166
+ readonly label?: string;
167
+ readonly help?: string;
168
+ readonly tracking?: boolean;
169
+ readonly extensible?: boolean;
170
+ }
171
+ /** Options of `f.json`. */
172
+ export interface JsonOptions<V> extends CommonOptions<V> {
173
+ }
174
+ /** Options of `f.binary`. */
175
+ export interface BinaryOptions extends CommonOptions<Uint8Array> {
176
+ }
177
+ /** `ON DELETE` behaviour of a many2one. */
178
+ export type OnDelete = 'restrict' | 'cascade' | 'set null';
179
+ /** Options of `f.many2one`. */
180
+ export interface Many2oneOptions<T extends string> extends CommonOptions<Id<T>> {
181
+ readonly ondelete?: OnDelete;
182
+ /** The target must be shared or in the same company as the record (RFC 0003 §7.4). The model must be companyScoped. */
183
+ readonly checkCompany?: boolean;
184
+ }
185
+ /** Options of x2many fields (never stored as a column). */
186
+ export interface X2ManyOptions<T extends string> extends Omit<CommonOptions<X2ManyCommands<T>>, 'store' | 'index' | 'tracking'> {
187
+ }
188
+ /** Options of `f.many2many`. */
189
+ export interface Many2manyOptions<T extends string> extends X2ManyOptions<T> {
190
+ /** Junction table name; derived deterministically when omitted. */
191
+ readonly relation?: string;
192
+ }
193
+ /** The descriptor of a field whose faces are plain values. */
194
+ export interface ScalarField<K extends FieldKind, R, W, Fl extends FieldFlags> extends FieldBase<K, R, W, Fl> {
195
+ }
196
+ /** The write type of a text field: a map of translations too when `translate: true` (RFC 0011). */
197
+ export type TextWrite<O> = O extends {
198
+ readonly translate: true;
199
+ } ? string | Translations : string;
200
+ /** `f.char` descriptor. */
201
+ export interface CharField<Fl extends FieldFlags, W extends string | Translations = string> extends FieldBase<'char', string, W, Fl> {
202
+ readonly size: number | undefined;
203
+ }
204
+ /** `f.float` descriptor. */
205
+ export interface FloatField<Fl extends FieldFlags> extends FieldBase<'float', number, number, Fl> {
206
+ readonly discouraged: string;
207
+ }
208
+ /** `f.decimal` descriptor. */
209
+ export interface DecimalField<Fl extends FieldFlags> extends FieldBase<'decimal', Decimal, Decimal | string, Fl> {
210
+ readonly scale: number | string;
211
+ readonly precision: number | undefined;
212
+ }
213
+ /** `f.monetary` descriptor. */
214
+ export interface MonetaryField<C extends string, Fl extends FieldFlags> extends FieldBase<'monetary', Money, Decimal | string | Money, Fl> {
215
+ readonly currencyField: C;
216
+ }
217
+ /** `f.quantity` descriptor. */
218
+ export interface QuantityField<U extends string, Fl extends FieldFlags> extends FieldBase<'quantity', Quantity, Decimal | string | Quantity, Fl> {
219
+ readonly uomField: U;
220
+ readonly scale: number | string | undefined;
221
+ }
222
+ /** `f.selection` descriptor. */
223
+ export interface SelectionField<V extends string, Fl extends FieldFlags> extends FieldBase<'selection', V, V, Fl> {
224
+ /** `[value, label]` pairs, or the function of a dynamic selection. */
225
+ readonly selection: readonly (readonly [string, string])[] | (() => readonly string[]);
226
+ readonly extensible: boolean;
227
+ }
228
+ /** `f.state` descriptor. Never writable: values change through transitions. */
229
+ export interface StateField<S extends string, T extends Record<string, Transition<S>>, Fl extends FieldFlags> extends FieldBase<'state', S, S, Fl> {
230
+ readonly selection: readonly (readonly [string, string])[];
231
+ readonly initial: S;
232
+ readonly transitions: T;
233
+ /** Events per state (RFC 0010 §7.2). */
234
+ readonly stateHooks: Readonly<Record<string, StateHooks>>;
235
+ readonly extensible: boolean;
236
+ }
237
+ /** `f.json` descriptor. */
238
+ export interface JsonField<V, Fl extends FieldFlags> extends FieldBase<'json', V, V, Fl> {
239
+ }
240
+ /** `f.binary` descriptor. Never prefetched. */
241
+ export interface BinaryField<Fl extends FieldFlags> extends FieldBase<'binary', Uint8Array, Uint8Array, Fl> {
242
+ readonly prefetch: false;
243
+ }
244
+ /** `f.many2one` descriptor. The read face is the id until recordsets exist (RFC 0004). */
245
+ export interface Many2oneField<T extends string, Fl extends FieldFlags> extends FieldBase<'many2one', {
246
+ readonly id: Id<T>;
247
+ }, Id<T>, Fl> {
248
+ readonly target: T;
249
+ readonly ondelete: OnDelete;
250
+ readonly checkCompany: boolean;
251
+ }
252
+ /** `f.one2many` descriptor. */
253
+ export interface One2manyField<T extends string, Fl extends FieldFlags> extends FieldBase<'one2many', {
254
+ readonly model: T;
255
+ }, X2ManyCommands<T>, Fl> {
256
+ readonly target: T;
257
+ /** The `many2one` on the target that points back here. */
258
+ readonly inverseField: string;
259
+ }
260
+ /** `f.many2many` descriptor. */
261
+ export interface Many2manyField<T extends string, Fl extends FieldFlags> extends FieldBase<'many2many', {
262
+ readonly model: T;
263
+ }, X2ManyCommands<T>, Fl> {
264
+ readonly target: T;
265
+ readonly relation: string | undefined;
266
+ }
267
+ /** Any field descriptor. */
268
+ export type Field = FieldBase<FieldKind, unknown, unknown, FieldFlags>;
269
+ /** Any non-nullish value (what `{}` means in a conditional, spelled out). */
270
+ type Defined = NonNullable<unknown>;
271
+ /** The empty options object: the default when a builder is called without arguments. */
272
+ type NoOptions = Record<never, never>;
273
+ type IsTrue<O, K extends string> = O extends Record<K, true> ? true : false;
274
+ type Present<O, K extends string> = O extends Record<K, Defined | null> ? true : false;
275
+ /**
276
+ * Resolves a mapped object into a fresh structural type. Without it, two `FlagsOf` instantiations with the same
277
+ * result but different option objects are not *identical* to TypeScript (only mutually assignable), which breaks
278
+ * `toEqualTypeOf` between generated views (`F.*`, RFC 0002 §7.7) and declared fields.
279
+ */
280
+ type Resolved<T> = {
281
+ [K in keyof T]: T[K];
282
+ };
283
+ /** Derives the phantom flags from a builder's options and its kind defaults. */
284
+ export type FlagsOf<O, D extends {
285
+ required?: true;
286
+ readonly?: true;
287
+ hasDefault?: true;
288
+ } = NoOptions> = Resolved<RawFlagsOf<O, D>>;
289
+ type RawFlagsOf<O, D extends {
290
+ required?: true;
291
+ readonly?: true;
292
+ hasDefault?: true;
293
+ }> = {
294
+ readonly required: IsTrue<O, 'required'> extends true ? true : D extends {
295
+ required: true;
296
+ } ? true : false;
297
+ readonly computed: Present<O, 'compute'>;
298
+ readonly inverse: Present<O, 'inverse'>;
299
+ readonly sequence: Present<O, 'sequence'>;
300
+ readonly computeName: O extends {
301
+ compute: infer Cm extends string;
302
+ } ? Cm : undefined;
303
+ readonly store: Present<O, 'compute'> extends true ? IsTrue<O, 'store'> : true;
304
+ readonly hasDefault: Present<O, 'default'> extends true ? true : D extends {
305
+ hasDefault: true;
306
+ } ? true : false;
307
+ readonly readonly: IsTrue<O, 'readonly'> extends true ? true : Present<O, 'sequence'> extends true ? true : Present<O, 'compute'> extends true ? Present<O, 'inverse'> extends true ? false : true : D extends {
308
+ readonly: true;
309
+ } ? true : false;
310
+ };
311
+ /** Rejects option keys that the builder doesn't know (`requird: true`). */
312
+ type Exact<O, Allowed> = {
313
+ readonly [K in Exclude<keyof O, keyof Allowed>]: never;
314
+ };
315
+ /** `depends` / `inverse` / `store` only make sense with `compute`. */
316
+ type ComputeRules<O> = O extends {
317
+ compute: Defined;
318
+ } ? unknown : {
319
+ readonly depends?: never;
320
+ readonly inverse?: never;
321
+ readonly store?: never;
322
+ };
323
+ /** A required many2one can't be set to null on delete. */
324
+ type OnDeleteRules<O> = O extends {
325
+ required: true;
326
+ } ? {
327
+ readonly ondelete?: 'restrict' | 'cascade';
328
+ } : unknown;
329
+ type SequenceOption = {
330
+ readonly sequence?: string | {
331
+ readonly code: string;
332
+ /** Must agree with the definition when given. */
333
+ readonly gapless?: boolean;
334
+ /** `create` (default) fills the number on creation; `manual` stores '/' until assignNumber() (RFC 0012 §7.4). */
335
+ readonly on?: 'create' | 'manual';
336
+ /** The field whose value picks the period (and the placeholders); defaults to the environment's today. */
337
+ readonly dateField?: string;
338
+ };
339
+ };
340
+ type CharOpts = CharOptions & SequenceOption;
341
+ /** True when `value` is a field descriptor produced by `f.*`. */
342
+ export declare function isField(value: unknown): value is Field;
343
+ /** The field builders. See each method for its options; every descriptor is frozen metadata. */
344
+ export declare const f: {
345
+ /** Short text. `size` gives `varchar(size)`, otherwise `text`. */
346
+ char<const O extends CharOpts = NoOptions>(options?: O & Exact<O, CharOpts> & ComputeRules<O>): CharField<FlagsOf<O>, TextWrite<O>>;
347
+ /** Long text. */
348
+ text<const O extends TextOptions = NoOptions>(options?: O & Exact<O, TextOptions> & ComputeRules<O>): ScalarField<'text', string, TextWrite<O>, FlagsOf<O>>;
349
+ /** HTML, sanitized on write unless `sanitize: false`. */
350
+ html<const O extends HtmlOptions = NoOptions>(options?: O & Exact<O, HtmlOptions> & ComputeRules<O>): ScalarField<'html', string, TextWrite<O>, FlagsOf<O>> & {
351
+ readonly sanitize: boolean;
352
+ };
353
+ /** Never nullable; defaults to `false`. */
354
+ boolean<const O extends CommonOptions<boolean> = NoOptions>(options?: O & Exact<O, CommonOptions<boolean>> & ComputeRules<O>): ScalarField<'boolean', boolean, boolean, FlagsOf<O, {
355
+ required: true;
356
+ hasDefault: true;
357
+ }>>;
358
+ /** 32-bit integer. For counts and sequences, never for money or quantities. */
359
+ integer<const O extends CommonOptions<number> = NoOptions>(options?: O & Exact<O, CommonOptions<number>> & ComputeRules<O>): ScalarField<'integer', number, number, FlagsOf<O>>;
360
+ /** 64-bit integer. Read as a safe `number` (the driver rejects larger values); write `bigint` or `number`. */
361
+ bigint<const O extends CommonOptions<bigint | number> = NoOptions>(options?: O & Exact<O, CommonOptions<bigint | number>> & ComputeRules<O>): ScalarField<'bigint', number, bigint | number, FlagsOf<O>>;
362
+ /** Binary floating point. **Discouraged** for business values: use `decimal`, `monetary` or `quantity`. */
363
+ float<const O extends CommonOptions<number> = NoOptions>(options?: O & Exact<O, CommonOptions<number>> & ComputeRules<O>): FloatField<FlagsOf<O>>;
364
+ /** Exact decimal: `numeric(precision, scale)`, or `numeric` with a named precision resolved by the registry. */
365
+ decimal<const O extends DecimalOptions>(options: O & Exact<O, DecimalOptions> & ComputeRules<O>): DecimalField<FlagsOf<O>>;
366
+ /** An amount in the currency of the sibling `currency` field (read as `Money`, RFC 0006). */
367
+ monetary<const O extends MonetaryOptions>(options: O & Exact<O, MonetaryOptions> & ComputeRules<O>): MonetaryField<O['currency'], FlagsOf<O>>;
368
+ /** A quantity in the unit of the sibling `uom` field (read as `Quantity`). */
369
+ quantity<const O extends QuantityOptions>(options: O & Exact<O, QuantityOptions> & ComputeRules<O>): QuantityField<O['uom'], FlagsOf<O>>;
370
+ /** A calendar date (`'YYYY-MM-DD'`), never a JS `Date` (RFC 0001 W7). */
371
+ date<const O extends CommonOptions<string> = NoOptions>(options?: O & Exact<O, CommonOptions<string>> & ComputeRules<O>): ScalarField<'date', string, string, FlagsOf<O>>;
372
+ /** An instant (`timestamptz`), always UTC in storage. */
373
+ datetime<const O extends CommonOptions<Date> = NoOptions>(options?: O & Exact<O, CommonOptions<Date>> & ComputeRules<O>): ScalarField<'datetime', Date, Date, FlagsOf<O>>;
374
+ /**
375
+ * A closed set of values. The map form gives labels and a typed union; the function form is dynamic (`string`).
376
+ * @example f.selection({ consu: 'Goods', service: 'Service' }, { required: true, default: 'consu' })
377
+ */
378
+ selection: {
379
+ <const S extends Record<string, string>, const O extends SelectionOptions<keyof S & string> = NoOptions>(values: S, options?: O & Exact<O, SelectionOptions<keyof S & string>> & ComputeRules<O>): SelectionField<keyof S & string, FlagsOf<O>>;
380
+ <const O extends SelectionOptions<string> = NoOptions>(values: () => readonly string[], options?: O & Exact<O, SelectionOptions<string>> & ComputeRules<O>): SelectionField<string, FlagsOf<O>>;
381
+ };
382
+ /**
383
+ * A selection with declared transitions (RFC 0001 §7.3). Readonly: values change only through transitions.
384
+ * @example f.state({ states: { draft: 'Quotation', sale: 'Sales Order' }, initial: 'draft', transitions: { confirm: { from: ['draft'], to: 'sale' } } })
385
+ */
386
+ state<const S extends Record<string, string | StateSpec>, const T extends Record<string, Transition<keyof S & string>>>(options: StateOptions<S, T>): StateField<keyof S & string, T, {
387
+ required: true;
388
+ readonly: true;
389
+ computed: false;
390
+ inverse: false;
391
+ hasDefault: true;
392
+ sequence: false;
393
+ }>;
394
+ /** JSONB. Give the value type explicitly for now: `f.json<MyShape>()`. Standard Schema validation comes with constraints. */
395
+ json<V = unknown, const O extends JsonOptions<V> = NoOptions>(options?: O & Exact<O, JsonOptions<V>> & ComputeRules<O>): JsonField<V, FlagsOf<O>>;
396
+ /** Bytes. Never prefetched. */
397
+ binary<const O extends BinaryOptions = NoOptions>(options?: O & Exact<O, BinaryOptions> & ComputeRules<O>): BinaryField<FlagsOf<O>>;
398
+ /** A reference to one record of `target` (column `<name>_id`, `ON DELETE RESTRICT` by default). */
399
+ many2one<T extends string, const O extends Many2oneOptions<T> = NoOptions>(target: T, options?: O & Exact<O, Many2oneOptions<T>> & ComputeRules<O> & OnDeleteRules<O>): Many2oneField<T, FlagsOf<O>>;
400
+ /** The records of `target` whose `inverse` many2one points here. */
401
+ one2many<T extends string, const O extends X2ManyOptions<T> = NoOptions>(target: T, inverse: string, options?: O & Exact<O, X2ManyOptions<T>> & ComputeRules<O>): One2manyField<T, FlagsOf<O, {
402
+ required: true;
403
+ hasDefault: true;
404
+ }>>;
405
+ /** A set of `target` records through a junction table. */
406
+ many2many<T extends string, const O extends Many2manyOptions<T> = NoOptions>(target: T, options?: O & Exact<O, Many2manyOptions<T>> & ComputeRules<O>): Many2manyField<T, FlagsOf<O, {
407
+ required: true;
408
+ hasDefault: true;
409
+ }>>;
410
+ };
411
+ /** The type of the `f` builders. */
412
+ export type FieldBuilders = typeof f;
413
+ /** The implicit `id` field: `bigint GENERATED ALWAYS AS IDENTITY`, read as a branded number. */
414
+ export interface IdField<N extends string> extends FieldBase<'bigint', Id<N>, never, {
415
+ required: true;
416
+ readonly: true;
417
+ computed: false;
418
+ inverse: false;
419
+ hasDefault: false;
420
+ sequence: false;
421
+ }> {
422
+ readonly identity: true;
423
+ }
424
+ /** @internal Used by `defineModel` for the implicit `id` field. */
425
+ export declare function implicitIdField<N extends string>(model: N): IdField<N>;
426
+ /** @internal Returns the same descriptor with `label` filled in when it was missing. */
427
+ export declare function withDefaultLabel<F extends Field>(field: F, label: string): F;
428
+ /** Attributes an extension may change on an existing field (RFC 0002 §7.5 S2–S4). */
429
+ export interface FieldOverrideChanges {
430
+ readonly label?: string;
431
+ readonly help?: string;
432
+ readonly default?: unknown;
433
+ readonly index?: IndexKind;
434
+ readonly tracking?: boolean;
435
+ readonly copy?: boolean;
436
+ readonly groups?: FieldGroups;
437
+ readonly required?: boolean;
438
+ /** Replaces the selection list (used by `selectionAdd` after validation). */
439
+ readonly selection?: readonly (readonly [string, string])[];
440
+ /** State additions (RFC 0010 §7.4). */
441
+ readonly transitions?: Readonly<Record<string, Transition<string>>>;
442
+ readonly stateHooks?: Readonly<Record<string, StateHooks>>;
443
+ }
444
+ /** @internal Returns a new descriptor with `changes` applied; the registry validates them first. */
445
+ export declare function overrideField<F extends Field>(field: F, changes: FieldOverrideChanges): F;
446
+ export {};