@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,12 @@
|
|
|
1
|
+
export { connect, getConnection, disconnect } from "./connect.js";
|
|
2
|
+
export { createModel, extendModel, defineMixin } from "./create-model.js";
|
|
3
|
+
export { loadModels } from "./load-models.js";
|
|
4
|
+
export { syncModels } from "./sync-models.js";
|
|
5
|
+
export { syncSequences } from "./sequences.js";
|
|
6
|
+
export { createReport } from "./create-report.js";
|
|
7
|
+
export { detectNPlusOne, assertNoNPlusOne, watchNPlusOne } from "./n-plus-one.js";
|
|
8
|
+
export { transaction } from "./transaction.js";
|
|
9
|
+
export { withIdentityMap } from "./identity-scope.js";
|
|
10
|
+
export { diffModels, migrateModels, getDefaultModelRegistry } from "./migrate.js";
|
|
11
|
+
export { generateModelDTOs, generateAllModelDTOs, generateModelJSDoc } from "./dto.js";
|
|
12
|
+
export { __resetGlobalStateForTests } from "./state.js";
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Imports every file matching `patterns`, for the side effect of each file's
|
|
3
|
+
* `createModel()` / `extendModel()` / `defineMixin()` calls registering into the
|
|
4
|
+
* default registry. This replaces the hand-written list of imports an app
|
|
5
|
+
* otherwise keeps in its database setup file: a new model is a new file, and
|
|
6
|
+
* nothing central has to be edited to know about it.
|
|
7
|
+
*
|
|
8
|
+
* await loadModels("./src/**\/*.model.js");
|
|
9
|
+
* await loadModels(["./modules/*\/models/*.js", "!**\/*.test.js"]);
|
|
10
|
+
*
|
|
11
|
+
* Order doesn't matter - the registry already lets a model wait for a mixin, a
|
|
12
|
+
* relation target or a base model that's defined later - so files are simply
|
|
13
|
+
* imported in sorted order. Importing is idempotent: Node loads a file once, so
|
|
14
|
+
* calling this again (or importing a model file by hand as well) never defines a
|
|
15
|
+
* model twice.
|
|
16
|
+
*
|
|
17
|
+
* Relative patterns resolve against `cwd`, which defaults to the process's
|
|
18
|
+
* working directory; pass `{ cwd: import.meta.dirname }` to anchor them to the
|
|
19
|
+
* calling file instead. Patterns use forward slashes (see glob.js for the syntax).
|
|
20
|
+
*
|
|
21
|
+
* Throws if nothing matches (almost always a wrong path or pattern; pass
|
|
22
|
+
* `{ allowEmpty: true }` to accept that) and names the file if one fails to load.
|
|
23
|
+
*
|
|
24
|
+
* @param {string|string[]} patterns
|
|
25
|
+
* @param {{ cwd?: string, allowEmpty?: boolean }} [options]
|
|
26
|
+
* @returns {Promise<string[]>} the files that were loaded (absolute paths)
|
|
27
|
+
*/
|
|
28
|
+
export declare function loadModels(patterns: string | string[], options?: {
|
|
29
|
+
cwd?: string;
|
|
30
|
+
allowEmpty?: boolean;
|
|
31
|
+
}): Promise<string[]>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Diffs the models currently registered via `createModel()` against
|
|
3
|
+
* what's recorded in `_meta_fields` (same as `diffSchema()` elsewhere
|
|
4
|
+
* in this ORM), using the default connection/registry - the piece
|
|
5
|
+
* that was missing entirely: there was no way to reach the global
|
|
6
|
+
* API's internal registry to run a migration against it at all,
|
|
7
|
+
* meaning removing a field from a schema had no path to actually
|
|
8
|
+
* dropping the column, ever, through this API.
|
|
9
|
+
*
|
|
10
|
+
* @param {Object} [options]
|
|
11
|
+
* @param {Record<string, string>} [options.modelRenames]
|
|
12
|
+
* @param {Record<string, Record<string, string>>} [options.fieldRenames]
|
|
13
|
+
* @returns {Promise<import('../migrations/diff.js').DiffPlan>}
|
|
14
|
+
*/
|
|
15
|
+
export declare function diffModels(options?: {
|
|
16
|
+
modelRenames?: Record<string, string>;
|
|
17
|
+
fieldRenames?: Record<string, Record<string, string>>;
|
|
18
|
+
}): Promise<import('../migrations/diff.js').DiffPlan>;
|
|
19
|
+
/**
|
|
20
|
+
* Diffs, then generates and applies a migration for every change
|
|
21
|
+
* between the currently-registered models and what's actually in the
|
|
22
|
+
* database - the one-call path for "I removed/renamed/added a field,
|
|
23
|
+
* make the database match."
|
|
24
|
+
*
|
|
25
|
+
* Same safety rule as `generateMigration()` everywhere else in this
|
|
26
|
+
* ORM: destructive changes (a dropped model or field) are refused
|
|
27
|
+
* unless `{ allowDestructive: true }` is passed explicitly. Field
|
|
28
|
+
* type changes are always refused, with no override - resolve those
|
|
29
|
+
* by hand (see `generateMigration()`'s docs for why).
|
|
30
|
+
*
|
|
31
|
+
* After applying, this also re-syncs (Phase 3/4's `sync()`) so
|
|
32
|
+
* `_meta_fields` reflects the new reality - a later call to
|
|
33
|
+
* `migrateModels()`/`diffModels()` will correctly report no changes
|
|
34
|
+
* rather than re-diffing against stale metadata.
|
|
35
|
+
*
|
|
36
|
+
* Pass `{ dryRun: true }` to get the plan and its human-readable
|
|
37
|
+
* `describePlan()` summary back WITHOUT applying anything - useful to
|
|
38
|
+
* review what would happen first.
|
|
39
|
+
*
|
|
40
|
+
* @param {Object} [options]
|
|
41
|
+
* @param {boolean} [options.allowDestructive]
|
|
42
|
+
* @param {boolean} [options.dryRun]
|
|
43
|
+
* @param {Record<string, string>} [options.modelRenames]
|
|
44
|
+
* @param {Record<string, Record<string, string>>} [options.fieldRenames]
|
|
45
|
+
* @returns {Promise<{ plan: import('../migrations/diff.js').DiffPlan, summary: string[], applied: boolean }>}
|
|
46
|
+
*/
|
|
47
|
+
export declare function migrateModels(options?: {
|
|
48
|
+
allowDestructive?: boolean;
|
|
49
|
+
dryRun?: boolean;
|
|
50
|
+
modelRenames?: Record<string, string>;
|
|
51
|
+
fieldRenames?: Record<string, Record<string, string>>;
|
|
52
|
+
}): Promise<{
|
|
53
|
+
plan: import('../migrations/diff.js').DiffPlan;
|
|
54
|
+
summary: string[];
|
|
55
|
+
applied: boolean;
|
|
56
|
+
}>;
|
|
57
|
+
/**
|
|
58
|
+
* Escape hatch for anyone who wants to call the lower-level, explicit
|
|
59
|
+
* migration functions (`generateMigration`, `applyMigration`,
|
|
60
|
+
* `rollbackMigration`, `describePlan` - all already available from
|
|
61
|
+
* `@kiri-orm/core` directly, not re-exported again here to avoid a
|
|
62
|
+
* duplicate-export collision) with more control than
|
|
63
|
+
* `migrateModels()` gives - e.g. to inspect and modify a migration
|
|
64
|
+
* before applying it, or to roll one back.
|
|
65
|
+
*
|
|
66
|
+
* @returns {import('../models/registry.js').ModelRegistry}
|
|
67
|
+
*/
|
|
68
|
+
export declare function getDefaultModelRegistry(): import('../models/registry.js').ModelRegistry;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs `fn` while watching the global connection, and returns what repeated: `{ result, findings }`.
|
|
3
|
+
* Each finding is `{ shape, sql, statement, count }` for a statement that ran `threshold`
|
|
4
|
+
* (default 5) or more times. Never throws for findings - see assertNoNPlusOne() for that.
|
|
5
|
+
*
|
|
6
|
+
* ```js
|
|
7
|
+
* const { findings } = await detectNPlusOne(async () => {
|
|
8
|
+
* for (const post of await Post.find()) await Post.load(post, "author"); // one query per post
|
|
9
|
+
* });
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* A heuristic for development and tests: a legitimate loop repeats a statement too. Options:
|
|
13
|
+
* `threshold`, `windowMs`, `statements` (default `["select"]`).
|
|
14
|
+
*
|
|
15
|
+
* @template T
|
|
16
|
+
* @param {() => Promise<T>} fn
|
|
17
|
+
* @param {object} [options]
|
|
18
|
+
* @returns {Promise<{ result: T, findings: import('../diagnostics/n-plus-one.js').Finding[] }>}
|
|
19
|
+
*/
|
|
20
|
+
export declare function detectNPlusOne<T>(fn: () => Promise<T>, options?: object): Promise<{
|
|
21
|
+
result: T;
|
|
22
|
+
findings: import('../diagnostics/n-plus-one.js').Finding[];
|
|
23
|
+
}>;
|
|
24
|
+
/**
|
|
25
|
+
* Like detectNPlusOne(), but throws an NPlusOneError (with `.findings`) if anything repeated, and
|
|
26
|
+
* otherwise returns what `fn` returned. For tests that guard a code path against regressing into N+1.
|
|
27
|
+
*
|
|
28
|
+
* @template T
|
|
29
|
+
* @param {() => Promise<T>} fn
|
|
30
|
+
* @param {object} [options]
|
|
31
|
+
* @returns {Promise<T>}
|
|
32
|
+
*/
|
|
33
|
+
export declare function assertNoNPlusOne<T>(fn: () => Promise<T>, options?: object): Promise<T>;
|
|
34
|
+
/**
|
|
35
|
+
* A live watcher for development: warns on the console, once per statement, as soon as one repeats
|
|
36
|
+
* `threshold` times within `windowMs` (default 250ms). Returns the monitor; call `.detach()` to stop.
|
|
37
|
+
* Pass `onDetect(finding)` to do something other than console.warn.
|
|
38
|
+
*
|
|
39
|
+
* @param {object} [options]
|
|
40
|
+
* @returns {import('../diagnostics/n-plus-one.js').QueryMonitor}
|
|
41
|
+
*/
|
|
42
|
+
export declare function watchNPlusOne(options?: object): import('../diagnostics/n-plus-one.js').QueryMonitor;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Moves every auto-increment model's Postgres sequence past the ids already in its
|
|
3
|
+
* table. Run it after anything that inserts ids outside this ORM - a raw insert, a seed
|
|
4
|
+
* script, a restored dump - so the next auto-assigned id doesn't collide.
|
|
5
|
+
*
|
|
6
|
+
* Idempotent and never moves a sequence backwards. On SQLite it does nothing (a
|
|
7
|
+
* rowid alias continues from the highest id by itself). Pass `{ trx }` to run it inside
|
|
8
|
+
* a transaction.
|
|
9
|
+
*
|
|
10
|
+
* @param {{ trx?: import('knex').Knex.Transaction }} [options]
|
|
11
|
+
* @returns {Promise<Array<{ model: string, table: string, synced: boolean }>>}
|
|
12
|
+
*/
|
|
13
|
+
export declare function syncSequences(options?: {
|
|
14
|
+
trx?: import('knex').Knex.Transaction;
|
|
15
|
+
}): Promise<Array<{
|
|
16
|
+
model: string;
|
|
17
|
+
table: string;
|
|
18
|
+
synced: boolean;
|
|
19
|
+
}>>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ModelRegistry } from "../models/registry.js";
|
|
2
|
+
import { HookRegistry } from "../uow/hooks.js";
|
|
3
|
+
/**
|
|
4
|
+
* The state backing the Mongoose-style global API (`connect()`,
|
|
5
|
+
* `createModel()`, `syncModels()`). Exactly one of each, module-level,
|
|
6
|
+
* shared by every file that imports from this package - the same
|
|
7
|
+
* mechanism that makes `import { User } from './models/user.js'`
|
|
8
|
+
* usable from anywhere without threading a `client`/`db`/`registry`
|
|
9
|
+
* through every call site: every `createModel()` call, in any file,
|
|
10
|
+
* registers into THIS registry and reads from THIS connection,
|
|
11
|
+
* because it's the same module instance across the whole app (Node's
|
|
12
|
+
* module cache gives every importer the same object).
|
|
13
|
+
*
|
|
14
|
+
* This is genuinely global, mutable state - the trade-off Mongoose
|
|
15
|
+
* itself makes (`mongoose.connect()` sets its one default connection
|
|
16
|
+
* the same way). It means exactly one connection/schema-set per Node
|
|
17
|
+
* process using this API. Apps that need more than one distinct
|
|
18
|
+
* connection or a fully isolated per-request scope should use
|
|
19
|
+
* `createClient()`/`UnitOfWork` instead (see the client module),
|
|
20
|
+
* which take an explicit `ModelRegistry`/connection rather than
|
|
21
|
+
* sharing one implicitly.
|
|
22
|
+
*/
|
|
23
|
+
export declare const state: {
|
|
24
|
+
connection: null;
|
|
25
|
+
registry: ModelRegistry;
|
|
26
|
+
hooks: HookRegistry;
|
|
27
|
+
wrappers: Map<any, any>;
|
|
28
|
+
models: Map<any, any>;
|
|
29
|
+
mixinHooks: Map<any, any>;
|
|
30
|
+
reports: Map<any, any>;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Resets all global state. Not part of the normal API surface - for
|
|
34
|
+
* tests only, so multiple test files (or multiple describe blocks
|
|
35
|
+
* within one file) can each start from a clean slate rather than
|
|
36
|
+
* colliding on model names registered by an earlier test.
|
|
37
|
+
*/
|
|
38
|
+
export declare function __resetGlobalStateForTests(): void;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Syncs every model defined via `createModel()` against the default
|
|
3
|
+
* connection - tables, meta-tables, and belongsToMany pivot tables
|
|
4
|
+
* (Phases 3/4/7's `sync()`, run against the default registry).
|
|
5
|
+
*
|
|
6
|
+
* Always an explicit call, never implicit - same principle as `sync()`
|
|
7
|
+
* everywhere else in this ORM: schema changes are something the
|
|
8
|
+
* caller chooses to run, not something that happens as a side effect
|
|
9
|
+
* of defining a model or making a query.
|
|
10
|
+
*/
|
|
11
|
+
export declare function syncModels(): Promise<import("../schema/sync.js").SyncResult>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs `fn` inside a transaction on the default connection, passing
|
|
3
|
+
* the transaction object through so it can be handed to any Model
|
|
4
|
+
* method's `{ trx }` option (`Order.create(data, { trx })`) to
|
|
5
|
+
* include it in the same atomic unit. Every `Model` method - `create`,
|
|
6
|
+
* `find`, `findOne`, `updateMany`, `deleteMany`, `attach`, `detach`,
|
|
7
|
+
* and a document's own `save()`/`delete()` - accepts `{ trx }` for
|
|
8
|
+
* exactly this reason.
|
|
9
|
+
*
|
|
10
|
+
* Without this, a multi-step write across Models (e.g. creating an
|
|
11
|
+
* order plus each of its line items) has no way to be atomic: each
|
|
12
|
+
* `Model.create()` call would hit the default connection directly,
|
|
13
|
+
* with no way to roll every step back together if a later one fails.
|
|
14
|
+
*
|
|
15
|
+
* If `fn` throws (or returns a rejected promise), Knex rolls the
|
|
16
|
+
* transaction back automatically and the rejection propagates here.
|
|
17
|
+
*
|
|
18
|
+
* @template T
|
|
19
|
+
* @param {(trx: import('knex').Knex.Transaction) => Promise<T>} fn
|
|
20
|
+
* @returns {Promise<T>}
|
|
21
|
+
*/
|
|
22
|
+
export declare function transaction<T>(fn: (trx: import('knex').Knex.Transaction) => Promise<T>): Promise<T>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { createConnection, closeConnection, DIALECTS } from "./connection.js";
|
|
2
|
+
export * from "./fields/index.js";
|
|
3
|
+
export * from "./models/index.js";
|
|
4
|
+
export * from "./schema/index.js";
|
|
5
|
+
export * from "./meta/index.js";
|
|
6
|
+
export * from "./crud/index.js";
|
|
7
|
+
export * from "./migrations/index.js";
|
|
8
|
+
export * from "./relations/index.js";
|
|
9
|
+
export * from "./domain/index.js";
|
|
10
|
+
export * from "./dialect/index.js";
|
|
11
|
+
export * from "./reports/index.js";
|
|
12
|
+
export * from "./diagnostics/index.js";
|
|
13
|
+
export * from "./uow/index.js";
|
|
14
|
+
export * from "./client/index.js";
|
|
15
|
+
export * from "./global/index.js";
|
|
16
|
+
export * from "./dto/index.js";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { ensureMetaTables } from "./schema.js";
|
|
2
|
+
export { syncMeta } from "./sync-meta.js";
|
|
3
|
+
export { getModelMeta, getFieldsMeta, listModelsMeta } from "./query.js";
|
|
4
|
+
export { META_TABLE_PREFIX, META_MODELS_TABLE, META_FIELDS_TABLE } from "./reserved-tables.js";
|
|
5
|
+
export { snapshotModelMeta, restoreModelMeta } from "./snapshot.js";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the `_meta_models` row for a model, or undefined if it's
|
|
3
|
+
* never been synced. Excludes models flagged as removed unless
|
|
4
|
+
* `includeRemoved` is set.
|
|
5
|
+
*
|
|
6
|
+
* @param {import('knex').Knex} db
|
|
7
|
+
* @param {string} modelName
|
|
8
|
+
* @param {{ includeRemoved?: boolean }} [options]
|
|
9
|
+
*/
|
|
10
|
+
export declare function getModelMeta(db: import('knex').Knex, modelName: string, options?: {
|
|
11
|
+
includeRemoved?: boolean;
|
|
12
|
+
}): Promise<any>;
|
|
13
|
+
/**
|
|
14
|
+
* Returns the `_meta_fields` rows for a model, ordered by field name.
|
|
15
|
+
* This is the plain-SQL-queryable answer to "what fields does model X
|
|
16
|
+
* have, and what are their types" that Phase 4 exists to provide.
|
|
17
|
+
*
|
|
18
|
+
* @param {import('knex').Knex} db
|
|
19
|
+
* @param {string} modelName
|
|
20
|
+
* @param {{ includeRemoved?: boolean }} [options]
|
|
21
|
+
*/
|
|
22
|
+
export declare function getFieldsMeta(db: import('knex').Knex, modelName: string, options?: {
|
|
23
|
+
includeRemoved?: boolean;
|
|
24
|
+
}): Promise<any[]>;
|
|
25
|
+
/**
|
|
26
|
+
* Returns all `_meta_models` rows, ordered by name. Excludes models
|
|
27
|
+
* flagged as removed unless `includeRemoved` is set.
|
|
28
|
+
*
|
|
29
|
+
* @param {import('knex').Knex} db
|
|
30
|
+
* @param {{ includeRemoved?: boolean }} [options]
|
|
31
|
+
*/
|
|
32
|
+
export declare function listModelsMeta(db: import('knex').Knex, options?: {
|
|
33
|
+
includeRemoved?: boolean;
|
|
34
|
+
}): Promise<any[]>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Table names starting with this prefix are reserved for internal
|
|
3
|
+
* metadata tables and cannot be used by user-defined models (enforced
|
|
4
|
+
* in ModelRegistry#register). Exported from here rather than defined
|
|
5
|
+
* inline in the registry so the meta module stays the single source
|
|
6
|
+
* of truth for what it owns.
|
|
7
|
+
*/
|
|
8
|
+
export declare const META_TABLE_PREFIX = "_meta_";
|
|
9
|
+
export declare const META_MODELS_TABLE = "_meta_models";
|
|
10
|
+
export declare const META_FIELDS_TABLE = "_meta_fields";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates the two internal metadata tables if they don't already
|
|
3
|
+
* exist. Idempotent, same spirit as Phase 3's additive sync() - safe
|
|
4
|
+
* to call on every sync().
|
|
5
|
+
*
|
|
6
|
+
* `_meta_models` - one row per registered model (name, resolved table
|
|
7
|
+
* name, lifecycle timestamps).
|
|
8
|
+
*
|
|
9
|
+
* `_meta_fields` - one row per field on a model (name, resolved
|
|
10
|
+
* column name, type, constraints, and the full field descriptor as
|
|
11
|
+
* JSON for fidelity). Denormalizes the model name onto each row
|
|
12
|
+
* (alongside the `model_id` FK) specifically so a flat query like
|
|
13
|
+
* `SELECT * FROM _meta_fields WHERE model = 'invoice'` works without
|
|
14
|
+
* a join - that ergonomic is the whole point of this phase.
|
|
15
|
+
*
|
|
16
|
+
* @param {import('knex').Knex} db
|
|
17
|
+
*/
|
|
18
|
+
export declare function ensureMetaTables(db: import('knex').Knex): Promise<void>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type ModelMetaSnapshot = {
|
|
2
|
+
table_name: string;
|
|
3
|
+
removed: boolean;
|
|
4
|
+
/**
|
|
5
|
+
* - ordered by name
|
|
6
|
+
*/
|
|
7
|
+
fields: Array<{
|
|
8
|
+
name: string;
|
|
9
|
+
column: string;
|
|
10
|
+
type: string;
|
|
11
|
+
required: boolean;
|
|
12
|
+
is_unique: boolean;
|
|
13
|
+
is_indexed: boolean;
|
|
14
|
+
options: unknown;
|
|
15
|
+
removed: boolean;
|
|
16
|
+
}>;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* @param {import('knex').Knex} db
|
|
20
|
+
* @param {string} modelName
|
|
21
|
+
* @returns {Promise<ModelMetaSnapshot|null>} null if the model has no metadata row at all
|
|
22
|
+
*/
|
|
23
|
+
export declare function snapshotModelMeta(db: import('knex').Knex, modelName: string): Promise<ModelMetaSnapshot | null>;
|
|
24
|
+
/**
|
|
25
|
+
* Makes a model's metadata rows match `snapshot` exactly: rows are
|
|
26
|
+
* created or updated to the snapshot's values, rows the snapshot
|
|
27
|
+
* doesn't have are deleted, and `null` deletes the model's metadata
|
|
28
|
+
* entirely (it did not exist in that state). Run it inside the same
|
|
29
|
+
* transaction as the schema change it accompanies.
|
|
30
|
+
*
|
|
31
|
+
* @param {import('knex').Knex} db - ideally a transaction
|
|
32
|
+
* @param {string} modelName
|
|
33
|
+
* @param {ModelMetaSnapshot|null} snapshot
|
|
34
|
+
*/
|
|
35
|
+
export declare function restoreModelMeta(db: import('knex').Knex, modelName: string, snapshot: ModelMetaSnapshot | null): Promise<void>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export type MetaSyncResult = {
|
|
2
|
+
modelsUpserted: string[];
|
|
3
|
+
/**
|
|
4
|
+
* - "modelName.fieldName"
|
|
5
|
+
*/
|
|
6
|
+
fieldsUpserted: string[];
|
|
7
|
+
/**
|
|
8
|
+
* - flagged, not deleted
|
|
9
|
+
*/
|
|
10
|
+
modelsRemoved: string[];
|
|
11
|
+
/**
|
|
12
|
+
* - "modelName.fieldName", flagged not deleted
|
|
13
|
+
*/
|
|
14
|
+
fieldsRemoved: string[];
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {Object} MetaSyncResult
|
|
18
|
+
* @property {string[]} modelsUpserted
|
|
19
|
+
* @property {string[]} fieldsUpserted - "modelName.fieldName"
|
|
20
|
+
* @property {string[]} modelsRemoved - flagged, not deleted
|
|
21
|
+
* @property {string[]} fieldsRemoved - "modelName.fieldName", flagged not deleted
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* Upserts `_meta_models`/`_meta_fields` rows to reflect the current
|
|
25
|
+
* state of `modelRegistry`. Called by schema.sync() after the real
|
|
26
|
+
* data tables have been created/altered, so the metadata always
|
|
27
|
+
* describes what's actually in the database.
|
|
28
|
+
*
|
|
29
|
+
* Assumes ensureMetaTables() has already been run - deliberately not
|
|
30
|
+
* called from here, since that's a DDL operation kept outside the
|
|
31
|
+
* DML transaction this function runs in.
|
|
32
|
+
*
|
|
33
|
+
* A model or field that existed in a previous sync but is no longer
|
|
34
|
+
* in the registry is flagged via `removed_at`, never deleted - so
|
|
35
|
+
* `_meta_fields` remains a durable record answerable by a plain SQL
|
|
36
|
+
* query even for things that used to exist.
|
|
37
|
+
*
|
|
38
|
+
* @param {import('knex').Knex} db
|
|
39
|
+
* @param {import('../models/registry.js').ModelRegistry} modelRegistry
|
|
40
|
+
* @returns {Promise<MetaSyncResult>}
|
|
41
|
+
*/
|
|
42
|
+
export declare function syncMeta(db: import('knex').Knex, modelRegistry: import('../models/registry.js').ModelRegistry): Promise<MetaSyncResult>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inserts or updates a `_meta_models` row for the given model
|
|
3
|
+
* descriptor, and returns its id. Clears `removed_at` if the model
|
|
4
|
+
* was previously flagged as removed and has now reappeared.
|
|
5
|
+
*
|
|
6
|
+
* Uses select-then-insert-or-update rather than an upsert/onConflict
|
|
7
|
+
* clause: `.onConflict().merge()` support varies enough across Knex's
|
|
8
|
+
* sqlite and postgres dialects that the extra round trip here is a
|
|
9
|
+
* worthwhile trade for staying dialect-portable without surprises.
|
|
10
|
+
*
|
|
11
|
+
* @param {import('knex').Knex} trx
|
|
12
|
+
* @param {import('../models/define-model.js').ModelDescriptor} model
|
|
13
|
+
* @param {Date} now
|
|
14
|
+
* @returns {Promise<number>}
|
|
15
|
+
*/
|
|
16
|
+
export declare function ensureMetaModel(trx: import('knex').Knex, model: import('../models/define-model.js').ModelDescriptor, now: Date): Promise<number>;
|
|
17
|
+
/**
|
|
18
|
+
* Inserts or updates a `_meta_fields` row for one field on a model.
|
|
19
|
+
* The full field descriptor is stored as JSON in `options` for
|
|
20
|
+
* fidelity (factory-function defaults are silently dropped by
|
|
21
|
+
* JSON.stringify, consistent with Phase 3's schema-sync treatment of
|
|
22
|
+
* them as a JS-side, not a persisted, concern).
|
|
23
|
+
*
|
|
24
|
+
* @param {import('knex').Knex} trx
|
|
25
|
+
* @param {number} modelId
|
|
26
|
+
* @param {string} modelName
|
|
27
|
+
* @param {string} fieldName
|
|
28
|
+
* @param {import('../fields/define-field.js').FieldDescriptor} field
|
|
29
|
+
* @param {string} columnName
|
|
30
|
+
* @param {Date} now
|
|
31
|
+
*/
|
|
32
|
+
export declare function ensureMetaField(trx: import('knex').Knex, modelId: number, modelName: string, fieldName: string, field: import('../fields/define-field.js').FieldDescriptor, columnName: string, now: Date): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Inserts or updates a `_meta_fields` row for one relation on a model
|
|
35
|
+
* (Phase 7). Relations share the `_meta_fields` table with scalar
|
|
36
|
+
* fields rather than getting a table of their own - the roadmap calls
|
|
37
|
+
* for exactly this ("Relation fields stored in _meta_fields too"),
|
|
38
|
+
* and it's a natural fit since `options` already exists as a flexible
|
|
39
|
+
* JSON column for full-fidelity storage regardless of shape.
|
|
40
|
+
*
|
|
41
|
+
* `column` is set to the underlying foreign-key column when this
|
|
42
|
+
* model owns it (belongsTo), or left as an empty string for
|
|
43
|
+
* hasOne/hasMany/belongsToMany, which don't have a column of their
|
|
44
|
+
* own on this model. `required`/`is_unique`/`is_indexed` don't apply
|
|
45
|
+
* to relations and are always stored as false; the full descriptor
|
|
46
|
+
* (type, model, foreignKey, through, otherKey, localKey) lives in
|
|
47
|
+
* `options` for introspection.
|
|
48
|
+
*
|
|
49
|
+
* @param {import('knex').Knex} trx
|
|
50
|
+
* @param {number} modelId
|
|
51
|
+
* @param {string} modelName
|
|
52
|
+
* @param {string} relationName
|
|
53
|
+
* @param {import('../relations/define-relation.js').RelationDescriptor} relation
|
|
54
|
+
* @param {import('../models/define-model.js').ModelDescriptor} model
|
|
55
|
+
* @param {Date} now
|
|
56
|
+
*/
|
|
57
|
+
export declare function ensureMetaRelation(trx: import('knex').Knex, modelId: number, modelName: string, relationName: string, relation: import('../relations/define-relation.js').RelationDescriptor, model: import('../models/define-model.js').ModelDescriptor, now: Date): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Flags `_meta_models` rows as removed (sets `removed_at`) when their
|
|
60
|
+
* name is no longer present in the current registry. Never deletes -
|
|
61
|
+
* metadata history is preserved so a later phase (migrations) can
|
|
62
|
+
* still see what used to exist.
|
|
63
|
+
*
|
|
64
|
+
* @param {import('knex').Knex} trx
|
|
65
|
+
* @param {import('../models/registry.js').ModelRegistry} modelRegistry
|
|
66
|
+
* @param {Date} now
|
|
67
|
+
* @returns {Promise<string[]>} names of models newly flagged as removed
|
|
68
|
+
*/
|
|
69
|
+
export declare function flagRemovedModels(trx: import('knex').Knex, modelRegistry: import('../models/registry.js').ModelRegistry, now: Date): Promise<string[]>;
|
|
70
|
+
/**
|
|
71
|
+
* Flags `_meta_fields` rows as removed for one model when their field
|
|
72
|
+
* name is no longer present on that model's current field set. Never
|
|
73
|
+
* deletes, same rationale as flagRemovedModels().
|
|
74
|
+
*
|
|
75
|
+
* @param {import('knex').Knex} trx
|
|
76
|
+
* @param {number} modelId
|
|
77
|
+
* @param {Set<string>} currentFieldNames
|
|
78
|
+
* @param {Date} now
|
|
79
|
+
* @returns {Promise<string[]>} field names newly flagged as removed
|
|
80
|
+
*/
|
|
81
|
+
export declare function flagRemovedFields(trx: import('knex').Knex, modelId: number, currentFieldNames: Set<string>, now: Date): Promise<string[]>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs a migration's `up`. Thin pass-through kept mainly so
|
|
3
|
+
* applying/rolling back reads symmetrically and is discoverable from
|
|
4
|
+
* `@kiri-orm/core`'s exports.
|
|
5
|
+
*
|
|
6
|
+
* @param {import('knex').Knex} db
|
|
7
|
+
* @param {import('./generate.js').Migration} migration
|
|
8
|
+
*/
|
|
9
|
+
export declare function applyMigration(db: import('knex').Knex, migration: import('./generate.js').Migration): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Runs a migration's `down`. See generateMigration() for exactly what
|
|
12
|
+
* "reversible" does and doesn't mean here (schema-level, not
|
|
13
|
+
* data-level, for destructive operations).
|
|
14
|
+
*
|
|
15
|
+
* @param {import('knex').Knex} db
|
|
16
|
+
* @param {import('./generate.js').Migration} migration
|
|
17
|
+
*/
|
|
18
|
+
export declare function rollbackMigration(db: import('knex').Knex, migration: import('./generate.js').Migration): Promise<void>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders a `DiffPlan` (from diffSchema) into human-readable lines,
|
|
3
|
+
* prefixed so destructive/ambiguous items are impossible to miss when
|
|
4
|
+
* reviewing before applying. This is the "dry-run" preview the
|
|
5
|
+
* roadmap calls for - the plan itself is already inert data, this
|
|
6
|
+
* just formats it for a human (or a future CLI) to review.
|
|
7
|
+
*
|
|
8
|
+
* @param {import('./diff.js').DiffPlan} plan
|
|
9
|
+
* @returns {string[]}
|
|
10
|
+
*/
|
|
11
|
+
export declare function describePlan(plan: import('./diff.js').DiffPlan): string[];
|