shutro 0.0.0-stage → 0.2.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 (165) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +99 -2
  3. package/dist/audit/index.d.ts +59 -0
  4. package/dist/audit/index.js +96 -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 +164 -0
  9. package/dist/cli/config.d.ts +18 -0
  10. package/dist/cli/config.js +49 -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 +84 -0
  15. package/dist/cli/generate/render.d.ts +23 -0
  16. package/dist/cli/generate/render.js +249 -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/cli/resolve.d.ts +2 -0
  28. package/dist/cli/resolve.js +28 -0
  29. package/dist/core/compute/graph.d.ts +52 -0
  30. package/dist/core/compute/graph.js +162 -0
  31. package/dist/core/data/data.d.ts +59 -0
  32. package/dist/core/data/data.js +119 -0
  33. package/dist/core/data/load.d.ts +37 -0
  34. package/dist/core/data/load.js +236 -0
  35. package/dist/core/env/env.d.ts +468 -0
  36. package/dist/core/env/env.js +916 -0
  37. package/dist/core/env/identity.d.ts +43 -0
  38. package/dist/core/env/identity.js +65 -0
  39. package/dist/core/env/inspector.d.ts +55 -0
  40. package/dist/core/env/inspector.js +85 -0
  41. package/dist/core/env/time.d.ts +12 -0
  42. package/dist/core/env/time.js +38 -0
  43. package/dist/core/errors.d.ts +256 -0
  44. package/dist/core/errors.js +360 -0
  45. package/dist/core/events/events.d.ts +44 -0
  46. package/dist/core/events/events.js +58 -0
  47. package/dist/core/events/workers.d.ts +56 -0
  48. package/dist/core/events/workers.js +155 -0
  49. package/dist/core/index.d.ts +51 -0
  50. package/dist/core/index.js +42 -0
  51. package/dist/core/model/company.d.ts +6 -0
  52. package/dist/core/model/company.js +31 -0
  53. package/dist/core/model/faces.d.ts +42 -0
  54. package/dist/core/model/faces.js +1 -0
  55. package/dist/core/model/field.d.ts +479 -0
  56. package/dist/core/model/field.js +461 -0
  57. package/dist/core/model/generated.d.ts +77 -0
  58. package/dist/core/model/generated.js +1 -0
  59. package/dist/core/model/id.d.ts +33 -0
  60. package/dist/core/model/id.js +4 -0
  61. package/dist/core/model/model.d.ts +387 -0
  62. package/dist/core/model/model.js +566 -0
  63. package/dist/core/model/partition.d.ts +31 -0
  64. package/dist/core/model/partition.js +71 -0
  65. package/dist/core/model/related.d.ts +17 -0
  66. package/dist/core/model/related.js +60 -0
  67. package/dist/core/model/typed.d.ts +107 -0
  68. package/dist/core/model/typed.js +46 -0
  69. package/dist/core/money/currency.d.ts +39 -0
  70. package/dist/core/money/currency.js +37 -0
  71. package/dist/core/money/decimal.d.ts +26 -0
  72. package/dist/core/money/decimal.js +51 -0
  73. package/dist/core/money/fx.d.ts +68 -0
  74. package/dist/core/money/fx.js +124 -0
  75. package/dist/core/money/money.d.ts +84 -0
  76. package/dist/core/money/money.js +173 -0
  77. package/dist/core/money/quantity.d.ts +67 -0
  78. package/dist/core/money/quantity.js +108 -0
  79. package/dist/core/money/rounding.d.ts +11 -0
  80. package/dist/core/money/rounding.js +12 -0
  81. package/dist/core/recordset/aggregate.d.ts +22 -0
  82. package/dist/core/recordset/aggregate.js +258 -0
  83. package/dist/core/recordset/audit.d.ts +33 -0
  84. package/dist/core/recordset/audit.js +262 -0
  85. package/dist/core/recordset/compute.d.ts +36 -0
  86. package/dist/core/recordset/compute.js +414 -0
  87. package/dist/core/recordset/constraints.d.ts +9 -0
  88. package/dist/core/recordset/constraints.js +61 -0
  89. package/dist/core/recordset/domain.d.ts +31 -0
  90. package/dist/core/recordset/domain.js +300 -0
  91. package/dist/core/recordset/onchange.d.ts +43 -0
  92. package/dist/core/recordset/onchange.js +240 -0
  93. package/dist/core/recordset/recordset.d.ts +110 -0
  94. package/dist/core/recordset/recordset.js +789 -0
  95. package/dist/core/recordset/relations.d.ts +15 -0
  96. package/dist/core/recordset/relations.js +13 -0
  97. package/dist/core/recordset/rls.d.ts +15 -0
  98. package/dist/core/recordset/rls.js +108 -0
  99. package/dist/core/recordset/security.d.ts +56 -0
  100. package/dist/core/recordset/security.js +269 -0
  101. package/dist/core/recordset/sequences.d.ts +31 -0
  102. package/dist/core/recordset/sequences.js +154 -0
  103. package/dist/core/recordset/source.d.ts +9 -0
  104. package/dist/core/recordset/source.js +19 -0
  105. package/dist/core/recordset/sqlErrors.d.ts +6 -0
  106. package/dist/core/recordset/sqlErrors.js +39 -0
  107. package/dist/core/recordset/transitions.d.ts +10 -0
  108. package/dist/core/recordset/transitions.js +80 -0
  109. package/dist/core/recordset/types.d.ts +415 -0
  110. package/dist/core/recordset/types.js +8 -0
  111. package/dist/core/recordset/values.d.ts +33 -0
  112. package/dist/core/recordset/values.js +177 -0
  113. package/dist/core/recordset/writes.d.ts +42 -0
  114. package/dist/core/recordset/writes.js +786 -0
  115. package/dist/core/registry/ddl.d.ts +31 -0
  116. package/dist/core/registry/ddl.js +174 -0
  117. package/dist/core/registry/extension.d.ts +94 -0
  118. package/dist/core/registry/extension.js +90 -0
  119. package/dist/core/registry/module.d.ts +89 -0
  120. package/dist/core/registry/module.js +98 -0
  121. package/dist/core/registry/registry.d.ts +130 -0
  122. package/dist/core/registry/registry.js +591 -0
  123. package/dist/core/security/runtime.d.ts +102 -0
  124. package/dist/core/security/runtime.js +232 -0
  125. package/dist/driver-pg/driver.d.ts +88 -0
  126. package/dist/driver-pg/driver.js +177 -0
  127. package/dist/driver-pg/errors.d.ts +72 -0
  128. package/dist/driver-pg/errors.js +182 -0
  129. package/dist/driver-pg/index.d.ts +7 -0
  130. package/dist/driver-pg/index.js +7 -0
  131. package/dist/i18n/index.d.ts +25 -0
  132. package/dist/i18n/index.js +51 -0
  133. package/dist/migrate/apply.d.ts +78 -0
  134. package/dist/migrate/apply.js +167 -0
  135. package/dist/migrate/catalog.d.ts +10 -0
  136. package/dist/migrate/catalog.js +122 -0
  137. package/dist/migrate/diff.d.ts +98 -0
  138. package/dist/migrate/diff.js +160 -0
  139. package/dist/migrate/errors.d.ts +21 -0
  140. package/dist/migrate/errors.js +31 -0
  141. package/dist/migrate/index.d.ts +18 -0
  142. package/dist/migrate/index.js +18 -0
  143. package/dist/migrate/render.d.ts +5 -0
  144. package/dist/migrate/render.js +101 -0
  145. package/dist/migrate/schema.d.ts +61 -0
  146. package/dist/migrate/schema.js +233 -0
  147. package/dist/security/index.d.ts +147 -0
  148. package/dist/security/index.js +278 -0
  149. package/dist/sequence/index.d.ts +63 -0
  150. package/dist/sequence/index.js +106 -0
  151. package/dist/sql/ast.d.ts +49 -0
  152. package/dist/sql/ast.js +47 -0
  153. package/dist/sql/builder/builder.d.ts +215 -0
  154. package/dist/sql/builder/builder.js +308 -0
  155. package/dist/sql/builder/expression.d.ts +80 -0
  156. package/dist/sql/builder/expression.js +126 -0
  157. package/dist/sql/compile.d.ts +18 -0
  158. package/dist/sql/compile.js +41 -0
  159. package/dist/sql/errors.d.ts +13 -0
  160. package/dist/sql/errors.js +16 -0
  161. package/dist/sql/index.d.ts +19 -0
  162. package/dist/sql/index.js +19 -0
  163. package/dist/sql/tag.d.ts +47 -0
  164. package/dist/sql/tag.js +123 -0
  165. package/package.json +82 -4
@@ -0,0 +1,9 @@
1
+ /** Where reads come from: the table, or the past in `env.asOf` scopes (RFC 0018 §7.4). */
2
+ import { type SqlFragment } from '../../sql/index.js';
3
+ import type { ScopeInternals } from '../env/env.js';
4
+ import type { AnyModel } from '../model/model.js';
5
+ /**
6
+ * The relation `model` is read from: its table, or in `env.asOf` scopes and for models with history, the rows valid
7
+ * at that instant (current rows unchanged since, plus archived versions), aliased as the table.
8
+ */
9
+ export declare function sourceOf(internals: ScopeInternals, model: AnyModel): SqlFragment;
@@ -0,0 +1,19 @@
1
+ /** Where reads come from: the table, or the past in `env.asOf` scopes (RFC 0018 §7.4). */
2
+ import { sql } from '../../sql/index.js';
3
+ /**
4
+ * The relation `model` is read from: its table, or in `env.asOf` scopes and for models with history, the rows valid
5
+ * at that instant (current rows unchanged since, plus archived versions), aliased as the table.
6
+ */
7
+ export function sourceOf(internals, model) {
8
+ const at = internals.asOf;
9
+ if (at === undefined || !model.history)
10
+ return sql.id(model.table);
11
+ const columns = sql.join([
12
+ ...new Set([
13
+ 'id',
14
+ ...Object.values(model.columns).filter((c) => c !== undefined),
15
+ ]),
16
+ ].map((c) => sql.id(c)));
17
+ return sql `(SELECT ${columns} FROM ${sql.id(model.table)} WHERE ${sql.id('updated_at')} <= ${at}
18
+ UNION ALL SELECT ${columns} FROM ${sql.id(`${model.table}_history`.slice(0, 63))} WHERE ${sql.id('valid_from')} <= ${at} AND ${sql.id('valid_to')} > ${at}) AS ${sql.id(model.table)}`;
19
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Maps PostgreSQL constraint violations to typed core errors carrying the declared message (RFC 0009 §7.2).
3
+ */
4
+ import type { Registry } from '../registry/registry.js';
5
+ /** The core error for a driver constraint violation, or the original error when no model declares the constraint. */
6
+ export declare function mapConstraintError(registry: Registry, error: unknown): unknown;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Maps PostgreSQL constraint violations to typed core errors carrying the declared message (RFC 0009 §7.2).
3
+ */
4
+ import { ConstraintViolationError } from '../errors.js';
5
+ const HINTS = {
6
+ unique: 'Change the value so it is unique, or update the existing record instead.',
7
+ check: 'Correct the value so it satisfies the rule.',
8
+ exclusion: 'Change the value so it does not overlap an existing record.',
9
+ foreign_key: 'Reference an existing record, or remove the records that still reference it first.',
10
+ };
11
+ /** The core error for a driver constraint violation, or the original error when no model declares the constraint. */
12
+ export function mapConstraintError(registry, error) {
13
+ // Shape-checked rather than instanceof: the driver's error class may come from another copy of the package.
14
+ if (!(error instanceof Error))
15
+ return error;
16
+ const { code, kind, constraint: name, } = error;
17
+ if (code !== 'CONSTRAINT_VIOLATION' || typeof name !== 'string')
18
+ return error;
19
+ if (kind !== 'unique' && kind !== 'check' && kind !== 'exclusion' && kind !== 'foreign_key')
20
+ return error;
21
+ for (const { model } of registry.models.values()) {
22
+ for (const c of model.sqlConstraints) {
23
+ if (c.name !== name)
24
+ continue;
25
+ return new ConstraintViolationError(model.name, c.key, kind, c.fields, c.message, HINTS[kind], {
26
+ cause: error,
27
+ });
28
+ }
29
+ if (kind === 'foreign_key') {
30
+ for (const [key, field] of Object.entries(model.fields)) {
31
+ if (field.kind !== 'many2one' || `${model.table}_${model.columns[key] ?? ''}_fkey` !== name)
32
+ continue;
33
+ const target = field.target ?? '?';
34
+ return new ConstraintViolationError(model.name, key, kind, [key], `'${key}' references a ${target} record that does not exist, or a ${model.name} record still references the ${target} record being deleted.`, HINTS.foreign_key, { cause: error });
35
+ }
36
+ }
37
+ }
38
+ return error;
39
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Running a state transition (RFC 0010 §7.2): validate the source states, run the guard, run the events, write the
3
+ * state through the unit of work.
4
+ */
5
+ import type { AnyModel } from '../model/model.js';
6
+ import type { ComputeHost } from './compute.js';
7
+ /** Runs transition `name` of state field `key` on `ids` of `model`. */
8
+ export declare function runTransition(host: ComputeHost, model: AnyModel, ids: readonly number[], key: string, name: string): Promise<void>;
9
+ /** Transition names available from the current state of one record (state must be loaded). */
10
+ export declare function availableTransitions(model: AnyModel, key: string, state: unknown): string[];
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Running a state transition (RFC 0010 §7.2): validate the source states, run the guard, run the events, write the
3
+ * state through the unit of work.
4
+ */
5
+ import { InvalidTransitionError, TransitionGuardError } from '../errors.js';
6
+ import { trackChange } from './audit.js';
7
+ import { failingIds } from './constraints.js';
8
+ import { assertAccess, assertPermission, assertRecords } from './security.js';
9
+ /** Runs transition `name` of state field `key` on `ids` of `model`. */
10
+ export async function runTransition(host, model, ids, key, name) {
11
+ if (ids.length === 0)
12
+ return;
13
+ const { internals } = host;
14
+ assertAccess(internals, model, 'write');
15
+ await assertRecords(internals, model, ids, 'write');
16
+ const field = model.fields[key];
17
+ const transition = field.transitions[name];
18
+ if (transition === undefined)
19
+ throw new InvalidTransitionError(model.name, name, [], []);
20
+ const requires = transition.requires;
21
+ if (requires !== undefined)
22
+ assertPermission(internals, requires);
23
+ await host.load(model, ids, { fields: new Set(['id', key]), relations: new Map() });
24
+ const stateOf = (id) => String(host.values(model.name, id)?.get(key));
25
+ const invalid = ids.filter((id) => !transition.from.includes(stateOf(id)));
26
+ if (invalid.length > 0) {
27
+ throw new InvalidTransitionError(model.name, name, invalid.map((id) => ({ id, state: stateOf(id) })), transition.from);
28
+ }
29
+ const records = host.recordset(model, ids);
30
+ const guard = transition.guard;
31
+ if (guard !== undefined) {
32
+ const failing = failingIds(await guard.call(records, records));
33
+ if (failing.length > 0) {
34
+ throw new TransitionGuardError(model.name, name, failing, transition.message ?? `${failing.length} record(s) do not satisfy the guard of '${name}'.`);
35
+ }
36
+ }
37
+ const byState = new Map();
38
+ for (const id of ids)
39
+ byState.set(stateOf(id), [...(byState.get(stateOf(id)) ?? []), id]);
40
+ for (const [state, group] of byState) {
41
+ const onExit = field.stateHooks[state]?.onExit;
42
+ if (onExit !== undefined) {
43
+ const set = host.recordset(model, group);
44
+ await onExit.call(set, set);
45
+ }
46
+ }
47
+ const before = transition.before;
48
+ if (before !== undefined)
49
+ await before.call(records, records);
50
+ // The state write: cache, journal, dirty, triggers (RFC 0008 §7.2). The field is readonly for write(); this is the one path that sets it.
51
+ for (const id of ids) {
52
+ const values = host.values(model.name, id, true);
53
+ internals.journal({
54
+ kind: 'value',
55
+ model: model.name,
56
+ id,
57
+ key,
58
+ had: values.has(key),
59
+ prev: values.get(key),
60
+ wasDirty: internals.isDirty(model.name, id, key),
61
+ });
62
+ trackChange(internals, model, id, key, values.get(key), transition.to, (k) => values.get(k));
63
+ values.set(key, transition.to);
64
+ internals.markDirty(model.name, id, key);
65
+ }
66
+ internals.trigger(model.name, key, ids);
67
+ const after = transition.after;
68
+ if (after !== undefined)
69
+ await after.call(records, records);
70
+ const onEnter = field.stateHooks[transition.to]?.onEnter;
71
+ if (onEnter !== undefined)
72
+ await onEnter.call(records, records);
73
+ }
74
+ /** Transition names available from the current state of one record (state must be loaded). */
75
+ export function availableTransitions(model, key, state) {
76
+ const field = model.fields[key];
77
+ return Object.entries(field.transitions)
78
+ .filter(([, t]) => t.from.includes(String(state)))
79
+ .map(([n]) => n);
80
+ }
@@ -0,0 +1,415 @@
1
+ /**
2
+ * Recordset types (RFC 0004 §7.2): the loaded shape `L` of a recordset is tracked in the types, so reading a field
3
+ * that was not loaded is a compile error whose message says which `load()` is missing.
4
+ *
5
+ * Shapes are `{ s: <loaded field keys>; r: { <relation>: <shape> } }` and are read through `infer` helpers
6
+ * (spike 0001, lesson 2).
7
+ */
8
+ import type { AuditAction, AuditEntry } from '../../audit/index.js';
9
+ import type { Translations } from '../../i18n/index.js';
10
+ import type { Env } from '../env/env.js';
11
+ import type { CreateOf, FlagsOfField, ReadValue, WriteOf, WriteType } from '../model/faces.js';
12
+ import type { Id, X2ManyCommands } from '../model/id.js';
13
+ import type { Currency } from '../money/currency.js';
14
+ import type { Decimal } from '../money/decimal.js';
15
+ import type { Money, MoneyJson } from '../money/money.js';
16
+ /** Model names of a schema view. */
17
+ export type ModelName<S> = keyof S & string;
18
+ /** Fields of model `M`. */
19
+ export type FieldsOf<S, M extends ModelName<S>> = S[M] extends {
20
+ readonly fields: infer F;
21
+ } ? F : never;
22
+ type KindOf<F> = F extends {
23
+ readonly kind: infer K extends string;
24
+ } ? K : never;
25
+ type TargetOf<F> = F extends {
26
+ readonly target: infer T extends string;
27
+ } ? T : never;
28
+ /** Target model of relation field `K` of `M`. */
29
+ export type Target<S, M extends ModelName<S>, K> = K extends keyof FieldsOf<S, M> ? TargetOf<FieldsOf<S, M>[K]> & ModelName<S> : never;
30
+ type IsX2Many<F> = KindOf<F> extends 'one2many' | 'many2many' ? true : false;
31
+ type IsRelation<F> = KindOf<F> extends 'many2one' | 'one2many' | 'many2many' ? true : false;
32
+ type IsDefault<F> = IsX2Many<F> extends true ? false : KindOf<F> extends 'binary' ? false : FlagsOfField<F>['computed'] extends true ? FlagsOfField<F>['store'] extends true ? true : false : true;
33
+ type Keys<S, M extends ModelName<S>> = keyof FieldsOf<S, M> & string;
34
+ /** Fields `search()` / `browse().load()` load by default: stored, non-binary, not x2many. */
35
+ export type Defaults<S, M extends ModelName<S>> = {
36
+ [K in Keys<S, M>]: IsDefault<FieldsOf<S, M>[K]> extends true ? K : never;
37
+ }[Keys<S, M>];
38
+ /** Relation field keys. */
39
+ export type RelKeys<S, M extends ModelName<S>> = {
40
+ [K in Keys<S, M>]: IsRelation<FieldsOf<S, M>[K]> extends true ? K : never;
41
+ }[Keys<S, M>];
42
+ /** Keys that can appear in a list load spec: everything but x2many. */
43
+ export type ListKeys<S, M extends ModelName<S>> = {
44
+ [K in Keys<S, M>]: IsX2Many<FieldsOf<S, M>[K]> extends true ? never : K;
45
+ }[Keys<S, M>];
46
+ /** Non-relational keys. */
47
+ export type ScalarKeys<S, M extends ModelName<S>> = {
48
+ [K in Keys<S, M>]: IsRelation<FieldsOf<S, M>[K]> extends true ? never : K;
49
+ }[Keys<S, M>];
50
+ type NoRels = Record<never, never>;
51
+ type SOf<L> = L extends {
52
+ readonly s: infer X extends string;
53
+ } ? X : never;
54
+ type ROf<L> = L extends {
55
+ readonly r: infer X;
56
+ } ? X : NoRels;
57
+ /** What `search()` and `browse().load()` load. */
58
+ export type DefaultLoaded<S, M extends ModelName<S>> = {
59
+ readonly s: Defaults<S, M>;
60
+ readonly r: NoRels;
61
+ };
62
+ /** Exactly the listed keys (plus `id`). */
63
+ export type ListLoaded<K extends string> = {
64
+ readonly s: K | 'id';
65
+ readonly r: NoRels;
66
+ };
67
+ type Dec = [0, 0, 1, 2, 3];
68
+ /** The `load()` argument: relation keys mapped to `true`, a key list, or a nested spec (depth ≤ 4). */
69
+ export type LoadSpec<S, M extends ModelName<S>, D extends number = 4> = {
70
+ readonly [K in RelKeys<S, M>]?: true | readonly ListKeys<S, Target<S, M, K>>[] | (D extends 0 ? never : LoadSpec<S, Target<S, M, K>, Dec[D]>);
71
+ };
72
+ /** Converts a spec into a loaded shape. A nested object loads the relation's defaults plus its nested relations. */
73
+ export type ToLoaded<S, M extends ModelName<S>, X> = X extends true ? DefaultLoaded<S, M> : X extends readonly (infer K extends string)[] ? ListLoaded<K> : {
74
+ readonly s: Defaults<S, M>;
75
+ readonly r: {
76
+ readonly [K in keyof X & RelKeys<S, M>]: ToLoaded<S, Target<S, M, K>, X[K]>;
77
+ };
78
+ };
79
+ /** Union of two loaded shapes, recursively. */
80
+ export type Merge<A, B> = {
81
+ readonly s: SOf<A> | SOf<B>;
82
+ readonly r: {
83
+ readonly [K in keyof ROf<A> | keyof ROf<B>]: K extends keyof ROf<A> ? K extends keyof ROf<B> ? Merge<ROf<A>[K], ROf<B>[K]> : ROf<A>[K] : K extends keyof ROf<B> ? ROf<B>[K] : never;
84
+ };
85
+ };
86
+ /** Branded error type: using it where a value is expected shows the fix in the message. */
87
+ export interface NotLoaded<M extends string, K extends string> {
88
+ readonly __notLoaded: `${M}.${K} is not loaded: add it to .load({ ... })`;
89
+ }
90
+ /** A many2one that was not loaded: only its id is known. */
91
+ export interface IdOnly<M extends string> {
92
+ readonly id: Id<M>;
93
+ readonly modelName: M;
94
+ }
95
+ type Req<V, F> = FlagsOfField<F>['required'] extends true ? V : V | null;
96
+ type FieldValue<S, M extends ModelName<S>, K extends string, F, L> = IsX2Many<F> extends true ? K extends keyof ROf<L> ? RecordSet<S, Target<S, M, K>, ROf<L>[K]> : PendingSet<S, Target<S, M, K>> : KindOf<F> extends 'many2one' ? K extends keyof ROf<L> ? Req<Rec<S, Target<S, M, K>, ROf<L>[K]>, F> : K extends SOf<L> ? Req<IdOnly<Target<S, M, K>>, F> : NotLoaded<M, K> : K extends SOf<L> ? ReadValue<F> : NotLoaded<M, K>;
97
+ /** Field access on a single record. */
98
+ export type Access<S, M extends ModelName<S>, L> = {
99
+ readonly [K in Keys<S, M>]: FieldValue<S, M, K, FieldsOf<S, M>[K], L>;
100
+ };
101
+ /** A singleton: a recordset of one plus field access. */
102
+ export type Rec<S, M extends ModelName<S>, L> = RecordSet<S, M, L> & Access<S, M, L>;
103
+ type PlainValue<S, M extends ModelName<S>, K extends string, F> = IsX2Many<F> extends true ? readonly Id<Target<S, M, K>>[] : KindOf<F> extends 'many2one' ? Req<Id<Target<S, M, K>>, F> : KindOf<F> extends 'monetary' ? Req<MoneyJson, F> : KindOf<F> extends 'quantity' ? Req<{
104
+ readonly value: string;
105
+ readonly uom: string;
106
+ }, F> : KindOf<F> extends 'datetime' ? Req<string, F> : KindOf<F> extends 'decimal' ? Req<string, F> : ReadValue<F>;
107
+ /** Plain, JSON-serializable rows as `read()` returns them. */
108
+ export type PlainRow<S, M extends ModelName<S>, K extends string> = {
109
+ readonly id: Id<M>;
110
+ } & {
111
+ readonly [X in K]: PlainValue<S, M, X, X extends keyof FieldsOf<S, M> ? FieldsOf<S, M>[X] : never>;
112
+ };
113
+ /** Keys readable from a shape: loaded scalars/many2one ids and loaded relations. */
114
+ export type LoadedKeys<L> = SOf<L> | (keyof ROf<L> & string);
115
+ /** An x2many reached without loading it: navigable, but no synchronous data. */
116
+ export interface PendingSet<S, M extends ModelName<S>> {
117
+ readonly modelName: M;
118
+ /** Loads the defaults for every record of the prefetch group. */
119
+ load(): Promise<RecordSet<S, M, DefaultLoaded<S, M>>>;
120
+ load<const K extends readonly ListKeys<S, M>[]>(keys: K): Promise<RecordSet<S, M, ListLoaded<K[number]>>>;
121
+ load<const X extends LoadSpec<S, M>>(spec: X): Promise<RecordSet<S, M, Merge<DefaultLoaded<S, M>, ToLoaded<S, M, X>>>>;
122
+ ids(): Promise<readonly Id<M>[]>;
123
+ }
124
+ /** An ordered, immutable set of records of one model, bound to an environment (RFC 0004). */
125
+ export interface RecordSetApi<S, M extends ModelName<S>, L> {
126
+ readonly env: Env<S>;
127
+ readonly modelName: M;
128
+ readonly ids: readonly Id<M>[];
129
+ readonly length: number;
130
+ [Symbol.iterator](): Iterator<Rec<S, M, L>>;
131
+ /** @throws {SingletonError} when empty */
132
+ first(): Rec<S, M, L>;
133
+ /** @throws {SingletonError} unless exactly one record */
134
+ ensureOne(): Rec<S, M, L>;
135
+ isEmpty(): boolean;
136
+ load(): Promise<RecordSet<S, M, Merge<L, DefaultLoaded<S, M>>>>;
137
+ load<const K extends readonly ListKeys<S, M>[]>(keys: K): Promise<RecordSet<S, M, Merge<L, ListLoaded<K[number]>>>>;
138
+ load<const X extends LoadSpec<S, M>>(spec: X): Promise<RecordSet<S, M, Merge<L, ToLoaded<S, M, X>>>>;
139
+ map<T>(fn: (record: Rec<S, M, L>, index: number) => T): T[];
140
+ filter(fn: (record: Rec<S, M, L>, index: number) => boolean): RecordSet<S, M, L>;
141
+ /** The values of a loaded scalar field, in order. */
142
+ mapped<K extends ScalarKeys<S, M> & SOf<L>>(key: K): ReadValue<FieldsOf<S, M>[K]>[];
143
+ /**
144
+ * Total of a loaded numeric field, `NULL`s skipped: `Money` for monetary fields (one currency, else
145
+ * `CurrencyMismatchError`; an empty set needs `currency`: a `Currency`, a record's `currency` or an id), `Decimal` for decimals, `number` for integers and floats.
146
+ *
147
+ * ```ts
148
+ * order.untaxed = order.lines.sum('subtotal', order.currency);
149
+ * ```
150
+ */
151
+ sum<K extends SummableKeys<S, M> & SOf<L>>(key: K, currency?: Currency | IdOnly<'res.currency'> | number): SumOf<FieldsOf<S, M>[K]>;
152
+ /** Updates the records in the cache now; the database is updated at the next flush point (search, raw SQL, savepoint end, commit). */
153
+ write(values: WriteValues<S, M>, options?: {
154
+ /**
155
+ * Compare-and-set (RFC 0018 §7.1): the stored values these records must still have, typically the
156
+ * `updatedAt` read with them. Checked under a row lock. @throws {StaleRecordError} when someone saved first.
157
+ */
158
+ readonly expect?: {
159
+ readonly [K in Keys<S, M>]?: unknown;
160
+ };
161
+ }): Promise<void>;
162
+ /** Deletes the records now (`ON DELETE` rules apply). */
163
+ unlink(): Promise<void>;
164
+ /** Plain JSON rows (ids for relations, `Money` / `Quantity` as objects, dates as ISO strings). */
165
+ read<const K extends readonly LoadedKeys<L>[]>(keys: K): PlainRow<S, M, K[number]>[];
166
+ /** The audit rows of these records, newest first (RFC 0013 §7.4). */
167
+ trail(options?: {
168
+ readonly field?: Keys<S, M>;
169
+ readonly limit?: number;
170
+ readonly action?: AuditAction;
171
+ /** Rows older than this row id: the next page after the last `id` of the previous one. */
172
+ readonly before?: number;
173
+ }): Promise<readonly AuditEntry[]>;
174
+ /** Numbers these records from their manual sequence (RFC 0012 §7.4); records with a number are refused. */
175
+ assignNumber<K extends SequenceKeys<S, M>>(key: K, options?: {
176
+ readonly date?: string;
177
+ }): Promise<void>;
178
+ /** The full translation map of a translatable field (singleton, loaded), or `null` (RFC 0011). */
179
+ translations<K extends TranslatedKeys<S, M>>(key: K): Translations | null;
180
+ /** Transition names the current state allows (singleton with its state loaded; RFC 0010 §7.3). */
181
+ transitions(): readonly string[];
182
+ toJSON(): readonly Id<M>[];
183
+ }
184
+ /** Date buckets of {@link ModelRef.aggregate}: `'dateOrder:month'`. */
185
+ export type Granularity = 'day' | 'week' | 'month' | 'quarter' | 'year';
186
+ type KindKeys<S, M extends ModelName<S>, Kinds> = {
187
+ [K in keyof FieldsOf<S, M> & string]: FieldsOf<S, M>[K] extends {
188
+ readonly kind: Kinds;
189
+ } ? K : never;
190
+ }[keyof FieldsOf<S, M> & string];
191
+ /** What `aggregate()` can group by: plain fields, and dates with a granularity. */
192
+ export type GroupKey<S, M extends ModelName<S>> = Exclude<KindKeys<S, M, 'char' | 'selection' | 'state' | 'boolean' | 'integer' | 'date' | 'many2one'>, TranslatedKeys<S, M>> | `${KindKeys<S, M, 'date' | 'datetime'>}:${Granularity}`;
193
+ /** One measure of `aggregate()`: a row count, or a function of a numeric field. */
194
+ export type Measure<S, M extends ModelName<S>> = 'count' | {
195
+ readonly sum: SummableKeys<S, M>;
196
+ } | {
197
+ readonly avg: SummableKeys<S, M>;
198
+ } | {
199
+ readonly min: SummableKeys<S, M>;
200
+ } | {
201
+ readonly max: SummableKeys<S, M>;
202
+ };
203
+ /** The query of {@link ModelRef.aggregate}. */
204
+ export interface AggregateQuery<S, M extends ModelName<S>, G extends string, Ms> {
205
+ readonly where?: Domain<S, M>;
206
+ readonly groupBy?: readonly G[];
207
+ readonly measures: Ms;
208
+ /** Group or measure names, optionally followed by `asc` / `desc`. Defaults to the groups, ascending. */
209
+ readonly order?: readonly (G | (keyof Ms & string) | `${G | (keyof Ms & string)} ${'asc' | 'desc'}`)[];
210
+ readonly limit?: number;
211
+ /**
212
+ * Several groupings in one statement (`GROUP BY GROUPING SETS`, RFC 0016 amendment for Kosh pivots): each set is a
213
+ * subset of `groupBy`; `[]` is the grand total. Rows carry `$groups`, the names they are grouped by.
214
+ */
215
+ readonly groupingSets?: readonly (readonly G[])[];
216
+ /** Shorthand: every prefix of `groupBy`, down to the grand total (`ROLLUP`). */
217
+ readonly rollup?: boolean;
218
+ /** Shorthand: every subset of `groupBy` (`CUBE`, at most 6 groups). */
219
+ readonly cube?: boolean;
220
+ }
221
+ type GroupValue<S, M extends ModelName<S>, K> = K extends `${string}:${string}` ? string : K extends keyof FieldsOf<S, M> ? FieldsOf<S, M>[K] extends {
222
+ readonly kind: 'many2one';
223
+ } ? Id<Target<S, M, K>> | null : ReadValue<FieldsOf<S, M>[K]> | null : never;
224
+ type FieldOf<S, M extends ModelName<S>, K> = K extends keyof FieldsOf<S, M> ? FieldsOf<S, M>[K] : never;
225
+ type MeasureValue<S, M extends ModelName<S>, X> = X extends 'count' ? number : X extends {
226
+ readonly sum: infer K;
227
+ } ? SumOf<FieldOf<S, M, K>> : X extends {
228
+ readonly avg: infer K;
229
+ } ? (FieldOf<S, M, K> extends {
230
+ readonly kind: 'monetary';
231
+ } ? Money : Decimal) | null : X extends {
232
+ readonly min: infer K;
233
+ } | {
234
+ readonly max: infer K;
235
+ } ? SumOf<FieldOf<S, M, K>> | null : never;
236
+ /** A row of {@link ModelRef.aggregate}: the group values and the measures. */
237
+ export type AggregateRow<S, M extends ModelName<S>, G extends string, Ms> = {
238
+ readonly [K in G]: GroupValue<S, M, K>;
239
+ } & {
240
+ readonly [K in keyof Ms]: MeasureValue<S, M, Ms[K]>;
241
+ } & {
242
+ /** With grouping sets: the groups this row is grouped by (others are totals over them, not `null` values). */
243
+ readonly $groups?: readonly G[];
244
+ };
245
+ /** Fields {@link RecordSetApi.sum} accepts: integer, float, decimal and monetary. */
246
+ export type SummableKeys<S, M extends ModelName<S>> = {
247
+ [K in keyof FieldsOf<S, M> & string]: FieldsOf<S, M>[K] extends {
248
+ readonly kind: 'integer' | 'float' | 'decimal' | 'monetary';
249
+ } ? K : never;
250
+ }[keyof FieldsOf<S, M> & string];
251
+ /** What {@link RecordSetApi.sum} returns for field `F`. */
252
+ export type SumOf<F> = F extends {
253
+ readonly kind: 'monetary';
254
+ } ? Money : F extends {
255
+ readonly kind: 'decimal';
256
+ } ? Decimal : number;
257
+ /** The model's methods (declared ones and generated transitions), callable on recordsets. */
258
+ export type MethodsOf<S, M extends ModelName<S>> = S[M] extends {
259
+ readonly methods: infer Ms;
260
+ } ? Ms : NonNullable<unknown>;
261
+ /** An ordered, immutable set of records of one model, with the model's methods (RFC 0004, RFC 0010 §7.6). */
262
+ export type RecordSet<S, M extends ModelName<S>, L> = RecordSetApi<S, M, L> & MethodsOf<S, M>;
263
+ type DomainKeys<S, M extends ModelName<S>> = ListKeys<S, M>;
264
+ type DomainOperators<V> = {
265
+ readonly eq?: V | null;
266
+ readonly ne?: V | null;
267
+ readonly lt?: V;
268
+ readonly lte?: V;
269
+ readonly gt?: V;
270
+ readonly gte?: V;
271
+ readonly in?: readonly V[];
272
+ readonly notIn?: readonly V[];
273
+ readonly isNull?: boolean;
274
+ readonly like?: string;
275
+ readonly ilike?: string;
276
+ /** Hierarchies: the records in the subtrees of these ids (`id`, or a many2one to a model with `hierarchy`). */
277
+ readonly childOf?: V | readonly V[];
278
+ /** Hierarchies: the ancestors of these ids, themselves included. */
279
+ readonly parentOf?: V | readonly V[];
280
+ };
281
+ type DomainValue<F> = WriteType<F> extends never ? unknown : WriteType<F> | DomainOperators<WriteType<F>>;
282
+ /** A search filter: field → value (equality) or operators. Keys are ANDed. Relation paths come with the domain compiler. */
283
+ export type Domain<S, M extends ModelName<S>> = {
284
+ readonly [K in DomainKeys<S, M>]?: K extends 'id' ? Id<M> | DomainOperators<Id<M>> : DomainValue<FieldsOf<S, M>[K]>;
285
+ } & {
286
+ /** Records with at least one / no related record matching the domain. */
287
+ readonly [K in X2ManyKeys<S, M>]?: {
288
+ readonly any?: Domain<S, Target<S, M, K>>;
289
+ readonly none?: Domain<S, Target<S, M, K>>;
290
+ };
291
+ } & {
292
+ /** Any of the domains. */
293
+ readonly or?: readonly Domain<S, M>[];
294
+ readonly and?: readonly Domain<S, M>[];
295
+ readonly not?: Domain<S, M>;
296
+ /** A relation path such as `'partner.name'` (checked at runtime; typed paths come with the generator). */
297
+ readonly [path: `${string}.${string}`]: unknown;
298
+ };
299
+ /** `'field'`, `'field asc'` or `'field desc'` over sortable (non-x2many) fields. */
300
+ export type SearchOrder<S, M extends ModelName<S>> = ListKeys<S, M> | `${ListKeys<S, M>} asc` | `${ListKeys<S, M>} desc`;
301
+ /** Argument of `search()`. */
302
+ export interface SearchQuery<S, M extends ModelName<S>> {
303
+ readonly where?: Domain<S, M>;
304
+ readonly order?: readonly SearchOrder<S, M>[];
305
+ readonly limit?: number;
306
+ readonly offset?: number;
307
+ }
308
+ /** Values accepted by `create()`: the create face of the model's fields (x2many fields take commands). */
309
+ export type CreateValues<S, M extends ModelName<S>> = CreateOf<FieldsOf<S, M>>;
310
+ /** Values accepted by `write()`. */
311
+ export type WriteValues<S, M extends ModelName<S>> = WriteOf<FieldsOf<S, M>>;
312
+ /** `env.model(name)`: the entry point for a model. */
313
+ export interface ModelRef<S, M extends ModelName<S>> {
314
+ readonly modelName: M;
315
+ /** Inserts records (one statement per model) and returns them with their defaults loaded (RFC 0004 §7.6). */
316
+ create(values: readonly CreateValues<S, M>[]): Promise<RecordSet<S, M, DefaultLoaded<S, M>>>;
317
+ search(query?: SearchQuery<S, M>): Promise<RecordSet<S, M, DefaultLoaded<S, M>>>;
318
+ /**
319
+ * The records with these ids, nothing loaded yet: `write`, `unlink` and methods work directly; `load()` fetches
320
+ * values (rules apply, missing ids are a `MissingRecordError`).
321
+ */
322
+ browse(ids: readonly number[]): RecordSet<S, M, ListLoaded<never>>;
323
+ /** Form assist on an in-memory draft: derived values and warnings, nothing written (RFC 0009 §7.4). */
324
+ onchange(input: OnchangeInput<S, M>): Promise<OnchangeResult<S, M>>;
325
+ /**
326
+ * Bulk create through PostgreSQL COPY (RFC 0015 §7.1): the same semantics as create() (defaults, sequences,
327
+ * computes, constraints, audit, rules), one streamed statement instead of 1,000-row INSERTs. Needs the `import`
328
+ * permission. x2many commands are not accepted.
329
+ */
330
+ copy(values: readonly CreateValues<S, M>[]): Promise<RecordSet<S, M, DefaultLoaded<S, M>>>;
331
+ /**
332
+ * Grouped totals for reports (Odoo's `read_group`), in one `GROUP BY` statement with the read ACL, field groups,
333
+ * record rules and company scope of `search`. Monetary measures are also split by currency.
334
+ *
335
+ * ```ts
336
+ * const rows = await env.model('sale.order').aggregate({
337
+ * where: { state: 'sale' },
338
+ * groupBy: ['partner', 'dateOrder:month'],
339
+ * measures: { orders: 'count', total: { sum: 'amountTotal' } },
340
+ * order: ['total desc'],
341
+ * });
342
+ * rows[0].total; // Money
343
+ * ```
344
+ */
345
+ aggregate<const G extends GroupKey<S, M> = never, const Ms extends Readonly<Record<string, Measure<S, M>>> = Readonly<Record<string, Measure<S, M>>>>(query: AggregateQuery<S, M, G, Ms>): Promise<AggregateRow<S, M, G, Ms>[]>;
346
+ }
347
+ /** Keys of sequence-numbered fields. */
348
+ export type SequenceKeys<S, M extends ModelName<S>> = {
349
+ [K in Keys<S, M>]: FlagsOfField<FieldsOf<S, M>[K]>['sequence'] extends true ? K : never;
350
+ }[Keys<S, M>];
351
+ /** Keys of translatable text fields (their write type accepts a translation map). */
352
+ export type TranslatedKeys<S, M extends ModelName<S>> = {
353
+ [K in Keys<S, M>]: Translations extends WriteType<FieldsOf<S, M>[K]> ? K : never;
354
+ }[Keys<S, M>];
355
+ /** Keys of computed fields. */
356
+ export type ComputedKeys<S, M extends ModelName<S>> = {
357
+ [K in Keys<S, M>]: FlagsOfField<FieldsOf<S, M>[K]>['computed'] extends true ? K : never;
358
+ }[Keys<S, M>];
359
+ /**
360
+ * A record inside a compute: the `A` computed keys are assignable (RFC 0008 §7.7), other computed keys read with their
361
+ * read type. Built without `Omit` over the recordset (whose keys include the model's methods), so typing a compute
362
+ * against a generated view never needs the methods' types (feedback #1: no TS7022 cycle through the module).
363
+ */
364
+ export type ComputeRec<S, M extends ModelName<S>, L, A extends ComputedKeys<S, M> = ComputedKeys<S, M>, Ms = MethodsOf<S, M>> = RecordSetApi<S, M, L> & Ms & Omit<Access<S, M, L>, A> & {
365
+ -readonly [K in A]: FieldValue<S, M, K, FieldsOf<S, M>[K], L> | WriteType<FieldsOf<S, M>[K]>;
366
+ };
367
+ /** A recordset inside a compute: iteration yields {@link ComputeRec} (these members come first, so they win). */
368
+ export type ComputeSet<S, M extends ModelName<S>, L, A extends ComputedKeys<S, M> = ComputedKeys<S, M>, Ms = MethodsOf<S, M>> = Omit<RecordSetApi<S, M, L>, typeof Symbol.iterator | 'first' | 'ensureOne'> & Ms & {
369
+ [Symbol.iterator](): Iterator<ComputeRec<S, M, L, A, Ms>>;
370
+ first(): ComputeRec<S, M, L, A, Ms>;
371
+ ensureOne(): ComputeRec<S, M, L, A, Ms>;
372
+ };
373
+ /** Many2one keys. */
374
+ export type Many2oneKeys<S, M extends ModelName<S>> = {
375
+ [K in Keys<S, M>]: KindOf<FieldsOf<S, M>[K]> extends 'many2one' ? K : never;
376
+ }[Keys<S, M>];
377
+ /** x2many keys. */
378
+ export type X2ManyKeys<S, M extends ModelName<S>> = {
379
+ [K in Keys<S, M>]: IsX2Many<FieldsOf<S, M>[K]> extends true ? K : never;
380
+ }[Keys<S, M>];
381
+ /** What a draft holds for a field: an id for many2one, commands for x2many, a Decimal for amounts, read values otherwise. */
382
+ export type DraftValue<S, M extends ModelName<S>, K extends Keys<S, M>> = IsX2Many<FieldsOf<S, M>[K]> extends true ? X2ManyCommands<Target<S, M, K>> | undefined : KindOf<FieldsOf<S, M>[K]> extends 'many2one' ? Id<Target<S, M, K>> | null : KindOf<FieldsOf<S, M>[K]> extends 'monetary' | 'quantity' ? Decimal | null : ReadValue<FieldsOf<S, M>[K]> | null;
383
+ /** The draft an onchange handler receives (RFC 0009 §7.3). */
384
+ export interface Draft<S, M extends ModelName<S>> {
385
+ readonly id: Id<M> | null;
386
+ readonly env: Env<S>;
387
+ get<K extends Keys<S, M>>(key: K): DraftValue<S, M, K>;
388
+ /** Normalized like a write; `null` is always accepted in a draft. Marks the key changed (its handler runs next). */
389
+ set<K extends Keys<S, M>>(key: K, value: WriteType<FieldsOf<S, M>[K]> | null): void;
390
+ /** The related record of a many2one, loaded with its defaults, or null. */
391
+ record<K extends Many2oneKeys<S, M>>(key: K): Promise<Rec<S, Target<S, M, K>, DefaultLoaded<S, Target<S, M, K>>> | null>;
392
+ warn(message: string): void;
393
+ }
394
+ /** The input of `env.model(M).onchange()`. */
395
+ export interface OnchangeInput<S, M extends ModelName<S>> {
396
+ /** An existing record: its values are the base of the draft. */
397
+ readonly id?: Id<M> | undefined;
398
+ /** The form's values (x2many as commands; `create` rows become virtual children). */
399
+ readonly values: {
400
+ readonly [K in Keys<S, M>]?: WriteType<FieldsOf<S, M>[K]> | null;
401
+ };
402
+ /** Fields whose handlers run, in order. */
403
+ readonly changed: readonly Keys<S, M>[];
404
+ }
405
+ /** The output of `env.model(M).onchange()`. */
406
+ export interface OnchangeResult<S, M extends ModelName<S>> {
407
+ /** Scalar and many2one fields whose plain value differs from the input. */
408
+ readonly values: Partial<PlainRow<S, M, Exclude<ScalarKeys<S, M> | Many2oneKeys<S, M>, 'id'>>>;
409
+ readonly warnings: readonly string[];
410
+ /** Plain rows of the virtual children created by `create` commands, per x2many field. */
411
+ readonly children: {
412
+ readonly [K in X2ManyKeys<S, M>]?: readonly Record<string, unknown>[];
413
+ };
414
+ }
415
+ export {};
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Recordset types (RFC 0004 §7.2): the loaded shape `L` of a recordset is tracked in the types, so reading a field
3
+ * that was not loaded is a compile error whose message says which `load()` is missing.
4
+ *
5
+ * Shapes are `{ s: <loaded field keys>; r: { <relation>: <shape> } }` and are read through `infer` helpers
6
+ * (spike 0001, lesson 2).
7
+ */
8
+ export {};
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Value normalization shared by writes and computes: validates one value against its field and converts it to what
3
+ * the cache holds (Decimal amounts, ids, ...), and converts cached values into what the driver binds.
4
+ */
5
+ import { type SqlFragment } from '../../sql/index.js';
6
+ /** The environment language and the source language, for translatable values (RFC 0011). */
7
+ export interface TranslationContext {
8
+ readonly lang: string;
9
+ readonly source: string;
10
+ /** The current company: company-dependent values are read and written for it. */
11
+ readonly company?: number;
12
+ }
13
+ /** A company-dependent value as stored: `{ "<companyId>": value }`. */
14
+ export type CompanyValues = Readonly<Record<string, unknown>>;
15
+ /** The value of a company-dependent field for `company` (the default when the company has none). */
16
+ export declare function companyValue(field: Field, stored: unknown, company: number): unknown;
17
+ import type { Field } from '../model/field.js';
18
+ import type { AnyModel } from '../model/model.js';
19
+ /** Throws a {@link ValidationError} for `model`. */
20
+ export declare function fail(model: AnyModel, fields: readonly string[], problem: string, hint: string): never;
21
+ /** The PostgreSQL array element type for a field (for `unnest($n::type[])`). */
22
+ export declare function arrayType(field: Field): string;
23
+ /** The SQL cast applied to a bound value before it reaches the column (`jsonb` for json and translatable fields). */
24
+ export declare function columnCast(field: Field): 'jsonb' | undefined;
25
+ /** A reference to the bound value of `column` in the `v` row alias, cast when needed. */
26
+ export declare function valueRef(field: Field, column: string): SqlFragment;
27
+ /** Converts a cached value into what the driver binds. */
28
+ export declare function bindValue(field: Field, value: unknown): unknown;
29
+ /**
30
+ * Validates and normalizes one value for storage in the cache. `siblings` gives the record's current currency / unit
31
+ * id for `Money` / `Quantity` checks.
32
+ */
33
+ export declare function normalize(model: AnyModel, key: string, field: Field, value: unknown, siblings: (key: string) => unknown, translation?: TranslationContext): unknown;