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.
- package/LICENSE +21 -0
- package/README.md +89 -2
- package/dist/audit/index.d.ts +57 -0
- package/dist/audit/index.js +94 -0
- package/dist/cli/bin.d.ts +2 -0
- package/dist/cli/bin.js +11 -0
- package/dist/cli/cli.d.ts +10 -0
- package/dist/cli/cli.js +149 -0
- package/dist/cli/config.d.ts +18 -0
- package/dist/cli/config.js +47 -0
- package/dist/cli/errors.d.ts +16 -0
- package/dist/cli/errors.js +23 -0
- package/dist/cli/generate/generate.d.ts +33 -0
- package/dist/cli/generate/generate.js +82 -0
- package/dist/cli/generate/render.d.ts +23 -0
- package/dist/cli/generate/render.js +200 -0
- package/dist/cli/index.d.ts +12 -0
- package/dist/cli/index.js +12 -0
- package/dist/cli/init.d.ts +2 -0
- package/dist/cli/init.js +65 -0
- package/dist/cli/inspect.d.ts +10 -0
- package/dist/cli/inspect.js +82 -0
- package/dist/cli/packages.d.ts +6 -0
- package/dist/cli/packages.js +6 -0
- package/dist/cli/project.d.ts +18 -0
- package/dist/cli/project.js +39 -0
- package/dist/core/compute/graph.d.ts +52 -0
- package/dist/core/compute/graph.js +162 -0
- package/dist/core/env/env.d.ts +295 -0
- package/dist/core/env/env.js +631 -0
- package/dist/core/env/identity.d.ts +33 -0
- package/dist/core/env/identity.js +49 -0
- package/dist/core/env/inspector.d.ts +55 -0
- package/dist/core/env/inspector.js +85 -0
- package/dist/core/env/time.d.ts +12 -0
- package/dist/core/env/time.js +38 -0
- package/dist/core/errors.d.ts +242 -0
- package/dist/core/errors.js +339 -0
- package/dist/core/index.d.ts +43 -0
- package/dist/core/index.js +34 -0
- package/dist/core/model/company.d.ts +6 -0
- package/dist/core/model/company.js +31 -0
- package/dist/core/model/faces.d.ts +42 -0
- package/dist/core/model/faces.js +1 -0
- package/dist/core/model/field.d.ts +446 -0
- package/dist/core/model/field.js +398 -0
- package/dist/core/model/generated.d.ts +75 -0
- package/dist/core/model/generated.js +1 -0
- package/dist/core/model/id.d.ts +33 -0
- package/dist/core/model/id.js +4 -0
- package/dist/core/model/model.d.ts +347 -0
- package/dist/core/model/model.js +538 -0
- package/dist/core/model/partition.d.ts +31 -0
- package/dist/core/model/partition.js +71 -0
- package/dist/core/money/currency.d.ts +39 -0
- package/dist/core/money/currency.js +37 -0
- package/dist/core/money/decimal.d.ts +26 -0
- package/dist/core/money/decimal.js +51 -0
- package/dist/core/money/fx.d.ts +60 -0
- package/dist/core/money/fx.js +109 -0
- package/dist/core/money/money.d.ts +84 -0
- package/dist/core/money/money.js +173 -0
- package/dist/core/money/quantity.d.ts +67 -0
- package/dist/core/money/quantity.js +108 -0
- package/dist/core/money/rounding.d.ts +11 -0
- package/dist/core/money/rounding.js +12 -0
- package/dist/core/recordset/audit.d.ts +25 -0
- package/dist/core/recordset/audit.js +205 -0
- package/dist/core/recordset/compute.d.ts +34 -0
- package/dist/core/recordset/compute.js +405 -0
- package/dist/core/recordset/constraints.d.ts +9 -0
- package/dist/core/recordset/constraints.js +61 -0
- package/dist/core/recordset/domain.d.ts +27 -0
- package/dist/core/recordset/domain.js +228 -0
- package/dist/core/recordset/onchange.d.ts +43 -0
- package/dist/core/recordset/onchange.js +207 -0
- package/dist/core/recordset/recordset.d.ts +101 -0
- package/dist/core/recordset/recordset.js +723 -0
- package/dist/core/recordset/relations.d.ts +15 -0
- package/dist/core/recordset/relations.js +13 -0
- package/dist/core/recordset/rls.d.ts +15 -0
- package/dist/core/recordset/rls.js +108 -0
- package/dist/core/recordset/security.d.ts +42 -0
- package/dist/core/recordset/security.js +184 -0
- package/dist/core/recordset/sequences.d.ts +31 -0
- package/dist/core/recordset/sequences.js +154 -0
- package/dist/core/recordset/sqlErrors.d.ts +6 -0
- package/dist/core/recordset/sqlErrors.js +39 -0
- package/dist/core/recordset/transitions.d.ts +10 -0
- package/dist/core/recordset/transitions.js +77 -0
- package/dist/core/recordset/types.d.ts +296 -0
- package/dist/core/recordset/types.js +8 -0
- package/dist/core/recordset/values.d.ts +27 -0
- package/dist/core/recordset/values.js +147 -0
- package/dist/core/recordset/writes.d.ts +37 -0
- package/dist/core/recordset/writes.js +581 -0
- package/dist/core/registry/ddl.d.ts +22 -0
- package/dist/core/registry/ddl.js +127 -0
- package/dist/core/registry/extension.d.ts +94 -0
- package/dist/core/registry/extension.js +90 -0
- package/dist/core/registry/module.d.ts +80 -0
- package/dist/core/registry/module.js +96 -0
- package/dist/core/registry/registry.d.ts +105 -0
- package/dist/core/registry/registry.js +496 -0
- package/dist/driver-pg/driver.d.ts +86 -0
- package/dist/driver-pg/driver.js +176 -0
- package/dist/driver-pg/errors.d.ts +72 -0
- package/dist/driver-pg/errors.js +182 -0
- package/dist/driver-pg/index.d.ts +7 -0
- package/dist/driver-pg/index.js +7 -0
- package/dist/i18n/index.d.ts +25 -0
- package/dist/i18n/index.js +51 -0
- package/dist/migrate/apply.d.ts +78 -0
- package/dist/migrate/apply.js +167 -0
- package/dist/migrate/catalog.d.ts +10 -0
- package/dist/migrate/catalog.js +122 -0
- package/dist/migrate/diff.d.ts +98 -0
- package/dist/migrate/diff.js +160 -0
- package/dist/migrate/errors.d.ts +21 -0
- package/dist/migrate/errors.js +31 -0
- package/dist/migrate/index.d.ts +18 -0
- package/dist/migrate/index.js +18 -0
- package/dist/migrate/render.d.ts +5 -0
- package/dist/migrate/render.js +101 -0
- package/dist/migrate/schema.d.ts +61 -0
- package/dist/migrate/schema.js +195 -0
- package/dist/security/index.d.ts +123 -0
- package/dist/security/index.js +262 -0
- package/dist/sequence/index.d.ts +63 -0
- package/dist/sequence/index.js +106 -0
- package/dist/sql/ast.d.ts +49 -0
- package/dist/sql/ast.js +47 -0
- package/dist/sql/builder/builder.d.ts +215 -0
- package/dist/sql/builder/builder.js +308 -0
- package/dist/sql/builder/expression.d.ts +80 -0
- package/dist/sql/builder/expression.js +126 -0
- package/dist/sql/compile.d.ts +18 -0
- package/dist/sql/compile.js +41 -0
- package/dist/sql/errors.d.ts +13 -0
- package/dist/sql/errors.js +16 -0
- package/dist/sql/index.d.ts +19 -0
- package/dist/sql/index.js +19 -0
- package/dist/sql/tag.d.ts +42 -0
- package/dist/sql/tag.js +95 -0
- package/package.json +82 -4
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `defineModel()` (RFC 0001 §7): merges implicit fields, derives names, validates everything that only needs the
|
|
3
|
+
* model itself (sibling references, methods, constraints), and exposes runtime metadata for Kosh.
|
|
4
|
+
* Cross-model checks (relation targets, `depends` paths, one2many inverses) belong to the registry (RFC 0002).
|
|
5
|
+
*/
|
|
6
|
+
import { type AccessSpec, type FieldGroups, type RuleSpec } from '../../security/index.js';
|
|
7
|
+
import { type SqlFragment } from '../../sql/index.js';
|
|
8
|
+
import type { FlagsOfField } from './faces.js';
|
|
9
|
+
import { type CharField, type Field, type IdField, type Many2oneField, type ScalarField } from './field.js';
|
|
10
|
+
import type { PartitionSpec } from './partition.js';
|
|
11
|
+
/** A record of field descriptors. */
|
|
12
|
+
export type FieldsRecord = Readonly<Record<string, Field>>;
|
|
13
|
+
/** Model methods. Parameter and return types must be explicit (RFC 0001 §7.8); `this` is typed by the recordset layer. */
|
|
14
|
+
export type MethodsRecord = Readonly<Record<string, (...args: never[]) => unknown>>;
|
|
15
|
+
type NoMethods = Record<never, never>;
|
|
16
|
+
type NoFields = Record<never, never>;
|
|
17
|
+
/** Generated transition methods of the state fields in `Fs` (RFC 0010 §7.1). */
|
|
18
|
+
export type TransitionMethods<Fs> = {
|
|
19
|
+
readonly [K in keyof Fs as Fs[K] extends {
|
|
20
|
+
readonly transitions: infer T;
|
|
21
|
+
} ? keyof T & string : never]: () => Promise<void>;
|
|
22
|
+
};
|
|
23
|
+
/** The state field and transition a generated (or wrapped) method runs, if it is one (RFC 0010 §7.1). */
|
|
24
|
+
export declare function transitionOf(fn: unknown): {
|
|
25
|
+
readonly field: string;
|
|
26
|
+
readonly name: string;
|
|
27
|
+
} | undefined;
|
|
28
|
+
/** @internal Marks `fn` as running transition `name` of `field`. */
|
|
29
|
+
export declare function markTransition<F>(fn: F, field: string, name: string): F;
|
|
30
|
+
/** `companyScoped` option: adds a `company` many2one. Nullable means "shared across companies". */
|
|
31
|
+
export interface CompanyScope {
|
|
32
|
+
readonly required: boolean;
|
|
33
|
+
}
|
|
34
|
+
type ReadonlyRequired = {
|
|
35
|
+
required: true;
|
|
36
|
+
readonly: true;
|
|
37
|
+
computed: false;
|
|
38
|
+
inverse: false;
|
|
39
|
+
hasDefault: false;
|
|
40
|
+
sequence: false;
|
|
41
|
+
};
|
|
42
|
+
type ReadonlyOptional = {
|
|
43
|
+
required: false;
|
|
44
|
+
readonly: true;
|
|
45
|
+
computed: false;
|
|
46
|
+
inverse: false;
|
|
47
|
+
hasDefault: false;
|
|
48
|
+
sequence: false;
|
|
49
|
+
};
|
|
50
|
+
/** The fields every model has (RFC 0001 §7.4). */
|
|
51
|
+
export interface ImplicitFieldsBase<N extends string> {
|
|
52
|
+
readonly id: IdField<N>;
|
|
53
|
+
readonly createdAt: ScalarField<'datetime', Date, Date, ReadonlyRequired>;
|
|
54
|
+
readonly updatedAt: ScalarField<'datetime', Date, Date, ReadonlyRequired>;
|
|
55
|
+
readonly createdBy: Many2oneField<'res.users', ReadonlyOptional>;
|
|
56
|
+
readonly updatedBy: Many2oneField<'res.users', ReadonlyOptional>;
|
|
57
|
+
/** Non-stored, computed from `recName` unless the model defines it. */
|
|
58
|
+
readonly displayName: CharField<{
|
|
59
|
+
required: true;
|
|
60
|
+
readonly: true;
|
|
61
|
+
computed: true;
|
|
62
|
+
inverse: false;
|
|
63
|
+
hasDefault: false;
|
|
64
|
+
sequence: false;
|
|
65
|
+
}>;
|
|
66
|
+
}
|
|
67
|
+
/** `archivable: true` adds `active`. */
|
|
68
|
+
export type ActiveField = ScalarField<'boolean', boolean, boolean, {
|
|
69
|
+
required: true;
|
|
70
|
+
readonly: false;
|
|
71
|
+
computed: false;
|
|
72
|
+
inverse: false;
|
|
73
|
+
hasDefault: true;
|
|
74
|
+
sequence: false;
|
|
75
|
+
}>;
|
|
76
|
+
/** `companyScoped` adds `company`. */
|
|
77
|
+
export type CompanyField<R extends boolean> = Many2oneField<'res.company', {
|
|
78
|
+
required: R;
|
|
79
|
+
readonly: false;
|
|
80
|
+
computed: false;
|
|
81
|
+
inverse: false;
|
|
82
|
+
hasDefault: true;
|
|
83
|
+
sequence: false;
|
|
84
|
+
}>;
|
|
85
|
+
/** Implicit fields for the given opt-ins. */
|
|
86
|
+
export type ImplicitFields<N extends string, A extends boolean, C extends CompanyScope | undefined> = ImplicitFieldsBase<N> & (A extends true ? {
|
|
87
|
+
readonly active: ActiveField;
|
|
88
|
+
} : NoFields) & (C extends {
|
|
89
|
+
readonly required: infer R extends boolean;
|
|
90
|
+
} ? {
|
|
91
|
+
readonly company: CompanyField<R>;
|
|
92
|
+
} : NoFields);
|
|
93
|
+
type Simplify<T> = {
|
|
94
|
+
[K in keyof T]: T[K];
|
|
95
|
+
} & NonNullable<unknown>;
|
|
96
|
+
/** The model's fields: implicit ones (a user-defined `displayName` wins) plus the declared ones. */
|
|
97
|
+
export type ModelFields<N extends string, Fs extends FieldsRecord, A extends boolean, C extends CompanyScope | undefined> = Simplify<Omit<ImplicitFields<N, A, C>, keyof Fs> & Fs>;
|
|
98
|
+
/** `'field'`, `'field asc'` or `'field desc'`. */
|
|
99
|
+
export type OrderSpec<K extends string> = K | `${K} asc` | `${K} desc`;
|
|
100
|
+
/** A unique constraint over stored fields, optionally partial. */
|
|
101
|
+
export interface SqlUniqueConstraint<K extends string> {
|
|
102
|
+
readonly unique: readonly K[];
|
|
103
|
+
readonly where?: SqlFragment;
|
|
104
|
+
/** Shown to users when the constraint fails. Say what to do. */
|
|
105
|
+
readonly message: string;
|
|
106
|
+
}
|
|
107
|
+
/** A check constraint written with the `sql` tag. */
|
|
108
|
+
export interface SqlCheckConstraint {
|
|
109
|
+
readonly check: SqlFragment;
|
|
110
|
+
readonly message: string;
|
|
111
|
+
}
|
|
112
|
+
/** An index over stored fields. */
|
|
113
|
+
export interface IndexSpec<K extends string> {
|
|
114
|
+
readonly fields: readonly K[];
|
|
115
|
+
readonly unique?: boolean;
|
|
116
|
+
readonly where?: SqlFragment;
|
|
117
|
+
readonly method?: 'btree' | 'gin' | 'gist' | 'brin';
|
|
118
|
+
}
|
|
119
|
+
/** A batch business constraint (RFC 0001 §7.7). Typed by the recordset layer. */
|
|
120
|
+
export interface ConstraintSpec {
|
|
121
|
+
/** Fields whose change triggers the check. */
|
|
122
|
+
readonly depends: readonly string[];
|
|
123
|
+
/** Returns the failing records. */
|
|
124
|
+
readonly check: (records: never) => unknown;
|
|
125
|
+
readonly message: string | ((failing: never) => string);
|
|
126
|
+
}
|
|
127
|
+
/** An onchange handler (RFC 0009 §7.3): receives the draft and the environment. Typed by the recordset layer. */
|
|
128
|
+
export type OnchangeFn = (draft: never, env: never) => void | Promise<void>;
|
|
129
|
+
/** The argument of {@link defineModel} after `name`. */
|
|
130
|
+
export interface ModelDefinition<Fs extends FieldsRecord, Ms extends MethodsRecord, A extends boolean, C extends CompanyScope | undefined> {
|
|
131
|
+
readonly fields: Fs;
|
|
132
|
+
readonly methods?: Ms;
|
|
133
|
+
/** Human label; defaults to the humanized model name. */
|
|
134
|
+
readonly label?: string;
|
|
135
|
+
/** Table name; defaults to the model name with dots replaced by underscores. */
|
|
136
|
+
readonly table?: string;
|
|
137
|
+
/** Default ordering; defaults to `['id asc']`. An `id` tiebreaker is appended by the query layer. */
|
|
138
|
+
readonly order?: readonly OrderSpec<ModelKeys<Fs, A, C>>[];
|
|
139
|
+
/** The field shown as a record's name; defaults to `name` when present, else `id`. */
|
|
140
|
+
readonly recName?: ModelKeys<Fs, A, C>;
|
|
141
|
+
readonly archivable?: A;
|
|
142
|
+
readonly companyScoped?: C;
|
|
143
|
+
/** Fields whose changes are tracked in the audit trail. */
|
|
144
|
+
readonly tracking?: readonly ModelKeys<Fs, A, C>[];
|
|
145
|
+
readonly indexes?: readonly IndexSpec<ModelKeys<Fs, A, C>>[];
|
|
146
|
+
readonly sqlConstraints?: Readonly<Record<string, SqlUniqueConstraint<ModelKeys<Fs, A, C>> | SqlCheckConstraint>>;
|
|
147
|
+
readonly constraints?: Readonly<Record<string, ConstraintSpec>>;
|
|
148
|
+
/** Form-assist handlers keyed by the field they react to (RFC 0009 §7.3). */
|
|
149
|
+
readonly onchange?: Readonly<Record<string, OnchangeFn>>;
|
|
150
|
+
/** Model ACL: group code → permissions (RFC 0014 §7.2). Absent groups are denied. */
|
|
151
|
+
readonly access?: AccessSpec;
|
|
152
|
+
/** Record rules (RFC 0014 §7.3). */
|
|
153
|
+
readonly rules?: Readonly<Record<string, RuleSpec>>;
|
|
154
|
+
/** Partition the table (RFC 0015 §7.2); the key joins the primary key and must be a required stored field. */
|
|
155
|
+
readonly partition?: PartitionSpec;
|
|
156
|
+
}
|
|
157
|
+
type ModelKeys<Fs extends FieldsRecord, A extends boolean, C extends CompanyScope | undefined> = keyof ModelFields<string, Fs, A, C> & string;
|
|
158
|
+
/** Keys that can't be user fields or methods: implicit fields and the recordset API (RFC 0001 §7.5). */
|
|
159
|
+
export declare const RESERVED_KEYS: readonly string[];
|
|
160
|
+
/** @see RESERVED_KEYS */
|
|
161
|
+
export type ReservedKey = 'id' | 'createdAt' | 'updatedAt' | 'createdBy' | 'updatedBy' | 'env' | 'ids' | 'model' | 'length' | 'load' | 'read' | 'write' | 'create' | 'unlink' | 'search' | 'browse' | 'exists' | 'ensureOne' | 'first' | 'filter' | 'map' | 'mapped' | 'sortedBy' | 'groupBy' | 'sum' | 'union' | 'intersect' | 'minus' | 'concat' | 'copy' | 'withEnv' | 'withCompany' | 'withLang' | 'withFlags' | 'toJSON' | 'describe' | 'equals' | 'isEmpty' | 'names' | 'modelName' | 'registered' | 'resolveIds' | 'fieldValue';
|
|
162
|
+
type KeysTargeting<Fs, T extends string> = {
|
|
163
|
+
[K in keyof Fs]: Fs[K] extends {
|
|
164
|
+
readonly kind: 'many2one';
|
|
165
|
+
readonly target: T;
|
|
166
|
+
} ? K : never;
|
|
167
|
+
}[keyof Fs] & string;
|
|
168
|
+
type SiblingRule<K, F, Fs> = K extends ReservedKey ? never : F extends {
|
|
169
|
+
readonly kind: 'monetary';
|
|
170
|
+
readonly currencyField: infer C;
|
|
171
|
+
} ? C extends KeysTargeting<Fs, 'res.currency'> ? unknown : {
|
|
172
|
+
readonly currencyField: KeysTargeting<Fs, 'res.currency'>;
|
|
173
|
+
} : F extends {
|
|
174
|
+
readonly kind: 'quantity';
|
|
175
|
+
readonly uomField: infer U;
|
|
176
|
+
} ? U extends KeysTargeting<Fs, 'uom.uom'> ? unknown : {
|
|
177
|
+
readonly uomField: KeysTargeting<Fs, 'uom.uom'>;
|
|
178
|
+
} : unknown;
|
|
179
|
+
type ComputeRule<F, Ms> = FlagsOfField<F>['computeName'] extends infer Cm ? Cm extends string ? string extends Cm ? unknown : Cm extends keyof Ms ? unknown : {
|
|
180
|
+
readonly compute: keyof Ms & string;
|
|
181
|
+
} : unknown : unknown;
|
|
182
|
+
/**
|
|
183
|
+
* Per-field constraints that need the whole `fields` record: reserved keys, `monetary.currency` →
|
|
184
|
+
* `many2one('res.currency')`, `quantity.uom` → `many2one('uom.uom')`, `compute: 'name'` → a method.
|
|
185
|
+
* Non-homomorphic on purpose so it is never an inference source for `Fs`.
|
|
186
|
+
*/
|
|
187
|
+
export type FieldRules<Fs extends FieldsRecord, Ms extends MethodsRecord> = {
|
|
188
|
+
readonly [K in keyof Fs & string]: SiblingRule<K, Fs[K], Fs> & ComputeRule<Fs[K], Ms>;
|
|
189
|
+
};
|
|
190
|
+
/** One term of a model's default order. */
|
|
191
|
+
export interface OrderTerm<K extends string> {
|
|
192
|
+
readonly field: K;
|
|
193
|
+
readonly direction: 'asc' | 'desc';
|
|
194
|
+
}
|
|
195
|
+
/** A resolved SQL constraint with its PostgreSQL name. */
|
|
196
|
+
export interface ResolvedSqlConstraint<K extends string> {
|
|
197
|
+
readonly key: string;
|
|
198
|
+
readonly name: string;
|
|
199
|
+
readonly kind: 'unique' | 'check';
|
|
200
|
+
readonly fields: readonly K[];
|
|
201
|
+
readonly columns: readonly string[];
|
|
202
|
+
readonly where?: SqlFragment | undefined;
|
|
203
|
+
readonly check?: SqlFragment;
|
|
204
|
+
readonly message: string;
|
|
205
|
+
}
|
|
206
|
+
/** A resolved index with its PostgreSQL name. */
|
|
207
|
+
export interface ResolvedIndex<K extends string> {
|
|
208
|
+
readonly name: string;
|
|
209
|
+
readonly fields: readonly K[];
|
|
210
|
+
readonly columns: readonly string[];
|
|
211
|
+
readonly unique: boolean;
|
|
212
|
+
readonly where: SqlFragment | undefined;
|
|
213
|
+
readonly method: 'btree' | 'gin' | 'gist' | 'brin';
|
|
214
|
+
}
|
|
215
|
+
/** A resolved business constraint. */
|
|
216
|
+
export interface ResolvedConstraint extends ConstraintSpec {
|
|
217
|
+
readonly key: string;
|
|
218
|
+
}
|
|
219
|
+
/** A defined model: frozen metadata plus the types of its fields and methods. */
|
|
220
|
+
export interface Model<N extends string, Fs extends FieldsRecord, Ms extends MethodsRecord> {
|
|
221
|
+
readonly name: N;
|
|
222
|
+
readonly table: string;
|
|
223
|
+
readonly label: string;
|
|
224
|
+
readonly fields: Fs;
|
|
225
|
+
/** Column per field key; `undefined` for x2many relations and non-stored computes. */
|
|
226
|
+
readonly columns: {
|
|
227
|
+
readonly [K in keyof Fs]: string | undefined;
|
|
228
|
+
};
|
|
229
|
+
readonly order: readonly OrderTerm<keyof Fs & string>[];
|
|
230
|
+
readonly recName: keyof Fs & string;
|
|
231
|
+
readonly archivable: boolean;
|
|
232
|
+
readonly companyScoped: CompanyScope | undefined;
|
|
233
|
+
readonly tracking: readonly (keyof Fs & string)[];
|
|
234
|
+
readonly indexes: readonly ResolvedIndex<keyof Fs & string>[];
|
|
235
|
+
readonly sqlConstraints: readonly ResolvedSqlConstraint<keyof Fs & string>[];
|
|
236
|
+
readonly constraints: readonly ResolvedConstraint[];
|
|
237
|
+
readonly onchange: Readonly<Record<string, OnchangeFn>>;
|
|
238
|
+
readonly access: AccessSpec;
|
|
239
|
+
readonly rules: Readonly<Record<string, RuleSpec>>;
|
|
240
|
+
readonly partition: PartitionSpec | undefined;
|
|
241
|
+
readonly methods: Ms;
|
|
242
|
+
/** Keys of the fields the model declared itself (implicit fields excluded); extensions add to these. */
|
|
243
|
+
readonly declaredFields: readonly string[];
|
|
244
|
+
/** The definition as given, so the registry can rebuild the model with extensions applied. */
|
|
245
|
+
readonly definition: ModelDefinition<FieldsRecord, MethodsRecord, boolean, CompanyScope | undefined>;
|
|
246
|
+
/** Plain, JSON-serializable metadata (RFC 0001 §9). */
|
|
247
|
+
describe(): ModelDescription;
|
|
248
|
+
}
|
|
249
|
+
/** Any model. */
|
|
250
|
+
export type AnyModel = Model<string, FieldsRecord, MethodsRecord>;
|
|
251
|
+
/** JSON-serializable description of a field. Kind-specific keys are present only where they apply. */
|
|
252
|
+
export interface FieldDescription {
|
|
253
|
+
readonly kind: string;
|
|
254
|
+
readonly label: string;
|
|
255
|
+
readonly help: string | null;
|
|
256
|
+
readonly column: string | null;
|
|
257
|
+
readonly pgType: string;
|
|
258
|
+
readonly required: boolean;
|
|
259
|
+
readonly readonly: boolean;
|
|
260
|
+
readonly store: boolean;
|
|
261
|
+
readonly computed: boolean;
|
|
262
|
+
/** The method name, `'inline'`, or `null`. */
|
|
263
|
+
readonly compute: string | null;
|
|
264
|
+
readonly depends: readonly string[];
|
|
265
|
+
readonly hasDefault: boolean;
|
|
266
|
+
readonly index: string | null;
|
|
267
|
+
readonly copy: boolean;
|
|
268
|
+
readonly tracking: boolean;
|
|
269
|
+
readonly translate: boolean;
|
|
270
|
+
readonly sequence: {
|
|
271
|
+
readonly code: string;
|
|
272
|
+
readonly gapless: boolean | undefined;
|
|
273
|
+
readonly on: 'create' | 'manual';
|
|
274
|
+
readonly dateField: string | undefined;
|
|
275
|
+
} | null;
|
|
276
|
+
readonly deprecated: string | null;
|
|
277
|
+
readonly groups: FieldGroups;
|
|
278
|
+
readonly [extra: string]: unknown;
|
|
279
|
+
}
|
|
280
|
+
/** JSON-serializable description of a model. */
|
|
281
|
+
export interface ModelDescription {
|
|
282
|
+
readonly name: string;
|
|
283
|
+
readonly label: string;
|
|
284
|
+
readonly table: string;
|
|
285
|
+
readonly recName: string;
|
|
286
|
+
readonly order: readonly OrderTerm<string>[];
|
|
287
|
+
readonly archivable: boolean;
|
|
288
|
+
readonly companyScoped: CompanyScope | null;
|
|
289
|
+
readonly tracking: readonly string[];
|
|
290
|
+
readonly fields: Readonly<Record<string, FieldDescription>>;
|
|
291
|
+
readonly sqlConstraints: readonly {
|
|
292
|
+
readonly key: string;
|
|
293
|
+
readonly name: string;
|
|
294
|
+
readonly kind: 'unique' | 'check';
|
|
295
|
+
readonly fields: readonly string[];
|
|
296
|
+
readonly message: string;
|
|
297
|
+
}[];
|
|
298
|
+
readonly indexes: readonly {
|
|
299
|
+
readonly name: string;
|
|
300
|
+
readonly fields: readonly string[];
|
|
301
|
+
readonly unique: boolean;
|
|
302
|
+
readonly method: string;
|
|
303
|
+
readonly partial: boolean;
|
|
304
|
+
}[];
|
|
305
|
+
readonly constraints: readonly {
|
|
306
|
+
readonly key: string;
|
|
307
|
+
readonly depends: readonly string[];
|
|
308
|
+
readonly message: string | null;
|
|
309
|
+
}[];
|
|
310
|
+
readonly methods: readonly string[];
|
|
311
|
+
/** Fields with an onchange handler. */
|
|
312
|
+
readonly onchange: readonly string[];
|
|
313
|
+
readonly access: AccessSpec;
|
|
314
|
+
readonly rules: Readonly<Record<string, RuleSpec>>;
|
|
315
|
+
readonly partition: PartitionSpec | null;
|
|
316
|
+
}
|
|
317
|
+
/** @internal The compute of the implicit `displayName`: the engine derives it from `recName` (RFC 0008 §7.1). */
|
|
318
|
+
export declare const defaultDisplayName: () => undefined;
|
|
319
|
+
/** True when `value` was produced by {@link defineModel}. */
|
|
320
|
+
export declare function isModel(value: unknown): value is AnyModel;
|
|
321
|
+
/** `amountTotal` → `amount_total`. */
|
|
322
|
+
export declare function snakeCase(key: string): string;
|
|
323
|
+
/** `amountTotal` → `Amount Total`, `sale.order` → `Sale Order`. */
|
|
324
|
+
export declare function humanize(name: string): string;
|
|
325
|
+
/**
|
|
326
|
+
* Defines a model (RFC 0001).
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* ```ts
|
|
330
|
+
* export const SaleOrder = defineModel('sale.order', {
|
|
331
|
+
* companyScoped: { required: true },
|
|
332
|
+
* fields: {
|
|
333
|
+
* name: f.char({ required: true, readonly: true, sequence: 'sale.order' }),
|
|
334
|
+
* partner: f.many2one('res.partner', { required: true }),
|
|
335
|
+
* currency: f.many2one('res.currency', { required: true }),
|
|
336
|
+
* amountTotal: f.monetary({ currency: 'currency', store: true, depends: ['lines.subtotal'], compute: 'computeAmounts' }),
|
|
337
|
+
* },
|
|
338
|
+
* methods: { computeAmounts(): void { … } },
|
|
339
|
+
* });
|
|
340
|
+
* ```
|
|
341
|
+
* @throws {InvalidModelError} for reserved or non-camelCase keys, column collisions, a `monetary.currency` that
|
|
342
|
+
* isn't a `many2one('res.currency')`, a `compute` name that isn't a method, and invalid order / constraints / indexes.
|
|
343
|
+
*/
|
|
344
|
+
export declare function defineModel<const N extends string, const Fs extends FieldsRecord, const Ms extends MethodsRecord = NoMethods, const A extends boolean = false, const C extends CompanyScope | undefined = undefined>(name: N, definition: ModelDefinition<Fs, Ms, A, C> & {
|
|
345
|
+
readonly fields: FieldRules<NoInfer<Fs>, NoInfer<Ms>>;
|
|
346
|
+
}): Model<N, ModelFields<N, Fs, A, C>, Ms & TransitionMethods<Fs>>;
|
|
347
|
+
export {};
|