@mikro-orm/knex 6.4.6-dev.8 → 7.0.0-dev.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/AbstractSqlConnection.d.ts +15 -25
- package/AbstractSqlConnection.js +98 -126
- package/AbstractSqlDriver.d.ts +20 -13
- package/AbstractSqlDriver.js +73 -54
- package/AbstractSqlPlatform.d.ts +15 -3
- package/AbstractSqlPlatform.js +32 -11
- package/README.md +0 -2
- package/SqlEntityManager.d.ts +5 -6
- package/SqlEntityManager.js +5 -5
- package/SqlEntityRepository.d.ts +1 -6
- package/SqlEntityRepository.js +0 -6
- package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +12 -0
- package/dialects/mssql/MsSqlNativeQueryBuilder.js +161 -0
- package/dialects/mssql/index.d.ts +1 -1
- package/dialects/mssql/index.js +1 -1
- package/dialects/mysql/MySqlExceptionConverter.js +1 -0
- package/dialects/mysql/MySqlNativeQueryBuilder.d.ts +7 -0
- package/dialects/mysql/MySqlNativeQueryBuilder.js +81 -0
- package/dialects/mysql/MySqlPlatform.d.ts +5 -1
- package/dialects/mysql/MySqlPlatform.js +9 -1
- package/dialects/mysql/MySqlSchemaHelper.d.ts +6 -12
- package/dialects/mysql/MySqlSchemaHelper.js +42 -75
- package/dialects/mysql/index.d.ts +1 -3
- package/dialects/mysql/index.js +1 -3
- package/dialects/postgresql/PostgreSqlNativeQueryBuilder.d.ts +5 -0
- package/dialects/postgresql/PostgreSqlNativeQueryBuilder.js +12 -0
- package/dialects/postgresql/index.d.ts +1 -1
- package/dialects/postgresql/index.js +1 -1
- package/dialects/sqlite/BaseSqliteConnection.d.ts +0 -5
- package/dialects/sqlite/BaseSqliteConnection.js +4 -42
- package/dialects/sqlite/BaseSqlitePlatform.d.ts +15 -3
- package/dialects/sqlite/BaseSqlitePlatform.js +20 -4
- package/dialects/sqlite/SqliteExceptionConverter.d.ts +9 -0
- package/dialects/sqlite/SqliteExceptionConverter.js +55 -0
- package/dialects/sqlite/SqliteNativeQueryBuilder.d.ts +6 -0
- package/dialects/sqlite/SqliteNativeQueryBuilder.js +15 -0
- package/dialects/sqlite/SqliteSchemaHelper.d.ts +38 -0
- package/dialects/sqlite/SqliteSchemaHelper.js +384 -0
- package/dialects/sqlite/index.d.ts +3 -5
- package/dialects/sqlite/index.js +3 -5
- package/index.d.ts +1 -2
- package/index.js +3 -5
- package/index.mjs +10 -13
- package/package.json +4 -18
- package/query/CriteriaNodeFactory.js +5 -5
- package/query/NativeQueryBuilder.d.ts +108 -0
- package/query/NativeQueryBuilder.js +429 -0
- package/query/ObjectCriteriaNode.js +3 -3
- package/query/QueryBuilder.d.ts +30 -34
- package/query/QueryBuilder.js +112 -123
- package/query/QueryBuilderHelper.d.ts +27 -23
- package/query/QueryBuilderHelper.js +174 -168
- package/query/ScalarCriteriaNode.js +4 -0
- package/query/index.d.ts +1 -0
- package/query/index.js +1 -0
- package/schema/DatabaseSchema.js +9 -6
- package/schema/DatabaseTable.d.ts +2 -1
- package/schema/DatabaseTable.js +9 -5
- package/schema/SchemaComparator.d.ts +1 -2
- package/schema/SchemaComparator.js +31 -18
- package/schema/SchemaHelper.d.ts +27 -33
- package/schema/SchemaHelper.js +294 -184
- package/schema/SqlSchemaGenerator.d.ts +3 -9
- package/schema/SqlSchemaGenerator.js +105 -229
- package/typings.d.ts +7 -17
- package/MonkeyPatchable.d.ts +0 -18
- package/MonkeyPatchable.js +0 -60
- package/dialects/mssql/MsSqlColumnCompiler.d.ts +0 -4
- package/dialects/mssql/MsSqlColumnCompiler.js +0 -10
- package/dialects/mssql/MsSqlKnexDialect.d.ts +0 -6
- package/dialects/mssql/MsSqlKnexDialect.js +0 -22
- package/dialects/mssql/MsSqlQueryCompiler.d.ts +0 -12
- package/dialects/mssql/MsSqlQueryCompiler.js +0 -94
- package/dialects/mssql/MsSqlTableCompiler.d.ts +0 -9
- package/dialects/mssql/MsSqlTableCompiler.js +0 -40
- package/dialects/mysql/MariaDbKnexDialect.d.ts +0 -6
- package/dialects/mysql/MariaDbKnexDialect.js +0 -16
- package/dialects/mysql/MySqlColumnCompiler.d.ts +0 -7
- package/dialects/mysql/MySqlColumnCompiler.js +0 -26
- package/dialects/mysql/MySqlConnection.d.ts +0 -8
- package/dialects/mysql/MySqlConnection.js +0 -43
- package/dialects/mysql/MySqlKnexDialect.d.ts +0 -5
- package/dialects/mysql/MySqlKnexDialect.js +0 -17
- package/dialects/mysql/MySqlQueryCompiler.d.ts +0 -5
- package/dialects/mysql/MySqlQueryCompiler.js +0 -23
- package/dialects/postgresql/PostgreSqlKnexDialect.d.ts +0 -7
- package/dialects/postgresql/PostgreSqlKnexDialect.js +0 -20
- package/dialects/postgresql/PostgreSqlQueryCompiler.d.ts +0 -4
- package/dialects/postgresql/PostgreSqlQueryCompiler.js +0 -13
- package/dialects/postgresql/PostgreSqlTableCompiler.d.ts +0 -11
- package/dialects/postgresql/PostgreSqlTableCompiler.js +0 -89
- package/dialects/sqlite/BaseSqliteSchemaHelper.d.ts +0 -28
- package/dialects/sqlite/BaseSqliteSchemaHelper.js +0 -200
- package/dialects/sqlite/BetterSqliteKnexDialect.d.ts +0 -5
- package/dialects/sqlite/BetterSqliteKnexDialect.js +0 -15
- package/dialects/sqlite/LibSqlKnexDialect.d.ts +0 -11
- package/dialects/sqlite/LibSqlKnexDialect.js +0 -85
- package/dialects/sqlite/SqliteKnexDialect.d.ts +0 -8
- package/dialects/sqlite/SqliteKnexDialect.js +0 -67
- package/dialects/sqlite/SqliteTableCompiler.d.ts +0 -6
- package/dialects/sqlite/SqliteTableCompiler.js +0 -71
|
@@ -27,6 +27,7 @@ class MySqlExceptionConverter extends core_1.ExceptionConverter {
|
|
|
27
27
|
case 1701:
|
|
28
28
|
return new core_1.ForeignKeyConstraintViolationException(exception);
|
|
29
29
|
case 3819:
|
|
30
|
+
case 4025:
|
|
30
31
|
return new core_1.CheckConstraintViolationException(exception);
|
|
31
32
|
case 1062:
|
|
32
33
|
case 1557:
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { NativeQueryBuilder } from '../../query/NativeQueryBuilder';
|
|
2
|
+
/** @internal */
|
|
3
|
+
export declare class MySqlNativeQueryBuilder extends NativeQueryBuilder {
|
|
4
|
+
protected compileInsert(): void;
|
|
5
|
+
protected addLockClause(): void;
|
|
6
|
+
protected addOnConflictClause(): void;
|
|
7
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MySqlNativeQueryBuilder = void 0;
|
|
4
|
+
const core_1 = require("@mikro-orm/core");
|
|
5
|
+
const NativeQueryBuilder_1 = require("../../query/NativeQueryBuilder");
|
|
6
|
+
/** @internal */
|
|
7
|
+
class MySqlNativeQueryBuilder extends NativeQueryBuilder_1.NativeQueryBuilder {
|
|
8
|
+
compileInsert() {
|
|
9
|
+
if (!this.options.data) {
|
|
10
|
+
throw new Error('No data provided');
|
|
11
|
+
}
|
|
12
|
+
this.parts.push('insert');
|
|
13
|
+
if (this.options.onConflict?.ignore) {
|
|
14
|
+
this.parts.push('ignore');
|
|
15
|
+
}
|
|
16
|
+
this.addHintComment();
|
|
17
|
+
this.parts.push(`into ${this.getTableName()}`);
|
|
18
|
+
if (Object.keys(this.options.data).length === 0) {
|
|
19
|
+
this.parts.push('default values');
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const parts = this.processInsertData();
|
|
23
|
+
this.parts.push(parts.join(', '));
|
|
24
|
+
}
|
|
25
|
+
addLockClause() {
|
|
26
|
+
if (!this.options.lockMode) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const map = {
|
|
30
|
+
[core_1.LockMode.PESSIMISTIC_READ]: 'lock in share mode',
|
|
31
|
+
[core_1.LockMode.PESSIMISTIC_WRITE]: 'for update',
|
|
32
|
+
[core_1.LockMode.PESSIMISTIC_PARTIAL_WRITE]: 'for update skip locked',
|
|
33
|
+
[core_1.LockMode.PESSIMISTIC_WRITE_OR_FAIL]: 'for update nowait',
|
|
34
|
+
[core_1.LockMode.PESSIMISTIC_PARTIAL_READ]: 'lock in share mode skip locked',
|
|
35
|
+
[core_1.LockMode.PESSIMISTIC_READ_OR_FAIL]: 'lock in share mode nowait',
|
|
36
|
+
};
|
|
37
|
+
if (this.options.lockMode !== core_1.LockMode.OPTIMISTIC) {
|
|
38
|
+
this.parts.push(map[this.options.lockMode]);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
addOnConflictClause() {
|
|
42
|
+
const clause = this.options.onConflict;
|
|
43
|
+
if (!clause || clause.ignore) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (clause.merge) {
|
|
47
|
+
this.parts.push('on duplicate key update');
|
|
48
|
+
if (core_1.Utils.isObject(clause.merge)) {
|
|
49
|
+
const fields = Object.keys(clause.merge).map(field => {
|
|
50
|
+
this.params.push(clause.merge[field]);
|
|
51
|
+
return `${this.quote(field)} = ?`;
|
|
52
|
+
});
|
|
53
|
+
this.parts.push(fields.join(', '));
|
|
54
|
+
}
|
|
55
|
+
else if (clause.merge.length === 0) {
|
|
56
|
+
const dataAsArray = core_1.Utils.asArray(this.options.data);
|
|
57
|
+
const keys = Object.keys(dataAsArray[0]);
|
|
58
|
+
this.parts.push(keys.map(key => `${this.quote(key)} = values(${this.quote(key)})`).join(', '));
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const fields = clause.merge.map(key => `${this.quote(key)} = values(${this.quote(key)})`);
|
|
62
|
+
this.parts.push(fields.join(', '));
|
|
63
|
+
}
|
|
64
|
+
if (clause.where) {
|
|
65
|
+
this.parts.push(`where ${clause.where.sql}`);
|
|
66
|
+
this.params.push(...clause.where.params);
|
|
67
|
+
}
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this.parts.push('on conflict');
|
|
71
|
+
if (clause.fields instanceof core_1.RawQueryFragment) {
|
|
72
|
+
this.parts.push(clause.fields.sql);
|
|
73
|
+
this.params.push(...clause.fields.params);
|
|
74
|
+
}
|
|
75
|
+
else if (clause.fields.length > 0) {
|
|
76
|
+
const fields = clause.fields.map(field => this.quote(field));
|
|
77
|
+
this.parts.push(`(${fields.join(', ')})`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.MySqlNativeQueryBuilder = MySqlNativeQueryBuilder;
|
|
@@ -3,6 +3,7 @@ import { MySqlSchemaHelper } from './MySqlSchemaHelper';
|
|
|
3
3
|
import { MySqlExceptionConverter } from './MySqlExceptionConverter';
|
|
4
4
|
import { AbstractSqlPlatform } from '../../AbstractSqlPlatform';
|
|
5
5
|
import type { IndexDef } from '../../typings';
|
|
6
|
+
import { MySqlNativeQueryBuilder } from './MySqlNativeQueryBuilder';
|
|
6
7
|
export declare class MySqlPlatform extends AbstractSqlPlatform {
|
|
7
8
|
protected readonly schemaHelper: MySqlSchemaHelper;
|
|
8
9
|
protected readonly exceptionConverter: MySqlExceptionConverter;
|
|
@@ -12,11 +13,13 @@ export declare class MySqlPlatform extends AbstractSqlPlatform {
|
|
|
12
13
|
readonly "desc nulls first": "is not null";
|
|
13
14
|
readonly "desc nulls last": "is null";
|
|
14
15
|
};
|
|
16
|
+
/** @internal */
|
|
17
|
+
createNativeQueryBuilder(): MySqlNativeQueryBuilder;
|
|
15
18
|
getDefaultCharset(): string;
|
|
16
19
|
convertJsonToDatabaseValue(value: unknown, context?: TransformContext): unknown;
|
|
17
20
|
getJsonIndexDefinition(index: IndexDef): string[];
|
|
18
21
|
getBooleanTypeDeclarationSQL(): string;
|
|
19
|
-
normalizeColumnType(type: string, options
|
|
22
|
+
normalizeColumnType(type: string, options: {
|
|
20
23
|
length?: number;
|
|
21
24
|
precision?: number;
|
|
22
25
|
scale?: number;
|
|
@@ -34,4 +37,5 @@ export declare class MySqlPlatform extends AbstractSqlPlatform {
|
|
|
34
37
|
getFullTextWhereClause(): string;
|
|
35
38
|
getFullTextIndexExpression(indexName: string, schemaName: string | undefined, tableName: string, columns: SimpleColumnMeta[]): string;
|
|
36
39
|
getOrderByExpression(column: string, direction: string): string[];
|
|
40
|
+
getDefaultClientUrl(): string;
|
|
37
41
|
}
|
|
@@ -5,6 +5,7 @@ const core_1 = require("@mikro-orm/core");
|
|
|
5
5
|
const MySqlSchemaHelper_1 = require("./MySqlSchemaHelper");
|
|
6
6
|
const MySqlExceptionConverter_1 = require("./MySqlExceptionConverter");
|
|
7
7
|
const AbstractSqlPlatform_1 = require("../../AbstractSqlPlatform");
|
|
8
|
+
const MySqlNativeQueryBuilder_1 = require("./MySqlNativeQueryBuilder");
|
|
8
9
|
class MySqlPlatform extends AbstractSqlPlatform_1.AbstractSqlPlatform {
|
|
9
10
|
schemaHelper = new MySqlSchemaHelper_1.MySqlSchemaHelper(this);
|
|
10
11
|
exceptionConverter = new MySqlExceptionConverter_1.MySqlExceptionConverter();
|
|
@@ -14,6 +15,10 @@ class MySqlPlatform extends AbstractSqlPlatform_1.AbstractSqlPlatform {
|
|
|
14
15
|
[core_1.QueryOrder.desc_nulls_first]: 'is not null',
|
|
15
16
|
[core_1.QueryOrder.desc_nulls_last]: 'is null',
|
|
16
17
|
};
|
|
18
|
+
/** @internal */
|
|
19
|
+
createNativeQueryBuilder() {
|
|
20
|
+
return new MySqlNativeQueryBuilder_1.MySqlNativeQueryBuilder(this);
|
|
21
|
+
}
|
|
17
22
|
getDefaultCharset() {
|
|
18
23
|
return 'utf8mb4';
|
|
19
24
|
}
|
|
@@ -36,7 +41,7 @@ class MySqlPlatform extends AbstractSqlPlatform_1.AbstractSqlPlatform {
|
|
|
36
41
|
getBooleanTypeDeclarationSQL() {
|
|
37
42
|
return 'tinyint(1)';
|
|
38
43
|
}
|
|
39
|
-
normalizeColumnType(type, options
|
|
44
|
+
normalizeColumnType(type, options) {
|
|
40
45
|
const simpleType = this.extractSimpleType(type);
|
|
41
46
|
if (['decimal', 'numeric'].includes(simpleType)) {
|
|
42
47
|
return this.getDecimalTypeDeclarationSQL(options);
|
|
@@ -94,5 +99,8 @@ class MySqlPlatform extends AbstractSqlPlatform_1.AbstractSqlPlatform {
|
|
|
94
99
|
ret.push(`${column} ${dir.replace(/(\s|nulls|first|last)*/gi, '')}`);
|
|
95
100
|
return ret;
|
|
96
101
|
}
|
|
102
|
+
getDefaultClientUrl() {
|
|
103
|
+
return 'mysql://root@127.0.0.1:3306';
|
|
104
|
+
}
|
|
97
105
|
}
|
|
98
106
|
exports.MySqlPlatform = MySqlPlatform;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { Knex } from 'knex';
|
|
2
|
-
import type { CheckDef, Column, IndexDef, TableDifference, Table, ForeignKey, MySqlTableBuilder } from '../../typings';
|
|
3
1
|
import { type Dictionary, type Type } from '@mikro-orm/core';
|
|
2
|
+
import type { CheckDef, Column, IndexDef, TableDifference, Table, ForeignKey } from '../../typings';
|
|
4
3
|
import type { AbstractSqlConnection } from '../../AbstractSqlConnection';
|
|
5
4
|
import { SchemaHelper } from '../../schema/SchemaHelper';
|
|
6
5
|
import type { DatabaseSchema } from '../../schema/DatabaseSchema';
|
|
@@ -15,28 +14,23 @@ export declare class MySqlSchemaHelper extends SchemaHelper {
|
|
|
15
14
|
getSchemaBeginning(charset: string, disableForeignKeys?: boolean): string;
|
|
16
15
|
disableForeignKeysSQL(): string;
|
|
17
16
|
enableForeignKeysSQL(): string;
|
|
18
|
-
finalizeTable(table:
|
|
17
|
+
finalizeTable(table: DatabaseTable, charset: string, collate?: string): string;
|
|
19
18
|
getListTablesSQL(): string;
|
|
20
19
|
loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[]): Promise<void>;
|
|
21
20
|
getAllIndexes(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<IndexDef[]>>;
|
|
21
|
+
getCreateIndexSQL(tableName: string, index: IndexDef, partialExpression?: boolean): string;
|
|
22
22
|
getAllColumns(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<Column[]>>;
|
|
23
23
|
getAllChecks(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<CheckDef[]>>;
|
|
24
24
|
getAllForeignKeys(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<Dictionary<ForeignKey>>>;
|
|
25
|
-
getPreAlterTable(tableDiff: TableDifference, safe: boolean): string;
|
|
26
|
-
createTableColumn(table: MySqlTableBuilder, column: Column, fromTable: DatabaseTable, changedProperties?: Set<string>, alter?: boolean): Knex.ColumnBuilder | undefined;
|
|
27
|
-
configureColumnDefault(column: Column, col: Knex.ColumnBuilder, knex: Knex, changedProperties?: Set<string>): Knex.ColumnBuilder;
|
|
25
|
+
getPreAlterTable(tableDiff: TableDifference, safe: boolean): string[];
|
|
28
26
|
getRenameColumnSQL(tableName: string, oldColumnName: string, to: Column): string;
|
|
29
|
-
getRenameIndexSQL(tableName: string, index: IndexDef, oldIndexName: string): string;
|
|
27
|
+
getRenameIndexSQL(tableName: string, index: IndexDef, oldIndexName: string): string[];
|
|
30
28
|
getChangeColumnCommentSQL(tableName: string, to: Column, schemaName?: string): string;
|
|
29
|
+
alterTableColumn(column: Column, table: DatabaseTable, changedProperties: Set<string>): string[];
|
|
31
30
|
private getColumnDeclarationSQL;
|
|
32
|
-
getForeignKeysSQL(tableName: string, schemaName?: string): string;
|
|
33
31
|
getAllEnumDefinitions(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<Dictionary<string[]>>>;
|
|
34
32
|
private supportsCheckConstraints;
|
|
35
33
|
protected getChecksSQL(tables: Table[]): string;
|
|
36
|
-
getChecks(connection: AbstractSqlConnection, tableName: string, schemaName: string, columns?: Column[]): Promise<CheckDef[]>;
|
|
37
|
-
getEnumDefinitions(connection: AbstractSqlConnection, checks: CheckDef[], tableName: string, schemaName?: string): Promise<Dictionary<string[]>>;
|
|
38
|
-
getColumns(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<Column[]>;
|
|
39
|
-
getIndexes(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<IndexDef[]>;
|
|
40
34
|
normalizeDefaultValue(defaultValue: string, length: number): string | number;
|
|
41
35
|
protected wrap(val: string | null | undefined, type: Type<unknown>): string | null | undefined;
|
|
42
36
|
}
|
|
@@ -23,11 +23,15 @@ class MySqlSchemaHelper extends SchemaHelper_1.SchemaHelper {
|
|
|
23
23
|
return 'set foreign_key_checks = 1;';
|
|
24
24
|
}
|
|
25
25
|
finalizeTable(table, charset, collate) {
|
|
26
|
-
|
|
27
|
-
table.charset(charset);
|
|
26
|
+
let sql = ` default character set ${charset}`;
|
|
28
27
|
if (collate) {
|
|
29
|
-
|
|
28
|
+
sql += ` collate ${collate}`;
|
|
30
29
|
}
|
|
30
|
+
sql += ' engine = InnoDB';
|
|
31
|
+
if (table.comment) {
|
|
32
|
+
sql += ` comment = ${this.platform.quoteValue(table.comment)}`;
|
|
33
|
+
}
|
|
34
|
+
return sql;
|
|
31
35
|
}
|
|
32
36
|
getListTablesSQL() {
|
|
33
37
|
return `select table_name as table_name, nullif(table_schema, schema()) as schema_name, table_comment as table_comment from information_schema.tables where table_type = 'BASE TABLE' and table_schema = schema()`;
|
|
@@ -76,6 +80,25 @@ class MySqlSchemaHelper extends SchemaHelper_1.SchemaHelper {
|
|
|
76
80
|
}
|
|
77
81
|
return ret;
|
|
78
82
|
}
|
|
83
|
+
getCreateIndexSQL(tableName, index, partialExpression = false) {
|
|
84
|
+
/* istanbul ignore next */
|
|
85
|
+
if (index.expression && !partialExpression) {
|
|
86
|
+
return index.expression;
|
|
87
|
+
}
|
|
88
|
+
tableName = this.quote(tableName);
|
|
89
|
+
const keyName = this.quote(index.keyName);
|
|
90
|
+
const sql = `alter table ${tableName} add ${index.unique ? 'unique' : 'index'} ${keyName} `;
|
|
91
|
+
if (index.expression && partialExpression) {
|
|
92
|
+
return `${sql}(${index.expression})`;
|
|
93
|
+
}
|
|
94
|
+
// JSON columns can have unique index but not unique constraint, and we need to distinguish those, so we can properly drop them
|
|
95
|
+
if (index.columnNames.some(column => column.includes('.'))) {
|
|
96
|
+
const columns = this.platform.getJsonIndexDefinition(index);
|
|
97
|
+
const sql = `alter table ${tableName} add ${index.unique ? 'unique ' : ''}index ${keyName} `;
|
|
98
|
+
return `${sql}(${columns.join(', ')})`;
|
|
99
|
+
}
|
|
100
|
+
return `${sql}(${index.columnNames.map(c => this.quote(c)).join(', ')})`;
|
|
101
|
+
}
|
|
79
102
|
async getAllColumns(connection, tables) {
|
|
80
103
|
const sql = `select table_name as table_name,
|
|
81
104
|
nullif(table_schema, schema()) as schema_name,
|
|
@@ -171,81 +194,45 @@ class MySqlSchemaHelper extends SchemaHelper_1.SchemaHelper {
|
|
|
171
194
|
// Dropping primary keys requires to unset autoincrement attribute on the particular column first.
|
|
172
195
|
const pk = Object.values(tableDiff.removedIndexes).find(idx => idx.primary);
|
|
173
196
|
if (!pk || safe) {
|
|
174
|
-
return
|
|
197
|
+
return [];
|
|
175
198
|
}
|
|
176
199
|
return pk.columnNames
|
|
177
200
|
.filter(col => tableDiff.fromTable.hasColumn(col))
|
|
178
201
|
.map(col => tableDiff.fromTable.getColumn(col))
|
|
179
202
|
.filter(col => col.autoincrement)
|
|
180
|
-
.map(col => `alter table \`${tableDiff.name}\` modify \`${col.name}\` ${this.getColumnDeclarationSQL({ ...col, autoincrement: false })}`)
|
|
181
|
-
.join(';\n');
|
|
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
|
-
}
|
|
201
|
-
configureColumnDefault(column, col, knex, changedProperties) {
|
|
202
|
-
if (changedProperties || column.default !== undefined) {
|
|
203
|
-
if (column.default == null) {
|
|
204
|
-
col.defaultTo(null);
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
col.defaultTo(knex.raw(column.default + (column.extra ? ' ' + column.extra : '')));
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
return col;
|
|
203
|
+
.map(col => `alter table \`${tableDiff.name}\` modify \`${col.name}\` ${this.getColumnDeclarationSQL({ ...col, autoincrement: false })}`);
|
|
211
204
|
}
|
|
212
205
|
getRenameColumnSQL(tableName, oldColumnName, to) {
|
|
213
|
-
tableName = this.
|
|
214
|
-
oldColumnName = this.
|
|
215
|
-
const columnName = this.
|
|
206
|
+
tableName = this.quote(tableName);
|
|
207
|
+
oldColumnName = this.quote(oldColumnName);
|
|
208
|
+
const columnName = this.quote(to.name);
|
|
216
209
|
return `alter table ${tableName} change ${oldColumnName} ${columnName} ${this.getColumnDeclarationSQL(to)}`;
|
|
217
210
|
}
|
|
218
211
|
getRenameIndexSQL(tableName, index, oldIndexName) {
|
|
219
|
-
tableName = this.
|
|
220
|
-
oldIndexName = this.
|
|
221
|
-
const keyName = this.
|
|
222
|
-
return `alter table ${tableName} rename index ${oldIndexName} to ${keyName}
|
|
212
|
+
tableName = this.quote(tableName);
|
|
213
|
+
oldIndexName = this.quote(oldIndexName);
|
|
214
|
+
const keyName = this.quote(index.keyName);
|
|
215
|
+
return [`alter table ${tableName} rename index ${oldIndexName} to ${keyName}`];
|
|
223
216
|
}
|
|
224
217
|
getChangeColumnCommentSQL(tableName, to, schemaName) {
|
|
225
|
-
tableName = this.
|
|
226
|
-
const columnName = this.
|
|
218
|
+
tableName = this.quote(tableName);
|
|
219
|
+
const columnName = this.quote(to.name);
|
|
227
220
|
return `alter table ${tableName} modify ${columnName} ${this.getColumnDeclarationSQL(to)}`;
|
|
228
221
|
}
|
|
229
|
-
|
|
222
|
+
alterTableColumn(column, table, changedProperties) {
|
|
223
|
+
const col = this.createTableColumn(column, table, changedProperties);
|
|
224
|
+
return [`alter table ${table.getQuotedName()} modify ${col}`];
|
|
225
|
+
}
|
|
226
|
+
getColumnDeclarationSQL(col) {
|
|
230
227
|
let ret = col.type;
|
|
231
228
|
ret += col.unsigned ? ' unsigned' : '';
|
|
232
229
|
ret += col.autoincrement ? ' auto_increment' : '';
|
|
233
230
|
ret += ' ';
|
|
234
231
|
ret += col.nullable ? 'null' : 'not null';
|
|
235
232
|
ret += col.default ? ' default ' + col.default : '';
|
|
236
|
-
if (addPrimary && col.primary) {
|
|
237
|
-
ret += ' primary key';
|
|
238
|
-
}
|
|
239
233
|
ret += col.comment ? ` comment ${this.platform.quoteValue(col.comment)}` : '';
|
|
240
234
|
return ret;
|
|
241
235
|
}
|
|
242
|
-
/* istanbul ignore next kept for BC */
|
|
243
|
-
getForeignKeysSQL(tableName, schemaName) {
|
|
244
|
-
return `select distinct k.constraint_name as constraint_name, k.column_name as column_name, k.referenced_table_name as referenced_table_name, k.referenced_column_name as referenced_column_name, c.update_rule as update_rule, c.delete_rule as delete_rule `
|
|
245
|
-
+ `from information_schema.key_column_usage k `
|
|
246
|
-
+ `inner join information_schema.referential_constraints c on c.constraint_name = k.constraint_name and c.table_name = '${tableName}' `
|
|
247
|
-
+ `where k.table_name = '${tableName}' and k.table_schema = database() and c.constraint_schema = database() and k.referenced_column_name is not null`;
|
|
248
|
-
}
|
|
249
236
|
async getAllEnumDefinitions(connection, tables) {
|
|
250
237
|
const sql = `select column_name as column_name, column_type as column_type, table_name as table_name
|
|
251
238
|
from information_schema.columns
|
|
@@ -275,26 +262,6 @@ class MySqlSchemaHelper extends SchemaHelper_1.SchemaHelper {
|
|
|
275
262
|
where tc.table_name in (${tables.map(t => this.platform.quoteValue(t.table_name))}) and tc.constraint_schema = database()
|
|
276
263
|
order by tc.constraint_name`;
|
|
277
264
|
}
|
|
278
|
-
/* istanbul ignore next */
|
|
279
|
-
async getChecks(connection, tableName, schemaName, columns) {
|
|
280
|
-
const res = await this.getAllChecks(connection, [{ table_name: tableName, schema_name: schemaName }]);
|
|
281
|
-
return res[tableName];
|
|
282
|
-
}
|
|
283
|
-
/* istanbul ignore next */
|
|
284
|
-
async getEnumDefinitions(connection, checks, tableName, schemaName) {
|
|
285
|
-
const res = await this.getAllEnumDefinitions(connection, [{ table_name: tableName, schema_name: schemaName }]);
|
|
286
|
-
return res[tableName];
|
|
287
|
-
}
|
|
288
|
-
/* istanbul ignore next */
|
|
289
|
-
async getColumns(connection, tableName, schemaName) {
|
|
290
|
-
const res = await this.getAllColumns(connection, [{ table_name: tableName, schema_name: schemaName }]);
|
|
291
|
-
return res[tableName];
|
|
292
|
-
}
|
|
293
|
-
/* istanbul ignore next */
|
|
294
|
-
async getIndexes(connection, tableName, schemaName) {
|
|
295
|
-
const res = await this.getAllIndexes(connection, [{ table_name: tableName, schema_name: schemaName }]);
|
|
296
|
-
return res[tableName];
|
|
297
|
-
}
|
|
298
265
|
normalizeDefaultValue(defaultValue, length) {
|
|
299
266
|
return super.normalizeDefaultValue(defaultValue, length, MySqlSchemaHelper.DEFAULT_VALUES);
|
|
300
267
|
}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
export * from './MariaDbKnexDialect';
|
|
2
|
-
export * from './MySqlKnexDialect';
|
|
3
|
-
export * from './MySqlConnection';
|
|
4
1
|
export * from './MySqlExceptionConverter';
|
|
5
2
|
export * from './MySqlSchemaHelper';
|
|
6
3
|
export * from './MySqlPlatform';
|
|
4
|
+
export * from './MySqlNativeQueryBuilder';
|
package/dialects/mysql/index.js
CHANGED
|
@@ -14,9 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./MariaDbKnexDialect"), exports);
|
|
18
|
-
__exportStar(require("./MySqlKnexDialect"), exports);
|
|
19
|
-
__exportStar(require("./MySqlConnection"), exports);
|
|
20
17
|
__exportStar(require("./MySqlExceptionConverter"), exports);
|
|
21
18
|
__exportStar(require("./MySqlSchemaHelper"), exports);
|
|
22
19
|
__exportStar(require("./MySqlPlatform"), exports);
|
|
20
|
+
__exportStar(require("./MySqlNativeQueryBuilder"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PostgreSqlNativeQueryBuilder = void 0;
|
|
4
|
+
const NativeQueryBuilder_1 = require("../../query/NativeQueryBuilder");
|
|
5
|
+
/** @internal */
|
|
6
|
+
class PostgreSqlNativeQueryBuilder extends NativeQueryBuilder_1.NativeQueryBuilder {
|
|
7
|
+
compileTruncate() {
|
|
8
|
+
super.compileTruncate();
|
|
9
|
+
this.parts.push('restart identity cascade');
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.PostgreSqlNativeQueryBuilder = PostgreSqlNativeQueryBuilder;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './PostgreSqlNativeQueryBuilder';
|
|
@@ -14,4 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./PostgreSqlNativeQueryBuilder"), exports);
|
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import type { Knex } from 'knex';
|
|
2
1
|
import { AbstractSqlConnection } from '../../AbstractSqlConnection';
|
|
3
2
|
export declare abstract class BaseSqliteConnection extends AbstractSqlConnection {
|
|
4
3
|
connect(): Promise<void>;
|
|
5
|
-
getDefaultClientUrl(): string;
|
|
6
4
|
getClientUrl(): string;
|
|
7
|
-
loadFile(path: string): Promise<void>;
|
|
8
|
-
protected getKnexOptions(type: string): Knex.Config;
|
|
9
|
-
protected transformRawResult<T>(res: any, method: 'all' | 'get' | 'run'): T;
|
|
10
5
|
}
|
|
@@ -1,56 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BaseSqliteConnection = void 0;
|
|
4
|
-
const fs_extra_1 = require("fs-extra");
|
|
5
4
|
const node_path_1 = require("node:path");
|
|
5
|
+
const fs_extra_1 = require("fs-extra");
|
|
6
|
+
const kysely_1 = require("kysely");
|
|
6
7
|
const AbstractSqlConnection_1 = require("../../AbstractSqlConnection");
|
|
7
|
-
const core_1 = require("@mikro-orm/core");
|
|
8
8
|
class BaseSqliteConnection extends AbstractSqlConnection_1.AbstractSqlConnection {
|
|
9
9
|
async connect() {
|
|
10
|
-
|
|
10
|
+
await super.connect();
|
|
11
11
|
await (0, fs_extra_1.ensureDir)((0, node_path_1.dirname)(this.config.get('dbName')));
|
|
12
|
-
await this.client.raw('pragma foreign_keys = on');
|
|
13
|
-
}
|
|
14
|
-
getDefaultClientUrl() {
|
|
15
|
-
return '';
|
|
12
|
+
await this.client.executeQuery(kysely_1.CompiledQuery.raw('pragma foreign_keys = on'));
|
|
16
13
|
}
|
|
17
14
|
getClientUrl() {
|
|
18
15
|
return '';
|
|
19
16
|
}
|
|
20
|
-
async loadFile(path) {
|
|
21
|
-
const conn = await this.client.client.acquireConnection();
|
|
22
|
-
await conn.exec((await (0, fs_extra_1.readFile)(path)).toString());
|
|
23
|
-
await this.client.client.releaseConnection(conn);
|
|
24
|
-
}
|
|
25
|
-
getKnexOptions(type) {
|
|
26
|
-
return core_1.Utils.mergeConfig({
|
|
27
|
-
client: type,
|
|
28
|
-
connection: {
|
|
29
|
-
filename: this.config.get('dbName'),
|
|
30
|
-
},
|
|
31
|
-
pool: this.config.get('pool'),
|
|
32
|
-
useNullAsDefault: true,
|
|
33
|
-
}, this.config.get('driverOptions'));
|
|
34
|
-
}
|
|
35
|
-
transformRawResult(res, method) {
|
|
36
|
-
if (method === 'get') {
|
|
37
|
-
return res[0];
|
|
38
|
-
}
|
|
39
|
-
if (method === 'all') {
|
|
40
|
-
return res;
|
|
41
|
-
}
|
|
42
|
-
if (Array.isArray(res)) {
|
|
43
|
-
return {
|
|
44
|
-
insertId: res[res.length - 1]?.id ?? 0,
|
|
45
|
-
affectedRows: res.length,
|
|
46
|
-
row: res[0],
|
|
47
|
-
rows: res,
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
return {
|
|
51
|
-
insertId: res.lastInsertRowid,
|
|
52
|
-
affectedRows: res.changes,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
17
|
}
|
|
56
18
|
exports.BaseSqliteConnection = BaseSqliteConnection;
|
|
@@ -1,12 +1,24 @@
|
|
|
1
|
-
import { type EntityProperty } from '@mikro-orm/core';
|
|
1
|
+
import { type EntityProperty, type IsolationLevel } from '@mikro-orm/core';
|
|
2
2
|
import { AbstractSqlPlatform } from '../../AbstractSqlPlatform';
|
|
3
|
+
import { SqliteNativeQueryBuilder } from './SqliteNativeQueryBuilder';
|
|
4
|
+
import { SqliteSchemaHelper } from './SqliteSchemaHelper';
|
|
5
|
+
import { SqliteExceptionConverter } from './SqliteExceptionConverter';
|
|
3
6
|
export declare abstract class BaseSqlitePlatform extends AbstractSqlPlatform {
|
|
7
|
+
protected readonly schemaHelper: SqliteSchemaHelper;
|
|
8
|
+
protected readonly exceptionConverter: SqliteExceptionConverter;
|
|
9
|
+
/** @internal */
|
|
10
|
+
createNativeQueryBuilder(): SqliteNativeQueryBuilder;
|
|
4
11
|
usesDefaultKeyword(): boolean;
|
|
5
12
|
usesReturningStatement(): boolean;
|
|
13
|
+
usesEnumCheckConstraints(): boolean;
|
|
6
14
|
getCurrentTimestampSQL(length: number): string;
|
|
7
15
|
getDateTimeTypeDeclarationSQL(column: {
|
|
8
16
|
length: number;
|
|
9
17
|
}): string;
|
|
18
|
+
getBeginTransactionSQL(options?: {
|
|
19
|
+
isolationLevel?: IsolationLevel;
|
|
20
|
+
readOnly?: boolean;
|
|
21
|
+
}): string[];
|
|
10
22
|
getEnumTypeDeclarationSQL(column: {
|
|
11
23
|
items?: unknown[];
|
|
12
24
|
fieldNames: string[];
|
|
@@ -37,7 +49,7 @@ export declare abstract class BaseSqlitePlatform extends AbstractSqlPlatform {
|
|
|
37
49
|
getVarcharTypeDeclarationSQL(column: {
|
|
38
50
|
length?: number;
|
|
39
51
|
}): string;
|
|
40
|
-
normalizeColumnType(type: string, options
|
|
52
|
+
normalizeColumnType(type: string, options: {
|
|
41
53
|
length?: number;
|
|
42
54
|
precision?: number;
|
|
43
55
|
scale?: number;
|
|
@@ -51,8 +63,8 @@ export declare abstract class BaseSqlitePlatform extends AbstractSqlPlatform {
|
|
|
51
63
|
*/
|
|
52
64
|
processDateProperty(value: unknown): string | number | Date;
|
|
53
65
|
getIndexName(tableName: string, columns: string[], type: 'index' | 'unique' | 'foreign' | 'primary' | 'sequence'): string;
|
|
54
|
-
getDefaultPrimaryName(tableName: string, columns: string[]): string;
|
|
55
66
|
supportsDownMigrations(): boolean;
|
|
67
|
+
supportsDeferredUniqueConstraints(): boolean;
|
|
56
68
|
getFullTextWhereClause(): string;
|
|
57
69
|
quoteVersionValue(value: Date | number, prop: EntityProperty): Date | string | number;
|
|
58
70
|
quoteValue(value: any): string;
|
|
@@ -3,19 +3,34 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.BaseSqlitePlatform = void 0;
|
|
4
4
|
const core_1 = require("@mikro-orm/core");
|
|
5
5
|
const AbstractSqlPlatform_1 = require("../../AbstractSqlPlatform");
|
|
6
|
+
const SqliteNativeQueryBuilder_1 = require("./SqliteNativeQueryBuilder");
|
|
7
|
+
const SqliteSchemaHelper_1 = require("./SqliteSchemaHelper");
|
|
8
|
+
const SqliteExceptionConverter_1 = require("./SqliteExceptionConverter");
|
|
6
9
|
class BaseSqlitePlatform extends AbstractSqlPlatform_1.AbstractSqlPlatform {
|
|
10
|
+
schemaHelper = new SqliteSchemaHelper_1.SqliteSchemaHelper(this);
|
|
11
|
+
exceptionConverter = new SqliteExceptionConverter_1.SqliteExceptionConverter();
|
|
12
|
+
/** @internal */
|
|
13
|
+
createNativeQueryBuilder() {
|
|
14
|
+
return new SqliteNativeQueryBuilder_1.SqliteNativeQueryBuilder(this);
|
|
15
|
+
}
|
|
7
16
|
usesDefaultKeyword() {
|
|
8
17
|
return false;
|
|
9
18
|
}
|
|
10
19
|
usesReturningStatement() {
|
|
11
20
|
return true;
|
|
12
21
|
}
|
|
22
|
+
usesEnumCheckConstraints() {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
13
25
|
getCurrentTimestampSQL(length) {
|
|
14
26
|
return super.getCurrentTimestampSQL(0);
|
|
15
27
|
}
|
|
16
28
|
getDateTimeTypeDeclarationSQL(column) {
|
|
17
29
|
return 'datetime';
|
|
18
30
|
}
|
|
31
|
+
getBeginTransactionSQL(options) {
|
|
32
|
+
return ['begin'];
|
|
33
|
+
}
|
|
19
34
|
getEnumTypeDeclarationSQL(column) {
|
|
20
35
|
if (column.items?.every(item => core_1.Utils.isString(item))) {
|
|
21
36
|
return 'text';
|
|
@@ -44,7 +59,7 @@ class BaseSqlitePlatform extends AbstractSqlPlatform_1.AbstractSqlPlatform {
|
|
|
44
59
|
getVarcharTypeDeclarationSQL(column) {
|
|
45
60
|
return 'text';
|
|
46
61
|
}
|
|
47
|
-
normalizeColumnType(type, options
|
|
62
|
+
normalizeColumnType(type, options) {
|
|
48
63
|
const simpleType = this.extractSimpleType(type);
|
|
49
64
|
if (['varchar', 'text'].includes(simpleType)) {
|
|
50
65
|
return this.getVarcharTypeDeclarationSQL(options);
|
|
@@ -72,12 +87,13 @@ class BaseSqlitePlatform extends AbstractSqlPlatform_1.AbstractSqlPlatform {
|
|
|
72
87
|
}
|
|
73
88
|
return super.getIndexName(tableName, columns, type);
|
|
74
89
|
}
|
|
75
|
-
|
|
76
|
-
return 'primary';
|
|
77
|
-
}
|
|
90
|
+
// TODO enable once tests are green
|
|
78
91
|
supportsDownMigrations() {
|
|
79
92
|
return false;
|
|
80
93
|
}
|
|
94
|
+
supportsDeferredUniqueConstraints() {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
81
97
|
getFullTextWhereClause() {
|
|
82
98
|
return `:column: match :query`;
|
|
83
99
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ExceptionConverter, type Dictionary, type DriverException } from '@mikro-orm/core';
|
|
2
|
+
export declare class SqliteExceptionConverter extends ExceptionConverter {
|
|
3
|
+
/**
|
|
4
|
+
* @inheritDoc
|
|
5
|
+
* @link http://www.sqlite.org/c3ref/c_abort.html
|
|
6
|
+
* @link https://github.com/doctrine/dbal/blob/master/src/Driver/AbstractSQLiteDriver.php
|
|
7
|
+
*/
|
|
8
|
+
convertException(exception: Error & Dictionary): DriverException;
|
|
9
|
+
}
|