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.
- package/LICENSE +21 -0
- package/README.md +99 -2
- package/dist/audit/index.d.ts +59 -0
- package/dist/audit/index.js +96 -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 +164 -0
- package/dist/cli/config.d.ts +18 -0
- package/dist/cli/config.js +49 -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 +84 -0
- package/dist/cli/generate/render.d.ts +23 -0
- package/dist/cli/generate/render.js +249 -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/cli/resolve.d.ts +2 -0
- package/dist/cli/resolve.js +28 -0
- package/dist/core/compute/graph.d.ts +52 -0
- package/dist/core/compute/graph.js +162 -0
- package/dist/core/data/data.d.ts +59 -0
- package/dist/core/data/data.js +119 -0
- package/dist/core/data/load.d.ts +37 -0
- package/dist/core/data/load.js +236 -0
- package/dist/core/env/env.d.ts +468 -0
- package/dist/core/env/env.js +916 -0
- package/dist/core/env/identity.d.ts +43 -0
- package/dist/core/env/identity.js +65 -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 +256 -0
- package/dist/core/errors.js +360 -0
- package/dist/core/events/events.d.ts +44 -0
- package/dist/core/events/events.js +58 -0
- package/dist/core/events/workers.d.ts +56 -0
- package/dist/core/events/workers.js +155 -0
- package/dist/core/index.d.ts +51 -0
- package/dist/core/index.js +42 -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 +479 -0
- package/dist/core/model/field.js +461 -0
- package/dist/core/model/generated.d.ts +77 -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 +387 -0
- package/dist/core/model/model.js +566 -0
- package/dist/core/model/partition.d.ts +31 -0
- package/dist/core/model/partition.js +71 -0
- package/dist/core/model/related.d.ts +17 -0
- package/dist/core/model/related.js +60 -0
- package/dist/core/model/typed.d.ts +107 -0
- package/dist/core/model/typed.js +46 -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 +68 -0
- package/dist/core/money/fx.js +124 -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/aggregate.d.ts +22 -0
- package/dist/core/recordset/aggregate.js +258 -0
- package/dist/core/recordset/audit.d.ts +33 -0
- package/dist/core/recordset/audit.js +262 -0
- package/dist/core/recordset/compute.d.ts +36 -0
- package/dist/core/recordset/compute.js +414 -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 +31 -0
- package/dist/core/recordset/domain.js +300 -0
- package/dist/core/recordset/onchange.d.ts +43 -0
- package/dist/core/recordset/onchange.js +240 -0
- package/dist/core/recordset/recordset.d.ts +110 -0
- package/dist/core/recordset/recordset.js +789 -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 +56 -0
- package/dist/core/recordset/security.js +269 -0
- package/dist/core/recordset/sequences.d.ts +31 -0
- package/dist/core/recordset/sequences.js +154 -0
- package/dist/core/recordset/source.d.ts +9 -0
- package/dist/core/recordset/source.js +19 -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 +80 -0
- package/dist/core/recordset/types.d.ts +415 -0
- package/dist/core/recordset/types.js +8 -0
- package/dist/core/recordset/values.d.ts +33 -0
- package/dist/core/recordset/values.js +177 -0
- package/dist/core/recordset/writes.d.ts +42 -0
- package/dist/core/recordset/writes.js +786 -0
- package/dist/core/registry/ddl.d.ts +31 -0
- package/dist/core/registry/ddl.js +174 -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 +89 -0
- package/dist/core/registry/module.js +98 -0
- package/dist/core/registry/registry.d.ts +130 -0
- package/dist/core/registry/registry.js +591 -0
- package/dist/core/security/runtime.d.ts +102 -0
- package/dist/core/security/runtime.js +232 -0
- package/dist/driver-pg/driver.d.ts +88 -0
- package/dist/driver-pg/driver.js +177 -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 +233 -0
- package/dist/security/index.d.ts +147 -0
- package/dist/security/index.js +278 -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 +47 -0
- package/dist/sql/tag.js +123 -0
- package/package.json +82 -4
|
@@ -0,0 +1,461 @@
|
|
|
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 { normalizeFieldGroups } from '../../security/index.js';
|
|
6
|
+
import { InvalidFieldError, ShutroError } from '../errors.js';
|
|
7
|
+
import { toDecimal } from '../money/decimal.js';
|
|
8
|
+
import { related } from './related.js';
|
|
9
|
+
// ------------------------------------------------------------------------------------------------ validation
|
|
10
|
+
/** SQL-level names (columns, relation tables): snake_case. */
|
|
11
|
+
const IDENTIFIER = /^[a-z_][a-z0-9_]*$/;
|
|
12
|
+
/** Field keys in TypeScript: camelCase (RFC 0001 §7.5); the model maps them to snake_case columns. */
|
|
13
|
+
const FIELD_KEY = /^[a-z][a-zA-Z0-9]*$/;
|
|
14
|
+
const MODEL_NAME = /^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)*$/;
|
|
15
|
+
const created = new WeakSet();
|
|
16
|
+
/** True when `value` is a field descriptor produced by `f.*`. */
|
|
17
|
+
export function isField(value) {
|
|
18
|
+
return typeof value === 'object' && value !== null && created.has(value);
|
|
19
|
+
}
|
|
20
|
+
function assertModelName(name, what) {
|
|
21
|
+
if (typeof name !== 'string' || !MODEL_NAME.test(name)) {
|
|
22
|
+
throw new InvalidFieldError(`${JSON.stringify(name)} is not a valid model name for ${what}.`, "Model names are dotted lower-case identifiers such as 'res.partner' or 'sale.order.line'.");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function assertIdentifier(name, what) {
|
|
26
|
+
if (typeof name !== 'string' || !IDENTIFIER.test(name)) {
|
|
27
|
+
throw new InvalidFieldError(`${JSON.stringify(name)} is not a valid ${what}.`, 'Use lower-case snake_case (a-z, 0-9, _), starting with a letter or underscore.');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function assertHook(where, name, fn) {
|
|
31
|
+
if (fn !== undefined && typeof fn !== 'function') {
|
|
32
|
+
throw new InvalidFieldError(`${where}: ${name} is not a function.`, `Pass ${name}: (records) => { ... }.`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function assertFieldKey(name, what) {
|
|
36
|
+
if (typeof name !== 'string' || !FIELD_KEY.test(name)) {
|
|
37
|
+
throw new InvalidFieldError(`${JSON.stringify(name)} is not a valid ${what}.`, "Refer to the sibling field by its TypeScript key in camelCase, e.g. 'currency' or 'weightUom'.");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function assertPositiveInt(value, what) {
|
|
41
|
+
if (typeof value !== 'number' || !Number.isInteger(value) || value <= 0) {
|
|
42
|
+
throw new InvalidFieldError(`${what} must be a positive integer, got ${JSON.stringify(value)}.`, `Set ${what} to 1 or more.`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function assertDecimalDefault(value, kind) {
|
|
46
|
+
if (value === undefined || typeof value === 'function')
|
|
47
|
+
return;
|
|
48
|
+
try {
|
|
49
|
+
toDecimal(value);
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
const hint = e instanceof ShutroError ? e.hint : 'Use a decimal string or a Decimal.';
|
|
53
|
+
throw new InvalidFieldError(`The default of this ${kind} field is not an exact decimal.`, hint);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function normalize(options, defaults = {}) {
|
|
57
|
+
const storable = defaults.storable ?? true;
|
|
58
|
+
if (!storable && options.store !== undefined) {
|
|
59
|
+
throw new InvalidFieldError('A one2many or many2many field cannot be stored.', 'Remove `store`: x2many relations are read through the inverse field or the junction table.');
|
|
60
|
+
}
|
|
61
|
+
const compute = options.compute;
|
|
62
|
+
if (compute !== undefined &&
|
|
63
|
+
(typeof compute === 'string' ? compute.trim() === '' : typeof compute !== 'function')) {
|
|
64
|
+
throw new InvalidFieldError('`compute` must be a function or the name of a model method.', "Pass compute(records) { ... } or compute: 'computeAmounts'.");
|
|
65
|
+
}
|
|
66
|
+
if (compute === undefined && (options.depends !== undefined || options.inverse !== undefined)) {
|
|
67
|
+
throw new InvalidFieldError('`depends` and `inverse` need a `compute`.', 'Add the compute function, or remove depends/inverse.');
|
|
68
|
+
}
|
|
69
|
+
if (options.column !== undefined)
|
|
70
|
+
assertIdentifier(options.column, 'column name');
|
|
71
|
+
const sequence = options.sequence === undefined
|
|
72
|
+
? undefined
|
|
73
|
+
: typeof options.sequence === 'string'
|
|
74
|
+
? {
|
|
75
|
+
code: options.sequence,
|
|
76
|
+
gapless: undefined,
|
|
77
|
+
on: 'create',
|
|
78
|
+
dateField: undefined,
|
|
79
|
+
}
|
|
80
|
+
: {
|
|
81
|
+
code: options.sequence.code,
|
|
82
|
+
gapless: options.sequence.gapless,
|
|
83
|
+
on: options.sequence.on ?? 'create',
|
|
84
|
+
dateField: options.sequence.dateField,
|
|
85
|
+
};
|
|
86
|
+
if (sequence !== undefined && sequence.on !== 'create' && sequence.on !== 'manual') {
|
|
87
|
+
throw new InvalidFieldError(`sequence.on ${JSON.stringify(sequence.on)} is unknown.`, "Use on: 'create' (default) or on: 'manual'.");
|
|
88
|
+
}
|
|
89
|
+
if (sequence?.dateField !== undefined)
|
|
90
|
+
assertFieldKey(sequence.dateField, 'sequence.dateField');
|
|
91
|
+
if (sequence !== undefined && sequence.code.trim() === '') {
|
|
92
|
+
throw new InvalidFieldError('A sequence needs a code.', "Pass sequence: 'sale.order' or { code: 'account.move', gapless: true }.");
|
|
93
|
+
}
|
|
94
|
+
const computed = compute !== undefined;
|
|
95
|
+
if (options.search !== undefined &&
|
|
96
|
+
(!computed || options.store === true || typeof options.search !== 'function'))
|
|
97
|
+
throw new InvalidFieldError('`search` applies to non-stored computed fields.', 'Stored fields are searched directly; give non-stored computes search(operator, value) => domain.');
|
|
98
|
+
if (options.companyDependent === true &&
|
|
99
|
+
(options.required === true ||
|
|
100
|
+
computed ||
|
|
101
|
+
options.translate === true ||
|
|
102
|
+
options.encrypted === true ||
|
|
103
|
+
sequence !== undefined))
|
|
104
|
+
throw new InvalidFieldError('A company-dependent field cannot be required, computed, translated, encrypted or numbered.', 'Give it a default instead of required; companies without a value read the default.');
|
|
105
|
+
return {
|
|
106
|
+
label: options.label,
|
|
107
|
+
help: options.help,
|
|
108
|
+
required: options.required ?? defaults.required ?? false,
|
|
109
|
+
readonly: options.readonly ??
|
|
110
|
+
(sequence !== undefined ||
|
|
111
|
+
(computed && options.inverse === undefined) ||
|
|
112
|
+
(defaults.readonly ?? false)),
|
|
113
|
+
store: computed ? (options.store ?? false) : storable,
|
|
114
|
+
computed,
|
|
115
|
+
compute,
|
|
116
|
+
inverse: options.inverse,
|
|
117
|
+
depends: Object.freeze([...(options.depends ?? [])]),
|
|
118
|
+
default: options.default === undefined ? defaults.default : options.default,
|
|
119
|
+
index: options.index,
|
|
120
|
+
copy: options.copy ?? true,
|
|
121
|
+
tracking: options.tracking ?? false,
|
|
122
|
+
column: options.column,
|
|
123
|
+
groups: normalizeFieldGroups(options.groups),
|
|
124
|
+
deprecated: options.deprecated,
|
|
125
|
+
sequence,
|
|
126
|
+
translate: options.translate ?? false,
|
|
127
|
+
encrypted: options.encrypted ?? false,
|
|
128
|
+
companyDependent: options.companyDependent ?? false,
|
|
129
|
+
search: options.search,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
const COMPANY_DEPENDENT_KINDS = new Set([
|
|
133
|
+
'char',
|
|
134
|
+
'text',
|
|
135
|
+
'html',
|
|
136
|
+
'integer',
|
|
137
|
+
'float',
|
|
138
|
+
'decimal',
|
|
139
|
+
'boolean',
|
|
140
|
+
'date',
|
|
141
|
+
'selection',
|
|
142
|
+
'many2one',
|
|
143
|
+
]);
|
|
144
|
+
function make(input) {
|
|
145
|
+
const d = input;
|
|
146
|
+
let descriptor = input;
|
|
147
|
+
if (d.valueType === undefined && d.pgType !== undefined) {
|
|
148
|
+
if (d.companyDependent === true && !COMPANY_DEPENDENT_KINDS.has(d.kind ?? ''))
|
|
149
|
+
throw new InvalidFieldError(`A ${d.kind} field cannot be company-dependent.`, 'Company-dependent fields are char, text, html, integer, float, decimal, boolean, date, selection or many2one.');
|
|
150
|
+
descriptor = {
|
|
151
|
+
...input,
|
|
152
|
+
valueType: d.kind === 'many2one' ? 'bigint' : d.pgType,
|
|
153
|
+
pgType: d.companyDependent === true ? 'jsonb' : d.pgType,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
const frozen = Object.freeze(descriptor);
|
|
157
|
+
created.add(frozen);
|
|
158
|
+
return frozen;
|
|
159
|
+
}
|
|
160
|
+
function textLike(kind, options, pgType) {
|
|
161
|
+
const base = normalize(options);
|
|
162
|
+
if (base.translate && base.encrypted)
|
|
163
|
+
throw new InvalidFieldError('A field cannot be both translate and encrypted.', 'Pick one.');
|
|
164
|
+
return make({
|
|
165
|
+
kind,
|
|
166
|
+
pgType: base.translate ? 'jsonb' : base.encrypted ? 'text' : pgType,
|
|
167
|
+
...base,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
// -------------------------------------------------------------------------------------------------- builders
|
|
171
|
+
/** The field builders. See each method for its options; every descriptor is frozen metadata. */
|
|
172
|
+
export const f = {
|
|
173
|
+
/** Short text. `size` gives `varchar(size)`, otherwise `text`. */
|
|
174
|
+
char(options) {
|
|
175
|
+
const opts = options ?? {};
|
|
176
|
+
if (opts.size !== undefined)
|
|
177
|
+
assertPositiveInt(opts.size, 'size');
|
|
178
|
+
const base = textLike('char', opts, opts.size === undefined ? 'text' : `varchar(${opts.size})`);
|
|
179
|
+
return make({ ...base, size: opts.size });
|
|
180
|
+
},
|
|
181
|
+
/** Long text. */
|
|
182
|
+
text(options) {
|
|
183
|
+
return textLike('text', options ?? {}, 'text');
|
|
184
|
+
},
|
|
185
|
+
/** HTML, sanitized on write unless `sanitize: false`. */
|
|
186
|
+
html(options) {
|
|
187
|
+
const opts = options ?? {};
|
|
188
|
+
const base = textLike('html', opts, 'text');
|
|
189
|
+
return make({ ...base, sanitize: opts.sanitize ?? true });
|
|
190
|
+
},
|
|
191
|
+
/** Never nullable; defaults to `false`. */
|
|
192
|
+
boolean(options) {
|
|
193
|
+
const base = normalize(options ?? {}, { required: true, default: false });
|
|
194
|
+
return make({ kind: 'boolean', pgType: 'boolean', ...base, required: true });
|
|
195
|
+
},
|
|
196
|
+
/** 32-bit integer. For counts and sequences, never for money or quantities. */
|
|
197
|
+
integer(options) {
|
|
198
|
+
return make({ kind: 'integer', pgType: 'integer', ...normalize(options ?? {}) });
|
|
199
|
+
},
|
|
200
|
+
/** 64-bit integer. Read as a safe `number` (the driver rejects larger values); write `bigint` or `number`. */
|
|
201
|
+
bigint(options) {
|
|
202
|
+
return make({ kind: 'bigint', pgType: 'bigint', ...normalize(options ?? {}) });
|
|
203
|
+
},
|
|
204
|
+
/** Binary floating point. **Discouraged** for business values: use `decimal`, `monetary` or `quantity`. */
|
|
205
|
+
float(options) {
|
|
206
|
+
return make({
|
|
207
|
+
kind: 'float',
|
|
208
|
+
pgType: 'double precision',
|
|
209
|
+
...normalize(options ?? {}),
|
|
210
|
+
discouraged: 'float is binary floating point. Use decimal, monetary or quantity for business values (Decimal in code, numeric in PostgreSQL).',
|
|
211
|
+
});
|
|
212
|
+
},
|
|
213
|
+
/** Exact decimal: `numeric(precision, scale)`, or `numeric` with a named precision resolved by the registry. */
|
|
214
|
+
decimal(options) {
|
|
215
|
+
const { scale, precision } = options;
|
|
216
|
+
if (typeof scale === 'number') {
|
|
217
|
+
if (!Number.isInteger(scale) || scale < 0) {
|
|
218
|
+
throw new InvalidFieldError(`scale must be a non-negative integer or a named precision, got ${JSON.stringify(scale)}.`, "Use e.g. scale: 2, or scale: 'product.price'.");
|
|
219
|
+
}
|
|
220
|
+
if (precision !== undefined && (!Number.isInteger(precision) || precision <= scale)) {
|
|
221
|
+
throw new InvalidFieldError(`precision (${String(precision)}) must be an integer greater than scale (${scale}).`, 'Precision is the total number of digits, scale the digits after the point.');
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else if (typeof scale !== 'string' || scale.trim() === '') {
|
|
225
|
+
throw new InvalidFieldError('A named precision must be a non-empty string.', "Use e.g. scale: 'product.price', declared by a module's `precisions`.");
|
|
226
|
+
}
|
|
227
|
+
assertDecimalDefault(options.default, 'decimal');
|
|
228
|
+
const pgType = typeof scale === 'number' ? `numeric(${precision ?? 32}, ${scale})` : 'numeric';
|
|
229
|
+
return make({
|
|
230
|
+
kind: 'decimal',
|
|
231
|
+
pgType,
|
|
232
|
+
...normalize(options),
|
|
233
|
+
scale,
|
|
234
|
+
precision,
|
|
235
|
+
});
|
|
236
|
+
},
|
|
237
|
+
/** An amount in the currency of the sibling `currency` field (read as `Money`, RFC 0006). */
|
|
238
|
+
monetary(options) {
|
|
239
|
+
assertFieldKey(options.currency, 'currency field name');
|
|
240
|
+
assertDecimalDefault(options.default, 'monetary');
|
|
241
|
+
return make({
|
|
242
|
+
kind: 'monetary',
|
|
243
|
+
pgType: 'numeric',
|
|
244
|
+
...normalize(options),
|
|
245
|
+
currencyField: options.currency,
|
|
246
|
+
});
|
|
247
|
+
},
|
|
248
|
+
/** A quantity in the unit of the sibling `uom` field (read as `Quantity`). */
|
|
249
|
+
quantity(options) {
|
|
250
|
+
assertFieldKey(options.uom, 'uom field name');
|
|
251
|
+
assertDecimalDefault(options.default, 'quantity');
|
|
252
|
+
return make({
|
|
253
|
+
kind: 'quantity',
|
|
254
|
+
pgType: 'numeric',
|
|
255
|
+
...normalize(options),
|
|
256
|
+
uomField: options.uom,
|
|
257
|
+
scale: options.scale,
|
|
258
|
+
});
|
|
259
|
+
},
|
|
260
|
+
/** A calendar date (`'YYYY-MM-DD'`), never a JS `Date` (RFC 0001 W7). */
|
|
261
|
+
date(options) {
|
|
262
|
+
return make({ kind: 'date', pgType: 'date', ...normalize(options ?? {}) });
|
|
263
|
+
},
|
|
264
|
+
/** An instant (`timestamptz`), always UTC in storage. */
|
|
265
|
+
datetime(options) {
|
|
266
|
+
return make({
|
|
267
|
+
kind: 'datetime',
|
|
268
|
+
pgType: 'timestamptz',
|
|
269
|
+
...normalize(options ?? {}),
|
|
270
|
+
});
|
|
271
|
+
},
|
|
272
|
+
/**
|
|
273
|
+
* A closed set of values. The map form gives labels and a typed union; the function form is dynamic (`string`).
|
|
274
|
+
* @example f.selection({ consu: 'Goods', service: 'Service' }, { required: true, default: 'consu' })
|
|
275
|
+
*/
|
|
276
|
+
selection: ((values, options) => {
|
|
277
|
+
let selection;
|
|
278
|
+
if (typeof values === 'function') {
|
|
279
|
+
selection = values;
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
const entries = Object.entries(values);
|
|
283
|
+
if (entries.length === 0)
|
|
284
|
+
throw new InvalidFieldError('A selection needs at least one value.', "Pass a map such as { draft: 'Draft', done: 'Done' }.");
|
|
285
|
+
for (const [key] of entries)
|
|
286
|
+
assertIdentifier(key, 'selection value');
|
|
287
|
+
selection = Object.freeze(entries.map(([k, v]) => Object.freeze([k, v])));
|
|
288
|
+
}
|
|
289
|
+
const opts = options ?? {};
|
|
290
|
+
return make({
|
|
291
|
+
kind: 'selection',
|
|
292
|
+
pgType: 'text',
|
|
293
|
+
...normalize(opts),
|
|
294
|
+
selection,
|
|
295
|
+
extensible: opts.extensible ?? false,
|
|
296
|
+
});
|
|
297
|
+
}),
|
|
298
|
+
/**
|
|
299
|
+
* A selection with declared transitions (RFC 0001 §7.3). Readonly: values change only through transitions.
|
|
300
|
+
* @example f.state({ states: { draft: 'Quotation', sale: 'Sales Order' }, initial: 'draft', transitions: { confirm: { from: ['draft'], to: 'sale' } } })
|
|
301
|
+
*/
|
|
302
|
+
state(options) {
|
|
303
|
+
const states = Object.entries(options.states);
|
|
304
|
+
if (states.length === 0)
|
|
305
|
+
throw new InvalidFieldError('A state field needs at least one state.', 'Pass states: { draft: "Draft", ... }.');
|
|
306
|
+
const names = new Set();
|
|
307
|
+
const stateHooks = {};
|
|
308
|
+
for (const [key, spec] of states) {
|
|
309
|
+
assertIdentifier(key, 'state name');
|
|
310
|
+
names.add(key);
|
|
311
|
+
if (typeof spec !== 'string') {
|
|
312
|
+
if (typeof spec?.label !== 'string')
|
|
313
|
+
throw new InvalidFieldError(`State '${key}' has no label.`, "Use 'Label' or { label: 'Label', onEnter, onExit }.");
|
|
314
|
+
assertHook(`State '${key}'`, 'onEnter', spec.onEnter);
|
|
315
|
+
assertHook(`State '${key}'`, 'onExit', spec.onExit);
|
|
316
|
+
if (spec.onEnter !== undefined || spec.onExit !== undefined)
|
|
317
|
+
stateHooks[key] = {
|
|
318
|
+
...(spec.onEnter === undefined ? {} : { onEnter: spec.onEnter }),
|
|
319
|
+
...(spec.onExit === undefined ? {} : { onExit: spec.onExit }),
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (!names.has(options.initial)) {
|
|
324
|
+
throw new InvalidFieldError(`The initial state ${JSON.stringify(options.initial)} is not one of the states.`, `Use one of: ${[...names].join(', ')}.`);
|
|
325
|
+
}
|
|
326
|
+
for (const [name, t] of Object.entries(options.transitions)) {
|
|
327
|
+
assertIdentifier(name, 'transition name');
|
|
328
|
+
if (!Array.isArray(t.from) || t.from.length === 0) {
|
|
329
|
+
throw new InvalidFieldError(`Transition '${name}' has no source states.`, "Give at least one state in `from`, e.g. { from: ['draft'], to: 'sale' }.");
|
|
330
|
+
}
|
|
331
|
+
for (const s of [...t.from, t.to]) {
|
|
332
|
+
if (!names.has(s))
|
|
333
|
+
throw new InvalidFieldError(`Transition '${name}' refers to unknown state ${JSON.stringify(s)}.`, `Use one of: ${[...names].join(', ')}.`);
|
|
334
|
+
}
|
|
335
|
+
assertHook(`Transition '${name}'`, 'guard', t.guard);
|
|
336
|
+
assertHook(`Transition '${name}'`, 'before', t.before);
|
|
337
|
+
assertHook(`Transition '${name}'`, 'after', t.after);
|
|
338
|
+
if (t.message !== undefined && typeof t.message !== 'string')
|
|
339
|
+
throw new InvalidFieldError(`Transition '${name}': message is not a string.`, 'Give the message shown when the guard rejects records.');
|
|
340
|
+
}
|
|
341
|
+
const base = normalize({
|
|
342
|
+
label: options.label,
|
|
343
|
+
help: options.help,
|
|
344
|
+
tracking: options.tracking ?? true, // state changes are audited by default (RFC 0013)
|
|
345
|
+
default: options.initial,
|
|
346
|
+
}, { required: true, readonly: true });
|
|
347
|
+
return make({
|
|
348
|
+
kind: 'state',
|
|
349
|
+
pgType: 'text',
|
|
350
|
+
...base,
|
|
351
|
+
selection: Object.freeze(states.map(([k, v]) => Object.freeze([k, typeof v === 'string' ? v : v.label]))),
|
|
352
|
+
initial: options.initial,
|
|
353
|
+
transitions: Object.freeze(options.transitions),
|
|
354
|
+
stateHooks: Object.freeze(stateHooks),
|
|
355
|
+
extensible: options.extensible ?? false,
|
|
356
|
+
});
|
|
357
|
+
},
|
|
358
|
+
/** JSONB. Give the value type explicitly for now: `f.json<MyShape>()`. Standard Schema validation comes with constraints. */
|
|
359
|
+
json(options) {
|
|
360
|
+
return make({ kind: 'json', pgType: 'jsonb', ...normalize(options ?? {}) });
|
|
361
|
+
},
|
|
362
|
+
/** Bytes. Never prefetched. */
|
|
363
|
+
binary(options) {
|
|
364
|
+
return make({
|
|
365
|
+
kind: 'binary',
|
|
366
|
+
pgType: 'bytea',
|
|
367
|
+
...normalize(options ?? {}),
|
|
368
|
+
prefetch: false,
|
|
369
|
+
});
|
|
370
|
+
},
|
|
371
|
+
/** A reference to one record of `target` (column `<name>_id`, `ON DELETE RESTRICT` by default). */
|
|
372
|
+
many2one(target, options) {
|
|
373
|
+
assertModelName(target, 'many2one');
|
|
374
|
+
const opts = options ?? {};
|
|
375
|
+
const ondelete = opts.ondelete ?? 'restrict';
|
|
376
|
+
if (opts.foreignKey === false && opts.ondelete !== undefined) {
|
|
377
|
+
throw new InvalidFieldError('A many2one with foreignKey: false cannot set ondelete: nothing in the database enforces it.', 'Remove ondelete, or keep the foreign key.');
|
|
378
|
+
}
|
|
379
|
+
if (opts.required === true && ondelete === 'set null') {
|
|
380
|
+
throw new InvalidFieldError("A required many2one cannot use ondelete: 'set null'.", "Use 'restrict' (default) or 'cascade', or make the field optional.");
|
|
381
|
+
}
|
|
382
|
+
return make({
|
|
383
|
+
kind: 'many2one',
|
|
384
|
+
pgType: 'bigint',
|
|
385
|
+
...normalize(opts),
|
|
386
|
+
target,
|
|
387
|
+
ondelete,
|
|
388
|
+
checkCompany: opts.checkCompany ?? false,
|
|
389
|
+
foreignKey: opts.foreignKey !== false,
|
|
390
|
+
});
|
|
391
|
+
},
|
|
392
|
+
/** The records of `target` whose `inverse` many2one points here. */
|
|
393
|
+
one2many(target, inverse, options) {
|
|
394
|
+
assertModelName(target, 'one2many');
|
|
395
|
+
assertFieldKey(inverse, 'inverse field name');
|
|
396
|
+
return make({
|
|
397
|
+
kind: 'one2many',
|
|
398
|
+
pgType: '',
|
|
399
|
+
...normalize(options ?? {}, { storable: false, required: true }),
|
|
400
|
+
target,
|
|
401
|
+
inverseField: inverse,
|
|
402
|
+
});
|
|
403
|
+
},
|
|
404
|
+
/** A set of `target` records through a junction table. */
|
|
405
|
+
many2many(target, options) {
|
|
406
|
+
assertModelName(target, 'many2many');
|
|
407
|
+
const opts = options ?? {};
|
|
408
|
+
if (opts.relation !== undefined)
|
|
409
|
+
assertIdentifier(opts.relation, 'relation table name');
|
|
410
|
+
return make({
|
|
411
|
+
kind: 'many2many',
|
|
412
|
+
pgType: '',
|
|
413
|
+
...normalize(opts, { storable: false, required: true }),
|
|
414
|
+
target,
|
|
415
|
+
relation: opts.relation,
|
|
416
|
+
});
|
|
417
|
+
},
|
|
418
|
+
};
|
|
419
|
+
/** @internal Used by `defineModel` for the implicit `id` field. */
|
|
420
|
+
export function implicitIdField(model) {
|
|
421
|
+
void model;
|
|
422
|
+
const base = normalize({ required: true, readonly: true, label: 'ID', copy: false });
|
|
423
|
+
return make({ kind: 'bigint', pgType: 'bigint', ...base, identity: true });
|
|
424
|
+
}
|
|
425
|
+
/** @internal Returns the same descriptor with `label` filled in when it was missing. */
|
|
426
|
+
export function withDefaultLabel(field, label) {
|
|
427
|
+
if (field.label !== undefined)
|
|
428
|
+
return field;
|
|
429
|
+
return make({ ...field, label });
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* @internal The child's copy of a parent field under delegation (`delegates`): a non-stored related field through
|
|
433
|
+
* the delegate many2one, writable unless the parent field is readonly.
|
|
434
|
+
*/
|
|
435
|
+
export function delegatedField(field, path) {
|
|
436
|
+
const r = field.readonly ? related(path) : related(path, { writable: true });
|
|
437
|
+
return make({
|
|
438
|
+
...field,
|
|
439
|
+
computed: true,
|
|
440
|
+
store: false,
|
|
441
|
+
compute: r.compute,
|
|
442
|
+
inverse: 'inverse' in r ? r.inverse : undefined,
|
|
443
|
+
depends: r.depends,
|
|
444
|
+
readonly: field.readonly,
|
|
445
|
+
required: false,
|
|
446
|
+
default: undefined,
|
|
447
|
+
sequence: undefined,
|
|
448
|
+
index: undefined,
|
|
449
|
+
column: undefined,
|
|
450
|
+
tracking: false,
|
|
451
|
+
delegated: path.slice(0, path.indexOf('.')),
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
/** @internal Returns a new descriptor with `changes` applied; the registry validates them first. */
|
|
455
|
+
export function overrideField(field, changes) {
|
|
456
|
+
const next = { ...field };
|
|
457
|
+
for (const [key, value] of Object.entries(changes))
|
|
458
|
+
if (value !== undefined)
|
|
459
|
+
next[key] = value;
|
|
460
|
+
return make(next);
|
|
461
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Field types as `shutro generate` spells them (RFC 0002 §7.7), exported as the `F` namespace.
|
|
3
|
+
*
|
|
4
|
+
* `O` uses the builders' option vocabulary with `true` standing for presence (`{ required: true; default: true;
|
|
5
|
+
* compute: 'computeAmounts'; store: true }`), so a generated field has exactly the type the builder gives the
|
|
6
|
+
* declared one: the generated view and the inferred view are interchangeable.
|
|
7
|
+
*/
|
|
8
|
+
import type { BinaryField, CharField, DecimalField, FlagsOf, FloatField, IdField, JsonField, Many2manyField, Many2oneField, MonetaryField, One2manyField, QuantityField, ScalarField, SelectionField, StateField, TextWrite, Transition } from './field.js';
|
|
9
|
+
type NoOptions = Record<never, never>;
|
|
10
|
+
type UnionToIntersection<U> = (U extends unknown ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
11
|
+
/** The implicit `id` of model `M`. */
|
|
12
|
+
export type Id<M extends string> = IdField<M>;
|
|
13
|
+
/** `f.char`. */
|
|
14
|
+
export type Char<O = NoOptions> = CharField<FlagsOf<O>, TextWrite<O>>;
|
|
15
|
+
/** `f.text`. */
|
|
16
|
+
export type Text<O = NoOptions> = ScalarField<'text', string, TextWrite<O>, FlagsOf<O>>;
|
|
17
|
+
/** `f.html`. */
|
|
18
|
+
export type Html<O = NoOptions> = ScalarField<'html', string, TextWrite<O>, FlagsOf<O>> & {
|
|
19
|
+
readonly sanitize: boolean;
|
|
20
|
+
};
|
|
21
|
+
/** `f.boolean` (always required, always defaulted). */
|
|
22
|
+
export type Bool<O = NoOptions> = ScalarField<'boolean', boolean, boolean, FlagsOf<O, {
|
|
23
|
+
required: true;
|
|
24
|
+
hasDefault: true;
|
|
25
|
+
}>>;
|
|
26
|
+
/** `f.integer`. */
|
|
27
|
+
export type Integer<O = NoOptions> = ScalarField<'integer', number, number, FlagsOf<O>>;
|
|
28
|
+
/** `f.bigint`. */
|
|
29
|
+
export type Bigint<O = NoOptions> = ScalarField<'bigint', number, bigint | number, FlagsOf<O>>;
|
|
30
|
+
/** `f.float`. */
|
|
31
|
+
export type Float<O = NoOptions> = FloatField<FlagsOf<O>>;
|
|
32
|
+
/** `f.decimal`. */
|
|
33
|
+
export type Decimal<O = NoOptions> = DecimalField<FlagsOf<O>>;
|
|
34
|
+
/** `f.monetary`; `C` is the sibling currency field. */
|
|
35
|
+
export type Monetary<C extends string, O = NoOptions> = MonetaryField<C, FlagsOf<O>>;
|
|
36
|
+
/** `f.quantity`; `U` is the sibling unit-of-measure field. */
|
|
37
|
+
export type Quantity<U extends string, O = NoOptions> = QuantityField<U, FlagsOf<O>>;
|
|
38
|
+
/** `f.selection`; `V` is the union of values (`string` for dynamic selections). */
|
|
39
|
+
export type Selection<V extends string, O = NoOptions> = SelectionField<V, FlagsOf<O>>;
|
|
40
|
+
/** `f.state`; `S` is the union of states. */
|
|
41
|
+
export type State<S extends string, O = NoOptions> = StateField<S, Record<string, Transition<S>>, FlagsOf<O>>;
|
|
42
|
+
/** `f.json`. */
|
|
43
|
+
export type Json<V = unknown, O = NoOptions> = JsonField<V, FlagsOf<O>>;
|
|
44
|
+
/** `f.binary`. */
|
|
45
|
+
export type Binary<O = NoOptions> = BinaryField<FlagsOf<O>>;
|
|
46
|
+
/** `f.date`. */
|
|
47
|
+
export type DateOnly<O = NoOptions> = ScalarField<'date', string, string, FlagsOf<O>>;
|
|
48
|
+
/** `f.datetime`. */
|
|
49
|
+
export type Datetime<O = NoOptions> = ScalarField<'datetime', Date, Date, FlagsOf<O>>;
|
|
50
|
+
/** `f.many2one`. */
|
|
51
|
+
export type Many2one<T extends string, O = NoOptions> = Many2oneField<T, FlagsOf<O>>;
|
|
52
|
+
/** `f.one2many`. */
|
|
53
|
+
export type One2many<T extends string, O = NoOptions> = One2manyField<T, FlagsOf<O, {
|
|
54
|
+
required: true;
|
|
55
|
+
hasDefault: true;
|
|
56
|
+
}>>;
|
|
57
|
+
/** `f.many2many`. */
|
|
58
|
+
export type Many2many<T extends string, O = NoOptions> = Many2manyField<T, FlagsOf<O, {
|
|
59
|
+
required: true;
|
|
60
|
+
hasDefault: true;
|
|
61
|
+
}>>;
|
|
62
|
+
type Contributions<Mod> = Mod extends {
|
|
63
|
+
readonly models: readonly (infer X)[];
|
|
64
|
+
readonly extensions: readonly (infer Y)[];
|
|
65
|
+
} ? X | Y : never;
|
|
66
|
+
/**
|
|
67
|
+
* The `methods` that module `Mod` (its models and extensions) declares on model `N`. Generated views intersect one
|
|
68
|
+
* of these per contributing module, so method types come from the sources without a compiler API.
|
|
69
|
+
*/
|
|
70
|
+
export type Methods<Mod, N extends string> = OrEmpty<UnionToIntersection<Extract<Contributions<Mod>, {
|
|
71
|
+
readonly name: N;
|
|
72
|
+
}> extends infer C ? C extends {
|
|
73
|
+
readonly methods: infer M;
|
|
74
|
+
} ? M : never : never>>;
|
|
75
|
+
/** `{}` instead of `unknown` when a module declares no method, so generated method interfaces can extend it. */
|
|
76
|
+
type OrEmpty<X> = unknown extends X ? Record<never, never> : X;
|
|
77
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The id of a record of model `M`: a `number` branded per model, so a product id can't be passed where a partner
|
|
3
|
+
* id is expected (RFC 0001 W6, Q2). Backed by PostgreSQL `bigint`; the driver rejects values above
|
|
4
|
+
* `Number.MAX_SAFE_INTEGER`.
|
|
5
|
+
*/
|
|
6
|
+
export type Id<M extends string> = number & {
|
|
7
|
+
readonly __model: M;
|
|
8
|
+
};
|
|
9
|
+
/** Brands a plain number as an id. Use only at trusted boundaries (driver rows, validated input). */
|
|
10
|
+
export declare function asId<M extends string>(value: number, _model: M): Id<M>;
|
|
11
|
+
/**
|
|
12
|
+
* Typed writes to a one2many or many2many field (RFC 0001 §7.6), replacing Odoo's command tuples. `C` and `W` are
|
|
13
|
+
* the target model's create and write faces once a schema view binds them; unbound, any record shape is accepted.
|
|
14
|
+
*/
|
|
15
|
+
export interface X2ManyCommands<T extends string, C = Readonly<Record<string, unknown>>, W = Readonly<Record<string, unknown>>> {
|
|
16
|
+
/** Create new records linked to this one (the inverse field is filled in by the ORM). */
|
|
17
|
+
readonly create?: readonly C[];
|
|
18
|
+
/** Update linked records. */
|
|
19
|
+
readonly update?: readonly {
|
|
20
|
+
readonly id: Id<T>;
|
|
21
|
+
readonly values: W;
|
|
22
|
+
}[];
|
|
23
|
+
/** Delete linked records. */
|
|
24
|
+
readonly delete?: readonly Id<T>[];
|
|
25
|
+
/** Link existing records (many2many) or re-point them here (one2many). */
|
|
26
|
+
readonly link?: readonly Id<T>[];
|
|
27
|
+
/** Remove the link without deleting the record. */
|
|
28
|
+
readonly unlink?: readonly Id<T>[];
|
|
29
|
+
/** Replace the whole set. */
|
|
30
|
+
readonly set?: readonly Id<T>[];
|
|
31
|
+
/** Remove every link. */
|
|
32
|
+
readonly clear?: true;
|
|
33
|
+
}
|