@mikro-orm/knex 6.0.0-dev.2 → 6.0.0-dev.200

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 (69) hide show
  1. package/{dist/AbstractSqlConnection.d.ts → AbstractSqlConnection.d.ts} +8 -4
  2. package/{dist/AbstractSqlConnection.js → AbstractSqlConnection.js} +35 -18
  3. package/AbstractSqlDriver.d.ts +73 -0
  4. package/AbstractSqlDriver.js +1085 -0
  5. package/AbstractSqlPlatform.d.ts +22 -0
  6. package/{dist/AbstractSqlPlatform.js → AbstractSqlPlatform.js} +46 -19
  7. package/{dist/MonkeyPatchable.d.ts → MonkeyPatchable.d.ts} +1 -0
  8. package/{dist/MonkeyPatchable.js → MonkeyPatchable.js} +3 -0
  9. package/PivotCollectionPersister.d.ts +17 -0
  10. package/PivotCollectionPersister.js +128 -0
  11. package/{dist/README.md → README.md} +66 -109
  12. package/{dist/SqlEntityManager.d.ts → SqlEntityManager.d.ts} +6 -10
  13. package/{dist/SqlEntityManager.js → SqlEntityManager.js} +5 -10
  14. package/SqlEntityRepository.d.ts +24 -0
  15. package/{dist/SqlEntityRepository.js → SqlEntityRepository.js} +11 -8
  16. package/{dist/index.d.ts → index.d.ts} +1 -0
  17. package/{dist/index.js → index.js} +6 -1
  18. package/index.mjs +211 -0
  19. package/package.json +20 -49
  20. package/query/ArrayCriteriaNode.d.ts +10 -0
  21. package/{dist/query → query}/ArrayCriteriaNode.js +7 -2
  22. package/{dist/query → query}/CriteriaNode.d.ts +11 -9
  23. package/{dist/query → query}/CriteriaNode.js +31 -15
  24. package/query/CriteriaNodeFactory.d.ts +12 -0
  25. package/{dist/query → query}/CriteriaNodeFactory.js +32 -15
  26. package/query/ObjectCriteriaNode.d.ts +15 -0
  27. package/{dist/query → query}/ObjectCriteriaNode.js +68 -21
  28. package/query/QueryBuilder.d.ts +284 -0
  29. package/query/QueryBuilder.js +1218 -0
  30. package/{dist/query → query}/QueryBuilderHelper.d.ts +26 -23
  31. package/query/QueryBuilderHelper.js +661 -0
  32. package/query/ScalarCriteriaNode.d.ts +10 -0
  33. package/{dist/query → query}/ScalarCriteriaNode.js +12 -8
  34. package/query/enums.d.ts +15 -0
  35. package/query/enums.js +20 -0
  36. package/{dist/query → query}/index.js +5 -1
  37. package/{dist/schema → schema}/DatabaseSchema.d.ts +6 -3
  38. package/{dist/schema → schema}/DatabaseSchema.js +35 -25
  39. package/{dist/schema → schema}/DatabaseTable.d.ts +21 -13
  40. package/schema/DatabaseTable.js +645 -0
  41. package/{dist/schema → schema}/SchemaComparator.d.ts +9 -7
  42. package/{dist/schema → schema}/SchemaComparator.js +99 -25
  43. package/{dist/schema → schema}/SchemaHelper.d.ts +20 -13
  44. package/{dist/schema → schema}/SchemaHelper.js +70 -18
  45. package/{dist/schema/SchemaGenerator.d.ts → schema/SqlSchemaGenerator.d.ts} +11 -14
  46. package/{dist/schema/SchemaGenerator.js → schema/SqlSchemaGenerator.js} +149 -63
  47. package/{dist/schema → schema}/index.d.ts +1 -1
  48. package/{dist/schema → schema}/index.js +6 -2
  49. package/{dist/typings.d.ts → typings.d.ts} +45 -22
  50. package/CHANGELOG.md +0 -661
  51. package/dist/AbstractSqlDriver.d.ts +0 -55
  52. package/dist/AbstractSqlDriver.js +0 -644
  53. package/dist/AbstractSqlPlatform.d.ts +0 -21
  54. package/dist/LICENSE +0 -21
  55. package/dist/SqlEntityRepository.d.ts +0 -23
  56. package/dist/index.mjs +0 -26
  57. package/dist/package.json +0 -99
  58. package/dist/query/ArrayCriteriaNode.d.ts +0 -9
  59. package/dist/query/CriteriaNodeFactory.d.ts +0 -12
  60. package/dist/query/ObjectCriteriaNode.d.ts +0 -14
  61. package/dist/query/QueryBuilder.d.ts +0 -211
  62. package/dist/query/QueryBuilder.js +0 -688
  63. package/dist/query/QueryBuilderHelper.js +0 -523
  64. package/dist/query/ScalarCriteriaNode.d.ts +0 -9
  65. package/dist/query/enums.d.ts +0 -8
  66. package/dist/query/enums.js +0 -12
  67. package/dist/schema/DatabaseTable.js +0 -319
  68. /package/{dist/query → query}/index.d.ts +0 -0
  69. /package/{dist/typings.js → typings.js} +0 -0
@@ -3,33 +3,37 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ScalarCriteriaNode = void 0;
4
4
  const core_1 = require("@mikro-orm/core");
5
5
  const CriteriaNode_1 = require("./CriteriaNode");
6
+ const enums_1 = require("./enums");
6
7
  /**
7
8
  * @internal
8
9
  */
9
10
  class ScalarCriteriaNode extends CriteriaNode_1.CriteriaNode {
10
- process(qb, alias) {
11
+ process(qb, options) {
11
12
  if (this.shouldJoin()) {
12
13
  const path = this.getPath();
13
14
  const parentPath = this.parent.getPath(); // the parent is always there, otherwise `shouldJoin` would return `false`
14
15
  const nestedAlias = qb.getAliasForJoinPath(path) || qb.getNextAlias(this.prop?.pivotTable ?? this.entityName);
15
- const field = `${alias}.${this.prop.name}`;
16
- const type = this.prop.reference === core_1.ReferenceType.MANY_TO_MANY ? 'pivotJoin' : 'leftJoin';
16
+ const field = `${options?.alias}.${this.prop.name}`;
17
+ const type = this.prop.kind === core_1.ReferenceKind.MANY_TO_MANY ? enums_1.JoinType.pivotJoin : enums_1.JoinType.leftJoin;
17
18
  qb.join(field, nestedAlias, undefined, type, path);
18
19
  // select the owner as virtual property when joining from 1:1 inverse side, but only if the parent is root entity
19
- if (this.prop.reference === core_1.ReferenceType.ONE_TO_ONE && !parentPath.includes('.')) {
20
+ if (this.prop.kind === core_1.ReferenceKind.ONE_TO_ONE && !parentPath.includes('.') && !qb._fields?.includes(field)) {
20
21
  qb.addSelect(field);
21
22
  }
22
23
  }
23
24
  return this.payload;
24
25
  }
26
+ willAutoJoin(qb, alias) {
27
+ return this.shouldJoin();
28
+ }
25
29
  shouldJoin() {
26
30
  if (!this.parent || !this.prop) {
27
31
  return false;
28
32
  }
29
- switch (this.prop.reference) {
30
- case core_1.ReferenceType.ONE_TO_MANY: return true;
31
- case core_1.ReferenceType.MANY_TO_MANY: return true;
32
- case core_1.ReferenceType.ONE_TO_ONE: return !this.prop.owner;
33
+ switch (this.prop.kind) {
34
+ case core_1.ReferenceKind.ONE_TO_MANY: return true;
35
+ case core_1.ReferenceKind.MANY_TO_MANY: return true;
36
+ case core_1.ReferenceKind.ONE_TO_ONE: return !this.prop.owner;
33
37
  default: return false; // SCALAR, MANY_TO_ONE
34
38
  }
35
39
  }
@@ -0,0 +1,15 @@
1
+ export declare enum QueryType {
2
+ TRUNCATE = "TRUNCATE",
3
+ SELECT = "SELECT",
4
+ COUNT = "COUNT",
5
+ INSERT = "INSERT",
6
+ UPDATE = "UPDATE",
7
+ DELETE = "DELETE"
8
+ }
9
+ export declare enum JoinType {
10
+ leftJoin = "left join",
11
+ innerJoin = "inner join",
12
+ pivotJoin = "pivot join",
13
+ innerJoinLateral = "inner join lateral",
14
+ leftJoinLateral = "left join lateral"
15
+ }
package/query/enums.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JoinType = exports.QueryType = void 0;
4
+ var QueryType;
5
+ (function (QueryType) {
6
+ QueryType["TRUNCATE"] = "TRUNCATE";
7
+ QueryType["SELECT"] = "SELECT";
8
+ QueryType["COUNT"] = "COUNT";
9
+ QueryType["INSERT"] = "INSERT";
10
+ QueryType["UPDATE"] = "UPDATE";
11
+ QueryType["DELETE"] = "DELETE";
12
+ })(QueryType || (exports.QueryType = QueryType = {}));
13
+ var JoinType;
14
+ (function (JoinType) {
15
+ JoinType["leftJoin"] = "left join";
16
+ JoinType["innerJoin"] = "inner join";
17
+ JoinType["pivotJoin"] = "pivot join";
18
+ JoinType["innerJoinLateral"] = "inner join lateral";
19
+ JoinType["leftJoinLateral"] = "left join lateral";
20
+ })(JoinType || (exports.JoinType = JoinType = {}));
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[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);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -1,4 +1,4 @@
1
- import type { Configuration, Dictionary, EntityMetadata } from '@mikro-orm/core';
1
+ import { type Configuration, type Dictionary, type EntityMetadata } from '@mikro-orm/core';
2
2
  import { DatabaseTable } from './DatabaseTable';
3
3
  import type { AbstractSqlConnection } from '../AbstractSqlConnection';
4
4
  import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
@@ -10,14 +10,17 @@ export declare class DatabaseSchema {
10
10
  readonly name: string;
11
11
  private tables;
12
12
  private namespaces;
13
+ private nativeEnums;
13
14
  constructor(platform: AbstractSqlPlatform, name: string);
14
- addTable(name: string, schema: string | undefined | null): DatabaseTable;
15
+ addTable(name: string, schema: string | undefined | null, comment?: string): DatabaseTable;
15
16
  getTables(): DatabaseTable[];
16
17
  getTable(name: string): DatabaseTable | undefined;
17
18
  hasTable(name: string): boolean;
19
+ setNativeEnums(nativeEnums: Dictionary<unknown[]>): void;
20
+ getNativeEnums(): Dictionary<unknown[]>;
18
21
  hasNamespace(namespace: string): boolean;
19
22
  getNamespaces(): string[];
20
- static create(connection: AbstractSqlConnection, platform: AbstractSqlPlatform, config: Configuration, schemaName?: string): Promise<DatabaseSchema>;
23
+ static create(connection: AbstractSqlConnection, platform: AbstractSqlPlatform, config: Configuration, schemaName?: string, schemas?: string[]): Promise<DatabaseSchema>;
21
24
  static fromMetadata(metadata: EntityMetadata[], platform: AbstractSqlPlatform, config: Configuration, schemaName?: string): DatabaseSchema;
22
25
  private static getSchemaName;
23
26
  private static shouldHaveColumn;
@@ -7,15 +7,20 @@ const DatabaseTable_1 = require("./DatabaseTable");
7
7
  * @internal
8
8
  */
9
9
  class DatabaseSchema {
10
+ platform;
11
+ name;
12
+ tables = [];
13
+ namespaces = new Set();
14
+ nativeEnums = {}; // for postgres
10
15
  constructor(platform, name) {
11
16
  this.platform = platform;
12
17
  this.name = name;
13
- this.tables = [];
14
- this.namespaces = new Set();
15
18
  }
16
- addTable(name, schema) {
19
+ addTable(name, schema, comment) {
17
20
  const namespaceName = schema ?? this.name;
18
21
  const table = new DatabaseTable_1.DatabaseTable(this.platform, name, namespaceName);
22
+ table.nativeEnums = this.nativeEnums;
23
+ table.comment = comment;
19
24
  this.tables.push(table);
20
25
  if (namespaceName != null) {
21
26
  this.namespaces.add(namespaceName);
@@ -31,39 +36,44 @@ class DatabaseSchema {
31
36
  hasTable(name) {
32
37
  return !!this.getTable(name);
33
38
  }
39
+ setNativeEnums(nativeEnums) {
40
+ this.nativeEnums = nativeEnums;
41
+ this.tables.forEach(t => t.nativeEnums = nativeEnums);
42
+ }
43
+ getNativeEnums() {
44
+ return this.nativeEnums;
45
+ }
34
46
  hasNamespace(namespace) {
35
47
  return this.namespaces.has(namespace);
36
48
  }
37
49
  getNamespaces() {
38
50
  return [...this.namespaces];
39
51
  }
40
- static async create(connection, platform, config, schemaName) {
41
- const schema = new DatabaseSchema(platform, schemaName ?? config.get('schema'));
42
- const tables = await connection.execute(platform.getSchemaHelper().getListTablesSQL());
43
- for (const t of tables) {
44
- if (t.table_name === config.get('migrations').tableName) {
45
- continue;
46
- }
47
- const table = schema.addTable(t.table_name, t.schema_name);
48
- table.comment = t.table_comment;
49
- const cols = await platform.getSchemaHelper().getColumns(connection, table.name, table.schema);
50
- const indexes = await platform.getSchemaHelper().getIndexes(connection, table.name, table.schema);
51
- const checks = await platform.getSchemaHelper().getChecks(connection, table.name, table.schema);
52
- const pks = await platform.getSchemaHelper().getPrimaryKeys(connection, indexes, table.name, table.schema);
53
- const fks = await platform.getSchemaHelper().getForeignKeys(connection, table.name, table.schema);
54
- const enums = await platform.getSchemaHelper().getEnumDefinitions(connection, checks, table.name, table.schema);
55
- table.init(cols, indexes, checks, pks, fks, enums);
56
- }
52
+ static async create(connection, platform, config, schemaName, schemas) {
53
+ const schema = new DatabaseSchema(platform, schemaName ?? config.get('schema') ?? platform.getDefaultSchemaName());
54
+ const allTables = await connection.execute(platform.getSchemaHelper().getListTablesSQL());
55
+ const parts = config.get('migrations').tableName.split('.');
56
+ const migrationsTableName = parts[1] ?? parts[0];
57
+ const migrationsSchemaName = parts.length > 1 ? parts[0] : config.get('schema', platform.getDefaultSchemaName());
58
+ const tables = allTables.filter(t => t.table_name !== migrationsTableName || (t.schema_name && t.schema_name !== migrationsSchemaName));
59
+ await platform.getSchemaHelper().loadInformationSchema(schema, connection, tables, schemas && schemas.length > 0 ? schemas : undefined);
57
60
  return schema;
58
61
  }
59
62
  static fromMetadata(metadata, platform, config, schemaName) {
60
63
  const schema = new DatabaseSchema(platform, schemaName ?? config.get('schema'));
64
+ const nativeEnums = {};
65
+ for (const meta of metadata) {
66
+ meta.props
67
+ .filter(prop => prop.nativeEnumName)
68
+ .forEach(prop => nativeEnums[prop.nativeEnumName] = prop.items?.map(val => '' + val) ?? []);
69
+ }
70
+ schema.setNativeEnums(nativeEnums);
61
71
  for (const meta of metadata) {
62
72
  const table = schema.addTable(meta.collection, this.getSchemaName(meta, config, schemaName));
63
73
  table.comment = meta.comment;
64
74
  meta.props
65
75
  .filter(prop => this.shouldHaveColumn(meta, prop))
66
- .forEach(prop => table.addColumnFromProperty(prop, meta));
76
+ .forEach(prop => table.addColumnFromProperty(prop, meta, config));
67
77
  meta.indexes.forEach(index => table.addIndex(meta, index, 'index'));
68
78
  meta.uniques.forEach(index => table.addIndex(meta, index, 'unique'));
69
79
  table.addIndex(meta, { properties: meta.props.filter(prop => prop.primary).map(prop => prop.name) }, 'primary');
@@ -83,18 +93,18 @@ class DatabaseSchema {
83
93
  return (meta.schema === '*' ? schema : meta.schema) ?? config.get('schema');
84
94
  }
85
95
  static shouldHaveColumn(meta, prop) {
86
- if (prop.persist === false || !prop.fieldNames) {
96
+ if (prop.persist === false || (prop.columnTypes?.length ?? 0) === 0) {
87
97
  return false;
88
98
  }
89
- if (meta.pivotTable || (core_1.ReferenceType.EMBEDDED && prop.object)) {
99
+ if (meta.pivotTable || (prop.kind === core_1.ReferenceKind.EMBEDDED && prop.object)) {
90
100
  return true;
91
101
  }
92
102
  const getRootProperty = (prop) => prop.embedded ? getRootProperty(meta.properties[prop.embedded[0]]) : prop;
93
103
  const rootProp = getRootProperty(prop);
94
- if (rootProp.reference === core_1.ReferenceType.EMBEDDED) {
104
+ if (rootProp.kind === core_1.ReferenceKind.EMBEDDED) {
95
105
  return prop === rootProp || !rootProp.object;
96
106
  }
97
- return [core_1.ReferenceType.SCALAR, core_1.ReferenceType.MANY_TO_ONE].includes(prop.reference) || (prop.reference === core_1.ReferenceType.ONE_TO_ONE && prop.owner);
107
+ return [core_1.ReferenceKind.SCALAR, core_1.ReferenceKind.MANY_TO_ONE].includes(prop.kind) || (prop.kind === core_1.ReferenceKind.ONE_TO_ONE && prop.owner);
98
108
  }
99
109
  toJSON() {
100
110
  const { platform, namespaces, ...rest } = this;
@@ -1,6 +1,6 @@
1
- import type { Dictionary, EntityMetadata, EntityProperty, NamingStrategy } from '@mikro-orm/core';
1
+ import { type Configuration, type Dictionary, type EntityMetadata, type EntityProperty, type MikroORMOptions, type NamingStrategy } from '@mikro-orm/core';
2
2
  import type { SchemaHelper } from './SchemaHelper';
3
- import type { Check, Column, ForeignKey, Index } from '../typings';
3
+ import type { CheckDef, Column, ForeignKey, IndexDef } from '../typings';
4
4
  import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
5
5
  /**
6
6
  * @internal
@@ -13,33 +13,41 @@ export declare class DatabaseTable {
13
13
  private indexes;
14
14
  private checks;
15
15
  private foreignKeys;
16
+ nativeEnums: Dictionary<unknown[]>;
16
17
  comment?: string;
17
18
  constructor(platform: AbstractSqlPlatform, name: string, schema?: string | undefined);
18
19
  getColumns(): Column[];
19
20
  getColumn(name: string): Column | undefined;
20
- getIndexes(): Index[];
21
- getChecks(): Check[];
22
- init(cols: Column[], indexes: Index[], checks: Check[], pks: string[], fks: Dictionary<ForeignKey>, enums: Dictionary<string[]>): void;
21
+ removeColumn(name: string): void;
22
+ getIndexes(): IndexDef[];
23
+ getChecks(): CheckDef[];
24
+ init(cols: Column[], indexes: IndexDef[] | undefined, checks: CheckDef<unknown>[] | undefined, pks: string[], fks?: Dictionary<ForeignKey>, enums?: Dictionary<string[]>): void;
23
25
  addColumn(column: Column): void;
24
- addColumnFromProperty(prop: EntityProperty, meta: EntityMetadata): void;
26
+ addColumnFromProperty(prop: EntityProperty, meta: EntityMetadata, config: Configuration): void;
25
27
  private getIndexName;
26
- getEntityDeclaration(namingStrategy: NamingStrategy, schemaHelper: SchemaHelper): EntityMetadata;
28
+ getEntityDeclaration(namingStrategy: NamingStrategy, schemaHelper: SchemaHelper, scalarPropertiesForRelations: NonNullable<MikroORMOptions['entityGenerator']['scalarPropertiesForRelations']>): EntityMetadata;
29
+ private foreignKeysToProps;
30
+ private findFkIndex;
31
+ private getIndexProperties;
32
+ private getSafeBaseNameForFkProp;
27
33
  /**
28
34
  * The shortest name is stripped of the default namespace. All other namespaced elements are returned as full-qualified names.
29
35
  */
30
36
  getShortestName(): string;
31
37
  getForeignKeys(): Dictionary<ForeignKey>;
32
38
  hasColumn(columnName: string): boolean;
33
- getIndex(indexName: string): Index | undefined;
39
+ getIndex(indexName: string): IndexDef | undefined;
34
40
  hasIndex(indexName: string): boolean;
35
- getCheck(checkName: string): Check<unknown> | undefined;
41
+ getCheck(checkName: string): CheckDef<unknown> | undefined;
36
42
  hasCheck(checkName: string): boolean;
37
- getPrimaryKey(): Index | undefined;
43
+ getPrimaryKey(): IndexDef | undefined;
38
44
  hasPrimaryKey(): boolean;
45
+ private getForeignKeyDeclaration;
39
46
  private getPropertyDeclaration;
40
- private getReferenceType;
47
+ private getReferenceKind;
41
48
  private getPropertyName;
42
- private getPropertyType;
49
+ private getPropertyTypeForForeignKey;
50
+ private getPropertyTypeForColumn;
43
51
  private getPropertyDefaultValue;
44
52
  addIndex(meta: EntityMetadata, index: {
45
53
  properties: string | string[];
@@ -48,6 +56,6 @@ export declare class DatabaseTable {
48
56
  expression?: string;
49
57
  options?: Dictionary;
50
58
  }, type: 'index' | 'unique' | 'primary'): void;
51
- addCheck(check: Check): void;
59
+ addCheck(check: CheckDef): void;
52
60
  toJSON(): Dictionary;
53
61
  }