@mikro-orm/knex 6.3.13-dev.1 → 6.3.13-dev.10
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/dialects/mysql/MySqlColumnCompiler.d.ts +3 -3
- package/dialects/mysql/MySqlSchemaHelper.d.ts +3 -1
- package/dialects/mysql/MySqlSchemaHelper.js +18 -0
- package/package.json +2 -2
- package/query/QueryBuilder.d.ts +5 -4
- package/query/QueryBuilderHelper.js +1 -1
- package/schema/SchemaComparator.js +8 -3
- package/schema/SchemaHelper.d.ts +2 -2
- package/schema/SchemaHelper.js +3 -3
- package/schema/SqlSchemaGenerator.js +8 -1
- package/typings.d.ts +4 -4
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import BaseMySqlColumnCompiler from 'knex/lib/dialects/mysql/schema/mysql-columncompiler';
|
|
2
|
-
import type {
|
|
2
|
+
import type { MySqlIncrementOptions } from '../../typings';
|
|
3
3
|
export declare class MySqlColumnCompiler extends BaseMySqlColumnCompiler {
|
|
4
|
-
increments(options:
|
|
5
|
-
bigincrements(options:
|
|
4
|
+
increments(options: MySqlIncrementOptions): string;
|
|
5
|
+
bigincrements(options: MySqlIncrementOptions): string;
|
|
6
6
|
private generateDDL;
|
|
7
7
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { Knex } from 'knex';
|
|
2
|
-
import type { CheckDef, Column, IndexDef, TableDifference, Table, ForeignKey } from '../../typings';
|
|
2
|
+
import type { CheckDef, Column, IndexDef, TableDifference, Table, ForeignKey, MySqlTableBuilder } from '../../typings';
|
|
3
3
|
import { type Dictionary, type Type } from '@mikro-orm/core';
|
|
4
4
|
import type { AbstractSqlConnection } from '../../AbstractSqlConnection';
|
|
5
5
|
import { SchemaHelper } from '../../schema/SchemaHelper';
|
|
6
6
|
import type { DatabaseSchema } from '../../schema/DatabaseSchema';
|
|
7
|
+
import type { DatabaseTable } from '../../schema/DatabaseTable';
|
|
7
8
|
export declare class MySqlSchemaHelper extends SchemaHelper {
|
|
8
9
|
private readonly _cache;
|
|
9
10
|
static readonly DEFAULT_VALUES: {
|
|
@@ -22,6 +23,7 @@ export declare class MySqlSchemaHelper extends SchemaHelper {
|
|
|
22
23
|
getAllChecks(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<CheckDef[]>>;
|
|
23
24
|
getAllForeignKeys(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<Dictionary<ForeignKey>>>;
|
|
24
25
|
getPreAlterTable(tableDiff: TableDifference, safe: boolean): string;
|
|
26
|
+
createTableColumn(table: MySqlTableBuilder, column: Column, fromTable: DatabaseTable, changedProperties?: Set<string>, alter?: boolean): Knex.ColumnBuilder | undefined;
|
|
25
27
|
configureColumnDefault(column: Column, col: Knex.ColumnBuilder, knex: Knex, changedProperties?: Set<string>): Knex.ColumnBuilder;
|
|
26
28
|
getRenameColumnSQL(tableName: string, oldColumnName: string, to: Column): string;
|
|
27
29
|
getRenameIndexSQL(tableName: string, index: IndexDef, oldIndexName: string): string;
|
|
@@ -180,6 +180,24 @@ class MySqlSchemaHelper extends SchemaHelper_1.SchemaHelper {
|
|
|
180
180
|
.map(col => `alter table \`${tableDiff.name}\` modify \`${col.name}\` ${this.getColumnDeclarationSQL({ ...col, autoincrement: false })}`)
|
|
181
181
|
.join(';\n');
|
|
182
182
|
}
|
|
183
|
+
createTableColumn(table, column, fromTable, changedProperties, alter) {
|
|
184
|
+
const compositePK = fromTable.getPrimaryKey()?.composite;
|
|
185
|
+
if (column.autoincrement && !column.generated && !compositePK && column.primary) {
|
|
186
|
+
const primaryKey = !changedProperties && !this.hasNonDefaultPrimaryKeyName(fromTable);
|
|
187
|
+
if (column.mappedType instanceof core_1.BigIntType) {
|
|
188
|
+
return table.bigIncrements(column.name, { primaryKey, unsigned: column.unsigned, type: column.type });
|
|
189
|
+
}
|
|
190
|
+
return table.increments(column.name, { primaryKey, unsigned: column.unsigned, type: column.type });
|
|
191
|
+
}
|
|
192
|
+
if (column.mappedType instanceof core_1.EnumType && column.enumItems?.every(item => core_1.Utils.isString(item))) {
|
|
193
|
+
return table.enum(column.name, column.enumItems);
|
|
194
|
+
}
|
|
195
|
+
let columnType = column.type;
|
|
196
|
+
if (column.generated) {
|
|
197
|
+
columnType += ` generated always as ${column.generated}`;
|
|
198
|
+
}
|
|
199
|
+
return table.specificType(column.name, columnType);
|
|
200
|
+
}
|
|
183
201
|
configureColumnDefault(column, col, knex, changedProperties) {
|
|
184
202
|
if (changedProperties || column.default !== undefined) {
|
|
185
203
|
if (column.default == null) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/knex",
|
|
3
|
-
"version": "6.3.13-dev.
|
|
3
|
+
"version": "6.3.13-dev.10",
|
|
4
4
|
"description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.mjs",
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"@mikro-orm/core": "^6.3.12"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
|
-
"@mikro-orm/core": "6.3.13-dev.
|
|
69
|
+
"@mikro-orm/core": "6.3.13-dev.10",
|
|
70
70
|
"better-sqlite3": "*",
|
|
71
71
|
"libsql": "*",
|
|
72
72
|
"mariadb": "*"
|
package/query/QueryBuilder.d.ts
CHANGED
|
@@ -31,6 +31,7 @@ type EntityRelations<T> = EntityKey<T, true>;
|
|
|
31
31
|
type AddAliasesFromContext<Context> = Context[keyof Context] extends infer Join ? Join extends any ? Join extends [string, infer Alias, infer Type, any] ? `${Alias & string}.${EntityRelations<Type & {}>}` : never : never : never;
|
|
32
32
|
export type QBField<Entity, RootAlias extends string, Context> = (EntityRelations<Entity> | `${RootAlias}.${EntityRelations<Entity>}` | AddAliasesFromContext<Context>) & {} | AnyString;
|
|
33
33
|
export type QBField2<Entity, RootAlias extends string, Context> = (EntityKey<Entity> | `${RootAlias}.${EntityKey<Entity>}` | AddAliasesFromContext<Context>) & {} | AnyString;
|
|
34
|
+
type EntityKeyOrString<Entity extends object = AnyEntity> = AnyString | keyof Entity;
|
|
34
35
|
/**
|
|
35
36
|
* SQL query builder with fluent interface.
|
|
36
37
|
*
|
|
@@ -125,12 +126,12 @@ export declare class QueryBuilder<Entity extends object = AnyEntity, RootAlias e
|
|
|
125
126
|
addSelect(fields: Field<Entity> | Field<Entity>[]): SelectQueryBuilder<Entity, RootAlias, Hint, Context>;
|
|
126
127
|
distinct(): SelectQueryBuilder<Entity, RootAlias, Hint, Context>;
|
|
127
128
|
/** postgres only */
|
|
128
|
-
distinctOn(fields:
|
|
129
|
+
distinctOn(fields: EntityKeyOrString<Entity> | EntityKeyOrString<Entity>[]): SelectQueryBuilder<Entity, RootAlias, Hint, Context>;
|
|
129
130
|
insert(data: RequiredEntityData<Entity> | RequiredEntityData<Entity>[]): InsertQueryBuilder<Entity>;
|
|
130
131
|
update(data: EntityData<Entity>): UpdateQueryBuilder<Entity>;
|
|
131
132
|
delete(cond?: QBFilterQuery): DeleteQueryBuilder<Entity>;
|
|
132
133
|
truncate(): TruncateQueryBuilder<Entity>;
|
|
133
|
-
count(field?:
|
|
134
|
+
count(field?: EntityKeyOrString<Entity> | EntityKeyOrString<Entity>[], distinct?: boolean): CountQueryBuilder<Entity>;
|
|
134
135
|
join<Field extends QBField<Entity, RootAlias, Context>, Alias extends string>(field: Field | Knex.QueryBuilder | QueryBuilder<any>, alias: Alias, cond?: QBFilterQuery, type?: JoinType, path?: string, schema?: string): SelectQueryBuilder<Entity, RootAlias, ModifyHint<RootAlias, Context, Hint, Field> & {}, ModifyContext<Entity, Context, Field, Alias>>;
|
|
135
136
|
innerJoin<Field extends QBField<Entity, RootAlias, Context>, Alias extends string>(field: Field | Knex.QueryBuilder | QueryBuilder<any>, alias: Alias, cond?: QBFilterQuery, schema?: string): SelectQueryBuilder<Entity, RootAlias, ModifyHint<RootAlias, Context, Hint, Field> & {}, ModifyContext<Entity, Context, Field, Alias>>;
|
|
136
137
|
innerJoinLateral(field: Knex.QueryBuilder | QueryBuilder<any>, alias: string, cond?: QBFilterQuery, schema?: string): this;
|
|
@@ -154,7 +155,7 @@ export declare class QueryBuilder<Entity extends object = AnyEntity, RootAlias e
|
|
|
154
155
|
orWhere(cond: QBFilterQuery<Entity>): this;
|
|
155
156
|
orWhere(cond: string, params?: any[]): this;
|
|
156
157
|
orderBy(orderBy: QBQueryOrderMap<Entity> | QBQueryOrderMap<Entity>[]): SelectQueryBuilder<Entity, RootAlias, Hint, Context>;
|
|
157
|
-
groupBy(fields:
|
|
158
|
+
groupBy(fields: EntityKeyOrString<Entity> | readonly EntityKeyOrString<Entity>[]): SelectQueryBuilder<Entity, RootAlias, Hint, Context>;
|
|
158
159
|
having(cond?: QBFilterQuery | string, params?: any[]): SelectQueryBuilder<Entity, RootAlias, Hint, Context>;
|
|
159
160
|
onConflict(fields?: Field<Entity> | Field<Entity>[]): InsertQueryBuilder<Entity>;
|
|
160
161
|
ignore(): this;
|
|
@@ -251,7 +252,7 @@ export declare class QueryBuilder<Entity extends object = AnyEntity, RootAlias e
|
|
|
251
252
|
/**
|
|
252
253
|
* Executes count query (without offset and limit), returning total count of results
|
|
253
254
|
*/
|
|
254
|
-
getCount(field?:
|
|
255
|
+
getCount(field?: EntityKeyOrString<Entity> | EntityKeyOrString<Entity>[], distinct?: boolean): Promise<number>;
|
|
255
256
|
/**
|
|
256
257
|
* Executes the query, returning both array of results and total count query (without offset and limit).
|
|
257
258
|
*/
|
|
@@ -129,7 +129,7 @@ class QueryBuilderHelper {
|
|
|
129
129
|
data = this.driver.mapDataToFieldNames(data, true, meta?.properties, convertCustomTypes);
|
|
130
130
|
if (!core_1.Utils.hasObjectKeys(data) && meta && multi) {
|
|
131
131
|
/* istanbul ignore next */
|
|
132
|
-
data[meta.
|
|
132
|
+
data[meta.getPrimaryProps()[0].fieldNames[0]] = this.platform.usesDefaultKeyword() ? this.knex.raw('default') : undefined;
|
|
133
133
|
}
|
|
134
134
|
return data;
|
|
135
135
|
}
|
|
@@ -390,7 +390,7 @@ class SchemaComparator {
|
|
|
390
390
|
const toProp = this.mapColumnToProperty({ ...toColumn, autoincrement: false });
|
|
391
391
|
const fromColumnType = this.platform.normalizeColumnType(fromColumn.mappedType.getColumnType(fromProp, this.platform).toLowerCase(), fromProp);
|
|
392
392
|
const fromNativeEnum = fromTable.nativeEnums[fromColumnType] ?? Object.values(fromTable.nativeEnums).find(e => e.name === fromColumnType && e.schema !== '*');
|
|
393
|
-
|
|
393
|
+
let toColumnType = this.platform.normalizeColumnType(toColumn.mappedType.getColumnType(toProp, this.platform).toLowerCase(), toProp);
|
|
394
394
|
const log = (msg, params) => {
|
|
395
395
|
if (tableName) {
|
|
396
396
|
const copy = core_1.Utils.copy(params);
|
|
@@ -402,8 +402,13 @@ class SchemaComparator {
|
|
|
402
402
|
(!fromNativeEnum || `${fromNativeEnum.schema}.${fromNativeEnum.name}` !== toColumnType) &&
|
|
403
403
|
!(fromColumn.ignoreSchemaChanges?.includes('type') || toColumn.ignoreSchemaChanges?.includes('type')) &&
|
|
404
404
|
!fromColumn.generated && !toColumn.generated) {
|
|
405
|
-
|
|
406
|
-
|
|
405
|
+
if (!toColumnType.includes('.') && fromTable.schema && fromTable.schema !== this.platform.getDefaultSchemaName()) {
|
|
406
|
+
toColumnType = `${fromTable.schema}.${toColumnType}`;
|
|
407
|
+
}
|
|
408
|
+
if (fromColumnType !== toColumnType) {
|
|
409
|
+
log(`'type' changed for column ${tableName}.${fromColumn.name}`, { fromColumnType, toColumnType });
|
|
410
|
+
changedProperties.add('type');
|
|
411
|
+
}
|
|
407
412
|
}
|
|
408
413
|
if (fromColumn.nullable !== toColumn.nullable && !fromColumn.generated && !toColumn.generated) {
|
|
409
414
|
log(`'nullable' changed for column ${tableName}.${fromColumn.name}`, { fromColumn, toColumn });
|
package/schema/SchemaHelper.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { type Connection, type Dictionary } from '@mikro-orm/core';
|
|
|
2
2
|
import type { Knex } from 'knex';
|
|
3
3
|
import type { AbstractSqlConnection } from '../AbstractSqlConnection';
|
|
4
4
|
import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
|
|
5
|
-
import type { CheckDef, Column,
|
|
5
|
+
import type { CheckDef, Column, ForeignKey, IndexDef, Table, TableDifference } from '../typings';
|
|
6
6
|
import type { DatabaseSchema } from './DatabaseSchema';
|
|
7
7
|
import type { DatabaseTable } from './DatabaseTable';
|
|
8
8
|
export declare abstract class SchemaHelper {
|
|
@@ -30,7 +30,7 @@ export declare abstract class SchemaHelper {
|
|
|
30
30
|
getRenameIndexSQL(tableName: string, index: IndexDef, oldIndexName: string): string;
|
|
31
31
|
getDropColumnsSQL(tableName: string, columns: Column[], schemaName?: string): string;
|
|
32
32
|
hasNonDefaultPrimaryKeyName(table: DatabaseTable): boolean;
|
|
33
|
-
createTableColumn(table:
|
|
33
|
+
createTableColumn(table: Knex.TableBuilder, column: Column, fromTable: DatabaseTable, changedProperties?: Set<string>, alter?: boolean): Knex.ColumnBuilder | undefined;
|
|
34
34
|
configureColumn(column: Column, col: Knex.ColumnBuilder, knex: Knex, changedProperties?: Set<string>): Knex.ColumnBuilder;
|
|
35
35
|
configureColumnDefault(column: Column, col: Knex.ColumnBuilder, knex: Knex, changedProperties?: Set<string>): Knex.ColumnBuilder;
|
|
36
36
|
getPreAlterTable(tableDiff: TableDifference, safe: boolean): string;
|
package/schema/SchemaHelper.js
CHANGED
|
@@ -126,12 +126,12 @@ class SchemaHelper {
|
|
|
126
126
|
}
|
|
127
127
|
createTableColumn(table, column, fromTable, changedProperties, alter) {
|
|
128
128
|
const compositePK = fromTable.getPrimaryKey()?.composite;
|
|
129
|
-
if (column.autoincrement && !column.generated && !compositePK &&
|
|
129
|
+
if (column.autoincrement && !column.generated && !compositePK && (!changedProperties || changedProperties.has('autoincrement') || changedProperties.has('type'))) {
|
|
130
130
|
const primaryKey = !changedProperties && !this.hasNonDefaultPrimaryKeyName(fromTable);
|
|
131
131
|
if (column.mappedType instanceof core_1.BigIntType) {
|
|
132
|
-
return table.bigIncrements(column.name, { primaryKey
|
|
132
|
+
return table.bigIncrements(column.name, { primaryKey });
|
|
133
133
|
}
|
|
134
|
-
return table.increments(column.name, { primaryKey
|
|
134
|
+
return table.increments(column.name, { primaryKey });
|
|
135
135
|
}
|
|
136
136
|
if (column.mappedType instanceof core_1.EnumType && column.enumItems?.every(item => core_1.Utils.isString(item))) {
|
|
137
137
|
return table.enum(column.name, column.enumItems);
|
|
@@ -284,7 +284,9 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
284
284
|
if (!column.nativeEnumName) {
|
|
285
285
|
continue;
|
|
286
286
|
}
|
|
287
|
-
const key = schemaName && schemaName !== this.platform.getDefaultSchemaName()
|
|
287
|
+
const key = schemaName && schemaName !== this.platform.getDefaultSchemaName() && !column.nativeEnumName.includes('.')
|
|
288
|
+
? schemaName + '.' + column.nativeEnumName
|
|
289
|
+
: column.nativeEnumName;
|
|
288
290
|
if (changedProperties.has('enumItems') && key in diff.fromTable.nativeEnums) {
|
|
289
291
|
changedNativeEnums.push([column.nativeEnumName, column.enumItems, diff.fromTable.nativeEnums[key].items]);
|
|
290
292
|
}
|
|
@@ -293,6 +295,11 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
293
295
|
// postgres allows only adding new items, the values are case insensitive
|
|
294
296
|
itemsOld = itemsOld.map(v => v.toLowerCase());
|
|
295
297
|
const newItems = itemsNew.filter(val => !itemsOld.includes(val.toLowerCase()));
|
|
298
|
+
if (enumName.includes('.')) {
|
|
299
|
+
const [enumSchemaName, rawEnumName] = enumName.split('.');
|
|
300
|
+
ret.push(...newItems.map(val => this.knex.schema.raw(this.helper.getAlterNativeEnumSQL(rawEnumName, enumSchemaName, val, itemsNew, itemsOld))));
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
296
303
|
ret.push(...newItems.map(val => this.knex.schema.raw(this.helper.getAlterNativeEnumSQL(enumName, schemaName, val, itemsNew, itemsOld))));
|
|
297
304
|
});
|
|
298
305
|
}
|
package/typings.d.ts
CHANGED
|
@@ -185,13 +185,13 @@ export interface ICriteriaNode<T extends object> {
|
|
|
185
185
|
getPath(addIndex?: boolean): string;
|
|
186
186
|
getPivotPath(path: string): string;
|
|
187
187
|
}
|
|
188
|
-
export type
|
|
188
|
+
export type MySqlIncrementOptions = {
|
|
189
189
|
primaryKey?: boolean;
|
|
190
190
|
unsigned?: boolean;
|
|
191
191
|
type?: Column['type'];
|
|
192
192
|
};
|
|
193
|
-
export interface
|
|
194
|
-
increments(columnName?: string, options?:
|
|
195
|
-
bigIncrements(columnName?: string, options?:
|
|
193
|
+
export interface MySqlTableBuilder extends Knex.TableBuilder {
|
|
194
|
+
increments(columnName?: string, options?: MySqlIncrementOptions): Knex.ColumnBuilder;
|
|
195
|
+
bigIncrements(columnName?: string, options?: MySqlIncrementOptions): Knex.ColumnBuilder;
|
|
196
196
|
}
|
|
197
197
|
export {};
|