@mikro-orm/knex 6.1.13-dev.32 → 6.1.13-dev.34
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/index.d.ts +2 -0
- package/dialects/index.js +2 -0
- package/dialects/mysql/MariaDbKnexDialect.d.ts +6 -0
- package/dialects/mysql/MariaDbKnexDialect.js +16 -0
- package/dialects/mysql/MySqlColumnCompiler.d.ts +9 -0
- package/dialects/mysql/MySqlColumnCompiler.js +19 -0
- package/dialects/mysql/MySqlConnection.d.ts +8 -0
- package/dialects/mysql/MySqlConnection.js +43 -0
- package/dialects/mysql/MySqlExceptionConverter.d.ts +9 -0
- package/dialects/mysql/MySqlExceptionConverter.js +83 -0
- package/dialects/mysql/MySqlKnexDialect.d.ts +5 -0
- package/dialects/mysql/MySqlKnexDialect.js +21 -0
- package/dialects/mysql/MySqlPlatform.d.ts +31 -0
- package/dialects/mysql/MySqlPlatform.js +88 -0
- package/dialects/mysql/MySqlQueryCompiler.d.ts +5 -0
- package/dialects/mysql/MySqlQueryCompiler.js +23 -0
- package/dialects/mysql/MySqlSchemaHelper.d.ts +43 -0
- package/dialects/mysql/MySqlSchemaHelper.js +297 -0
- package/dialects/mysql/index.d.ts +6 -0
- package/dialects/mysql/index.js +22 -0
- package/dialects/postgresql/PostgreSqlKnexDialect.d.ts +6 -0
- package/dialects/postgresql/PostgreSqlKnexDialect.js +19 -0
- package/dialects/postgresql/PostgreSqlTableCompiler.d.ts +11 -0
- package/dialects/postgresql/PostgreSqlTableCompiler.js +89 -0
- package/dialects/postgresql/index.d.ts +1 -0
- package/dialects/postgresql/index.js +17 -0
- package/index.mjs +7 -0
- package/package.json +2 -2
- package/query/QueryBuilderHelper.js +1 -1
package/dialects/index.d.ts
CHANGED
package/dialects/index.js
CHANGED
|
@@ -15,4 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./mssql"), exports);
|
|
18
|
+
__exportStar(require("./mysql"), exports);
|
|
19
|
+
__exportStar(require("./postgresql"), exports);
|
|
18
20
|
__exportStar(require("./sqlite"), exports);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MariaDbKnexDialect = void 0;
|
|
4
|
+
const MySqlKnexDialect_1 = require("./MySqlKnexDialect");
|
|
5
|
+
class MariaDbKnexDialect extends MySqlKnexDialect_1.MySqlKnexDialect {
|
|
6
|
+
get driverName() {
|
|
7
|
+
return 'mariadb';
|
|
8
|
+
}
|
|
9
|
+
_driver() {
|
|
10
|
+
return require('mariadb/callback');
|
|
11
|
+
}
|
|
12
|
+
validateConnection(connection) {
|
|
13
|
+
return connection.isValid();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.MariaDbKnexDialect = MariaDbKnexDialect;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import BaseMySqlColumnCompiler from 'knex/lib/dialects/mysql/schema/mysql-columncompiler';
|
|
2
|
+
export declare class MySqlColumnCompiler extends BaseMySqlColumnCompiler {
|
|
3
|
+
increments(this: any, options?: {
|
|
4
|
+
primaryKey: boolean;
|
|
5
|
+
}): string;
|
|
6
|
+
bigincrements(this: any, options?: {
|
|
7
|
+
primaryKey: boolean;
|
|
8
|
+
}): string;
|
|
9
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MySqlColumnCompiler = void 0;
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
const mysql_columncompiler_1 = __importDefault(require("knex/lib/dialects/mysql/schema/mysql-columncompiler"));
|
|
9
|
+
class MySqlColumnCompiler extends mysql_columncompiler_1.default {
|
|
10
|
+
// we need the old behaviour to be able to add auto_increment to a column that is already PK
|
|
11
|
+
increments(options = { primaryKey: true }) {
|
|
12
|
+
return 'int unsigned not null auto_increment' + (this.tableCompiler._canBeAddPrimaryKey(options) ? ' primary key' : '');
|
|
13
|
+
}
|
|
14
|
+
/* istanbul ignore next */
|
|
15
|
+
bigincrements(options = { primaryKey: true }) {
|
|
16
|
+
return 'bigint unsigned not null auto_increment' + (this.tableCompiler._canBeAddPrimaryKey(options) ? ' primary key' : '');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.MySqlColumnCompiler = MySqlColumnCompiler;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Knex } from 'knex';
|
|
2
|
+
import { AbstractSqlConnection } from '../../AbstractSqlConnection';
|
|
3
|
+
export declare class MySqlConnection extends AbstractSqlConnection {
|
|
4
|
+
createKnex(): void;
|
|
5
|
+
getDefaultClientUrl(): string;
|
|
6
|
+
getConnectionOptions(): Knex.MySqlConnectionConfig;
|
|
7
|
+
protected transformRawResult<T>(res: any, method: 'all' | 'get' | 'run'): T;
|
|
8
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MySqlConnection = void 0;
|
|
4
|
+
const MySqlKnexDialect_1 = require("./MySqlKnexDialect");
|
|
5
|
+
const AbstractSqlConnection_1 = require("../../AbstractSqlConnection");
|
|
6
|
+
class MySqlConnection extends AbstractSqlConnection_1.AbstractSqlConnection {
|
|
7
|
+
createKnex() {
|
|
8
|
+
this.client = this.createKnexClient(MySqlKnexDialect_1.MySqlKnexDialect);
|
|
9
|
+
this.connected = true;
|
|
10
|
+
}
|
|
11
|
+
getDefaultClientUrl() {
|
|
12
|
+
return 'mysql://root@127.0.0.1:3306';
|
|
13
|
+
}
|
|
14
|
+
getConnectionOptions() {
|
|
15
|
+
const ret = super.getConnectionOptions();
|
|
16
|
+
if (this.config.get('multipleStatements')) {
|
|
17
|
+
ret.multipleStatements = this.config.get('multipleStatements');
|
|
18
|
+
}
|
|
19
|
+
if (this.config.get('forceUtcTimezone')) {
|
|
20
|
+
ret.timezone = 'Z';
|
|
21
|
+
}
|
|
22
|
+
if (this.config.get('timezone')) {
|
|
23
|
+
ret.timezone = this.config.get('timezone');
|
|
24
|
+
}
|
|
25
|
+
ret.supportBigNumbers = true;
|
|
26
|
+
ret.dateStrings = true;
|
|
27
|
+
return ret;
|
|
28
|
+
}
|
|
29
|
+
transformRawResult(res, method) {
|
|
30
|
+
if (method === 'run' && ['OkPacket', 'ResultSetHeader'].includes(res[0].constructor.name)) {
|
|
31
|
+
return {
|
|
32
|
+
insertId: res[0].insertId,
|
|
33
|
+
affectedRows: res[0].affectedRows,
|
|
34
|
+
rows: [],
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
if (method === 'get') {
|
|
38
|
+
return res[0][0];
|
|
39
|
+
}
|
|
40
|
+
return res[0];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.MySqlConnection = MySqlConnection;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ExceptionConverter, type Dictionary, type DriverException } from '@mikro-orm/core';
|
|
2
|
+
export declare class MySqlExceptionConverter extends ExceptionConverter {
|
|
3
|
+
/**
|
|
4
|
+
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
|
|
5
|
+
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
|
|
6
|
+
* @link https://github.com/doctrine/dbal/blob/master/src/Driver/AbstractMySQLDriver.php
|
|
7
|
+
*/
|
|
8
|
+
convertException(exception: Error & Dictionary): DriverException;
|
|
9
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MySqlExceptionConverter = void 0;
|
|
4
|
+
const core_1 = require("@mikro-orm/core");
|
|
5
|
+
class MySqlExceptionConverter extends core_1.ExceptionConverter {
|
|
6
|
+
/* istanbul ignore next */
|
|
7
|
+
/**
|
|
8
|
+
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
|
|
9
|
+
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
|
|
10
|
+
* @link https://github.com/doctrine/dbal/blob/master/src/Driver/AbstractMySQLDriver.php
|
|
11
|
+
*/
|
|
12
|
+
convertException(exception) {
|
|
13
|
+
switch (exception.errno) {
|
|
14
|
+
case 1213:
|
|
15
|
+
return new core_1.DeadlockException(exception);
|
|
16
|
+
case 1205:
|
|
17
|
+
return new core_1.LockWaitTimeoutException(exception);
|
|
18
|
+
case 1050:
|
|
19
|
+
return new core_1.TableExistsException(exception);
|
|
20
|
+
case 1051:
|
|
21
|
+
case 1146:
|
|
22
|
+
return new core_1.TableNotFoundException(exception);
|
|
23
|
+
case 1216:
|
|
24
|
+
case 1217:
|
|
25
|
+
case 1451:
|
|
26
|
+
case 1452:
|
|
27
|
+
case 1701:
|
|
28
|
+
return new core_1.ForeignKeyConstraintViolationException(exception);
|
|
29
|
+
case 3819:
|
|
30
|
+
return new core_1.CheckConstraintViolationException(exception);
|
|
31
|
+
case 1062:
|
|
32
|
+
case 1557:
|
|
33
|
+
case 1569:
|
|
34
|
+
case 1586:
|
|
35
|
+
return new core_1.UniqueConstraintViolationException(exception);
|
|
36
|
+
case 1054:
|
|
37
|
+
case 1166:
|
|
38
|
+
case 1611:
|
|
39
|
+
return new core_1.InvalidFieldNameException(exception);
|
|
40
|
+
case 1052:
|
|
41
|
+
case 1060:
|
|
42
|
+
case 1110:
|
|
43
|
+
return new core_1.NonUniqueFieldNameException(exception);
|
|
44
|
+
case 1064:
|
|
45
|
+
case 1149:
|
|
46
|
+
case 1287:
|
|
47
|
+
case 1341:
|
|
48
|
+
case 1342:
|
|
49
|
+
case 1343:
|
|
50
|
+
case 1344:
|
|
51
|
+
case 1382:
|
|
52
|
+
case 1479:
|
|
53
|
+
case 1541:
|
|
54
|
+
case 1554:
|
|
55
|
+
case 1626:
|
|
56
|
+
return new core_1.SyntaxErrorException(exception);
|
|
57
|
+
case 1044:
|
|
58
|
+
case 1045:
|
|
59
|
+
case 1046:
|
|
60
|
+
case 1049:
|
|
61
|
+
case 1095:
|
|
62
|
+
case 1142:
|
|
63
|
+
case 1143:
|
|
64
|
+
case 1227:
|
|
65
|
+
case 1370:
|
|
66
|
+
case 1429:
|
|
67
|
+
case 2002:
|
|
68
|
+
case 2005:
|
|
69
|
+
return new core_1.ConnectionException(exception);
|
|
70
|
+
case 1048:
|
|
71
|
+
case 1121:
|
|
72
|
+
case 1138:
|
|
73
|
+
case 1171:
|
|
74
|
+
case 1252:
|
|
75
|
+
case 1263:
|
|
76
|
+
case 1364:
|
|
77
|
+
case 1566:
|
|
78
|
+
return new core_1.NotNullConstraintViolationException(exception);
|
|
79
|
+
}
|
|
80
|
+
return super.convertException(exception);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.MySqlExceptionConverter = MySqlExceptionConverter;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MySqlKnexDialect = void 0;
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
const mysql2_1 = __importDefault(require("knex/lib/dialects/mysql2"));
|
|
9
|
+
const MySqlQueryCompiler_1 = require("./MySqlQueryCompiler");
|
|
10
|
+
const MySqlColumnCompiler_1 = require("./MySqlColumnCompiler");
|
|
11
|
+
class MySqlKnexDialect extends mysql2_1.default {
|
|
12
|
+
queryCompiler() {
|
|
13
|
+
// eslint-disable-next-line prefer-rest-params
|
|
14
|
+
return new MySqlQueryCompiler_1.MySqlQueryCompiler(this, ...arguments);
|
|
15
|
+
}
|
|
16
|
+
columnCompiler() {
|
|
17
|
+
// eslint-disable-next-line prefer-rest-params
|
|
18
|
+
return new MySqlColumnCompiler_1.MySqlColumnCompiler(this, ...arguments);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.MySqlKnexDialect = MySqlKnexDialect;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type SimpleColumnMeta, type Type, type TransformContext } from '@mikro-orm/core';
|
|
2
|
+
import { MySqlSchemaHelper } from './MySqlSchemaHelper';
|
|
3
|
+
import { MySqlExceptionConverter } from './MySqlExceptionConverter';
|
|
4
|
+
import { AbstractSqlPlatform } from '../../AbstractSqlPlatform';
|
|
5
|
+
import type { IndexDef } from '../../typings';
|
|
6
|
+
export declare class MySqlPlatform extends AbstractSqlPlatform {
|
|
7
|
+
protected readonly schemaHelper: MySqlSchemaHelper;
|
|
8
|
+
protected readonly exceptionConverter: MySqlExceptionConverter;
|
|
9
|
+
protected readonly ORDER_BY_NULLS_TRANSLATE: {
|
|
10
|
+
readonly "asc nulls first": "is not null";
|
|
11
|
+
readonly "asc nulls last": "is null";
|
|
12
|
+
readonly "desc nulls first": "is not null";
|
|
13
|
+
readonly "desc nulls last": "is null";
|
|
14
|
+
};
|
|
15
|
+
getDefaultCharset(): string;
|
|
16
|
+
convertJsonToDatabaseValue(value: unknown, context?: TransformContext): unknown;
|
|
17
|
+
getJsonIndexDefinition(index: IndexDef): string[];
|
|
18
|
+
getBooleanTypeDeclarationSQL(): string;
|
|
19
|
+
getDefaultMappedType(type: string): Type<unknown>;
|
|
20
|
+
supportsUnsigned(): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Returns the default name of index for the given columns
|
|
23
|
+
* cannot go past 64 character length for identifiers in MySQL
|
|
24
|
+
*/
|
|
25
|
+
getIndexName(tableName: string, columns: string[], type: 'index' | 'unique' | 'foreign' | 'primary' | 'sequence'): string;
|
|
26
|
+
getDefaultPrimaryName(tableName: string, columns: string[]): string;
|
|
27
|
+
supportsCreatingFullTextIndex(): boolean;
|
|
28
|
+
getFullTextWhereClause(): string;
|
|
29
|
+
getFullTextIndexExpression(indexName: string, schemaName: string | undefined, tableName: string, columns: SimpleColumnMeta[]): string;
|
|
30
|
+
getOrderByExpression(column: string, direction: string): string[];
|
|
31
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MySqlPlatform = void 0;
|
|
4
|
+
const core_1 = require("@mikro-orm/core");
|
|
5
|
+
const MySqlSchemaHelper_1 = require("./MySqlSchemaHelper");
|
|
6
|
+
const MySqlExceptionConverter_1 = require("./MySqlExceptionConverter");
|
|
7
|
+
const AbstractSqlPlatform_1 = require("../../AbstractSqlPlatform");
|
|
8
|
+
class MySqlPlatform extends AbstractSqlPlatform_1.AbstractSqlPlatform {
|
|
9
|
+
schemaHelper = new MySqlSchemaHelper_1.MySqlSchemaHelper(this);
|
|
10
|
+
exceptionConverter = new MySqlExceptionConverter_1.MySqlExceptionConverter();
|
|
11
|
+
ORDER_BY_NULLS_TRANSLATE = {
|
|
12
|
+
[core_1.QueryOrder.asc_nulls_first]: 'is not null',
|
|
13
|
+
[core_1.QueryOrder.asc_nulls_last]: 'is null',
|
|
14
|
+
[core_1.QueryOrder.desc_nulls_first]: 'is not null',
|
|
15
|
+
[core_1.QueryOrder.desc_nulls_last]: 'is null',
|
|
16
|
+
};
|
|
17
|
+
getDefaultCharset() {
|
|
18
|
+
return 'utf8mb4';
|
|
19
|
+
}
|
|
20
|
+
convertJsonToDatabaseValue(value, context) {
|
|
21
|
+
if (context?.mode === 'query') {
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
return JSON.stringify(value);
|
|
25
|
+
}
|
|
26
|
+
getJsonIndexDefinition(index) {
|
|
27
|
+
return index.columnNames
|
|
28
|
+
.map(column => {
|
|
29
|
+
if (!column.includes('.')) {
|
|
30
|
+
return column;
|
|
31
|
+
}
|
|
32
|
+
const [root, ...path] = column.split('.');
|
|
33
|
+
return `(json_value(${this.quoteIdentifier(root)}, '$.${path.join('.')}' returning ${index.options?.returning ?? 'char(255)'}))`;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
getBooleanTypeDeclarationSQL() {
|
|
37
|
+
return 'tinyint(1)';
|
|
38
|
+
}
|
|
39
|
+
getDefaultMappedType(type) {
|
|
40
|
+
if (type === 'tinyint(1)') {
|
|
41
|
+
return super.getDefaultMappedType('boolean');
|
|
42
|
+
}
|
|
43
|
+
return super.getDefaultMappedType(type);
|
|
44
|
+
}
|
|
45
|
+
supportsUnsigned() {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns the default name of index for the given columns
|
|
50
|
+
* cannot go past 64 character length for identifiers in MySQL
|
|
51
|
+
*/
|
|
52
|
+
getIndexName(tableName, columns, type) {
|
|
53
|
+
if (type === 'primary') {
|
|
54
|
+
return this.getDefaultPrimaryName(tableName, columns);
|
|
55
|
+
}
|
|
56
|
+
const indexName = super.getIndexName(tableName, columns, type);
|
|
57
|
+
if (indexName.length > 64) {
|
|
58
|
+
return `${indexName.substring(0, 56 - type.length)}_${core_1.Utils.hash(indexName, 5)}_${type}`;
|
|
59
|
+
}
|
|
60
|
+
return indexName;
|
|
61
|
+
}
|
|
62
|
+
getDefaultPrimaryName(tableName, columns) {
|
|
63
|
+
return 'PRIMARY'; // https://dev.mysql.com/doc/refman/8.0/en/create-table.html#create-table-indexes-keys
|
|
64
|
+
}
|
|
65
|
+
supportsCreatingFullTextIndex() {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
getFullTextWhereClause() {
|
|
69
|
+
return `match(:column:) against (:query in boolean mode)`;
|
|
70
|
+
}
|
|
71
|
+
getFullTextIndexExpression(indexName, schemaName, tableName, columns) {
|
|
72
|
+
/* istanbul ignore next */
|
|
73
|
+
const quotedTableName = this.quoteIdentifier(schemaName ? `${schemaName}.${tableName}` : tableName);
|
|
74
|
+
const quotedColumnNames = columns.map(c => this.quoteIdentifier(c.name));
|
|
75
|
+
const quotedIndexName = this.quoteIdentifier(indexName);
|
|
76
|
+
return `alter table ${quotedTableName} add fulltext index ${quotedIndexName}(${quotedColumnNames.join(',')})`;
|
|
77
|
+
}
|
|
78
|
+
getOrderByExpression(column, direction) {
|
|
79
|
+
const ret = [];
|
|
80
|
+
const dir = direction.toLowerCase();
|
|
81
|
+
if (dir in this.ORDER_BY_NULLS_TRANSLATE) {
|
|
82
|
+
ret.push(`${column} ${this.ORDER_BY_NULLS_TRANSLATE[dir]}`);
|
|
83
|
+
}
|
|
84
|
+
ret.push(`${column} ${dir.replace(/(\s|nulls|first|last)*/gi, '')}`);
|
|
85
|
+
return ret;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.MySqlPlatform = MySqlPlatform;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MySqlQueryCompiler = void 0;
|
|
7
|
+
/* istanbul ignore file */
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
const mysql_querycompiler_1 = __importDefault(require("knex/lib/dialects/mysql/query/mysql-querycompiler"));
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
const querycompiler_1 = __importDefault(require("knex/lib/query/querycompiler"));
|
|
12
|
+
// upsert support from https://github.com/knex/knex/pull/6050
|
|
13
|
+
class MySqlQueryCompiler extends mysql_querycompiler_1.default {
|
|
14
|
+
// mysql dialect disallows query non scalar params, but we dont use it to execute the query, it always goes through the `platform.formatQuery()`
|
|
15
|
+
whereBasic(statement) {
|
|
16
|
+
return querycompiler_1.default.prototype.whereBasic.call(this, statement);
|
|
17
|
+
}
|
|
18
|
+
// mysql dialect disallows query non scalar params, but we dont use it to execute the query, it always goes through the `platform.formatQuery()`
|
|
19
|
+
whereRaw(statement) {
|
|
20
|
+
return querycompiler_1.default.prototype.whereRaw.call(this, statement);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.MySqlQueryCompiler = MySqlQueryCompiler;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Knex } from 'knex';
|
|
2
|
+
import type { CheckDef, Column, IndexDef, TableDifference, Table, ForeignKey } from '../../typings';
|
|
3
|
+
import { type Dictionary, type Type } from '@mikro-orm/core';
|
|
4
|
+
import type { AbstractSqlConnection } from '../../AbstractSqlConnection';
|
|
5
|
+
import { SchemaHelper } from '../../schema/SchemaHelper';
|
|
6
|
+
import type { DatabaseSchema } from '../../schema/DatabaseSchema';
|
|
7
|
+
import type { DatabaseTable } from '../../schema/DatabaseTable';
|
|
8
|
+
export declare class MySqlSchemaHelper extends SchemaHelper {
|
|
9
|
+
private readonly _cache;
|
|
10
|
+
static readonly DEFAULT_VALUES: {
|
|
11
|
+
'now()': string[];
|
|
12
|
+
'current_timestamp(?)': string[];
|
|
13
|
+
'0': string[];
|
|
14
|
+
};
|
|
15
|
+
getSchemaBeginning(charset: string): string;
|
|
16
|
+
disableForeignKeysSQL(): string;
|
|
17
|
+
enableForeignKeysSQL(): string;
|
|
18
|
+
finalizeTable(table: Knex.CreateTableBuilder, charset: string, collate?: string): void;
|
|
19
|
+
getListTablesSQL(): string;
|
|
20
|
+
loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[]): Promise<void>;
|
|
21
|
+
getAllIndexes(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<IndexDef[]>>;
|
|
22
|
+
getAllColumns(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<Column[]>>;
|
|
23
|
+
getAllChecks(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<CheckDef[]>>;
|
|
24
|
+
getAllForeignKeys(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<Dictionary<ForeignKey>>>;
|
|
25
|
+
getPreAlterTable(tableDiff: TableDifference, safe: boolean): string;
|
|
26
|
+
configureColumnDefault(column: Column, col: Knex.ColumnBuilder, knex: Knex, changedProperties?: Set<string>): Knex.ColumnBuilder;
|
|
27
|
+
getRenameColumnSQL(tableName: string, oldColumnName: string, to: Column): string;
|
|
28
|
+
getRenameIndexSQL(tableName: string, index: IndexDef, oldIndexName: string): string;
|
|
29
|
+
getChangeColumnCommentSQL(tableName: string, to: Column, schemaName?: string): string;
|
|
30
|
+
createTableColumn(table: Knex.TableBuilder, column: Column, fromTable: DatabaseTable, changedProperties?: Set<string>): Knex.ColumnBuilder | undefined;
|
|
31
|
+
configureColumn(column: Column, col: Knex.ColumnBuilder, knex: Knex, changedProperties?: Set<string>): Knex.ColumnBuilder;
|
|
32
|
+
private getColumnDeclarationSQL;
|
|
33
|
+
getForeignKeysSQL(tableName: string, schemaName?: string): string;
|
|
34
|
+
getAllEnumDefinitions(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<Dictionary<string[]>>>;
|
|
35
|
+
private supportsCheckConstraints;
|
|
36
|
+
protected getChecksSQL(tables: Table[]): string;
|
|
37
|
+
getChecks(connection: AbstractSqlConnection, tableName: string, schemaName: string, columns?: Column[]): Promise<CheckDef[]>;
|
|
38
|
+
getEnumDefinitions(connection: AbstractSqlConnection, checks: CheckDef[], tableName: string, schemaName?: string): Promise<Dictionary<string[]>>;
|
|
39
|
+
getColumns(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<Column[]>;
|
|
40
|
+
getIndexes(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<IndexDef[]>;
|
|
41
|
+
normalizeDefaultValue(defaultValue: string, length: number): string | number;
|
|
42
|
+
protected wrap(val: string | null | undefined, type: Type<unknown>): string | null | undefined;
|
|
43
|
+
}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MySqlSchemaHelper = void 0;
|
|
4
|
+
const core_1 = require("@mikro-orm/core");
|
|
5
|
+
const SchemaHelper_1 = require("../../schema/SchemaHelper");
|
|
6
|
+
class MySqlSchemaHelper extends SchemaHelper_1.SchemaHelper {
|
|
7
|
+
_cache = {};
|
|
8
|
+
static DEFAULT_VALUES = {
|
|
9
|
+
'now()': ['now()', 'current_timestamp'],
|
|
10
|
+
'current_timestamp(?)': ['current_timestamp(?)'],
|
|
11
|
+
'0': ['0', 'false'],
|
|
12
|
+
};
|
|
13
|
+
getSchemaBeginning(charset) {
|
|
14
|
+
return `set names ${charset};\n${this.disableForeignKeysSQL()}\n\n`;
|
|
15
|
+
}
|
|
16
|
+
disableForeignKeysSQL() {
|
|
17
|
+
return 'set foreign_key_checks = 0;';
|
|
18
|
+
}
|
|
19
|
+
enableForeignKeysSQL() {
|
|
20
|
+
return 'set foreign_key_checks = 1;';
|
|
21
|
+
}
|
|
22
|
+
finalizeTable(table, charset, collate) {
|
|
23
|
+
table.engine('InnoDB');
|
|
24
|
+
table.charset(charset);
|
|
25
|
+
if (collate) {
|
|
26
|
+
table.collate(collate);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
getListTablesSQL() {
|
|
30
|
+
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()`;
|
|
31
|
+
}
|
|
32
|
+
async loadInformationSchema(schema, connection, tables) {
|
|
33
|
+
if (tables.length === 0) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const columns = await this.getAllColumns(connection, tables);
|
|
37
|
+
const indexes = await this.getAllIndexes(connection, tables);
|
|
38
|
+
const checks = await this.getAllChecks(connection, tables);
|
|
39
|
+
const fks = await this.getAllForeignKeys(connection, tables);
|
|
40
|
+
const enums = await this.getAllEnumDefinitions(connection, tables);
|
|
41
|
+
for (const t of tables) {
|
|
42
|
+
const key = this.getTableKey(t);
|
|
43
|
+
const table = schema.addTable(t.table_name, t.schema_name, t.table_comment);
|
|
44
|
+
const pks = await this.getPrimaryKeys(connection, indexes[key], table.name, table.schema);
|
|
45
|
+
table.init(columns[key], indexes[key], checks[key], pks, fks[key], enums[key]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async getAllIndexes(connection, tables) {
|
|
49
|
+
const sql = `select table_name as table_name, nullif(table_schema, schema()) as schema_name, index_name as index_name, non_unique as non_unique, column_name as column_name, expression as expression
|
|
50
|
+
from information_schema.statistics where table_schema = database()
|
|
51
|
+
and table_name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(', ')})
|
|
52
|
+
order by schema_name, table_name, index_name, seq_in_index`;
|
|
53
|
+
const allIndexes = await connection.execute(sql);
|
|
54
|
+
const ret = {};
|
|
55
|
+
for (const index of allIndexes) {
|
|
56
|
+
const key = this.getTableKey(index);
|
|
57
|
+
const indexDef = {
|
|
58
|
+
columnNames: [index.column_name],
|
|
59
|
+
keyName: index.index_name,
|
|
60
|
+
unique: !index.non_unique,
|
|
61
|
+
primary: index.index_name === 'PRIMARY',
|
|
62
|
+
constraint: !index.non_unique,
|
|
63
|
+
};
|
|
64
|
+
if (!index.column_name || index.column_name.match(/[(): ,"'`]/) || index.expression?.match(/ where /i)) {
|
|
65
|
+
indexDef.expression = index.expression; // required for the `getCreateIndexSQL()` call
|
|
66
|
+
indexDef.expression = this.getCreateIndexSQL(index.table_name, indexDef, !!index.expression);
|
|
67
|
+
}
|
|
68
|
+
ret[key] ??= [];
|
|
69
|
+
ret[key].push(indexDef);
|
|
70
|
+
}
|
|
71
|
+
for (const key of Object.keys(ret)) {
|
|
72
|
+
ret[key] = await this.mapIndexes(ret[key]);
|
|
73
|
+
}
|
|
74
|
+
return ret;
|
|
75
|
+
}
|
|
76
|
+
async getAllColumns(connection, tables) {
|
|
77
|
+
const sql = `select table_name as table_name,
|
|
78
|
+
nullif(table_schema, schema()) as schema_name,
|
|
79
|
+
column_name as column_name,
|
|
80
|
+
column_default as column_default,
|
|
81
|
+
column_comment as column_comment,
|
|
82
|
+
is_nullable as is_nullable,
|
|
83
|
+
data_type as data_type,
|
|
84
|
+
column_type as column_type,
|
|
85
|
+
column_key as column_key,
|
|
86
|
+
extra as extra,
|
|
87
|
+
generation_expression as generation_expression,
|
|
88
|
+
numeric_precision as numeric_precision,
|
|
89
|
+
numeric_scale as numeric_scale,
|
|
90
|
+
ifnull(datetime_precision, character_maximum_length) length
|
|
91
|
+
from information_schema.columns where table_schema = database() and table_name in (${tables.map(t => this.platform.quoteValue(t.table_name))})
|
|
92
|
+
order by ordinal_position`;
|
|
93
|
+
const allColumns = await connection.execute(sql);
|
|
94
|
+
const str = (val) => val != null ? '' + val : val;
|
|
95
|
+
const extra = (val) => val.replace(/auto_increment|default_generated|(stored|virtual) generated/i, '').trim();
|
|
96
|
+
const ret = {};
|
|
97
|
+
for (const col of allColumns) {
|
|
98
|
+
const mappedType = this.platform.getMappedType(col.column_type);
|
|
99
|
+
const defaultValue = str(this.normalizeDefaultValue((mappedType.compareAsType() === 'boolean' && ['0', '1'].includes(col.column_default))
|
|
100
|
+
? ['false', 'true'][+col.column_default]
|
|
101
|
+
: col.column_default, col.length));
|
|
102
|
+
const key = this.getTableKey(col);
|
|
103
|
+
const generated = col.generation_expression ? `${col.generation_expression} ${col.extra.match(/stored generated/i) ? 'stored' : 'virtual'}` : undefined;
|
|
104
|
+
ret[key] ??= [];
|
|
105
|
+
ret[key].push({
|
|
106
|
+
name: col.column_name,
|
|
107
|
+
type: this.platform.isNumericColumn(mappedType) ? col.column_type.replace(/ unsigned$/, '').replace(/\(\d+\)$/, '') : col.column_type,
|
|
108
|
+
mappedType,
|
|
109
|
+
unsigned: col.column_type.endsWith(' unsigned'),
|
|
110
|
+
length: col.length,
|
|
111
|
+
default: this.wrap(defaultValue, mappedType),
|
|
112
|
+
nullable: col.is_nullable === 'YES',
|
|
113
|
+
primary: col.column_key === 'PRI',
|
|
114
|
+
unique: col.column_key === 'UNI',
|
|
115
|
+
autoincrement: col.extra === 'auto_increment',
|
|
116
|
+
precision: col.numeric_precision,
|
|
117
|
+
scale: col.numeric_scale,
|
|
118
|
+
comment: col.column_comment,
|
|
119
|
+
extra: extra(col.extra),
|
|
120
|
+
generated,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return ret;
|
|
124
|
+
}
|
|
125
|
+
async getAllChecks(connection, tables) {
|
|
126
|
+
/* istanbul ignore next */
|
|
127
|
+
if (!await this.supportsCheckConstraints(connection)) {
|
|
128
|
+
return {};
|
|
129
|
+
}
|
|
130
|
+
const sql = this.getChecksSQL(tables);
|
|
131
|
+
const allChecks = await connection.execute(sql);
|
|
132
|
+
const ret = {};
|
|
133
|
+
for (const check of allChecks) {
|
|
134
|
+
const key = this.getTableKey(check);
|
|
135
|
+
ret[key] ??= [];
|
|
136
|
+
ret[key].push({
|
|
137
|
+
name: check.name,
|
|
138
|
+
columnName: check.column_name,
|
|
139
|
+
definition: `check ${check.expression}`,
|
|
140
|
+
expression: check.expression.replace(/^\((.*)\)$/, '$1'),
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return ret;
|
|
144
|
+
}
|
|
145
|
+
async getAllForeignKeys(connection, tables) {
|
|
146
|
+
const sql = `select k.constraint_name as constraint_name, nullif(k.table_schema, schema()) as schema_name, k.table_name as table_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
|
|
147
|
+
from information_schema.key_column_usage k
|
|
148
|
+
inner join information_schema.referential_constraints c on c.constraint_name = k.constraint_name and c.table_name = k.table_name
|
|
149
|
+
where k.table_name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(', ')})
|
|
150
|
+
and k.table_schema = database() and c.constraint_schema = database() and k.referenced_column_name is not null
|
|
151
|
+
order by constraint_name, k.ordinal_position`;
|
|
152
|
+
const allFks = await connection.execute(sql);
|
|
153
|
+
const ret = {};
|
|
154
|
+
for (const fk of allFks) {
|
|
155
|
+
const key = this.getTableKey(fk);
|
|
156
|
+
ret[key] ??= [];
|
|
157
|
+
ret[key].push(fk);
|
|
158
|
+
}
|
|
159
|
+
Object.keys(ret).forEach(key => {
|
|
160
|
+
const parts = key.split('.');
|
|
161
|
+
/* istanbul ignore next */
|
|
162
|
+
const schemaName = parts.length > 1 ? parts[0] : undefined;
|
|
163
|
+
ret[key] = this.mapForeignKeys(ret[key], key, schemaName);
|
|
164
|
+
});
|
|
165
|
+
return ret;
|
|
166
|
+
}
|
|
167
|
+
getPreAlterTable(tableDiff, safe) {
|
|
168
|
+
// Dropping primary keys requires to unset autoincrement attribute on the particular column first.
|
|
169
|
+
const pk = Object.values(tableDiff.removedIndexes).find(idx => idx.primary);
|
|
170
|
+
if (!pk || safe) {
|
|
171
|
+
return '';
|
|
172
|
+
}
|
|
173
|
+
return pk.columnNames
|
|
174
|
+
.filter(col => tableDiff.fromTable.hasColumn(col))
|
|
175
|
+
.map(col => tableDiff.fromTable.getColumn(col))
|
|
176
|
+
.filter(col => col.autoincrement)
|
|
177
|
+
.map(col => `alter table \`${tableDiff.name}\` modify \`${col.name}\` ${this.getColumnDeclarationSQL({ ...col, autoincrement: false })}`)
|
|
178
|
+
.join(';\n');
|
|
179
|
+
}
|
|
180
|
+
configureColumnDefault(column, col, knex, changedProperties) {
|
|
181
|
+
if (changedProperties || column.default !== undefined) {
|
|
182
|
+
if (column.default == null) {
|
|
183
|
+
col.defaultTo(null);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
col.defaultTo(knex.raw(column.default + (column.extra ? ' ' + column.extra : '')));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return col;
|
|
190
|
+
}
|
|
191
|
+
getRenameColumnSQL(tableName, oldColumnName, to) {
|
|
192
|
+
tableName = this.platform.quoteIdentifier(tableName);
|
|
193
|
+
oldColumnName = this.platform.quoteIdentifier(oldColumnName);
|
|
194
|
+
const columnName = this.platform.quoteIdentifier(to.name);
|
|
195
|
+
return `alter table ${tableName} change ${oldColumnName} ${columnName} ${this.getColumnDeclarationSQL(to)}`;
|
|
196
|
+
}
|
|
197
|
+
getRenameIndexSQL(tableName, index, oldIndexName) {
|
|
198
|
+
tableName = this.platform.quoteIdentifier(tableName);
|
|
199
|
+
oldIndexName = this.platform.quoteIdentifier(oldIndexName);
|
|
200
|
+
const keyName = this.platform.quoteIdentifier(index.keyName);
|
|
201
|
+
return `alter table ${tableName} rename index ${oldIndexName} to ${keyName}`;
|
|
202
|
+
}
|
|
203
|
+
getChangeColumnCommentSQL(tableName, to, schemaName) {
|
|
204
|
+
tableName = this.platform.quoteIdentifier(tableName);
|
|
205
|
+
const columnName = this.platform.quoteIdentifier(to.name);
|
|
206
|
+
return `alter table ${tableName} modify ${columnName} ${this.getColumnDeclarationSQL(to)}`;
|
|
207
|
+
}
|
|
208
|
+
createTableColumn(table, column, fromTable, changedProperties) {
|
|
209
|
+
if (column.mappedType instanceof core_1.MediumIntType) {
|
|
210
|
+
return table.specificType(column.name, this.getColumnDeclarationSQL(column, true));
|
|
211
|
+
}
|
|
212
|
+
return super.createTableColumn(table, column, fromTable, changedProperties);
|
|
213
|
+
}
|
|
214
|
+
configureColumn(column, col, knex, changedProperties) {
|
|
215
|
+
if (column.mappedType instanceof core_1.MediumIntType) {
|
|
216
|
+
return col;
|
|
217
|
+
}
|
|
218
|
+
return super.configureColumn(column, col, knex, changedProperties);
|
|
219
|
+
}
|
|
220
|
+
getColumnDeclarationSQL(col, addPrimary = false) {
|
|
221
|
+
let ret = col.type;
|
|
222
|
+
ret += col.unsigned ? ' unsigned' : '';
|
|
223
|
+
ret += col.autoincrement ? ' auto_increment' : '';
|
|
224
|
+
ret += ' ';
|
|
225
|
+
ret += col.nullable ? 'null' : 'not null';
|
|
226
|
+
ret += col.default ? ' default ' + col.default : '';
|
|
227
|
+
if (addPrimary && col.primary) {
|
|
228
|
+
ret += ' primary key';
|
|
229
|
+
}
|
|
230
|
+
ret += col.comment ? ` comment ${this.platform.quoteValue(col.comment)}` : '';
|
|
231
|
+
return ret;
|
|
232
|
+
}
|
|
233
|
+
/* istanbul ignore next kept for BC */
|
|
234
|
+
getForeignKeysSQL(tableName, schemaName) {
|
|
235
|
+
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 `
|
|
236
|
+
+ `from information_schema.key_column_usage k `
|
|
237
|
+
+ `inner join information_schema.referential_constraints c on c.constraint_name = k.constraint_name and c.table_name = '${tableName}' `
|
|
238
|
+
+ `where k.table_name = '${tableName}' and k.table_schema = database() and c.constraint_schema = database() and k.referenced_column_name is not null`;
|
|
239
|
+
}
|
|
240
|
+
async getAllEnumDefinitions(connection, tables) {
|
|
241
|
+
const sql = `select column_name as column_name, column_type as column_type, table_name as table_name
|
|
242
|
+
from information_schema.columns
|
|
243
|
+
where data_type = 'enum' and table_name in (${tables.map(t => `'${t.table_name}'`).join(', ')}) and table_schema = database()`;
|
|
244
|
+
const enums = await connection.execute(sql);
|
|
245
|
+
return enums.reduce((o, item) => {
|
|
246
|
+
o[item.table_name] ??= {};
|
|
247
|
+
o[item.table_name][item.column_name] = item.column_type.match(/enum\((.*)\)/)[1].split(',').map((item) => item.match(/'(.*)'/)[1]);
|
|
248
|
+
return o;
|
|
249
|
+
}, {});
|
|
250
|
+
}
|
|
251
|
+
async supportsCheckConstraints(connection) {
|
|
252
|
+
if (this._cache.supportsCheckConstraints != null) {
|
|
253
|
+
return this._cache.supportsCheckConstraints;
|
|
254
|
+
}
|
|
255
|
+
const sql = `select 1 from information_schema.tables where table_name = 'CHECK_CONSTRAINTS' and table_schema = 'information_schema'`;
|
|
256
|
+
const res = await connection.execute(sql);
|
|
257
|
+
return this._cache.supportsCheckConstraints = res.length > 0;
|
|
258
|
+
}
|
|
259
|
+
getChecksSQL(tables) {
|
|
260
|
+
return `select cc.constraint_schema as table_schema, tc.table_name as table_name, cc.constraint_name as name, cc.check_clause as expression
|
|
261
|
+
from information_schema.check_constraints cc
|
|
262
|
+
join information_schema.table_constraints tc
|
|
263
|
+
on tc.constraint_schema = cc.constraint_schema
|
|
264
|
+
and tc.constraint_name = cc.constraint_name
|
|
265
|
+
and constraint_type = 'CHECK'
|
|
266
|
+
where tc.table_name in (${tables.map(t => this.platform.quoteValue(t.table_name))}) and tc.constraint_schema = database()
|
|
267
|
+
order by tc.constraint_name`;
|
|
268
|
+
}
|
|
269
|
+
/* istanbul ignore next */
|
|
270
|
+
async getChecks(connection, tableName, schemaName, columns) {
|
|
271
|
+
const res = await this.getAllChecks(connection, [{ table_name: tableName, schema_name: schemaName }]);
|
|
272
|
+
return res[tableName];
|
|
273
|
+
}
|
|
274
|
+
/* istanbul ignore next */
|
|
275
|
+
async getEnumDefinitions(connection, checks, tableName, schemaName) {
|
|
276
|
+
const res = await this.getAllEnumDefinitions(connection, [{ table_name: tableName, schema_name: schemaName }]);
|
|
277
|
+
return res[tableName];
|
|
278
|
+
}
|
|
279
|
+
/* istanbul ignore next */
|
|
280
|
+
async getColumns(connection, tableName, schemaName) {
|
|
281
|
+
const res = await this.getAllColumns(connection, [{ table_name: tableName, schema_name: schemaName }]);
|
|
282
|
+
return res[tableName];
|
|
283
|
+
}
|
|
284
|
+
/* istanbul ignore next */
|
|
285
|
+
async getIndexes(connection, tableName, schemaName) {
|
|
286
|
+
const res = await this.getAllIndexes(connection, [{ table_name: tableName, schema_name: schemaName }]);
|
|
287
|
+
return res[tableName];
|
|
288
|
+
}
|
|
289
|
+
normalizeDefaultValue(defaultValue, length) {
|
|
290
|
+
return super.normalizeDefaultValue(defaultValue, length, MySqlSchemaHelper.DEFAULT_VALUES);
|
|
291
|
+
}
|
|
292
|
+
wrap(val, type) {
|
|
293
|
+
const stringType = type instanceof core_1.StringType || type instanceof core_1.TextType || type instanceof core_1.EnumType;
|
|
294
|
+
return typeof val === 'string' && val.length > 0 && stringType ? this.platform.quoteValue(val) : val;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
exports.MySqlSchemaHelper = MySqlSchemaHelper;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./MariaDbKnexDialect"), exports);
|
|
18
|
+
__exportStar(require("./MySqlKnexDialect"), exports);
|
|
19
|
+
__exportStar(require("./MySqlConnection"), exports);
|
|
20
|
+
__exportStar(require("./MySqlExceptionConverter"), exports);
|
|
21
|
+
__exportStar(require("./MySqlSchemaHelper"), exports);
|
|
22
|
+
__exportStar(require("./MySqlPlatform"), exports);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.PostgreSqlKnexDialect = void 0;
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
const postgres_1 = __importDefault(require("knex/lib/dialects/postgres"));
|
|
9
|
+
const PostgreSqlTableCompiler_1 = require("./PostgreSqlTableCompiler");
|
|
10
|
+
class PostgreSqlKnexDialect extends postgres_1.default {
|
|
11
|
+
ormConfig;
|
|
12
|
+
tableCompiler() {
|
|
13
|
+
// eslint-disable-next-line prefer-rest-params
|
|
14
|
+
const tableCompiler = new PostgreSqlTableCompiler_1.PostgreSqlTableCompiler(this, ...arguments);
|
|
15
|
+
tableCompiler.ormConfig = this.ormConfig;
|
|
16
|
+
return tableCompiler;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.PostgreSqlKnexDialect = PostgreSqlKnexDialect;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import PostgresDialectTableCompiler from 'knex/lib/dialects/postgres/schema/pg-tablecompiler';
|
|
2
|
+
import type { Configuration, Dictionary } from '@mikro-orm/core';
|
|
3
|
+
export declare class PostgreSqlTableCompiler extends PostgresDialectTableCompiler {
|
|
4
|
+
ormConfig: Configuration;
|
|
5
|
+
alterColumnsPrefix: string;
|
|
6
|
+
addColumns(columns: Dictionary[], prefix: string, colCompilers: Dictionary[]): any;
|
|
7
|
+
private addColumn;
|
|
8
|
+
private alterColumnNullable;
|
|
9
|
+
private addColumnDefault;
|
|
10
|
+
private dropColumnDefault;
|
|
11
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.PostgreSqlTableCompiler = void 0;
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
const pg_tablecompiler_1 = __importDefault(require("knex/lib/dialects/postgres/schema/pg-tablecompiler"));
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
const tablecompiler_1 = __importDefault(require("knex/lib/schema/tablecompiler"));
|
|
11
|
+
class PostgreSqlTableCompiler extends pg_tablecompiler_1.default {
|
|
12
|
+
ormConfig;
|
|
13
|
+
addColumns(columns, prefix, colCompilers) {
|
|
14
|
+
if (prefix !== this.alterColumnsPrefix) {
|
|
15
|
+
// base class implementation for normal add
|
|
16
|
+
return tablecompiler_1.default.prototype.addColumns.call(this, columns, prefix);
|
|
17
|
+
}
|
|
18
|
+
// alter columns
|
|
19
|
+
for (const col of colCompilers) {
|
|
20
|
+
this.addColumn(col);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
addColumn(col) {
|
|
24
|
+
const options = this.ormConfig.get('schemaGenerator');
|
|
25
|
+
const quotedTableName = this.tableName();
|
|
26
|
+
const type = col.getColumnType();
|
|
27
|
+
const colName = this.client.wrapIdentifier(col.getColumnName(), col.columnBuilder.queryContext());
|
|
28
|
+
const constraintName = `${this.tableNameRaw.replace(/^.*\.(.*)$/, '$1')}_${col.getColumnName()}_check`;
|
|
29
|
+
const useNative = col.args?.[2]?.useNative;
|
|
30
|
+
const alterType = col.columnBuilder.alterType;
|
|
31
|
+
const alterNullable = col.columnBuilder.alterNullable;
|
|
32
|
+
const defaultTo = col.modified.defaultTo;
|
|
33
|
+
if (defaultTo != null) {
|
|
34
|
+
this.dropColumnDefault(col, colName);
|
|
35
|
+
}
|
|
36
|
+
/* istanbul ignore next */
|
|
37
|
+
if (col.type === 'enu' && !useNative) {
|
|
38
|
+
if (alterType) {
|
|
39
|
+
this.pushQuery({ sql: `alter table ${quotedTableName} alter column ${colName} type text using (${colName}::text)`, bindings: [] });
|
|
40
|
+
}
|
|
41
|
+
/* istanbul ignore else */
|
|
42
|
+
if (options.createForeignKeyConstraints && alterNullable) {
|
|
43
|
+
this.pushQuery({ sql: `alter table ${quotedTableName} add constraint "${constraintName}" ${type.replace(/^text /, '')}`, bindings: [] });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
else if (type === 'uuid') {
|
|
47
|
+
// we need to drop the default as it would be invalid
|
|
48
|
+
this.pushQuery({ sql: `alter table ${quotedTableName} alter column ${colName} drop default`, bindings: [] });
|
|
49
|
+
this.pushQuery({ sql: `alter table ${quotedTableName} alter column ${colName} type ${type} using (${colName}::text::uuid)`, bindings: [] });
|
|
50
|
+
}
|
|
51
|
+
else if (alterType) {
|
|
52
|
+
this.pushQuery({ sql: `alter table ${quotedTableName} alter column ${colName} type ${type} using (${colName}::${type})`, bindings: [] });
|
|
53
|
+
}
|
|
54
|
+
this.addColumnDefault(col, colName);
|
|
55
|
+
this.alterColumnNullable(col, colName);
|
|
56
|
+
}
|
|
57
|
+
alterColumnNullable(col, colName) {
|
|
58
|
+
const quotedTableName = this.tableName();
|
|
59
|
+
const nullable = col.modified.nullable;
|
|
60
|
+
if (!nullable) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (nullable[0] === false) {
|
|
64
|
+
this.pushQuery({ sql: `alter table ${quotedTableName} alter column ${colName} set not null`, bindings: [] });
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
this.pushQuery({ sql: `alter table ${quotedTableName} alter column ${colName} drop not null`, bindings: [] });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
addColumnDefault(col, colName) {
|
|
71
|
+
const quotedTableName = this.tableName();
|
|
72
|
+
const defaultTo = col.modified.defaultTo;
|
|
73
|
+
if (!defaultTo) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (defaultTo[0] !== null) {
|
|
77
|
+
const modifier = col.defaultTo(...defaultTo);
|
|
78
|
+
this.pushQuery({ sql: `alter table ${quotedTableName} alter column ${colName} set ${modifier}`, bindings: [] });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
dropColumnDefault(col, colName) {
|
|
82
|
+
const quotedTableName = this.tableName();
|
|
83
|
+
const defaultTo = col.modified.defaultTo;
|
|
84
|
+
if (defaultTo?.[0] == null) {
|
|
85
|
+
this.pushQuery({ sql: `alter table ${quotedTableName} alter column ${colName} drop default`, bindings: [] });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.PostgreSqlTableCompiler = PostgreSqlTableCompiler;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './PostgreSqlKnexDialect';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./PostgreSqlKnexDialect"), exports);
|
package/index.mjs
CHANGED
|
@@ -118,6 +118,7 @@ export const LockMode = mod.LockMode;
|
|
|
118
118
|
export const LockWaitTimeoutException = mod.LockWaitTimeoutException;
|
|
119
119
|
export const ManyToMany = mod.ManyToMany;
|
|
120
120
|
export const ManyToOne = mod.ManyToOne;
|
|
121
|
+
export const MariaDbKnexDialect = mod.MariaDbKnexDialect;
|
|
121
122
|
export const MediumIntType = mod.MediumIntType;
|
|
122
123
|
export const MemoryCacheAdapter = mod.MemoryCacheAdapter;
|
|
123
124
|
export const MetadataDiscovery = mod.MetadataDiscovery;
|
|
@@ -129,6 +130,11 @@ export const MikroORM = mod.MikroORM;
|
|
|
129
130
|
export const MongoNamingStrategy = mod.MongoNamingStrategy;
|
|
130
131
|
export const MonkeyPatchable = mod.MonkeyPatchable;
|
|
131
132
|
export const MsSqlKnexDialect = mod.MsSqlKnexDialect;
|
|
133
|
+
export const MySqlConnection = mod.MySqlConnection;
|
|
134
|
+
export const MySqlExceptionConverter = mod.MySqlExceptionConverter;
|
|
135
|
+
export const MySqlKnexDialect = mod.MySqlKnexDialect;
|
|
136
|
+
export const MySqlPlatform = mod.MySqlPlatform;
|
|
137
|
+
export const MySqlSchemaHelper = mod.MySqlSchemaHelper;
|
|
132
138
|
export const NodeState = mod.NodeState;
|
|
133
139
|
export const NonUniqueFieldNameException = mod.NonUniqueFieldNameException;
|
|
134
140
|
export const NotFoundError = mod.NotFoundError;
|
|
@@ -148,6 +154,7 @@ export const PlainObject = mod.PlainObject;
|
|
|
148
154
|
export const Platform = mod.Platform;
|
|
149
155
|
export const PopulateHint = mod.PopulateHint;
|
|
150
156
|
export const PopulatePath = mod.PopulatePath;
|
|
157
|
+
export const PostgreSqlKnexDialect = mod.PostgreSqlKnexDialect;
|
|
151
158
|
export const PrimaryKey = mod.PrimaryKey;
|
|
152
159
|
export const PrimaryKeyProp = mod.PrimaryKeyProp;
|
|
153
160
|
export const Property = mod.Property;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/knex",
|
|
3
|
-
"version": "6.1.13-dev.
|
|
3
|
+
"version": "6.1.13-dev.34",
|
|
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,6 +66,6 @@
|
|
|
66
66
|
"@mikro-orm/core": "^6.1.12"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
|
-
"@mikro-orm/core": "6.1.13-dev.
|
|
69
|
+
"@mikro-orm/core": "6.1.13-dev.34"
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -488,7 +488,7 @@ class QueryBuilderHelper {
|
|
|
488
488
|
const order = core_1.Utils.isNumber(direction) ? core_1.QueryOrderNumeric[direction] : direction;
|
|
489
489
|
const raw = core_1.RawQueryFragment.getKnownFragment(key);
|
|
490
490
|
if (raw) {
|
|
491
|
-
ret.push(
|
|
491
|
+
ret.push(...this.platform.getOrderByExpression(this.platform.formatQuery(raw.sql, raw.params), order));
|
|
492
492
|
continue;
|
|
493
493
|
}
|
|
494
494
|
for (const f of core_1.Utils.splitPrimaryKeys(key)) {
|