@cleverbrush/knex-schema 4.3.1 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -0
- package/dist/SchemaQueryBuilder.d.ts +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +12 -12
- package/dist/index.js.map +1 -1
- package/dist/migration.d.ts +34 -0
- package/dist/operations/insert.d.ts +47 -3
- package/dist/types.d.ts +24 -0
- package/package.json +2 -2
package/dist/migration.d.ts
CHANGED
|
@@ -95,6 +95,40 @@ export declare function generateMigration(diff: MigrationDiff, tableName: string
|
|
|
95
95
|
* ```
|
|
96
96
|
*/
|
|
97
97
|
export declare function tableExistsInDb(knex: Knex, tableName: string): Promise<boolean>;
|
|
98
|
+
/**
|
|
99
|
+
* A schema validation issue detected against the live database.
|
|
100
|
+
*/
|
|
101
|
+
export type EntitySchemaValidationIssue = {
|
|
102
|
+
type: 'missing-table';
|
|
103
|
+
tableName: string;
|
|
104
|
+
} | {
|
|
105
|
+
type: 'schema-drift';
|
|
106
|
+
tableName: string;
|
|
107
|
+
diff: MigrationDiff;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Result returned by {@link validateEntitiesAgainstDatabase}.
|
|
111
|
+
*/
|
|
112
|
+
export interface EntitySchemaValidationResult {
|
|
113
|
+
/** `true` when every entity table exists and has an empty schema diff. */
|
|
114
|
+
valid: boolean;
|
|
115
|
+
/** Table names inspected during validation. */
|
|
116
|
+
checkedTables: string[];
|
|
117
|
+
/** Missing tables or non-empty schema diffs. */
|
|
118
|
+
issues: EntitySchemaValidationIssue[];
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Validate entity schemas against a live database without changing anything.
|
|
122
|
+
*
|
|
123
|
+
* This is the read-only counterpart to `db push`: it checks every registered
|
|
124
|
+
* entity table, including class-table-inheritance variant tables, and reports
|
|
125
|
+
* missing tables or schema drift.
|
|
126
|
+
*
|
|
127
|
+
* @param knex - A configured Knex instance or transaction.
|
|
128
|
+
* @param entities - The entity definitions to validate.
|
|
129
|
+
* @returns A validation result with all detected issues.
|
|
130
|
+
*/
|
|
131
|
+
export declare function validateEntitiesAgainstDatabase(knex: Knex, entities: Entity<any, any>[]): Promise<EntitySchemaValidationResult>;
|
|
98
132
|
/**
|
|
99
133
|
* Return `true` when a {@link MigrationDiff} has no operations — i.e. the
|
|
100
134
|
* database table is already in sync with the schema.
|
|
@@ -1,11 +1,54 @@
|
|
|
1
|
-
import type { InferType } from '@cleverbrush/schema';
|
|
1
|
+
import type { InferType, ObjectSchemaBuilder } from '@cleverbrush/schema';
|
|
2
2
|
import type { Knex } from 'knex';
|
|
3
3
|
import type { SchemaQueryBuilder } from '../SchemaQueryBuilder.js';
|
|
4
4
|
import type { ColumnRef, InsertType } from '../types.js';
|
|
5
|
-
|
|
5
|
+
type AnyObjectSchema = ObjectSchemaBuilder<any, any, any, any, any, any, any>;
|
|
6
|
+
/**
|
|
7
|
+
* Helpers available to `onConflict().merge()` update expressions and
|
|
8
|
+
* conditional `where` callbacks.
|
|
9
|
+
*/
|
|
10
|
+
export interface OnConflictMergeHelpers<TLocalSchema extends AnyObjectSchema> {
|
|
11
|
+
/** The Knex instance used by the query builder. */
|
|
12
|
+
readonly knex: Knex;
|
|
13
|
+
/**
|
|
14
|
+
* Resolve a schema property reference to its mapped database column name.
|
|
15
|
+
*/
|
|
16
|
+
column(ref: ColumnRef<TLocalSchema>): string;
|
|
17
|
+
/**
|
|
18
|
+
* Create a raw SQL expression with Knex bindings.
|
|
19
|
+
*/
|
|
20
|
+
raw(sql: string, bindings?: readonly unknown[]): Knex.Raw;
|
|
21
|
+
/**
|
|
22
|
+
* Reference the `excluded.<column>` value in an upsert merge expression.
|
|
23
|
+
*/
|
|
24
|
+
excluded(ref: ColumnRef<TLocalSchema>): Knex.Raw;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Value accepted in explicit `onConflict().merge(data, updateData)` payloads.
|
|
28
|
+
*/
|
|
29
|
+
export type OnConflictUpdateValue<TLocalSchema extends AnyObjectSchema, TValue> = TValue | Knex.Raw | ((helpers: OnConflictMergeHelpers<TLocalSchema>) => TValue | Knex.Raw);
|
|
30
|
+
/**
|
|
31
|
+
* Explicit update payload accepted by `onConflict().merge()`.
|
|
32
|
+
*/
|
|
33
|
+
export type OnConflictUpdateData<TLocalSchema extends AnyObjectSchema> = Partial<{
|
|
34
|
+
[K in keyof InferType<TLocalSchema>]: OnConflictUpdateValue<TLocalSchema, InferType<TLocalSchema>[K]>;
|
|
35
|
+
}>;
|
|
36
|
+
/**
|
|
37
|
+
* Options for `onConflict().merge()`.
|
|
38
|
+
*/
|
|
39
|
+
export interface OnConflictMergeOptions<TLocalSchema extends AnyObjectSchema> {
|
|
40
|
+
/**
|
|
41
|
+
* Attach a conditional `WHERE` clause to the generated merge.
|
|
42
|
+
*
|
|
43
|
+
* This is useful for row-version or timestamp guarded upserts.
|
|
44
|
+
*/
|
|
45
|
+
where?: (query: Knex.QueryBuilder, helpers: OnConflictMergeHelpers<TLocalSchema>) => void;
|
|
46
|
+
}
|
|
47
|
+
export declare class OnConflictBuilder<TLocalSchema extends AnyObjectSchema, TResult> {
|
|
6
48
|
#private;
|
|
7
49
|
constructor(knex: Knex, localSchema: TLocalSchema, _parent: SchemaQueryBuilder<TLocalSchema, TResult>, conflictColumns: string[]);
|
|
8
|
-
merge(data: InsertType<TLocalSchema>,
|
|
50
|
+
merge(data: InsertType<TLocalSchema>, options?: OnConflictMergeOptions<TLocalSchema>): Promise<TResult>;
|
|
51
|
+
merge(data: InsertType<TLocalSchema>, updateData?: OnConflictUpdateData<TLocalSchema>, options?: OnConflictMergeOptions<TLocalSchema>): Promise<TResult>;
|
|
9
52
|
ignore(data: InsertType<TLocalSchema>): Promise<TResult | undefined>;
|
|
10
53
|
}
|
|
11
54
|
export declare function insertImpl(builder: SchemaQueryBuilder<any, any>, data: InsertType<any>): Promise<any>;
|
|
@@ -24,3 +67,4 @@ export declare function bulkUpsertImpl(builder: SchemaQueryBuilder<any, any>, ro
|
|
|
24
67
|
conflictColumns: ColumnRef<any>[];
|
|
25
68
|
chunkSize?: number;
|
|
26
69
|
}): Promise<any[]>;
|
|
70
|
+
export {};
|
package/dist/types.d.ts
CHANGED
|
@@ -89,6 +89,30 @@ export type ValidatedSpec = ({
|
|
|
89
89
|
type: 'many';
|
|
90
90
|
} & ValidatedJoinManySpec);
|
|
91
91
|
export type InsertType<T extends ObjectSchemaBuilder<any, any, any, any, any, any, any>> = InferType<ReturnType<T['makeAllPropsOptional']>>;
|
|
92
|
+
type OptionalKeys<T> = {
|
|
93
|
+
[K in keyof T]-?: undefined extends T[K] ? K : never;
|
|
94
|
+
}[keyof T];
|
|
95
|
+
type RequiredKeys<T> = Exclude<keyof T, OptionalKeys<T>>;
|
|
96
|
+
/**
|
|
97
|
+
* Normalize a schema-inferred property type to the value shape commonly
|
|
98
|
+
* returned by database rows.
|
|
99
|
+
*
|
|
100
|
+
* Optional schema properties may be absent in payloads, but database rows use
|
|
101
|
+
* `NULL` for persisted missing values.
|
|
102
|
+
*/
|
|
103
|
+
export type InferDatabaseValue<T> = undefined extends T ? Exclude<T, undefined> | null | undefined : T;
|
|
104
|
+
/**
|
|
105
|
+
* Infer the object shape of a persisted database row from an object schema.
|
|
106
|
+
*
|
|
107
|
+
* This is useful for mapper code and raw-query helpers where `InferType<T>`
|
|
108
|
+
* is too strict because optional schema properties can come back as `null`
|
|
109
|
+
* from SQL.
|
|
110
|
+
*/
|
|
111
|
+
export type InferDatabaseRow<T extends ObjectSchemaBuilder<any, any, any, any, any, any, any>> = {
|
|
112
|
+
[K in RequiredKeys<InferType<T>>]: InferDatabaseValue<InferType<T>[K]>;
|
|
113
|
+
} & {
|
|
114
|
+
[K in OptionalKeys<InferType<T>>]?: InferDatabaseValue<InferType<T>[K]>;
|
|
115
|
+
};
|
|
92
116
|
import type { COMPOSITE_PRIMARY_KEY_BRAND, PRIMARY_KEY_BRAND } from './extension.js';
|
|
93
117
|
/**
|
|
94
118
|
* Detect whether a property schema is branded as a primary key (i.e. its
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"email": "andrew_zol@cleverbrush.com"
|
|
6
6
|
},
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@cleverbrush/schema": "^4.
|
|
8
|
+
"@cleverbrush/schema": "^4.4.0"
|
|
9
9
|
},
|
|
10
10
|
"peerDependencies": {
|
|
11
11
|
"knex": ">=3.1.0"
|
|
@@ -52,5 +52,5 @@
|
|
|
52
52
|
},
|
|
53
53
|
"type": "module",
|
|
54
54
|
"types": "./dist/index.d.ts",
|
|
55
|
-
"version": "4.
|
|
55
|
+
"version": "4.4.0"
|
|
56
56
|
}
|