@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,141 @@
|
|
|
1
|
+
export type DiffPlan = {
|
|
2
|
+
/**
|
|
3
|
+
* - model names that are entirely new
|
|
4
|
+
*/
|
|
5
|
+
createModels: string[];
|
|
6
|
+
/**
|
|
7
|
+
* - previously-active models no longer
|
|
8
|
+
* in the registry: { modelName, tableName, fields, columns } where
|
|
9
|
+
* `fields`/`columns` are reconstructed from the stored meta rows'
|
|
10
|
+
* `options` JSON, so a rollback can recreate the table's schema
|
|
11
|
+
* even though the model no longer exists in code.
|
|
12
|
+
*/
|
|
13
|
+
dropModels: Object[];
|
|
14
|
+
/**
|
|
15
|
+
* - { modelName, fromTable, toTable } -
|
|
16
|
+
* detected with certainty (not a heuristic): the model's `name` is
|
|
17
|
+
* its stable identity in `_meta_models`, so if that same name's
|
|
18
|
+
* resolved table name changed, it's unambiguously a rename.
|
|
19
|
+
*/
|
|
20
|
+
renameModels: Object[];
|
|
21
|
+
/**
|
|
22
|
+
* -
|
|
23
|
+
* advisory only: models present in the old snapshot but absent from
|
|
24
|
+
* the registry, alongside newly-registered models, when neither list
|
|
25
|
+
* is empty and no explicit `modelRenames` resolved them. There's no
|
|
26
|
+
* way to know if these are genuinely related without the caller
|
|
27
|
+
* telling us - this exists purely to prompt "did you mean to rename
|
|
28
|
+
* one of these?" rather than to guess.
|
|
29
|
+
*/
|
|
30
|
+
possibleModelRenames: {
|
|
31
|
+
removed: string[];
|
|
32
|
+
added: string[];
|
|
33
|
+
} | null;
|
|
34
|
+
/**
|
|
35
|
+
* - per model
|
|
36
|
+
*/
|
|
37
|
+
fieldChanges: Record<string, FieldChangePlan>;
|
|
38
|
+
};
|
|
39
|
+
export type FieldChangePlan = {
|
|
40
|
+
/**
|
|
41
|
+
* - new field names on an existing model
|
|
42
|
+
*/
|
|
43
|
+
addFields: string[];
|
|
44
|
+
/**
|
|
45
|
+
* - { fieldName, columnName, field } for
|
|
46
|
+
* fields removed from the model; `field` is reconstructed from the
|
|
47
|
+
* stored meta row's `options` JSON for rollback purposes.
|
|
48
|
+
*/
|
|
49
|
+
dropFields: Object[];
|
|
50
|
+
/**
|
|
51
|
+
* - { from, to } - resolved via the
|
|
52
|
+
* caller's explicit `fieldRenames` option. Fully reversible: a
|
|
53
|
+
* RENAME COLUMN loses no data.
|
|
54
|
+
*/
|
|
55
|
+
renameFields: Object[];
|
|
56
|
+
/**
|
|
57
|
+
* - { fieldName, fromColumn, toColumn } -
|
|
58
|
+
* detected with certainty: same field `name` (the stable identity
|
|
59
|
+
* key in `_meta_fields`), only its resolved column name changed
|
|
60
|
+
* (e.g. an explicit `column` option was added/changed). Not a
|
|
61
|
+
* heuristic guess, same reasoning as renameModels above.
|
|
62
|
+
*/
|
|
63
|
+
columnRenames: Object[];
|
|
64
|
+
/**
|
|
65
|
+
* - { fieldName, from, to } - flagged
|
|
66
|
+
* but never auto-migrated (see generateMigration).
|
|
67
|
+
*/
|
|
68
|
+
typeChanges: Object[];
|
|
69
|
+
/**
|
|
70
|
+
* -
|
|
71
|
+
* advisory only, same rationale as possibleModelRenames but at the
|
|
72
|
+
* field level (e.g. `customerId` renamed to `clientId` in code).
|
|
73
|
+
*/
|
|
74
|
+
possibleFieldRenames: {
|
|
75
|
+
removed: string[];
|
|
76
|
+
added: string[];
|
|
77
|
+
} | null;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* @typedef {Object} DiffPlan
|
|
81
|
+
* @property {string[]} createModels - model names that are entirely new
|
|
82
|
+
* @property {Object[]} dropModels - previously-active models no longer
|
|
83
|
+
* in the registry: { modelName, tableName, fields, columns } where
|
|
84
|
+
* `fields`/`columns` are reconstructed from the stored meta rows'
|
|
85
|
+
* `options` JSON, so a rollback can recreate the table's schema
|
|
86
|
+
* even though the model no longer exists in code.
|
|
87
|
+
* @property {Object[]} renameModels - { modelName, fromTable, toTable } -
|
|
88
|
+
* detected with certainty (not a heuristic): the model's `name` is
|
|
89
|
+
* its stable identity in `_meta_models`, so if that same name's
|
|
90
|
+
* resolved table name changed, it's unambiguously a rename.
|
|
91
|
+
* @property {{removed: string[], added: string[]}|null} possibleModelRenames -
|
|
92
|
+
* advisory only: models present in the old snapshot but absent from
|
|
93
|
+
* the registry, alongside newly-registered models, when neither list
|
|
94
|
+
* is empty and no explicit `modelRenames` resolved them. There's no
|
|
95
|
+
* way to know if these are genuinely related without the caller
|
|
96
|
+
* telling us - this exists purely to prompt "did you mean to rename
|
|
97
|
+
* one of these?" rather than to guess.
|
|
98
|
+
* @property {Record<string, FieldChangePlan>} fieldChanges - per model
|
|
99
|
+
*/
|
|
100
|
+
/**
|
|
101
|
+
* @typedef {Object} FieldChangePlan
|
|
102
|
+
* @property {string[]} addFields - new field names on an existing model
|
|
103
|
+
* @property {Object[]} dropFields - { fieldName, columnName, field } for
|
|
104
|
+
* fields removed from the model; `field` is reconstructed from the
|
|
105
|
+
* stored meta row's `options` JSON for rollback purposes.
|
|
106
|
+
* @property {Object[]} renameFields - { from, to } - resolved via the
|
|
107
|
+
* caller's explicit `fieldRenames` option. Fully reversible: a
|
|
108
|
+
* RENAME COLUMN loses no data.
|
|
109
|
+
* @property {Object[]} columnRenames - { fieldName, fromColumn, toColumn } -
|
|
110
|
+
* detected with certainty: same field `name` (the stable identity
|
|
111
|
+
* key in `_meta_fields`), only its resolved column name changed
|
|
112
|
+
* (e.g. an explicit `column` option was added/changed). Not a
|
|
113
|
+
* heuristic guess, same reasoning as renameModels above.
|
|
114
|
+
* @property {Object[]} typeChanges - { fieldName, from, to } - flagged
|
|
115
|
+
* but never auto-migrated (see generateMigration).
|
|
116
|
+
* @property {{removed: string[], added: string[]}|null} possibleFieldRenames -
|
|
117
|
+
* advisory only, same rationale as possibleModelRenames but at the
|
|
118
|
+
* field level (e.g. `customerId` renamed to `clientId` in code).
|
|
119
|
+
*/
|
|
120
|
+
/**
|
|
121
|
+
* Compares `modelRegistry`'s current state against what's recorded in
|
|
122
|
+
* `_meta_models`/`_meta_fields` (Phase 4) - NOT live schema
|
|
123
|
+
* introspection. This is precisely the reason Phase 4 exists: model
|
|
124
|
+
* and field *identity* (their `name`) is stable in the metadata even
|
|
125
|
+
* when their resolved table/column name changes, so a table or column
|
|
126
|
+
* rename is detected with certainty rather than guessed. A field or
|
|
127
|
+
* model being renamed *in code* (a genuinely new identity) can't be
|
|
128
|
+
* known for certain and is only ever surfaced as an advisory
|
|
129
|
+
* `possible...Renames` hint unless the caller explicitly resolves it.
|
|
130
|
+
*
|
|
131
|
+
* @param {import('knex').Knex} db
|
|
132
|
+
* @param {import('../models/registry.js').ModelRegistry} modelRegistry
|
|
133
|
+
* @param {Object} [options]
|
|
134
|
+
* @param {Record<string, string>} [options.modelRenames] - { oldModelName: newModelName }
|
|
135
|
+
* @param {Record<string, Record<string, string>>} [options.fieldRenames] - { modelName: { oldFieldName: newFieldName } }
|
|
136
|
+
* @returns {Promise<DiffPlan>}
|
|
137
|
+
*/
|
|
138
|
+
export declare function diffSchema(db: import('knex').Knex, modelRegistry: import('../models/registry.js').ModelRegistry, options?: {
|
|
139
|
+
modelRenames?: Record<string, string>;
|
|
140
|
+
fieldRenames?: Record<string, Record<string, string>>;
|
|
141
|
+
}): Promise<DiffPlan>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown by generateMigration() when a plan contains destructive
|
|
3
|
+
* operations (dropping a model, dropping a field) that weren't
|
|
4
|
+
* explicitly allowed via `{ allowDestructive: true }`, or unresolved
|
|
5
|
+
* field-type changes that Phase 6 refuses to auto-generate ALTER
|
|
6
|
+
* statements for. This is the central safety gate for this phase:
|
|
7
|
+
* nothing destructive is ever silently applied.
|
|
8
|
+
*/
|
|
9
|
+
export declare class MigrationSafetyError extends Error {
|
|
10
|
+
constructor(message: any);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Thrown by the migration runner when the migrations on disk and the
|
|
14
|
+
* migrations recorded as applied in the database can't be reconciled
|
|
15
|
+
* safely - pending migrations blocking a generate, an applied
|
|
16
|
+
* migration whose file is gone, a new file that sorts before one
|
|
17
|
+
* already applied. Every one of these means the recorded "before"
|
|
18
|
+
* state a migration was generated against may no longer be true, so
|
|
19
|
+
* the runner stops rather than guess.
|
|
20
|
+
*/
|
|
21
|
+
export declare class MigrationStateError extends Error {
|
|
22
|
+
constructor(message: any);
|
|
23
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The foreign keys a migration plan has to create, worked out once so the in-memory
|
|
3
|
+
* migration (generate.js) and the rendered file (render.js) agree:
|
|
4
|
+
*
|
|
5
|
+
* creation new models in dependency order, with the keys that go inside each CREATE TABLE
|
|
6
|
+
* (`inline`) and the few that can't (a cycle) added afterwards (`deferred`)
|
|
7
|
+
* added per existing model, the keys belonging to columns this migration adds
|
|
8
|
+
*
|
|
9
|
+
* Keys on columns that already exist are not part of a plan: the diff only sees fields.
|
|
10
|
+
* (`sync({ addMissingForeignKeys: true })` is the way to add those.)
|
|
11
|
+
*
|
|
12
|
+
* @param {import('./diff.js').DiffPlan} plan
|
|
13
|
+
* @param {import('../models/registry.js').ModelRegistry} modelRegistry
|
|
14
|
+
*/
|
|
15
|
+
export declare function planMigrationForeignKeys(plan: import('./diff.js').DiffPlan, modelRegistry: import('../models/registry.js').ModelRegistry): {
|
|
16
|
+
creation: {
|
|
17
|
+
order: object[];
|
|
18
|
+
inline: Map<string, object[]>;
|
|
19
|
+
deferred: Array<{
|
|
20
|
+
model: object;
|
|
21
|
+
fk: object;
|
|
22
|
+
}>;
|
|
23
|
+
};
|
|
24
|
+
added: {};
|
|
25
|
+
};
|
|
26
|
+
/** The source line for one foreign key, as written into a rendered migration file. */
|
|
27
|
+
export declare const renderForeignKey: (fk: any) => string;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export type Migration = {
|
|
2
|
+
up: (db: import('knex').Knex) => Promise<void>;
|
|
3
|
+
down: (db: import('knex').Knex) => Promise<void>;
|
|
4
|
+
/**
|
|
5
|
+
* - describePlan() output, for logging/review
|
|
6
|
+
*/
|
|
7
|
+
summary: string[];
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {Object} Migration
|
|
11
|
+
* @property {(db: import('knex').Knex) => Promise<void>} up
|
|
12
|
+
* @property {(db: import('knex').Knex) => Promise<void>} down
|
|
13
|
+
* @property {string[]} summary - describePlan() output, for logging/review
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Turns a `DiffPlan` (from diffSchema) into a reversible migration:
|
|
17
|
+
* plain `up`/`down` async functions built from Knex's schema builder -
|
|
18
|
+
* generated as code you can inspect before running, not a black box.
|
|
19
|
+
*
|
|
20
|
+
* The central safety rule for this phase: **destructive operations are
|
|
21
|
+
* never included unless explicitly allowed**. If the plan drops a
|
|
22
|
+
* model or a field, this throws `MigrationSafetyError` describing
|
|
23
|
+
* exactly what's destructive, unless the caller passes
|
|
24
|
+
* `{ allowDestructive: true }` - a conscious, one-time
|
|
25
|
+
* acknowledgment, never a silent default.
|
|
26
|
+
*
|
|
27
|
+
* Field type changes are never auto-migrated at all, regardless of
|
|
28
|
+
* `allowDestructive` - they differ too much across dialects and risk
|
|
29
|
+
* silent truncation, so this always refuses until the caller resolves
|
|
30
|
+
* them by hand.
|
|
31
|
+
*
|
|
32
|
+
* Reversibility is schema-level, not data-level: a dropped model or
|
|
33
|
+
* field's `down` recreates the table/column shape (using the
|
|
34
|
+
* descriptor Phase 4 stored in `_meta_fields.options`), but rows that
|
|
35
|
+
* existed before the drop are gone - no migration tool can undo data
|
|
36
|
+
* a DROP already committed. Renames (table, column, or field) are the
|
|
37
|
+
* one case that's fully reversible with no data loss at all, since a
|
|
38
|
+
* RENAME never touches the underlying values.
|
|
39
|
+
*
|
|
40
|
+
* @param {import('./diff.js').DiffPlan} plan
|
|
41
|
+
* @param {import('../models/registry.js').ModelRegistry} modelRegistry
|
|
42
|
+
* @param {Object} [options]
|
|
43
|
+
* @param {boolean} [options.allowDestructive]
|
|
44
|
+
* @param {import('../schema/column-builder-registry.js').ColumnBuilderRegistry} [options.columnBuilderRegistry]
|
|
45
|
+
* @returns {Migration}
|
|
46
|
+
* @throws {MigrationSafetyError}
|
|
47
|
+
*/
|
|
48
|
+
export declare function generateMigration(plan: import('./diff.js').DiffPlan, modelRegistry: import('../models/registry.js').ModelRegistry, options?: {
|
|
49
|
+
allowDestructive?: boolean;
|
|
50
|
+
columnBuilderRegistry?: import('../schema/column-builder-registry.js').ColumnBuilderRegistry;
|
|
51
|
+
}): Migration;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { diffSchema } from "./diff.js";
|
|
2
|
+
export { generateMigration } from "./generate.js";
|
|
3
|
+
export { describePlan } from "./describe.js";
|
|
4
|
+
export { applyMigration, rollbackMigration } from "./apply.js";
|
|
5
|
+
export { MigrationSafetyError, MigrationStateError } from "./errors.js";
|
|
6
|
+
export { renderMigrationSource } from "./render.js";
|
|
7
|
+
export { MIGRATIONS_TABLE, ensureMigrationsTable, listMigrationFiles, getMigrationStatus, migrateUp, migrateDown, generateMigrationFile, } from "./runner.js";
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders a `DiffPlan` as the source of a standalone ESM migration
|
|
3
|
+
* file - the on-disk counterpart of generateMigration()'s in-memory
|
|
4
|
+
* `up`/`down`, for a migration workflow that has to survive between
|
|
5
|
+
* runs.
|
|
6
|
+
*
|
|
7
|
+
* The file is meant to be read, reviewed and, if needed, edited:
|
|
8
|
+
*
|
|
9
|
+
* export const summary - the describePlan() lines, for logs and review
|
|
10
|
+
* export async function up(db, helpers)
|
|
11
|
+
* export async function down(db, helpers)
|
|
12
|
+
* export const meta - per touched model, the `_meta_*` snapshot
|
|
13
|
+
* before and after (see meta/snapshot.js);
|
|
14
|
+
* applied/rolled back with the schema change
|
|
15
|
+
*
|
|
16
|
+
* It is self-contained on purpose. Every field descriptor a change
|
|
17
|
+
* needs is embedded, so the file keeps meaning the same thing after
|
|
18
|
+
* the models it was generated from have changed again. It imports
|
|
19
|
+
* nothing: the column-building helpers arrive as `up`/`down`'s second
|
|
20
|
+
* argument, so the file runs wherever it is loaded from (the runner
|
|
21
|
+
* in ./runner.js supplies them) without needing `@kiri-orm/core` to
|
|
22
|
+
* resolve from the migrations directory.
|
|
23
|
+
*
|
|
24
|
+
* The safety rules are exactly generateMigration()'s, because this
|
|
25
|
+
* calls it first: a plan with destructive operations throws
|
|
26
|
+
* `MigrationSafetyError` unless `allowDestructive` is set, and field
|
|
27
|
+
* type changes always throw.
|
|
28
|
+
*
|
|
29
|
+
* @param {import('./diff.js').DiffPlan} plan
|
|
30
|
+
* @param {import('../models/registry.js').ModelRegistry} modelRegistry
|
|
31
|
+
* @param {Object} [options]
|
|
32
|
+
* @param {boolean} [options.allowDestructive]
|
|
33
|
+
* @param {Record<string, { before: object|null, after: object|null }>} [options.meta]
|
|
34
|
+
* @param {string} [options.header] - text for the leading comment
|
|
35
|
+
* @returns {string}
|
|
36
|
+
* @throws {import('./errors.js').MigrationSafetyError}
|
|
37
|
+
*/
|
|
38
|
+
export declare function renderMigrationSource(plan: import('./diff.js').DiffPlan, modelRegistry: import('../models/registry.js').ModelRegistry, options?: {
|
|
39
|
+
allowDestructive?: boolean;
|
|
40
|
+
meta?: Record<string, {
|
|
41
|
+
before: object | null;
|
|
42
|
+
after: object | null;
|
|
43
|
+
}>;
|
|
44
|
+
header?: string;
|
|
45
|
+
}): string;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/** Applied-migration history. `_meta_` is a reserved prefix, so no model's table can collide with it. */
|
|
2
|
+
export declare const MIGRATIONS_TABLE = "_meta_migrations";
|
|
3
|
+
/** @param {import('knex').Knex} db */
|
|
4
|
+
export declare function ensureMigrationsTable(db: import('knex').Knex): Promise<void>;
|
|
5
|
+
/**
|
|
6
|
+
* Migration files in `dir`, oldest first. Only names shaped
|
|
7
|
+
* `YYYYMMDDHHMMSS_slug.js` count, so a README or editor backup file
|
|
8
|
+
* in the directory is ignored rather than executed.
|
|
9
|
+
*
|
|
10
|
+
* @param {string} dir
|
|
11
|
+
* @returns {Promise<Array<{ name: string, file: string }>>}
|
|
12
|
+
*/
|
|
13
|
+
export declare function listMigrationFiles(dir: string): Promise<Array<{
|
|
14
|
+
name: string;
|
|
15
|
+
file: string;
|
|
16
|
+
}>>;
|
|
17
|
+
/**
|
|
18
|
+
* Reconciles files on disk with the history table.
|
|
19
|
+
*
|
|
20
|
+
* @param {import('knex').Knex} db
|
|
21
|
+
* @param {string} dir
|
|
22
|
+
* @returns {Promise<{ applied: Array<{name: string, applied_at: unknown}>, pending: Array<{name: string, file: string}> }>}
|
|
23
|
+
* @throws {MigrationStateError} history and disk disagree in a way that makes the chain untrustworthy
|
|
24
|
+
*/
|
|
25
|
+
export declare function getMigrationStatus(db: import('knex').Knex, dir: string): Promise<{
|
|
26
|
+
applied: Array<{
|
|
27
|
+
name: string;
|
|
28
|
+
applied_at: unknown;
|
|
29
|
+
}>;
|
|
30
|
+
pending: Array<{
|
|
31
|
+
name: string;
|
|
32
|
+
file: string;
|
|
33
|
+
}>;
|
|
34
|
+
}>;
|
|
35
|
+
/**
|
|
36
|
+
* Applies pending migrations, oldest first. Each one runs in its own
|
|
37
|
+
* transaction together with its metadata update and its history row,
|
|
38
|
+
* so a failure leaves the database, `_meta_*` and the history exactly
|
|
39
|
+
* as they were before that migration.
|
|
40
|
+
*
|
|
41
|
+
* @param {import('knex').Knex} db
|
|
42
|
+
* @param {Object} options
|
|
43
|
+
* @param {string} options.dir
|
|
44
|
+
* @param {number} [options.steps] - apply at most this many
|
|
45
|
+
* @param {import('../schema/column-builder-registry.js').ColumnBuilderRegistry} [options.columnBuilderRegistry]
|
|
46
|
+
* @param {import('../models/registry.js').ModelRegistry} [options.modelRegistry] - if given, pivot tables for its
|
|
47
|
+
* belongsToMany relations are ensured afterwards (migrations don't cover relations; see README)
|
|
48
|
+
* @param {(name: string, summary: string[]) => void} [options.onApply]
|
|
49
|
+
* @returns {Promise<{ applied: string[], pivotTablesCreated: string[] }>}
|
|
50
|
+
*/
|
|
51
|
+
export declare function migrateUp(db: import('knex').Knex, options: {
|
|
52
|
+
dir: string;
|
|
53
|
+
steps?: number;
|
|
54
|
+
columnBuilderRegistry?: import('../schema/column-builder-registry.js').ColumnBuilderRegistry;
|
|
55
|
+
modelRegistry?: import('../models/registry.js').ModelRegistry;
|
|
56
|
+
onApply?: (name: string, summary: string[]) => void;
|
|
57
|
+
}): Promise<{
|
|
58
|
+
applied: string[];
|
|
59
|
+
pivotTablesCreated: string[];
|
|
60
|
+
}>;
|
|
61
|
+
/**
|
|
62
|
+
* Rolls back the most recently applied migrations, newest first (one,
|
|
63
|
+
* unless `steps` or `all` says otherwise), each in its own transaction
|
|
64
|
+
* with its metadata restored to the "before" snapshot.
|
|
65
|
+
*
|
|
66
|
+
* @param {import('knex').Knex} db
|
|
67
|
+
* @param {Object} options
|
|
68
|
+
* @param {string} options.dir
|
|
69
|
+
* @param {number} [options.steps] - default 1
|
|
70
|
+
* @param {boolean} [options.all]
|
|
71
|
+
* @param {import('../schema/column-builder-registry.js').ColumnBuilderRegistry} [options.columnBuilderRegistry]
|
|
72
|
+
* @param {(name: string, summary: string[]) => void} [options.onRollback]
|
|
73
|
+
* @returns {Promise<{ rolledBack: string[] }>}
|
|
74
|
+
*/
|
|
75
|
+
export declare function migrateDown(db: import('knex').Knex, options: {
|
|
76
|
+
dir: string;
|
|
77
|
+
steps?: number;
|
|
78
|
+
all?: boolean;
|
|
79
|
+
columnBuilderRegistry?: import('../schema/column-builder-registry.js').ColumnBuilderRegistry;
|
|
80
|
+
onRollback?: (name: string, summary: string[]) => void;
|
|
81
|
+
}): Promise<{
|
|
82
|
+
rolledBack: string[];
|
|
83
|
+
}>;
|
|
84
|
+
/**
|
|
85
|
+
* Diffs the registry against the recorded schema state and, if there
|
|
86
|
+
* are changes, writes a migration file for them.
|
|
87
|
+
*
|
|
88
|
+
* Refuses while unapplied migrations exist: the diff is against the
|
|
89
|
+
* *applied* state, so generating on top of pending migrations would
|
|
90
|
+
* write the same changes into a second file.
|
|
91
|
+
*
|
|
92
|
+
* @param {import('knex').Knex} db
|
|
93
|
+
* @param {import('../models/registry.js').ModelRegistry} modelRegistry
|
|
94
|
+
* @param {Object} options
|
|
95
|
+
* @param {string} options.dir
|
|
96
|
+
* @param {string} [options.name] - slug for the file name
|
|
97
|
+
* @param {boolean} [options.dryRun] - compute and return the plan, write nothing
|
|
98
|
+
* @param {boolean} [options.allowDestructive]
|
|
99
|
+
* @param {Record<string, string>} [options.modelRenames]
|
|
100
|
+
* @param {Record<string, Record<string, string>>} [options.fieldRenames]
|
|
101
|
+
* @returns {Promise<{ plan: object, summary: string[], file: string|null, name: string|null }>}
|
|
102
|
+
* `file` is null when there is nothing to write (no changes, or a dry run)
|
|
103
|
+
* @throws {MigrationStateError} pending migrations exist
|
|
104
|
+
* @throws {MigrationSafetyError} destructive changes not allowed / type changes; carries `planLines`
|
|
105
|
+
*/
|
|
106
|
+
export declare function generateMigrationFile(db: import('knex').Knex, modelRegistry: import('../models/registry.js').ModelRegistry, options: {
|
|
107
|
+
dir: string;
|
|
108
|
+
name?: string;
|
|
109
|
+
dryRun?: boolean;
|
|
110
|
+
allowDestructive?: boolean;
|
|
111
|
+
modelRenames?: Record<string, string>;
|
|
112
|
+
fieldRenames?: Record<string, Record<string, string>>;
|
|
113
|
+
}): Promise<{
|
|
114
|
+
plan: object;
|
|
115
|
+
summary: string[];
|
|
116
|
+
file: string | null;
|
|
117
|
+
name: string | null;
|
|
118
|
+
}>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export type ModelDescriptor = {
|
|
2
|
+
name: string;
|
|
3
|
+
tableName: string;
|
|
4
|
+
fields: Record<string, import('../fields/define-field.js').FieldDescriptor>;
|
|
5
|
+
/**
|
|
6
|
+
* - name of the primary-key field: the model's `primaryKey` option, else the
|
|
7
|
+
* one field marked `primaryKey: true`, else a field named `id`, else null. That field's descriptor carries
|
|
8
|
+
* `primary: true`. Single-field keys only.
|
|
9
|
+
*/
|
|
10
|
+
primaryKey: string | null;
|
|
11
|
+
/**
|
|
12
|
+
* - fieldName -> resolved column name
|
|
13
|
+
*/
|
|
14
|
+
columns: Record<string, string>;
|
|
15
|
+
relations: Record<string, import('../relations/define-relation.js').RelationDescriptor>;
|
|
16
|
+
/**
|
|
17
|
+
* -
|
|
18
|
+
* virtual fields: calculated on read, never stored. Deliberately separate from `fields`, which is
|
|
19
|
+
* everything that has a column; everything that persists data iterates `fields` and so can't see these.
|
|
20
|
+
*/
|
|
21
|
+
computed: Record<string, import('../fields/define-computed-field.js').ComputedFieldDescriptor>;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Defines a model: a named collection of fields (from Phase 1), plus
|
|
25
|
+
* the resolved table/column names a naming strategy produces from
|
|
26
|
+
* them. This is still purely in-memory - no DB access happens here.
|
|
27
|
+
* Schema sync (Phase 3) and the DB-persisted metamodel (Phase 4) both
|
|
28
|
+
* build on top of the descriptor this returns.
|
|
29
|
+
*
|
|
30
|
+
* Field entries may be either raw field options (`{ type: "char" }`)
|
|
31
|
+
* or an already-built descriptor from defineField() - both are run
|
|
32
|
+
* through defineField() here so validation always happens at model
|
|
33
|
+
* definition time, not deferred to first use.
|
|
34
|
+
*
|
|
35
|
+
* @param {Object} options
|
|
36
|
+
* @param {string} options.name - model name, e.g. "invoice"
|
|
37
|
+
* @param {string} [options.table] - explicit table name; defaults to
|
|
38
|
+
* namingStrategy.modelToTable(name) if omitted
|
|
39
|
+
* @param {string} [options.primaryKey] - the primary-key field; defaults to the field marked
|
|
40
|
+
* `primaryKey: true`, else a field named `id`. An integer or uuid key is assigned automatically
|
|
41
|
+
* (`auto: false` on the field opts out).
|
|
42
|
+
* @param {Record<string, object>} options.fields
|
|
43
|
+
* @param {import('./naming.js').NamingStrategy} [namingStrategy]
|
|
44
|
+
* @param {import('../fields/registry.js').FieldTypeRegistry} [fieldTypeRegistry]
|
|
45
|
+
* @returns {Readonly<ModelDescriptor>}
|
|
46
|
+
*/
|
|
47
|
+
export declare function defineModel(options: {
|
|
48
|
+
name: string;
|
|
49
|
+
table?: string;
|
|
50
|
+
primaryKey?: string;
|
|
51
|
+
fields: Record<string, object>;
|
|
52
|
+
}, namingStrategy?: import('./naming.js').NamingStrategy, fieldTypeRegistry?: import('../fields/registry.js').FieldTypeRegistry): Readonly<ModelDescriptor>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown by defineModel() when a model descriptor itself is malformed:
|
|
3
|
+
* missing name, no fields, an invalid field definition, or a naming
|
|
4
|
+
* collision between two fields' resolved column names within the same
|
|
5
|
+
* model. A developer-time error about the schema, not runtime data.
|
|
6
|
+
*/
|
|
7
|
+
export declare class InvalidModelDefinitionError extends Error {
|
|
8
|
+
constructor(message: any);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Thrown by ModelRegistry#register() when a model name or resolved
|
|
12
|
+
* table name is already taken by a previously registered model.
|
|
13
|
+
*/
|
|
14
|
+
export declare class ModelConflictError extends Error {
|
|
15
|
+
constructor(message: any);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Thrown by ModelRegistry#getModel()/#getFields() when the requested
|
|
19
|
+
* model name isn't registered.
|
|
20
|
+
*/
|
|
21
|
+
export declare class UnknownModelError extends Error {
|
|
22
|
+
constructor(message: any);
|
|
23
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { defineModel } from "./define-model.js";
|
|
2
|
+
export { ModelRegistry } from "./registry.js";
|
|
3
|
+
export { toSnakeCase, toCamelCase, defaultNamingStrategy, identityNamingStrategy, } from "./naming.js";
|
|
4
|
+
export { InvalidModelDefinitionError, ModelConflictError, UnknownModelError, } from "./errors.js";
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks the shape of a raw definition and says what kind of layer it is.
|
|
3
|
+
*
|
|
4
|
+
* @param {unknown} def
|
|
5
|
+
* @returns {{ kind: "primary"|"abstract"|"extension", name: string }} `name` is the model the layer belongs to
|
|
6
|
+
*/
|
|
7
|
+
export declare function classifyDefinition(def: unknown): {
|
|
8
|
+
kind: "primary" | "abstract" | "extension";
|
|
9
|
+
name: string;
|
|
10
|
+
};
|
|
11
|
+
/** How a layer is named in error messages and in `origins`. */
|
|
12
|
+
export declare function layerLabel(layer: any): string;
|
|
13
|
+
/** Names that must already be defined before this layer can be applied. */
|
|
14
|
+
export declare function requirementsOf(layer: any): any[];
|
|
15
|
+
/**
|
|
16
|
+
* Folds a model's layers (mixins expanded, in order) into the single
|
|
17
|
+
* definition defineModel() will validate. Reads everything fresh from
|
|
18
|
+
* `specs` every time - nothing is cached - so a change to a mixin is
|
|
19
|
+
* always seen by the models that use it.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} name
|
|
22
|
+
* @param {Map<string, { abstract: boolean, layers: Array<{ kind: string, def: object, origin?: string|null }> }>} specs
|
|
23
|
+
* @param {string[]} [stack] - models currently being folded, to catch mixin cycles
|
|
24
|
+
* @returns {{ table: string|undefined, primaryKey: string|undefined, fields: Record<string, object>, relations: Record<string, object>, origins: Record<string, string> }}
|
|
25
|
+
*/
|
|
26
|
+
export declare function foldModel(name: string, specs: Map<string, {
|
|
27
|
+
abstract: boolean;
|
|
28
|
+
layers: Array<{
|
|
29
|
+
kind: string;
|
|
30
|
+
def: object;
|
|
31
|
+
origin?: string | null;
|
|
32
|
+
}>;
|
|
33
|
+
}>, stack?: string[]): {
|
|
34
|
+
table: string | undefined;
|
|
35
|
+
primaryKey: string | undefined;
|
|
36
|
+
fields: Record<string, object>;
|
|
37
|
+
relations: Record<string, object>;
|
|
38
|
+
origins: Record<string, string>;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Every mixin used by `name`'s layers, transitively (a mixin using
|
|
42
|
+
* another mixin counts too). Used to decide which mixins' hooks a
|
|
43
|
+
* concrete model should pick up when it's created - see
|
|
44
|
+
* `global/create-model.js`'s `createModel()`.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} name
|
|
47
|
+
* @param {Map<string, { abstract: boolean, layers: Array<{ kind: string, def: object }> }>} specs
|
|
48
|
+
* @returns {string[]} deepest mixins first - a mixin's own mixins resolve
|
|
49
|
+
* before the mixin that includes them, matching how `foldModel()` folds
|
|
50
|
+
* their fields (so the same ordering intuition applies to both)
|
|
51
|
+
*/
|
|
52
|
+
export declare function collectMixinNames(name: string, specs: Map<string, {
|
|
53
|
+
abstract: boolean;
|
|
54
|
+
layers: Array<{
|
|
55
|
+
kind: string;
|
|
56
|
+
def: object;
|
|
57
|
+
}>;
|
|
58
|
+
}>, seen?: Set<any>): string[];
|
|
59
|
+
/**
|
|
60
|
+
* Names of every model that mixes in `name`, directly or through
|
|
61
|
+
* another mixin, according to `specs`.
|
|
62
|
+
*
|
|
63
|
+
* @param {string} name
|
|
64
|
+
* @param {Map<string, { abstract: boolean, layers: Array<{ kind: string, def: object }> }>} specs
|
|
65
|
+
* @returns {string[]}
|
|
66
|
+
*/
|
|
67
|
+
export declare function dependentsOf(name: string, specs: Map<string, {
|
|
68
|
+
abstract: boolean;
|
|
69
|
+
layers: Array<{
|
|
70
|
+
kind: string;
|
|
71
|
+
def: object;
|
|
72
|
+
}>;
|
|
73
|
+
}>): string[];
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts camelCase/PascalCase to snake_case.
|
|
3
|
+
* "customerId" -> "customer_id", "OrderLine" -> "order_line",
|
|
4
|
+
* "userID" -> "user_id" (acronym-aware).
|
|
5
|
+
*
|
|
6
|
+
* @param {string} str
|
|
7
|
+
* @returns {string}
|
|
8
|
+
*/
|
|
9
|
+
export declare function toSnakeCase(str: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* Converts snake_case to camelCase.
|
|
12
|
+
* "customer_id" -> "customerId"
|
|
13
|
+
*
|
|
14
|
+
* @param {string} str
|
|
15
|
+
* @returns {string}
|
|
16
|
+
*/
|
|
17
|
+
export declare function toCamelCase(str: string): string;
|
|
18
|
+
export type NamingStrategy = {
|
|
19
|
+
modelToTable: (modelName: string) => string;
|
|
20
|
+
fieldToColumn: (fieldName: string) => string;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* @typedef {Object} NamingStrategy
|
|
24
|
+
* @property {(modelName: string) => string} modelToTable
|
|
25
|
+
* @property {(fieldName: string) => string} fieldToColumn
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* The default naming strategy: model and field names (assumed
|
|
29
|
+
* camelCase, JS-idiomatic) map to snake_case table/column names
|
|
30
|
+
* (SQL-idiomatic). Table names are NOT pluralized automatically -
|
|
31
|
+
* pluralization rules are inconsistent enough across languages/plurals
|
|
32
|
+
* that guessing wrong silently is worse than requiring an explicit
|
|
33
|
+
* `table` option when the default isn't wanted.
|
|
34
|
+
*
|
|
35
|
+
* @type {NamingStrategy}
|
|
36
|
+
*/
|
|
37
|
+
export declare const defaultNamingStrategy: NamingStrategy;
|
|
38
|
+
/**
|
|
39
|
+
* A pass-through strategy for callers who want model/field names used
|
|
40
|
+
* verbatim as table/column names, with no transformation.
|
|
41
|
+
*
|
|
42
|
+
* @type {NamingStrategy}
|
|
43
|
+
*/
|
|
44
|
+
export declare const identityNamingStrategy: NamingStrategy;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The name of a model's primary-key FIELD, or null if it has none.
|
|
3
|
+
*
|
|
4
|
+
* A model built by defineModel() carries `primaryKey` explicitly. The
|
|
5
|
+
* fallback (a field literally named `id`) only serves descriptors that
|
|
6
|
+
* predate declared primary keys, e.g. ones a test builds by hand.
|
|
7
|
+
*
|
|
8
|
+
* @param {{ primaryKey?: string|null, fields: Record<string, unknown> }} model
|
|
9
|
+
* @returns {string|null}
|
|
10
|
+
*/
|
|
11
|
+
export declare function primaryKeyOf(model: {
|
|
12
|
+
primaryKey?: string | null;
|
|
13
|
+
fields: Record<string, unknown>;
|
|
14
|
+
}): string | null;
|
|
15
|
+
/** The primary key's column name, or null. */
|
|
16
|
+
export declare function primaryKeyColumnOf(model: any): any;
|
|
17
|
+
/**
|
|
18
|
+
* Like primaryKeyOf(), but throws `ErrorClass` when the model has no
|
|
19
|
+
* primary key. `needing` finishes the sentence "... needs a primary key".
|
|
20
|
+
*
|
|
21
|
+
* @param {object} model
|
|
22
|
+
* @param {string} needing - what needs it, for the message
|
|
23
|
+
* @param {new (message: string) => Error} ErrorClass
|
|
24
|
+
* @returns {string}
|
|
25
|
+
*/
|
|
26
|
+
export declare function requirePrimaryKey(model: object, needing: string, ErrorClass?: new (message: string) => Error): string;
|
|
27
|
+
/**
|
|
28
|
+
* Decides which field is the primary key while a model is being defined.
|
|
29
|
+
*
|
|
30
|
+
* Precedence: the model's `primaryKey` option, else the one field marked
|
|
31
|
+
* `primaryKey: true`, else a field named `id`, else none.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} modelName
|
|
34
|
+
* @param {Record<string, object>} storedFields - the model's stored (non-computed) fields, raw
|
|
35
|
+
* @param {unknown} declared - the model-level `primaryKey` option
|
|
36
|
+
* @param {new (message: string) => Error} ErrorClass
|
|
37
|
+
* @returns {string|null}
|
|
38
|
+
*/
|
|
39
|
+
export declare function resolvePrimaryKey(modelName: string, storedFields: Record<string, object>, declared: unknown, ErrorClass: new (message: string) => Error): string | null;
|