@kiri-orm/core 0.1.0 → 0.1.1
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/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 +9 -3
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Marks a record that was read with a field selection. Non-enumerable and symbol-keyed, so it
|
|
3
|
+
* never shows up in `Object.keys()`, spread or JSON. Carries `{ computed }`, the computed fields
|
|
4
|
+
* that were safe to define on it.
|
|
5
|
+
*/
|
|
6
|
+
export declare const PARTIAL: unique symbol;
|
|
7
|
+
/**
|
|
8
|
+
* Converts a raw DB row (keyed by column name, with driver-native
|
|
9
|
+
* value shapes) into a record keyed by field name, with values run
|
|
10
|
+
* through castValue() so callers always get back the same shapes they
|
|
11
|
+
* put in (Date objects for date/datetime, real objects for json,
|
|
12
|
+
* booleans for boolean) regardless of how a given dialect's driver
|
|
13
|
+
* represents them on the wire.
|
|
14
|
+
*
|
|
15
|
+
* `json` columns need one extra step before casting: SQLite has no
|
|
16
|
+
* native JSON type, so Knex stores it as a text column and the driver
|
|
17
|
+
* hands back a JSON *string*, not the parsed value. Postgres, in
|
|
18
|
+
* contrast, generally hands back an already-parsed object for
|
|
19
|
+
* json/jsonb columns. castValue() alone can't tell these apart
|
|
20
|
+
* (a string is technically valid JSON too), so the string case is
|
|
21
|
+
* parsed here first, before the value ever reaches castValue().
|
|
22
|
+
*
|
|
23
|
+
* @param {import('../models/define-model.js').ModelDescriptor} model
|
|
24
|
+
* @param {Record<string, unknown>} row
|
|
25
|
+
* @param {import('../fields/registry.js').FieldTypeRegistry} [fieldTypeRegistry]
|
|
26
|
+
* @param {{ computed?: Set<string>|null }} [options] - `computed`: which computed fields to define
|
|
27
|
+
* (default: all). A row read with `select` only defines the ones whose dependencies it carries; the
|
|
28
|
+
* choice is remembered under PARTIAL so an entity built from the record can make the same one.
|
|
29
|
+
* @returns {Record<string, unknown>}
|
|
30
|
+
*/
|
|
31
|
+
export declare function fromRow(model: import('../models/define-model.js').ModelDescriptor, row: Record<string, unknown>, fieldTypeRegistry?: import('../fields/registry.js').FieldTypeRegistry, options?: {
|
|
32
|
+
computed?: Set<string> | null;
|
|
33
|
+
}): Record<string, unknown>;
|
|
34
|
+
/**
|
|
35
|
+
* Converts a record keyed by field name into a plain object keyed by
|
|
36
|
+
* column name, ready to hand to Knex's insert()/update(). Assumes
|
|
37
|
+
* values have already been validated/cast by the caller (see
|
|
38
|
+
* prepareWrite()) - this function only renames keys, it does not
|
|
39
|
+
* validate or coerce anything itself.
|
|
40
|
+
*
|
|
41
|
+
* @param {import('../models/define-model.js').ModelDescriptor} model
|
|
42
|
+
* @param {Record<string, unknown>} fieldValues
|
|
43
|
+
* @returns {Record<string, unknown>}
|
|
44
|
+
*/
|
|
45
|
+
export declare function toRow(model: import('../models/define-model.js').ModelDescriptor, fieldValues: Record<string, unknown>): Record<string, unknown>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Field selection for `find({ select })`: which columns to read, and what has to ride
|
|
3
|
+
* along so the result is still correct.
|
|
4
|
+
*
|
|
5
|
+
* Three things are kept apart on purpose:
|
|
6
|
+
*
|
|
7
|
+
* - what the caller ASKED for (`select`, validated by normalizeSelect());
|
|
8
|
+
* - what that IMPLIES - a computed field can't be read without the stored fields it
|
|
9
|
+
* `depends` on (transitively, through other computed fields), so those are loaded
|
|
10
|
+
* too and appear on the record like any other selected field (expandSelect());
|
|
11
|
+
* - what the ORM needs for ITS OWN bookkeeping - the join keys eager loading matches
|
|
12
|
+
* rows by. These are fetched too but are NOT part of the result unless asked for,
|
|
13
|
+
* so `select: ["title"]` with `with: ["author"]` doesn't leak `authorId`
|
|
14
|
+
* (planSelect().hidden, removed by stripHidden() once loading is done).
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Validates `select` and returns it as a de-duplicated array of field names, or `null`
|
|
18
|
+
* when there is no selection (meaning: every field, exactly as before `select` existed).
|
|
19
|
+
* Stored and computed fields are both accepted.
|
|
20
|
+
*
|
|
21
|
+
* @param {import('../models/define-model.js').ModelDescriptor} model
|
|
22
|
+
* @param {unknown} select
|
|
23
|
+
* @returns {string[]|null}
|
|
24
|
+
* @throws {ValidationError}
|
|
25
|
+
*/
|
|
26
|
+
export declare function normalizeSelect(model: import('../models/define-model.js').ModelDescriptor, select: unknown): string[] | null;
|
|
27
|
+
/**
|
|
28
|
+
* Splits a validated selection into the stored fields to read and the computed fields to
|
|
29
|
+
* define, following `depends` transitively: `fullName` depends on `firstName` and
|
|
30
|
+
* `initials`, `initials` depends on `firstName` and `lastName` -> stored { firstName,
|
|
31
|
+
* lastName }, computed { fullName, initials }.
|
|
32
|
+
*
|
|
33
|
+
* @param {import('../models/define-model.js').ModelDescriptor} model
|
|
34
|
+
* @param {string[]} names - output of normalizeSelect()
|
|
35
|
+
* @returns {{ fields: Set<string>, computed: Set<string> }}
|
|
36
|
+
*/
|
|
37
|
+
export declare function expandSelect(model: import('../models/define-model.js').ModelDescriptor, names: string[]): {
|
|
38
|
+
fields: Set<string>;
|
|
39
|
+
computed: Set<string>;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Turns a (possibly absent) selection plus the join keys some relation needs into what
|
|
43
|
+
* the query should read.
|
|
44
|
+
*
|
|
45
|
+
* - `columns`: column names to `select`, or `null` for "all columns" (no selection made;
|
|
46
|
+
* the SQL is then identical to what it was before `select` existed).
|
|
47
|
+
* - `computed`: the computed fields to define on each record, or `null` for "all". A
|
|
48
|
+
* computed field whose dependencies weren't loaded would compute from `undefined`, so
|
|
49
|
+
* with a selection only the requested ones (and what they need) are defined.
|
|
50
|
+
* - `hidden`: fields read only because `requiredKeys` needs them; strip them from the
|
|
51
|
+
* result with stripHidden() once whatever needed them is done.
|
|
52
|
+
*
|
|
53
|
+
* @param {import('../models/define-model.js').ModelDescriptor} model
|
|
54
|
+
* @param {string[]|null} names - output of normalizeSelect()
|
|
55
|
+
* @param {string[]} [requiredKeys] - stored fields that must be read regardless
|
|
56
|
+
* @returns {{ columns: string[]|null, computed: Set<string>|null, hidden: string[] }}
|
|
57
|
+
*/
|
|
58
|
+
export declare function planSelect(model: import('../models/define-model.js').ModelDescriptor, names: string[] | null, requiredKeys?: string[]): {
|
|
59
|
+
columns: string[] | null;
|
|
60
|
+
computed: Set<string> | null;
|
|
61
|
+
hidden: string[];
|
|
62
|
+
};
|
|
63
|
+
/** Removes fields that were read only for the ORM's own use (see planSelect().hidden). */
|
|
64
|
+
export declare function stripHidden(records: any, hidden: any): void;
|
|
65
|
+
/**
|
|
66
|
+
* For the tracked APIs (`client.models.x.find`, global `Model.find`): a document needs its
|
|
67
|
+
* primary key for `save()`, `delete()` and the identity map, so it is always read even if
|
|
68
|
+
* `select` left it out. Plain `Repository.find` returns exactly what was asked for.
|
|
69
|
+
*
|
|
70
|
+
* @param {import('../models/define-model.js').ModelDescriptor} model
|
|
71
|
+
* @param {object} [options] - find options
|
|
72
|
+
* @returns {object} `options`, with the primary key added to `select` when needed
|
|
73
|
+
*/
|
|
74
|
+
export declare function withPrimaryKey(model: import('../models/define-model.js').ModelDescriptor, options?: object): object;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** True for a Postgres-family Knex instance. */
|
|
2
|
+
export declare function isPostgres(db: any): boolean;
|
|
3
|
+
/**
|
|
4
|
+
* Keeps a Postgres `serial` primary key's sequence ahead of ids that were
|
|
5
|
+
* supplied explicitly.
|
|
6
|
+
*
|
|
7
|
+
* Inserting `{ id: 5 }` into a serial column never touches its sequence, so
|
|
8
|
+
* a later auto-assigned id walks up to 5 and fails with a duplicate key
|
|
9
|
+
* (or, worse, in the SAME statement: `[{ id: 1 }, {}]` gives the second row
|
|
10
|
+
* nextval() = 1). SQLite has no such problem - it picks max(rowid) + 1.
|
|
11
|
+
*
|
|
12
|
+
* Called BEFORE inserting a batch that contains explicit ids. It moves the
|
|
13
|
+
* sequence to the largest of: its own next value, the largest explicit id in
|
|
14
|
+
* the batch, and the largest id already in the table (which also repairs a
|
|
15
|
+
* sequence an earlier explicit insert left behind). It never moves the
|
|
16
|
+
* sequence backwards; the cost is at most one skipped number.
|
|
17
|
+
*
|
|
18
|
+
* A no-op for any other dialect, for uuid / non-auto keys, and when the batch
|
|
19
|
+
* has no explicit key.
|
|
20
|
+
*
|
|
21
|
+
* @param {import('knex').Knex} db - connection or transaction
|
|
22
|
+
* @param {object} model
|
|
23
|
+
* @param {Record<string, unknown>[]} valuesList - validated rows about to be inserted
|
|
24
|
+
* @returns {Promise<boolean>} whether the sequence was touched
|
|
25
|
+
*/
|
|
26
|
+
export declare function advanceSequence(db: import('knex').Knex, model: object, valuesList: Record<string, unknown>[]): Promise<boolean>;
|
|
27
|
+
/**
|
|
28
|
+
* Repairs one model's auto-increment sequence so the next generated id is past every
|
|
29
|
+
* id in the table.
|
|
30
|
+
*
|
|
31
|
+
* advanceSequence() covers ids written through this ORM's own inserts. Anything that
|
|
32
|
+
* puts ids in behind its back - a raw `client.db` / `getConnection()` insert, a seed
|
|
33
|
+
* script, a dump restore, a manual renumbering - leaves the sequence behind, and the
|
|
34
|
+
* next auto-assigned id then collides. This is the remedy for those.
|
|
35
|
+
*
|
|
36
|
+
* Safe to run any time and any number of times. It never moves a sequence backwards
|
|
37
|
+
* (so ids of deleted rows aren't reused), and costs at most one skipped number.
|
|
38
|
+
* A no-op on SQLite, and for uuid / non-auto / missing keys.
|
|
39
|
+
*
|
|
40
|
+
* @param {import('knex').Knex} db - connection or transaction
|
|
41
|
+
* @param {object} model
|
|
42
|
+
* @returns {Promise<boolean>} whether a sequence was found and moved
|
|
43
|
+
*/
|
|
44
|
+
export declare function syncSequence(db: import('knex').Knex, model: object): Promise<boolean>;
|
|
45
|
+
/**
|
|
46
|
+
* syncSequence() for every model in a registry. (Named apart from the global API's `syncSequences()`, which wraps it: both are re-exported from the package root.)
|
|
47
|
+
*
|
|
48
|
+
* @param {import('knex').Knex} db - connection or transaction
|
|
49
|
+
* @param {import('../models/registry.js').ModelRegistry} registry
|
|
50
|
+
* @returns {Promise<Array<{ model: string, table: string, synced: boolean }>>} one entry per model
|
|
51
|
+
*/
|
|
52
|
+
export declare function syncRegistrySequences(db: import('knex').Knex, registry: import('../models/registry.js').ModelRegistry): Promise<Array<{
|
|
53
|
+
model: string;
|
|
54
|
+
table: string;
|
|
55
|
+
synced: boolean;
|
|
56
|
+
}>>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs `fn` inside a Knex transaction, passing the transaction object
|
|
3
|
+
* through so it can be handed to Repository methods' `{ trx }` option
|
|
4
|
+
* (e.g. `repo.create(data, { trx })`) to include them in the same
|
|
5
|
+
* atomic unit. This is a thin pass-through to Knex's own
|
|
6
|
+
* `db.transaction()` - it exists mainly so the transaction API is
|
|
7
|
+
* discoverable from `@kiri-orm/core`'s exports without needing to know
|
|
8
|
+
* Knex's API is what's underneath.
|
|
9
|
+
*
|
|
10
|
+
* If `fn` throws (or returns a rejected promise), Knex rolls the
|
|
11
|
+
* transaction back automatically and the rejection propagates to the
|
|
12
|
+
* caller here.
|
|
13
|
+
*
|
|
14
|
+
* @template T
|
|
15
|
+
* @param {import('knex').Knex} db
|
|
16
|
+
* @param {(trx: import('knex').Knex.Transaction) => Promise<T>} fn
|
|
17
|
+
* @returns {Promise<T>}
|
|
18
|
+
*/
|
|
19
|
+
export declare function runInTransaction<T>(db: import('knex').Knex, fn: (trx: import('knex').Knex.Transaction) => Promise<T>): Promise<T>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DEFAULT_THRESHOLD, NPlusOneError, QueryMonitor, describeFindings, normalizeSql, scanForNPlusOne, statementOf, watchForNPlusOne, } from "./n-plus-one.js";
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An N+1 detector: notices when the same SQL statement is sent over and over, which is what
|
|
3
|
+
* "load the parent, then query for each child" looks like from the database's side - one query
|
|
4
|
+
* for the list, then one more per row.
|
|
5
|
+
*
|
|
6
|
+
* It watches the connection's `query` events (which also carry the queries of any transaction on
|
|
7
|
+
* it), reduces every statement to its SHAPE, and counts repeats of a shape. Two statements have the
|
|
8
|
+
* same shape when only their values differ:
|
|
9
|
+
*
|
|
10
|
+
* select * from post where id = $1 -- and the same with = ?, or any id
|
|
11
|
+
* select * from comment where post_id in (?, ?, ?) -- and the same with a list of any length
|
|
12
|
+
*
|
|
13
|
+
* So looping `findOne({ id })` over a hundred ids is one shape seen 100 times, while one batched
|
|
14
|
+
* `find({ with })` is a few different shapes seen once each.
|
|
15
|
+
*
|
|
16
|
+
* It is a heuristic and says so: a legitimate loop (a batch job paging through rows) repeats a
|
|
17
|
+
* statement too. That is why it reports and, unless you ask it to, doesn't throw - and why the
|
|
18
|
+
* threshold and the time window are yours to set. Only SELECTs are counted by default: a loop of
|
|
19
|
+
* inserts is a different, usually deliberate, pattern.
|
|
20
|
+
*
|
|
21
|
+
* Meant for development and tests, not production: it adds a cheap string pass per statement.
|
|
22
|
+
*
|
|
23
|
+
* It reports WHAT repeated (the statement, and how many times), not WHERE it was called from. That
|
|
24
|
+
* would need the caller's stack, and by the time knex emits its query event that is gone: query builders
|
|
25
|
+
* are non-native thenables, and V8's async stack traces don't continue through them. The statement itself
|
|
26
|
+
* - it names the table and the column - is what to search your code for.
|
|
27
|
+
*
|
|
28
|
+
* @typedef {{ shape: string, sql: string, statement: string, count: number }} Finding
|
|
29
|
+
*/
|
|
30
|
+
export type Finding = {
|
|
31
|
+
shape: string;
|
|
32
|
+
sql: string;
|
|
33
|
+
statement: string;
|
|
34
|
+
count: number;
|
|
35
|
+
};
|
|
36
|
+
export declare const DEFAULT_THRESHOLD = 5;
|
|
37
|
+
/**
|
|
38
|
+
* Reduces a statement to its shape: the same text with the values and the list lengths taken out.
|
|
39
|
+
* Postgres `$1` and SQLite `?` placeholders both become `?`; `in (?, ?, ?)` becomes `in (?)`; a
|
|
40
|
+
* multi-row `values (?, ?), (?, ?)` becomes one row.
|
|
41
|
+
*
|
|
42
|
+
* @param {string} sql
|
|
43
|
+
* @returns {string}
|
|
44
|
+
*/
|
|
45
|
+
export declare function normalizeSql(sql: string): string;
|
|
46
|
+
/** The kind of statement: `select`, `insert`, `update`, `delete`, or a bookkeeping word. A CTE counts as a select. */
|
|
47
|
+
export declare function statementOf(sql: any): string;
|
|
48
|
+
export declare class NPlusOneError extends Error {
|
|
49
|
+
findings: Finding[];
|
|
50
|
+
/** @param {Finding[]} findings */
|
|
51
|
+
constructor(findings: Finding[]);
|
|
52
|
+
}
|
|
53
|
+
/** A readable report: each repeated statement, how often, and what to do about it. */
|
|
54
|
+
export declare function describeFindings(findings: any): string;
|
|
55
|
+
export declare class QueryMonitor {
|
|
56
|
+
#private;
|
|
57
|
+
/**
|
|
58
|
+
* @param {object} [options]
|
|
59
|
+
* @param {number} [options.threshold] - repeats of one shape that count as N+1 (default 5)
|
|
60
|
+
* @param {number} [options.windowMs] - a shape's count starts over after this long without it; `Infinity`
|
|
61
|
+
* (the default) counts everything seen. A live watcher usually wants a few hundred ms.
|
|
62
|
+
* @param {string[]} [options.statements] - which statements to count (default `["select"]`)
|
|
63
|
+
* @param {RegExp[]} [options.ignore] - statements to skip, matched against the SQL; added to the ORM's own
|
|
64
|
+
* (sequence maintenance), not replacing them
|
|
65
|
+
* @param {(finding: Finding) => void} [options.onDetect] - called once when a shape first reaches the threshold
|
|
66
|
+
* @param {() => number} [options.now] - clock, for tests
|
|
67
|
+
*/
|
|
68
|
+
constructor({ threshold, windowMs, statements, ignore, onDetect, now }?: {
|
|
69
|
+
threshold?: number;
|
|
70
|
+
windowMs?: number;
|
|
71
|
+
statements?: string[];
|
|
72
|
+
ignore?: RegExp[];
|
|
73
|
+
onDetect?: (finding: Finding) => void;
|
|
74
|
+
now?: () => number;
|
|
75
|
+
});
|
|
76
|
+
/** Starts listening on a connection (or transaction). One monitor watches one connection at a time. */
|
|
77
|
+
attach(db: any): this;
|
|
78
|
+
/** Stops listening. Safe to call twice. The findings so far are kept. */
|
|
79
|
+
detach(): this;
|
|
80
|
+
/** Forgets everything counted so far. */
|
|
81
|
+
reset(): this;
|
|
82
|
+
/** The shapes that reached the threshold, most repeated first. @returns {Finding[]} */
|
|
83
|
+
get findings(): Finding[];
|
|
84
|
+
/** Throws NPlusOneError if anything was found. */
|
|
85
|
+
assertClean(): void;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Runs `fn` while watching `db`, then reports what repeated. Never throws for findings.
|
|
89
|
+
*
|
|
90
|
+
* @template T
|
|
91
|
+
* @param {import('knex').Knex} db
|
|
92
|
+
* @param {() => Promise<T>} fn
|
|
93
|
+
* @param {ConstructorParameters<typeof QueryMonitor>[0]} [options]
|
|
94
|
+
* @returns {Promise<{ result: T, findings: Finding[] }>}
|
|
95
|
+
*/
|
|
96
|
+
export declare function scanForNPlusOne<T>(db: import('knex').Knex, fn: () => Promise<T>, options?: ConstructorParameters<typeof QueryMonitor>[0]): Promise<{
|
|
97
|
+
result: T;
|
|
98
|
+
findings: Finding[];
|
|
99
|
+
}>;
|
|
100
|
+
/**
|
|
101
|
+
* A live watcher for development: warns (once per statement) on the console the moment one shape
|
|
102
|
+
* repeats `threshold` times within `windowMs`. Call `.detach()` to stop.
|
|
103
|
+
*
|
|
104
|
+
* @param {import('knex').Knex} db
|
|
105
|
+
* @param {ConstructorParameters<typeof QueryMonitor>[0]} [options] - `windowMs` defaults to 250, `onDetect` to console.warn
|
|
106
|
+
* @returns {QueryMonitor}
|
|
107
|
+
*/
|
|
108
|
+
export declare function watchForNPlusOne(db: import('knex').Knex, options?: ConstructorParameters<typeof QueryMonitor>[0]): QueryMonitor;
|
|
@@ -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;
|