@kiri-orm/core 0.1.0 → 0.1.2
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/README.md +48 -3
- package/dist/types/client/create-client.d.ts +32 -0
- package/dist/types/client/index.d.ts +4 -0
- package/dist/types/client/model-handle.d.ts +148 -0
- package/dist/types/client/module.d.ts +54 -0
- package/dist/types/client/orm-client.d.ts +107 -0
- package/dist/types/connection.d.ts +40 -0
- package/dist/types/crud/aggregate-filter.d.ts +36 -0
- package/dist/types/crud/aggregate-paths.d.ts +74 -0
- package/dist/types/crud/aggregate-spec.d.ts +36 -0
- package/dist/types/crud/aggregate.d.ts +59 -0
- package/dist/types/crud/create-many.d.ts +42 -0
- package/dist/types/crud/errors.d.ts +16 -0
- package/dist/types/crud/index.d.ts +12 -0
- package/dist/types/crud/insert-many.d.ts +63 -0
- package/dist/types/crud/map-filter.d.ts +18 -0
- package/dist/types/crud/order.d.ts +58 -0
- package/dist/types/crud/paginate.d.ts +55 -0
- package/dist/types/crud/prepare-write.d.ts +55 -0
- package/dist/types/crud/repository-factory.d.ts +27 -0
- package/dist/types/crud/repository.d.ts +297 -0
- package/dist/types/crud/resolve-where.d.ts +24 -0
- package/dist/types/crud/row-mapping.d.ts +45 -0
- package/dist/types/crud/select.d.ts +74 -0
- package/dist/types/crud/sequence.d.ts +56 -0
- package/dist/types/crud/transaction.d.ts +19 -0
- package/dist/types/diagnostics/index.d.ts +1 -0
- package/dist/types/diagnostics/n-plus-one.d.ts +108 -0
- package/dist/types/dialect/date-bucket.d.ts +46 -0
- package/dist/types/dialect/index.d.ts +1 -0
- package/dist/types/domain/compile.d.ts +30 -0
- package/dist/types/domain/errors.d.ts +20 -0
- package/dist/types/domain/index.d.ts +4 -0
- package/dist/types/domain/parse.d.ts +30 -0
- package/dist/types/dto/field-schema.d.ts +12 -0
- package/dist/types/dto/index.d.ts +3 -0
- package/dist/types/dto/jsdoc-generator.d.ts +29 -0
- package/dist/types/dto/schema-generator.d.ts +59 -0
- package/dist/types/fields/auto-id.d.ts +27 -0
- package/dist/types/fields/cast-value.d.ts +19 -0
- package/dist/types/fields/computed-access.d.ts +22 -0
- package/dist/types/fields/default-for.d.ts +11 -0
- package/dist/types/fields/default-registry.d.ts +9 -0
- package/dist/types/fields/define-computed-field.d.ts +57 -0
- package/dist/types/fields/define-field.d.ts +35 -0
- package/dist/types/fields/errors.d.ts +30 -0
- package/dist/types/fields/index.d.ts +7 -0
- package/dist/types/fields/registry.d.ts +55 -0
- package/dist/types/fields/types/boolean.d.ts +5 -0
- package/dist/types/fields/types/char.d.ts +12 -0
- package/dist/types/fields/types/date.d.ts +10 -0
- package/dist/types/fields/types/datetime.d.ts +11 -0
- package/dist/types/fields/types/enum.d.ts +11 -0
- package/dist/types/fields/types/float.d.ts +11 -0
- package/dist/types/fields/types/integer.d.ts +11 -0
- package/dist/types/fields/types/json.d.ts +9 -0
- package/dist/types/fields/types/text.d.ts +10 -0
- package/dist/types/fields/types/uuid.d.ts +14 -0
- package/dist/types/fields/validate-value.d.ts +14 -0
- package/dist/types/global/connect.d.ts +29 -0
- package/dist/types/global/create-model.d.ts +189 -0
- package/dist/types/global/create-report.d.ts +32 -0
- package/dist/types/global/dto.d.ts +31 -0
- package/dist/types/global/glob.d.ts +12 -0
- package/dist/types/global/identity-scope.d.ts +18 -0
- package/dist/types/global/index.d.ts +12 -0
- package/dist/types/global/load-models.d.ts +31 -0
- package/dist/types/global/migrate.d.ts +68 -0
- package/dist/types/global/n-plus-one.d.ts +42 -0
- package/dist/types/global/sequences.d.ts +19 -0
- package/dist/types/global/state.d.ts +38 -0
- package/dist/types/global/sync-models.d.ts +11 -0
- package/dist/types/global/transaction.d.ts +22 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/meta/index.d.ts +5 -0
- package/dist/types/meta/query.d.ts +34 -0
- package/dist/types/meta/reserved-tables.d.ts +10 -0
- package/dist/types/meta/schema.d.ts +18 -0
- package/dist/types/meta/snapshot.d.ts +35 -0
- package/dist/types/meta/sync-meta.d.ts +42 -0
- package/dist/types/meta/upsert.d.ts +81 -0
- package/dist/types/migrations/apply.d.ts +18 -0
- package/dist/types/migrations/describe.d.ts +11 -0
- package/dist/types/migrations/diff.d.ts +141 -0
- package/dist/types/migrations/errors.d.ts +23 -0
- package/dist/types/migrations/foreign-keys-plan.d.ts +27 -0
- package/dist/types/migrations/generate.d.ts +51 -0
- package/dist/types/migrations/index.d.ts +7 -0
- package/dist/types/migrations/render.d.ts +45 -0
- package/dist/types/migrations/runner.d.ts +118 -0
- package/dist/types/models/define-model.d.ts +52 -0
- package/dist/types/models/errors.d.ts +23 -0
- package/dist/types/models/index.d.ts +4 -0
- package/dist/types/models/inheritance.d.ts +73 -0
- package/dist/types/models/naming.d.ts +44 -0
- package/dist/types/models/primary-key.d.ts +39 -0
- package/dist/types/models/registry.d.ts +112 -0
- package/dist/types/relations/attach-detach.d.ts +76 -0
- package/dist/types/relations/cascade-delete.d.ts +29 -0
- package/dist/types/relations/commands.d.ts +84 -0
- package/dist/types/relations/define-relation.d.ts +106 -0
- package/dist/types/relations/eager-load.d.ts +28 -0
- package/dist/types/relations/errors.d.ts +21 -0
- package/dist/types/relations/foreign-keys.d.ts +76 -0
- package/dist/types/relations/index.d.ts +7 -0
- package/dist/types/relations/keys.d.ts +14 -0
- package/dist/types/relations/pivot-schema.d.ts +24 -0
- package/dist/types/relations/with-spec.d.ts +33 -0
- package/dist/types/reports/define-report.d.ts +37 -0
- package/dist/types/reports/errors.d.ts +9 -0
- package/dist/types/reports/index.d.ts +3 -0
- package/dist/types/reports/report-handle.d.ts +58 -0
- package/dist/types/schema/build-column.d.ts +18 -0
- package/dist/types/schema/column-builder-registry.d.ts +30 -0
- package/dist/types/schema/default-column-builders.d.ts +9 -0
- package/dist/types/schema/errors.d.ts +9 -0
- package/dist/types/schema/foreign-keys.d.ts +33 -0
- package/dist/types/schema/index.d.ts +6 -0
- package/dist/types/schema/introspect-foreign-keys.d.ts +23 -0
- package/dist/types/schema/sync.d.ts +63 -0
- package/dist/types/uow/entity.d.ts +80 -0
- package/dist/types/uow/errors.d.ts +8 -0
- package/dist/types/uow/hooks.d.ts +35 -0
- package/dist/types/uow/index.d.ts +5 -0
- package/dist/types/uow/unit-of-work.d.ts +142 -0
- package/dist/types/uow/with-unit-of-work.d.ts +23 -0
- package/package.json +20 -3
- package/src/connection.js +1 -1
- package/src/crud/insert-many.js +1 -1
- package/src/crud/repository.js +9 -1
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Date buckets: truncating a date/datetime column to the start of its hour, day, week,
|
|
3
|
+
* month, quarter or year, so rows can be grouped by period.
|
|
4
|
+
*
|
|
5
|
+
* Postgres and SQLite spell this completely differently (`date_trunc` vs `strftime`), and
|
|
6
|
+
* store dates differently (a real `timestamptz`/`date` vs epoch milliseconds in an integer
|
|
7
|
+
* column), so the SQL lives here, once, behind one function. Both return the bucket the
|
|
8
|
+
* same way - an ISO-8601 UTC string, `2024-03-01T00:00:00Z` - which the caller turns
|
|
9
|
+
* into a Date. Returning a string instead of letting each driver hand back its own
|
|
10
|
+
* timestamp type is what keeps the result identical on both databases.
|
|
11
|
+
*
|
|
12
|
+
* Buckets are in UTC, always. Without that, Postgres would bucket by the session's time
|
|
13
|
+
* zone, and the same data would group differently depending on how the server is configured
|
|
14
|
+
* (a row at 23:30 UTC on March 15 lands in March 16 on a server set to Africa/Addis_Ababa).
|
|
15
|
+
* Weeks start on Monday (ISO 8601) on both.
|
|
16
|
+
*/
|
|
17
|
+
export declare const DATE_BUCKETS: string[];
|
|
18
|
+
/**
|
|
19
|
+
* Builds the SQL expression for one bucket.
|
|
20
|
+
*
|
|
21
|
+
* Nothing user-supplied is spliced in: `bucket` must be one of DATE_BUCKETS (checked here),
|
|
22
|
+
* and the column goes through Knex's `??` identifier binding. The bucket name is inlined as
|
|
23
|
+
* a literal on purpose - as a bound parameter, Postgres would see `date_trunc($1, ...)` in
|
|
24
|
+
* the SELECT list and `date_trunc($2, ...)` in GROUP BY as two different expressions and
|
|
25
|
+
* refuse to run the query.
|
|
26
|
+
*
|
|
27
|
+
* @param {import('knex').Knex} db - decides the dialect
|
|
28
|
+
* @param {string} column - the column name (not the field name)
|
|
29
|
+
* @param {"hour"|"day"|"week"|"month"|"quarter"|"year"} bucket
|
|
30
|
+
* @param {{ fieldType?: "date"|"datetime" }} [options] - Postgres stores `date` and
|
|
31
|
+
* `datetime` as different types and each needs its own conversion to a UTC timestamp
|
|
32
|
+
* @returns {{ sql: string, bindings: string[] }} an expression yielding the bucket start as `...Z` text
|
|
33
|
+
*/
|
|
34
|
+
export declare function dateBucketSql(db: import('knex').Knex, column: string, bucket: "hour" | "day" | "week" | "month" | "quarter" | "year", { fieldType }?: {
|
|
35
|
+
fieldType?: "date" | "datetime";
|
|
36
|
+
}): {
|
|
37
|
+
sql: string;
|
|
38
|
+
bindings: string[];
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Turns a bucket string produced by dateBucketSql() into a Date (or null for a row whose
|
|
42
|
+
* date was null).
|
|
43
|
+
* @param {string|null|undefined} value
|
|
44
|
+
* @returns {Date|null}
|
|
45
|
+
*/
|
|
46
|
+
export declare function parseBucket(value: string | null | undefined): Date | null;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DATE_BUCKETS, dateBucketSql, parseBucket } from "./date-bucket.js";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compiles a domain array into a function suitable for
|
|
3
|
+
* `queryBuilder.where(compileDomain(model, domain))`. Field names are
|
|
4
|
+
* validated against the model's registered fields at compile time
|
|
5
|
+
* (before any query runs), and each leaf's value is cast through the
|
|
6
|
+
* field's type the same way Repository's plain-object filters are
|
|
7
|
+
* (Phase 5) - so `["id", "=", "1"]` and `{ id: "1" }` behave
|
|
8
|
+
* identically.
|
|
9
|
+
*
|
|
10
|
+
* A field name containing a `.` (e.g. `"tags.name"`) traverses a
|
|
11
|
+
* relation instead of naming a plain column - see
|
|
12
|
+
* compileRelationLeaf() for exactly how that compiles. This requires
|
|
13
|
+
* `modelRegistry` to resolve the relation's target model; omit it and
|
|
14
|
+
* a plain (non-relation) domain still works exactly as before.
|
|
15
|
+
*
|
|
16
|
+
* @param {import('../models/define-model.js').ModelDescriptor} model
|
|
17
|
+
* @param {unknown[]} domain
|
|
18
|
+
* @param {import('../fields/registry.js').FieldTypeRegistry} [fieldTypeRegistry]
|
|
19
|
+
* @param {import('../models/registry.js').ModelRegistry} [modelRegistry]
|
|
20
|
+
* @returns {(qb: import('knex').Knex.QueryBuilder) => void}
|
|
21
|
+
*/
|
|
22
|
+
export declare function compileDomain(model: import('../models/define-model.js').ModelDescriptor, domain: unknown[], fieldTypeRegistry?: import('../fields/registry.js').FieldTypeRegistry, modelRegistry?: import('../models/registry.js').ModelRegistry): (qb: import('knex').Knex.QueryBuilder) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Like compileDomain(), for an AST that has already been parsed (see parseDomain()).
|
|
25
|
+
* Aggregate queries need this: they parse once, split the tree into WHERE and HAVING
|
|
26
|
+
* halves, then compile the WHERE half.
|
|
27
|
+
*
|
|
28
|
+
* @param {import('./parse.js').DomainNode} ast
|
|
29
|
+
*/
|
|
30
|
+
export declare function compileParsedDomain(model: any, ast: import('./parse.js').DomainNode, fieldTypeRegistry?: import("../fields/registry.js").FieldTypeRegistry, modelRegistry?: undefined): (qb: import('knex').Knex.QueryBuilder) => void;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when a domain array is malformed: not an array, a leaf that
|
|
3
|
+
* isn't a 3-element [field, operator, value] tuple, an unknown
|
|
4
|
+
* operator, or a prefix operator ('&', '|', '!') without enough
|
|
5
|
+
* following expressions to consume. A structural problem with the
|
|
6
|
+
* domain itself, independent of which model it's evaluated against.
|
|
7
|
+
*/
|
|
8
|
+
export declare class DomainSyntaxError extends Error {
|
|
9
|
+
constructor(message: any);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Thrown when a structurally valid domain references something that
|
|
13
|
+
* doesn't exist on the model it's compiled against - an unknown field
|
|
14
|
+
* name (the "catch typos before hitting the DB" goal), or a value
|
|
15
|
+
* shape that doesn't fit the operator (e.g. a non-array value for
|
|
16
|
+
* "in").
|
|
17
|
+
*/
|
|
18
|
+
export declare class DomainValidationError extends Error {
|
|
19
|
+
constructor(message: any);
|
|
20
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export declare const DOMAIN_OPERATORS: string[];
|
|
2
|
+
export type LeafNode = {
|
|
3
|
+
type: "leaf";
|
|
4
|
+
field: string;
|
|
5
|
+
op: string;
|
|
6
|
+
value: unknown;
|
|
7
|
+
};
|
|
8
|
+
export type GroupNode = {
|
|
9
|
+
type: "and" | "or";
|
|
10
|
+
children: DomainNode[];
|
|
11
|
+
};
|
|
12
|
+
export type NotNode = {
|
|
13
|
+
type: "not";
|
|
14
|
+
children: [DomainNode];
|
|
15
|
+
};
|
|
16
|
+
export type DomainNode = LeafNode | GroupNode | NotNode;
|
|
17
|
+
/**
|
|
18
|
+
* Parses a full domain array into an AST. Top-level list items are
|
|
19
|
+
* implicitly AND-ed together (matching the roadmap's example
|
|
20
|
+
* `[["state", "=", "done"], ["amount", ">", 100]]`), with explicit
|
|
21
|
+
* `'&'`/`'|'`/`'!'` available for anything implicit-AND can't express
|
|
22
|
+
* - most importantly OR and NOT. Nested groups can be written either
|
|
23
|
+
* flat (Odoo-style prefix notation) or wrapped in their own array for
|
|
24
|
+
* readability - see parseOperand() for exactly how that's
|
|
25
|
+
* disambiguated. An empty domain `[]` matches everything.
|
|
26
|
+
*
|
|
27
|
+
* @param {unknown[]} domain
|
|
28
|
+
* @returns {DomainNode}
|
|
29
|
+
*/
|
|
30
|
+
export declare function parseDomain(domain: unknown[]): DomainNode;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts one field descriptor (Phase 1) into a JSON Schema
|
|
3
|
+
* fragment. Pure data transformation - no DB, no compiler, just the
|
|
4
|
+
* field object that already exists at runtime. This is the whole
|
|
5
|
+
* point of not depending on TypeScript's type system for this: the
|
|
6
|
+
* schema IS the source of truth already, so generating a JSON Schema
|
|
7
|
+
* from it is just reading properties off an object.
|
|
8
|
+
*
|
|
9
|
+
* @param {import('../fields/define-field.js').FieldDescriptor} field
|
|
10
|
+
* @returns {object}
|
|
11
|
+
*/
|
|
12
|
+
export declare function fieldToJsonSchema(field: import('../fields/define-field.js').FieldDescriptor): object;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { fieldToJsonSchema } from "./field-schema.js";
|
|
2
|
+
export { generateCreateSchema, generateUpdateSchema, generateReadSchema, generateDTOSchemas, generateDTOSchemasForRegistry, } from "./schema-generator.js";
|
|
3
|
+
export { jsonSchemaToJSDoc, generateJSDocForModel, generateJSDocForRegistry, } from "./jsdoc-generator.js";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders one JSON Schema object (as produced by schema-generator.js)
|
|
3
|
+
* into a `@typedef` JSDoc comment block. Properties not in the
|
|
4
|
+
* schema's `required` list are rendered with the `[name]` optional
|
|
5
|
+
* syntax JSDoc/TypeScript's JSDoc-mode both recognize.
|
|
6
|
+
*
|
|
7
|
+
* @param {string} typeName
|
|
8
|
+
* @param {object} schema - a JSON Schema object with `type: "object"`
|
|
9
|
+
* @returns {string}
|
|
10
|
+
*/
|
|
11
|
+
export declare function jsonSchemaToJSDoc(typeName: string, schema: object): string;
|
|
12
|
+
/**
|
|
13
|
+
* Generates all three `@typedef` blocks (Create/Update/Read) for one
|
|
14
|
+
* model, concatenated into a single string ready to write to a `.js`
|
|
15
|
+
* file for editor intellisense - no TypeScript compilation involved,
|
|
16
|
+
* just JSDoc comments an editor's language server already understands.
|
|
17
|
+
*
|
|
18
|
+
* @param {import('../models/define-model.js').ModelDescriptor} model
|
|
19
|
+
* @returns {string}
|
|
20
|
+
*/
|
|
21
|
+
export declare function generateJSDocForModel(model: import('../models/define-model.js').ModelDescriptor): string;
|
|
22
|
+
/**
|
|
23
|
+
* Generates JSDoc typedefs for every model in a registry, concatenated
|
|
24
|
+
* into one file-ready string.
|
|
25
|
+
*
|
|
26
|
+
* @param {import('../models/registry.js').ModelRegistry} modelRegistry
|
|
27
|
+
* @returns {string}
|
|
28
|
+
*/
|
|
29
|
+
export declare function generateJSDocForRegistry(modelRegistry: import('../models/registry.js').ModelRegistry): string;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `create` DTO: every writable field except an automatically-assigned
|
|
3
|
+
* primary key (integer autoincrement or generated uuid - see
|
|
4
|
+
* fields/auto-id.js). A primary key that ISN'T auto-assigned (e.g. a
|
|
5
|
+
* client-supplied code) is a normal writable field and appears here like
|
|
6
|
+
* any other. A field is REQUIRED in this schema only if it's `required:
|
|
7
|
+
* true` AND has no `default` - a required field with a default is optional
|
|
8
|
+
* to supply, matching exactly how `prepareWrite()`'s create semantics
|
|
9
|
+
* already treat it (an omitted field with a default gets filled in, not
|
|
10
|
+
* rejected).
|
|
11
|
+
*
|
|
12
|
+
* @param {import('../models/define-model.js').ModelDescriptor} model
|
|
13
|
+
* @returns {object} JSON Schema
|
|
14
|
+
*/
|
|
15
|
+
export declare function generateCreateSchema(model: import('../models/define-model.js').ModelDescriptor): object;
|
|
16
|
+
/**
|
|
17
|
+
* The `update` DTO: every writable field except the primary key (a
|
|
18
|
+
* primary key is never updated in place, auto-assigned or not - see
|
|
19
|
+
* Repository#update(), which always identifies the row BY that key),
|
|
20
|
+
* ALL optional - matching `prepareWrite()`'s partial-update semantics
|
|
21
|
+
* (only fields explicitly provided are touched, nothing is required).
|
|
22
|
+
*
|
|
23
|
+
* @param {import('../models/define-model.js').ModelDescriptor} model
|
|
24
|
+
* @returns {object} JSON Schema
|
|
25
|
+
*/
|
|
26
|
+
export declare function generateUpdateSchema(model: import('../models/define-model.js').ModelDescriptor): object;
|
|
27
|
+
/**
|
|
28
|
+
* The `read` DTO: every field with `expose !== false`, including the
|
|
29
|
+
* primary key (present when read, even though absent - or, for a
|
|
30
|
+
* non-auto key, required - from create/update).
|
|
31
|
+
* A field that's genuinely required in the database is also required
|
|
32
|
+
* here - a successfully-read row always has a value for it.
|
|
33
|
+
*
|
|
34
|
+
* @param {import('../models/define-model.js').ModelDescriptor} model
|
|
35
|
+
* @returns {object} JSON Schema
|
|
36
|
+
*/
|
|
37
|
+
export declare function generateReadSchema(model: import('../models/define-model.js').ModelDescriptor): object;
|
|
38
|
+
/**
|
|
39
|
+
* All three DTO schemas for one model at once.
|
|
40
|
+
*
|
|
41
|
+
* @param {import('../models/define-model.js').ModelDescriptor} model
|
|
42
|
+
* @returns {{ create: object, update: object, read: object }}
|
|
43
|
+
*/
|
|
44
|
+
export declare function generateDTOSchemas(model: import('../models/define-model.js').ModelDescriptor): {
|
|
45
|
+
create: object;
|
|
46
|
+
update: object;
|
|
47
|
+
read: object;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Generates DTO schemas for every model in a registry.
|
|
51
|
+
*
|
|
52
|
+
* @param {import('../models/registry.js').ModelRegistry} modelRegistry
|
|
53
|
+
* @returns {Record<string, { create: object, update: object, read: object }>}
|
|
54
|
+
*/
|
|
55
|
+
export declare function generateDTOSchemasForRegistry(modelRegistry: import('../models/registry.js').ModelRegistry): Record<string, {
|
|
56
|
+
create: object;
|
|
57
|
+
update: object;
|
|
58
|
+
read: object;
|
|
59
|
+
}>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sentinel default for an auto-increment integer `id`: "leave this
|
|
3
|
+
* column out of the INSERT and let the database assign it". Never a
|
|
4
|
+
* real value - prepareWrite() recognizes it and skips the field.
|
|
5
|
+
*/
|
|
6
|
+
export declare const AUTO_INCREMENT: unique symbol;
|
|
7
|
+
/**
|
|
8
|
+
* Whether a field gets automatic primary-key assignment: it must be
|
|
9
|
+
* the model's primary key, typed `integer` or `uuid`, must not set its
|
|
10
|
+
* own `default` (yours wins), and must not opt out with `auto: false`.
|
|
11
|
+
*
|
|
12
|
+
* "The primary key" is a field flagged `primary: true` (defineModel()
|
|
13
|
+
* flags it), or - for a bare descriptor that carries no flag at all - one
|
|
14
|
+
* named `id`. A field explicitly flagged `primary: false` never qualifies.
|
|
15
|
+
*/
|
|
16
|
+
export declare function isAutoId(fieldName: any, field: any): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Returns `field` with its automatic-id default baked in (or `field`
|
|
19
|
+
* unchanged if it doesn't qualify). Baking it into the descriptor once,
|
|
20
|
+
* at definition time, means every existing consumer of `field.default`
|
|
21
|
+
* (defaultFor(), the DTO generator's "required unless it has a
|
|
22
|
+
* default" rule, schema building) handles it with no special cases.
|
|
23
|
+
*
|
|
24
|
+
* - integer: AUTO_INCREMENT sentinel; the DB assigns it.
|
|
25
|
+
* - uuid: a factory calling crypto.randomUUID() per record.
|
|
26
|
+
*/
|
|
27
|
+
export declare function applyAutoIdDefault(fieldName: any, field: any): any;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Coerces a runtime value into a field's canonical JS representation
|
|
3
|
+
* (e.g. numeric string -> number, "true" -> boolean). Pure function:
|
|
4
|
+
* no DB, no mutation.
|
|
5
|
+
*
|
|
6
|
+
* null/undefined pass through unchanged - casting is only concerned
|
|
7
|
+
* with type coercion, not presence, which is validateValue()'s job.
|
|
8
|
+
* Deliberately does NOT call validateValue() internally: callers that
|
|
9
|
+
* want both should call validate then cast, so cast failures and
|
|
10
|
+
* validation failures stay distinguishable (CastError vs an error
|
|
11
|
+
* list) rather than being silently merged.
|
|
12
|
+
*
|
|
13
|
+
* @param {import('./define-field.js').FieldDescriptor} field
|
|
14
|
+
* @param {unknown} value
|
|
15
|
+
* @param {import('./registry.js').FieldTypeRegistry} [registry]
|
|
16
|
+
* @returns {unknown}
|
|
17
|
+
* @throws {import('./errors.js').CastError} if value cannot be coerced
|
|
18
|
+
*/
|
|
19
|
+
export declare function castValue(field: import('./define-field.js').FieldDescriptor, value: unknown, registry?: import('./registry.js').FieldTypeRegistry): unknown;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Puts a model's computed fields onto a record or entity as read-only
|
|
3
|
+
* getters.
|
|
4
|
+
*
|
|
5
|
+
* They are enumerable own properties, so `JSON.stringify(record)`,
|
|
6
|
+
* `{ ...record }` and `Object.keys()` include them like any field -
|
|
7
|
+
* but they are NOT in `model.fields`, which is what every persistence
|
|
8
|
+
* path (prepareWrite, toRow, dirty tracking, schema sync, migrations)
|
|
9
|
+
* iterates, so a computed value can never be written to the database
|
|
10
|
+
* by accident.
|
|
11
|
+
*
|
|
12
|
+
* Each getter calls `compute(target)` afresh: nothing is cached, so the
|
|
13
|
+
* value always reflects the target's current field values, including
|
|
14
|
+
* unsaved in-memory edits on an entity.
|
|
15
|
+
*
|
|
16
|
+
* @template {object} T
|
|
17
|
+
* @param {T} target - a record from fromRow(), or an entity
|
|
18
|
+
* @param {import('../models/define-model.js').ModelDescriptor} model
|
|
19
|
+
* @param {Iterable<string>} [only] - define just these (a record read with `select`); default: all
|
|
20
|
+
* @returns {T}
|
|
21
|
+
*/
|
|
22
|
+
export declare function defineComputedGetters<T extends object>(target: T, model: import('../models/define-model.js').ModelDescriptor, only?: Iterable<string>): T;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves the default value for a field. Supports both a static
|
|
3
|
+
* value (`default: "draft"`) and a factory function
|
|
4
|
+
* (`default: () => new Date()`), so defaults that must be computed
|
|
5
|
+
* per-record (timestamps, UUIDs) work without needing a special case
|
|
6
|
+
* at every call site.
|
|
7
|
+
*
|
|
8
|
+
* @param {import('./define-field.js').FieldDescriptor} field
|
|
9
|
+
* @returns {unknown} undefined if the field has no default configured
|
|
10
|
+
*/
|
|
11
|
+
export declare function defaultFor(field: import('./define-field.js').FieldDescriptor): unknown;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FieldTypeRegistry } from "./registry.js";
|
|
2
|
+
/**
|
|
3
|
+
* The registry used by defineField()/validateValue()/castValue() when
|
|
4
|
+
* no explicit registry is passed in. Plugins can register additional
|
|
5
|
+
* types onto this instance, or callers can construct their own
|
|
6
|
+
* `new FieldTypeRegistry()` for isolation (e.g. in tests).
|
|
7
|
+
*/
|
|
8
|
+
export declare function createDefaultFieldTypeRegistry(): FieldTypeRegistry;
|
|
9
|
+
export declare const defaultFieldTypeRegistry: FieldTypeRegistry;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* True if these raw field options declare a computed (virtual) field.
|
|
3
|
+
* `compute` is the one option that decides it.
|
|
4
|
+
*
|
|
5
|
+
* @param {unknown} options
|
|
6
|
+
*/
|
|
7
|
+
export declare function isComputedField(options: unknown): boolean;
|
|
8
|
+
export type ComputedFieldDescriptor = {
|
|
9
|
+
/**
|
|
10
|
+
* - the type of the value `compute` returns (used for casting search values and for DTOs)
|
|
11
|
+
*/
|
|
12
|
+
type: string;
|
|
13
|
+
compute: (record: Record<string, unknown>) => unknown;
|
|
14
|
+
/**
|
|
15
|
+
* - fields of the same model `compute` reads
|
|
16
|
+
*/
|
|
17
|
+
depends: readonly string[];
|
|
18
|
+
search: ((operator: string, value: unknown) => unknown[]) | undefined;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* @typedef {Object} ComputedFieldDescriptor
|
|
22
|
+
* @property {string} type - the type of the value `compute` returns (used for casting search values and for DTOs)
|
|
23
|
+
* @property {(record: Record<string, unknown>) => unknown} compute
|
|
24
|
+
* @property {readonly string[]} depends - fields of the same model `compute` reads
|
|
25
|
+
* @property {((operator: string, value: unknown) => unknown[])|undefined} search
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* Defines a computed field: a value calculated from a record's other
|
|
29
|
+
* fields when it is read, never stored.
|
|
30
|
+
*
|
|
31
|
+
* fullName: {
|
|
32
|
+
* type: "char",
|
|
33
|
+
* depends: ["firstName", "lastName"],
|
|
34
|
+
* compute: (record) => `${record.firstName} ${record.lastName}`,
|
|
35
|
+
* // What makes it queryable - see compileComputedLeaf() in domain/compile.js:
|
|
36
|
+
* search: (operator, value) => [["firstName", operator, value]],
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* `compute` must be synchronous and a pure function of `record`. It
|
|
40
|
+
* runs on every access (there's no cache to go stale when a field is
|
|
41
|
+
* edited in memory), so it should be cheap.
|
|
42
|
+
*
|
|
43
|
+
* `search(operator, value)` is Odoo's compute-and-search pattern: given
|
|
44
|
+
* a comparison on the computed field, return a domain over fields that
|
|
45
|
+
* DO exist as columns (or other computed fields) that selects the same
|
|
46
|
+
* records. Without it the field can be read but not filtered on.
|
|
47
|
+
*
|
|
48
|
+
* Stored computed fields (`store: true`) are deliberately not supported:
|
|
49
|
+
* they'd need recomputation whenever a dependency changes - through
|
|
50
|
+
* update(), updateMany(), createMany(), and raw SQL - and a missed path
|
|
51
|
+
* silently leaves stale data in a column that queries trust.
|
|
52
|
+
*
|
|
53
|
+
* @param {Record<string, unknown>} options
|
|
54
|
+
* @param {import('./registry.js').FieldTypeRegistry} [registry]
|
|
55
|
+
* @returns {Readonly<ComputedFieldDescriptor>}
|
|
56
|
+
*/
|
|
57
|
+
export declare function defineComputedField(options: Record<string, unknown>, registry?: import('./registry.js').FieldTypeRegistry): Readonly<ComputedFieldDescriptor>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type FieldDescriptor = {
|
|
2
|
+
type: string;
|
|
3
|
+
required: boolean;
|
|
4
|
+
unique: boolean;
|
|
5
|
+
index: boolean;
|
|
6
|
+
default?: unknown | (() => unknown);
|
|
7
|
+
typeSpecificOptions?: unknown[];
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {Object} FieldDescriptor
|
|
11
|
+
* @property {string} type
|
|
12
|
+
* @property {boolean} required
|
|
13
|
+
* @property {boolean} unique
|
|
14
|
+
* @property {boolean} index
|
|
15
|
+
* @property {unknown|(() => unknown)} [default]
|
|
16
|
+
* @property {...unknown} [typeSpecificOptions]
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Defines a single field descriptor: a plain, serializable object
|
|
20
|
+
* describing a piece of data (its type, constraints, and default).
|
|
21
|
+
* This is the runtime "type system" the project uses instead of
|
|
22
|
+
* TypeScript - the object itself is both the schema and the contract
|
|
23
|
+
* that validateValue()/castValue() enforce at runtime.
|
|
24
|
+
*
|
|
25
|
+
* Fields are frozen shallowly so accidental in-place mutation (e.g.
|
|
26
|
+
* `field.required = true` after the fact) fails loudly instead of
|
|
27
|
+
* silently drifting from what was registered.
|
|
28
|
+
*
|
|
29
|
+
* @param {Partial<FieldDescriptor> & { type: string }} options
|
|
30
|
+
* @param {import('./registry.js').FieldTypeRegistry} [registry]
|
|
31
|
+
* @returns {Readonly<FieldDescriptor>}
|
|
32
|
+
*/
|
|
33
|
+
export declare function defineField(options: Partial<FieldDescriptor> & {
|
|
34
|
+
type: string;
|
|
35
|
+
}, registry?: import('./registry.js').FieldTypeRegistry): Readonly<FieldDescriptor>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown by castValue() when a value cannot be coerced into a field's
|
|
3
|
+
* declared type at all (e.g. "abc" cast as integer). This is distinct
|
|
4
|
+
* from validation failures (wrong length, out of range, etc.), which
|
|
5
|
+
* are reported as a list of error strings from validateValue() instead
|
|
6
|
+
* of thrown, since a caller (e.g. a future DTO layer) will often want
|
|
7
|
+
* to collect *all* validation problems for a record at once rather than
|
|
8
|
+
* stopping at the first one.
|
|
9
|
+
*/
|
|
10
|
+
export declare class CastError extends TypeError {
|
|
11
|
+
fieldType: string | undefined;
|
|
12
|
+
value: unknown;
|
|
13
|
+
/**
|
|
14
|
+
* @param {string} message
|
|
15
|
+
* @param {{ fieldType?: string, value?: unknown }} [details]
|
|
16
|
+
*/
|
|
17
|
+
constructor(message: string, details?: {
|
|
18
|
+
fieldType?: string;
|
|
19
|
+
value?: unknown;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Thrown by defineField() when a field descriptor itself is malformed
|
|
24
|
+
* (unknown type, missing required type-specific option, bad option
|
|
25
|
+
* value). This is a developer-time error about the schema, not a
|
|
26
|
+
* runtime error about a piece of data.
|
|
27
|
+
*/
|
|
28
|
+
export declare class InvalidFieldDefinitionError extends Error {
|
|
29
|
+
constructor(message: any);
|
|
30
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { defineField } from "./define-field.js";
|
|
2
|
+
export { validateValue } from "./validate-value.js";
|
|
3
|
+
export { castValue } from "./cast-value.js";
|
|
4
|
+
export { defaultFor } from "./default-for.js";
|
|
5
|
+
export { FieldTypeRegistry } from "./registry.js";
|
|
6
|
+
export { defaultFieldTypeRegistry, createDefaultFieldTypeRegistry, } from "./default-registry.js";
|
|
7
|
+
export { CastError, InvalidFieldDefinitionError } from "./errors.js";
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export type FieldTypeDefinition = {
|
|
2
|
+
name: string;
|
|
3
|
+
/**
|
|
4
|
+
* Validates the type-specific options on a field descriptor at
|
|
5
|
+
* definition time (e.g. enum requires a non-empty `values` array).
|
|
6
|
+
* Returns an array of error strings; empty array means valid.
|
|
7
|
+
*/
|
|
8
|
+
validateOptions?: (field: object) => string[];
|
|
9
|
+
/**
|
|
10
|
+
* Validates a runtime value against the field (assumes value is
|
|
11
|
+
* already non-null/undefined - required/null handling happens
|
|
12
|
+
* before this is called). Returns an array of error strings.
|
|
13
|
+
*/
|
|
14
|
+
validate: (field: object, value: unknown) => string[];
|
|
15
|
+
/**
|
|
16
|
+
* Coerces a runtime value into the field's canonical JS
|
|
17
|
+
* representation, or throws CastError if coercion isn't possible.
|
|
18
|
+
*/
|
|
19
|
+
cast: (field: object, value: unknown) => unknown;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* @typedef {Object} FieldTypeDefinition
|
|
23
|
+
* @property {string} name
|
|
24
|
+
* @property {(field: object) => string[]} [validateOptions]
|
|
25
|
+
* Validates the type-specific options on a field descriptor at
|
|
26
|
+
* definition time (e.g. enum requires a non-empty `values` array).
|
|
27
|
+
* Returns an array of error strings; empty array means valid.
|
|
28
|
+
* @property {(field: object, value: unknown) => string[]} validate
|
|
29
|
+
* Validates a runtime value against the field (assumes value is
|
|
30
|
+
* already non-null/undefined - required/null handling happens
|
|
31
|
+
* before this is called). Returns an array of error strings.
|
|
32
|
+
* @property {(field: object, value: unknown) => unknown} cast
|
|
33
|
+
* Coerces a runtime value into the field's canonical JS
|
|
34
|
+
* representation, or throws CastError if coercion isn't possible.
|
|
35
|
+
*/
|
|
36
|
+
/**
|
|
37
|
+
* Holds field type definitions (char, integer, etc.) keyed by name.
|
|
38
|
+
* Kept as a class rather than a bare object so multiple independent
|
|
39
|
+
* registries can exist later (useful for plugin isolation or testing)
|
|
40
|
+
* without global mutation.
|
|
41
|
+
*/
|
|
42
|
+
export declare class FieldTypeRegistry {
|
|
43
|
+
#private;
|
|
44
|
+
/**
|
|
45
|
+
* @param {FieldTypeDefinition} definition
|
|
46
|
+
*/
|
|
47
|
+
register(definition: FieldTypeDefinition): this;
|
|
48
|
+
/**
|
|
49
|
+
* @param {string} name
|
|
50
|
+
* @returns {FieldTypeDefinition}
|
|
51
|
+
*/
|
|
52
|
+
get(name: string): FieldTypeDefinition;
|
|
53
|
+
has(name: any): boolean;
|
|
54
|
+
names(): any[];
|
|
55
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A short, single-line string (maps to VARCHAR in most dialects).
|
|
3
|
+
* Options:
|
|
4
|
+
* - maxLength {number} default 255
|
|
5
|
+
* - minLength {number} default 0
|
|
6
|
+
*/
|
|
7
|
+
export declare const charType: {
|
|
8
|
+
name: string;
|
|
9
|
+
validateOptions(field: any): string[];
|
|
10
|
+
validate(field: any, value: any): string[];
|
|
11
|
+
cast(field: any, value: any): string;
|
|
12
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A calendar date (no time component, though the canonical runtime
|
|
3
|
+
* representation is still a JS Date - the schema-sync layer in a later
|
|
4
|
+
* phase is what maps this to a DATE vs DATETIME column per dialect).
|
|
5
|
+
*/
|
|
6
|
+
export declare const dateType: {
|
|
7
|
+
name: string;
|
|
8
|
+
validate(field: any, value: any): string[];
|
|
9
|
+
cast(field: any, value: any): any;
|
|
10
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A date + time value. Kept as a separate type from `date` (rather than
|
|
3
|
+
* an option on it) so the future schema-sync layer can map them to
|
|
4
|
+
* distinct column types (DATE vs TIMESTAMP) per dialect without
|
|
5
|
+
* inspecting field options.
|
|
6
|
+
*/
|
|
7
|
+
export declare const datetimeType: {
|
|
8
|
+
name: string;
|
|
9
|
+
validate(field: any, value: any): string[];
|
|
10
|
+
cast(field: any, value: any): any;
|
|
11
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A value constrained to a fixed set of options. Requires a `values`
|
|
3
|
+
* array on the field descriptor, e.g.:
|
|
4
|
+
* { type: "enum", values: ["draft", "sent", "paid"] }
|
|
5
|
+
*/
|
|
6
|
+
export declare const enumType: {
|
|
7
|
+
name: string;
|
|
8
|
+
validateOptions(field: any): string[];
|
|
9
|
+
validate(field: any, value: any): string[];
|
|
10
|
+
cast(field: any, value: any): string;
|
|
11
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A floating-point number. Options:
|
|
3
|
+
* - min {number}
|
|
4
|
+
* - max {number}
|
|
5
|
+
*/
|
|
6
|
+
export declare const floatType: {
|
|
7
|
+
name: string;
|
|
8
|
+
validateOptions(field: any): string[];
|
|
9
|
+
validate(field: any, value: any): string[];
|
|
10
|
+
cast(field: any, value: any): number;
|
|
11
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A whole number. Options:
|
|
3
|
+
* - min {number}
|
|
4
|
+
* - max {number}
|
|
5
|
+
*/
|
|
6
|
+
export declare const integerType: {
|
|
7
|
+
name: string;
|
|
8
|
+
validateOptions(field: any): string[];
|
|
9
|
+
validate(field: any, value: any): string[];
|
|
10
|
+
cast(field: any, value: any): number;
|
|
11
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Any JSON-serializable value (object, array, or primitive). Maps to a
|
|
3
|
+
* JSON/JSONB column depending on dialect in a later phase.
|
|
4
|
+
*/
|
|
5
|
+
export declare const jsonType: {
|
|
6
|
+
name: string;
|
|
7
|
+
validate(field: any, value: any): string[];
|
|
8
|
+
cast(field: any, value: any): any;
|
|
9
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Long-form text (maps to TEXT in most dialects). Unlike `char`, there
|
|
3
|
+
* is no default length cap - set `maxLength` explicitly if you want one.
|
|
4
|
+
*/
|
|
5
|
+
export declare const textType: {
|
|
6
|
+
name: string;
|
|
7
|
+
validateOptions(field: any): string[];
|
|
8
|
+
validate(field: any, value: any): string[];
|
|
9
|
+
cast(field: any, value: any): string;
|
|
10
|
+
};
|