@mikro-orm/sql 7.0.0-dev.97 → 7.0.0-dev.99

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.
Files changed (91) hide show
  1. package/AbstractSqlConnection.d.ts +57 -0
  2. package/AbstractSqlConnection.js +239 -0
  3. package/AbstractSqlDriver.d.ts +94 -0
  4. package/AbstractSqlDriver.js +1387 -0
  5. package/AbstractSqlPlatform.d.ts +38 -0
  6. package/AbstractSqlPlatform.js +104 -0
  7. package/LICENSE +21 -0
  8. package/PivotCollectionPersister.d.ts +22 -0
  9. package/PivotCollectionPersister.js +159 -0
  10. package/README.md +390 -0
  11. package/SqlEntityManager.d.ts +33 -0
  12. package/SqlEntityManager.js +44 -0
  13. package/SqlEntityRepository.d.ts +19 -0
  14. package/SqlEntityRepository.js +26 -0
  15. package/dialects/index.d.ts +4 -0
  16. package/dialects/index.js +4 -0
  17. package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +14 -0
  18. package/dialects/mssql/MsSqlNativeQueryBuilder.js +200 -0
  19. package/dialects/mssql/index.d.ts +1 -0
  20. package/dialects/mssql/index.js +1 -0
  21. package/dialects/mysql/MySqlExceptionConverter.d.ts +9 -0
  22. package/dialects/mysql/MySqlExceptionConverter.js +80 -0
  23. package/dialects/mysql/MySqlNativeQueryBuilder.d.ts +7 -0
  24. package/dialects/mysql/MySqlNativeQueryBuilder.js +77 -0
  25. package/dialects/mysql/MySqlPlatform.d.ts +45 -0
  26. package/dialects/mysql/MySqlPlatform.js +116 -0
  27. package/dialects/mysql/MySqlSchemaHelper.d.ts +36 -0
  28. package/dialects/mysql/MySqlSchemaHelper.js +269 -0
  29. package/dialects/mysql/index.d.ts +4 -0
  30. package/dialects/mysql/index.js +4 -0
  31. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.d.ts +5 -0
  32. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.js +8 -0
  33. package/dialects/postgresql/PostgreSqlTableCompiler.d.ts +1 -0
  34. package/dialects/postgresql/PostgreSqlTableCompiler.js +1 -0
  35. package/dialects/postgresql/index.d.ts +1 -0
  36. package/dialects/postgresql/index.js +1 -0
  37. package/dialects/sqlite/BaseSqliteConnection.d.ts +6 -0
  38. package/dialects/sqlite/BaseSqliteConnection.js +8 -0
  39. package/dialects/sqlite/BaseSqlitePlatform.d.ts +70 -0
  40. package/dialects/sqlite/BaseSqlitePlatform.js +104 -0
  41. package/dialects/sqlite/SqliteExceptionConverter.d.ts +9 -0
  42. package/dialects/sqlite/SqliteExceptionConverter.js +54 -0
  43. package/dialects/sqlite/SqliteNativeQueryBuilder.d.ts +6 -0
  44. package/dialects/sqlite/SqliteNativeQueryBuilder.js +11 -0
  45. package/dialects/sqlite/SqliteSchemaHelper.d.ts +38 -0
  46. package/dialects/sqlite/SqliteSchemaHelper.js +379 -0
  47. package/dialects/sqlite/index.d.ts +5 -0
  48. package/dialects/sqlite/index.js +5 -0
  49. package/index.d.ts +19 -0
  50. package/index.js +19 -0
  51. package/package.json +3 -3
  52. package/plugin/index.d.ts +53 -0
  53. package/plugin/index.js +42 -0
  54. package/plugin/transformer.d.ts +115 -0
  55. package/plugin/transformer.js +883 -0
  56. package/query/ArrayCriteriaNode.d.ts +11 -0
  57. package/query/ArrayCriteriaNode.js +24 -0
  58. package/query/CriteriaNode.d.ts +29 -0
  59. package/query/CriteriaNode.js +121 -0
  60. package/query/CriteriaNodeFactory.d.ts +12 -0
  61. package/query/CriteriaNodeFactory.js +90 -0
  62. package/query/NativeQueryBuilder.d.ts +108 -0
  63. package/query/NativeQueryBuilder.js +425 -0
  64. package/query/ObjectCriteriaNode.d.ts +19 -0
  65. package/query/ObjectCriteriaNode.js +249 -0
  66. package/query/QueryBuilder.d.ts +389 -0
  67. package/query/QueryBuilder.js +1558 -0
  68. package/query/QueryBuilderHelper.d.ts +73 -0
  69. package/query/QueryBuilderHelper.js +756 -0
  70. package/query/ScalarCriteriaNode.d.ts +10 -0
  71. package/query/ScalarCriteriaNode.js +49 -0
  72. package/query/enums.d.ts +18 -0
  73. package/query/enums.js +20 -0
  74. package/query/index.d.ts +10 -0
  75. package/query/index.js +10 -0
  76. package/query/raw.d.ts +59 -0
  77. package/query/raw.js +68 -0
  78. package/schema/DatabaseSchema.d.ts +45 -0
  79. package/schema/DatabaseSchema.js +185 -0
  80. package/schema/DatabaseTable.d.ts +68 -0
  81. package/schema/DatabaseTable.js +793 -0
  82. package/schema/SchemaComparator.d.ts +58 -0
  83. package/schema/SchemaComparator.js +577 -0
  84. package/schema/SchemaHelper.d.ts +76 -0
  85. package/schema/SchemaHelper.js +545 -0
  86. package/schema/SqlSchemaGenerator.d.ts +65 -0
  87. package/schema/SqlSchemaGenerator.js +375 -0
  88. package/schema/index.d.ts +5 -0
  89. package/schema/index.js +5 -0
  90. package/typings.d.ts +272 -0
  91. package/typings.js +1 -0
@@ -0,0 +1,38 @@
1
+ import { type Constructor, type EntityManager, type EntityRepository, type IDatabaseDriver, type IsolationLevel, type MikroORM, Platform } from '@mikro-orm/core';
2
+ import { SqlSchemaGenerator } from './schema/SqlSchemaGenerator.js';
3
+ import { type SchemaHelper } from './schema/SchemaHelper.js';
4
+ import type { IndexDef } from './typings.js';
5
+ import { NativeQueryBuilder } from './query/NativeQueryBuilder.js';
6
+ export declare abstract class AbstractSqlPlatform extends Platform {
7
+ protected readonly schemaHelper?: SchemaHelper;
8
+ usesPivotTable(): boolean;
9
+ indexForeignKeys(): boolean;
10
+ getRepositoryClass<T extends object>(): Constructor<EntityRepository<T>>;
11
+ getSchemaHelper(): SchemaHelper | undefined;
12
+ /** @inheritDoc */
13
+ lookupExtensions(orm: MikroORM): void;
14
+ getSchemaGenerator(driver: IDatabaseDriver, em?: EntityManager): SqlSchemaGenerator;
15
+ /** @internal */
16
+ createNativeQueryBuilder(): NativeQueryBuilder;
17
+ getBeginTransactionSQL(options?: {
18
+ isolationLevel?: IsolationLevel;
19
+ readOnly?: boolean;
20
+ }): string[];
21
+ getCommitTransactionSQL(): string;
22
+ getRollbackTransactionSQL(): string;
23
+ getSavepointSQL(savepointName: string): string;
24
+ getRollbackToSavepointSQL(savepointName: string): string;
25
+ getReleaseSavepointSQL(savepointName: string): string;
26
+ quoteValue(value: any): string;
27
+ escape(value: any): string;
28
+ getSearchJsonPropertySQL(path: string, type: string, aliased: boolean): string;
29
+ getSearchJsonPropertyKey(path: string[], type: string, aliased: boolean, value?: unknown): string;
30
+ getJsonIndexDefinition(index: IndexDef): string[];
31
+ supportsSchemas(): boolean;
32
+ /** @inheritDoc */
33
+ generateCustomOrder(escapedColumn: string, values: unknown[]): string;
34
+ /**
35
+ * @internal
36
+ */
37
+ getOrderByExpression(column: string, direction: string): string[];
38
+ }
@@ -0,0 +1,104 @@
1
+ import SqlString from 'sqlstring';
2
+ import { isRaw, JsonProperty, Platform, raw, Utils, } from '@mikro-orm/core';
3
+ import { SqlEntityRepository } from './SqlEntityRepository.js';
4
+ import { SqlSchemaGenerator } from './schema/SqlSchemaGenerator.js';
5
+ import { NativeQueryBuilder } from './query/NativeQueryBuilder.js';
6
+ export class AbstractSqlPlatform extends Platform {
7
+ schemaHelper;
8
+ usesPivotTable() {
9
+ return true;
10
+ }
11
+ indexForeignKeys() {
12
+ return true;
13
+ }
14
+ getRepositoryClass() {
15
+ return SqlEntityRepository;
16
+ }
17
+ getSchemaHelper() {
18
+ return this.schemaHelper;
19
+ }
20
+ /** @inheritDoc */
21
+ lookupExtensions(orm) {
22
+ SqlSchemaGenerator.register(orm);
23
+ }
24
+ /* v8 ignore next: kept for type inference only */
25
+ getSchemaGenerator(driver, em) {
26
+ return new SqlSchemaGenerator(em ?? driver);
27
+ }
28
+ /** @internal */
29
+ /* v8 ignore next */
30
+ createNativeQueryBuilder() {
31
+ return new NativeQueryBuilder(this);
32
+ }
33
+ getBeginTransactionSQL(options) {
34
+ if (options?.isolationLevel) {
35
+ return [`set transaction isolation level ${options.isolationLevel}`, 'begin'];
36
+ }
37
+ return ['begin'];
38
+ }
39
+ getCommitTransactionSQL() {
40
+ return 'commit';
41
+ }
42
+ getRollbackTransactionSQL() {
43
+ return 'rollback';
44
+ }
45
+ getSavepointSQL(savepointName) {
46
+ return `savepoint ${this.quoteIdentifier(savepointName)}`;
47
+ }
48
+ getRollbackToSavepointSQL(savepointName) {
49
+ return `rollback to savepoint ${this.quoteIdentifier(savepointName)}`;
50
+ }
51
+ getReleaseSavepointSQL(savepointName) {
52
+ return `release savepoint ${this.quoteIdentifier(savepointName)}`;
53
+ }
54
+ quoteValue(value) {
55
+ if (isRaw(value)) {
56
+ return this.formatQuery(value.sql, value.params);
57
+ }
58
+ if (Utils.isPlainObject(value) || value?.[JsonProperty]) {
59
+ return this.escape(JSON.stringify(value));
60
+ }
61
+ return this.escape(value);
62
+ }
63
+ escape(value) {
64
+ return SqlString.escape(value, true, this.timezone);
65
+ }
66
+ getSearchJsonPropertySQL(path, type, aliased) {
67
+ return this.getSearchJsonPropertyKey(path.split('->'), type, aliased);
68
+ }
69
+ getSearchJsonPropertyKey(path, type, aliased, value) {
70
+ const [a, ...b] = path;
71
+ const quoteKey = (key) => key.match(/^[a-z]\w*$/i) ? key : `"${key}"`;
72
+ if (aliased) {
73
+ return raw(alias => `json_extract(${this.quoteIdentifier(`${alias}.${a}`)}, '$.${b.map(quoteKey).join('.')}')`);
74
+ }
75
+ return raw(`json_extract(${this.quoteIdentifier(a)}, '$.${b.map(quoteKey).join('.')}')`);
76
+ }
77
+ getJsonIndexDefinition(index) {
78
+ return index.columnNames
79
+ .map(column => {
80
+ if (!column.includes('.')) {
81
+ return column;
82
+ }
83
+ const [root, ...path] = column.split('.');
84
+ return `(json_extract(${root}, '$.${path.join('.')}'))`;
85
+ });
86
+ }
87
+ supportsSchemas() {
88
+ return false;
89
+ }
90
+ /** @inheritDoc */
91
+ generateCustomOrder(escapedColumn, values) {
92
+ let ret = '(case ';
93
+ values.forEach((v, i) => {
94
+ ret += `when ${escapedColumn} = ${this.quoteValue(v)} then ${i} `;
95
+ });
96
+ return ret + 'else null end)';
97
+ }
98
+ /**
99
+ * @internal
100
+ */
101
+ getOrderByExpression(column, direction) {
102
+ return [`${column} ${direction.toLowerCase()}`];
103
+ }
104
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Martin Adámek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,22 @@
1
+ import { type Dictionary, type EntityMetadata, type EntityProperty, type Primary, type Transaction } from '@mikro-orm/core';
2
+ import { type AbstractSqlDriver } from './AbstractSqlDriver.js';
3
+ export declare class PivotCollectionPersister<Entity extends object> {
4
+ private readonly meta;
5
+ private readonly driver;
6
+ private readonly ctx?;
7
+ private readonly schema?;
8
+ private readonly loggerContext?;
9
+ private readonly inserts;
10
+ private readonly upserts;
11
+ private readonly deletes;
12
+ private readonly batchSize;
13
+ private order;
14
+ constructor(meta: EntityMetadata<Entity>, driver: AbstractSqlDriver, ctx?: Transaction | undefined, schema?: string | undefined, loggerContext?: Dictionary | undefined);
15
+ enqueueUpdate(prop: EntityProperty<Entity>, insertDiff: Primary<Entity>[][], deleteDiff: Primary<Entity>[][] | boolean, pks: Primary<Entity>[], isInitialized?: boolean): void;
16
+ private enqueueInsert;
17
+ private enqueueUpsert;
18
+ private createInsertStatement;
19
+ private enqueueDelete;
20
+ private collectStatements;
21
+ execute(): Promise<void>;
22
+ }
@@ -0,0 +1,159 @@
1
+ class InsertStatement {
2
+ keys;
3
+ data;
4
+ order;
5
+ constructor(keys, data, order) {
6
+ this.keys = keys;
7
+ this.data = data;
8
+ this.order = order;
9
+ }
10
+ getHash() {
11
+ return JSON.stringify(this.data);
12
+ }
13
+ getData() {
14
+ const data = {};
15
+ this.keys.forEach((key, idx) => data[key] = this.data[idx]);
16
+ return data;
17
+ }
18
+ }
19
+ class DeleteStatement {
20
+ keys;
21
+ cond;
22
+ constructor(keys, cond) {
23
+ this.keys = keys;
24
+ this.cond = cond;
25
+ }
26
+ getHash() {
27
+ return JSON.stringify(this.cond);
28
+ }
29
+ getCondition() {
30
+ const cond = {};
31
+ this.keys.forEach((key, idx) => cond[key] = this.cond[idx]);
32
+ return cond;
33
+ }
34
+ }
35
+ export class PivotCollectionPersister {
36
+ meta;
37
+ driver;
38
+ ctx;
39
+ schema;
40
+ loggerContext;
41
+ inserts = new Map();
42
+ upserts = new Map();
43
+ deletes = new Map();
44
+ batchSize;
45
+ order = 0;
46
+ constructor(meta, driver, ctx, schema, loggerContext) {
47
+ this.meta = meta;
48
+ this.driver = driver;
49
+ this.ctx = ctx;
50
+ this.schema = schema;
51
+ this.loggerContext = loggerContext;
52
+ this.batchSize = this.driver.config.get('batchSize');
53
+ }
54
+ enqueueUpdate(prop, insertDiff, deleteDiff, pks, isInitialized = true) {
55
+ if (insertDiff.length) {
56
+ if (isInitialized) {
57
+ this.enqueueInsert(prop, insertDiff, pks);
58
+ }
59
+ else {
60
+ this.enqueueUpsert(prop, insertDiff, pks);
61
+ }
62
+ }
63
+ if (deleteDiff === true || (Array.isArray(deleteDiff) && deleteDiff.length)) {
64
+ this.enqueueDelete(prop, deleteDiff, pks);
65
+ }
66
+ }
67
+ enqueueInsert(prop, insertDiff, pks) {
68
+ for (const fks of insertDiff) {
69
+ const statement = this.createInsertStatement(prop, fks, pks);
70
+ const hash = statement.getHash();
71
+ if (prop.owner || !this.inserts.has(hash)) {
72
+ this.inserts.set(hash, statement);
73
+ }
74
+ }
75
+ }
76
+ enqueueUpsert(prop, insertDiff, pks) {
77
+ for (const fks of insertDiff) {
78
+ const statement = this.createInsertStatement(prop, fks, pks);
79
+ const hash = statement.getHash();
80
+ if (prop.owner || !this.upserts.has(hash)) {
81
+ this.upserts.set(hash, statement);
82
+ }
83
+ }
84
+ }
85
+ createInsertStatement(prop, fks, pks) {
86
+ const data = prop.owner ? [...fks, ...pks] : [...pks, ...fks];
87
+ const keys = prop.owner
88
+ ? [...prop.inverseJoinColumns, ...prop.joinColumns]
89
+ : [...prop.joinColumns, ...prop.inverseJoinColumns];
90
+ return new InsertStatement(keys, data, this.order++);
91
+ }
92
+ enqueueDelete(prop, deleteDiff, pks) {
93
+ if (deleteDiff === true) {
94
+ const statement = new DeleteStatement(prop.joinColumns, pks);
95
+ this.deletes.set(statement.getHash(), statement);
96
+ return;
97
+ }
98
+ for (const fks of deleteDiff) {
99
+ const data = prop.owner ? [...fks, ...pks] : [...pks, ...fks];
100
+ const keys = prop.owner
101
+ ? [...prop.inverseJoinColumns, ...prop.joinColumns]
102
+ : [...prop.joinColumns, ...prop.inverseJoinColumns];
103
+ const statement = new DeleteStatement(keys, data);
104
+ this.deletes.set(statement.getHash(), statement);
105
+ }
106
+ }
107
+ collectStatements(statements) {
108
+ const items = [];
109
+ for (const statement of statements.values()) {
110
+ items[statement.order] = statement.getData();
111
+ }
112
+ return items.filter(Boolean);
113
+ }
114
+ async execute() {
115
+ if (this.deletes.size > 0) {
116
+ const deletes = [...this.deletes.values()];
117
+ for (let i = 0; i < deletes.length; i += this.batchSize) {
118
+ const chunk = deletes.slice(i, i + this.batchSize);
119
+ const cond = { $or: [] };
120
+ for (const item of chunk) {
121
+ cond.$or.push(item.getCondition());
122
+ }
123
+ await this.driver.nativeDelete(this.meta.className, cond, {
124
+ ctx: this.ctx,
125
+ schema: this.schema,
126
+ loggerContext: this.loggerContext,
127
+ });
128
+ }
129
+ }
130
+ if (this.inserts.size > 0) {
131
+ const filtered = this.collectStatements(this.inserts);
132
+ for (let i = 0; i < filtered.length; i += this.batchSize) {
133
+ const chunk = filtered.slice(i, i + this.batchSize);
134
+ await this.driver.nativeInsertMany(this.meta.className, chunk, {
135
+ ctx: this.ctx,
136
+ schema: this.schema,
137
+ convertCustomTypes: false,
138
+ processCollections: false,
139
+ loggerContext: this.loggerContext,
140
+ });
141
+ }
142
+ }
143
+ if (this.upserts.size > 0) {
144
+ const filtered = this.collectStatements(this.upserts);
145
+ for (let i = 0; i < filtered.length; i += this.batchSize) {
146
+ const chunk = filtered.slice(i, i + this.batchSize);
147
+ await this.driver.nativeUpdateMany(this.meta.className, [], chunk, {
148
+ ctx: this.ctx,
149
+ schema: this.schema,
150
+ convertCustomTypes: false,
151
+ processCollections: false,
152
+ upsert: true,
153
+ onConflictAction: 'ignore',
154
+ loggerContext: this.loggerContext,
155
+ });
156
+ }
157
+ }
158
+ }
159
+ }