@mikro-orm/knex 6.0.0-dev.25 → 6.0.0-dev.250

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 (44) hide show
  1. package/AbstractSqlConnection.d.ts +22 -4
  2. package/AbstractSqlConnection.js +48 -18
  3. package/AbstractSqlDriver.d.ts +33 -25
  4. package/AbstractSqlDriver.js +565 -274
  5. package/AbstractSqlPlatform.d.ts +5 -4
  6. package/AbstractSqlPlatform.js +34 -5
  7. package/MonkeyPatchable.d.ts +1 -0
  8. package/MonkeyPatchable.js +3 -0
  9. package/PivotCollectionPersister.d.ts +17 -0
  10. package/PivotCollectionPersister.js +128 -0
  11. package/README.md +64 -107
  12. package/SqlEntityManager.d.ts +2 -6
  13. package/SqlEntityManager.js +4 -8
  14. package/SqlEntityRepository.d.ts +7 -6
  15. package/SqlEntityRepository.js +11 -8
  16. package/index.mjs +23 -5
  17. package/package.json +8 -8
  18. package/query/ArrayCriteriaNode.d.ts +5 -4
  19. package/query/ArrayCriteriaNode.js +7 -2
  20. package/query/CriteriaNode.d.ts +12 -10
  21. package/query/CriteriaNode.js +30 -17
  22. package/query/CriteriaNodeFactory.d.ts +6 -6
  23. package/query/CriteriaNodeFactory.js +33 -17
  24. package/query/ObjectCriteriaNode.d.ts +5 -4
  25. package/query/ObjectCriteriaNode.js +67 -27
  26. package/query/QueryBuilder.d.ts +58 -26
  27. package/query/QueryBuilder.js +444 -148
  28. package/query/QueryBuilderHelper.d.ts +13 -18
  29. package/query/QueryBuilderHelper.js +194 -167
  30. package/query/ScalarCriteriaNode.d.ts +4 -3
  31. package/query/ScalarCriteriaNode.js +18 -8
  32. package/query/enums.d.ts +7 -0
  33. package/query/enums.js +10 -2
  34. package/schema/DatabaseSchema.d.ts +5 -2
  35. package/schema/DatabaseSchema.js +28 -10
  36. package/schema/DatabaseTable.d.ts +13 -5
  37. package/schema/DatabaseTable.js +385 -60
  38. package/schema/SchemaComparator.d.ts +7 -5
  39. package/schema/SchemaComparator.js +68 -35
  40. package/schema/SchemaHelper.d.ts +7 -5
  41. package/schema/SchemaHelper.js +25 -11
  42. package/schema/SqlSchemaGenerator.d.ts +3 -4
  43. package/schema/SqlSchemaGenerator.js +71 -28
  44. package/typings.d.ts +30 -11
@@ -3,33 +3,43 @@ 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 = this.aliased(this.prop.name, options?.alias);
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
  }
24
+ if (this.payload && typeof this.payload === 'object') {
25
+ const keys = Object.keys(this.payload).filter(key => core_1.Utils.isArrayOperator(key) && Array.isArray(this.payload[key]));
26
+ for (const key of keys) {
27
+ this.payload[key] = JSON.stringify(this.payload[key]);
28
+ }
29
+ }
23
30
  return this.payload;
24
31
  }
32
+ willAutoJoin(qb, alias) {
33
+ return this.shouldJoin();
34
+ }
25
35
  shouldJoin() {
26
36
  if (!this.parent || !this.prop) {
27
37
  return false;
28
38
  }
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;
39
+ switch (this.prop.kind) {
40
+ case core_1.ReferenceKind.ONE_TO_MANY: return true;
41
+ case core_1.ReferenceKind.MANY_TO_MANY: return true;
42
+ case core_1.ReferenceKind.ONE_TO_ONE: return !this.prop.owner;
33
43
  default: return false; // SCALAR, MANY_TO_ONE
34
44
  }
35
45
  }
package/query/enums.d.ts CHANGED
@@ -6,3 +6,10 @@ export declare enum QueryType {
6
6
  UPDATE = "UPDATE",
7
7
  DELETE = "DELETE"
8
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 CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.QueryType = void 0;
3
+ exports.JoinType = exports.QueryType = void 0;
4
4
  var QueryType;
5
5
  (function (QueryType) {
6
6
  QueryType["TRUNCATE"] = "TRUNCATE";
@@ -9,4 +9,12 @@ var QueryType;
9
9
  QueryType["INSERT"] = "INSERT";
10
10
  QueryType["UPDATE"] = "UPDATE";
11
11
  QueryType["DELETE"] = "DELETE";
12
- })(QueryType = exports.QueryType || (exports.QueryType = {}));
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,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
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,19 @@ 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
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;
19
23
  table.comment = comment;
20
24
  this.tables.push(table);
21
25
  if (namespaceName != null) {
@@ -32,30 +36,44 @@ class DatabaseSchema {
32
36
  hasTable(name) {
33
37
  return !!this.getTable(name);
34
38
  }
39
+ setNativeEnums(nativeEnums) {
40
+ this.nativeEnums = nativeEnums;
41
+ this.tables.forEach(t => t.nativeEnums = nativeEnums);
42
+ }
43
+ getNativeEnums() {
44
+ return this.nativeEnums;
45
+ }
35
46
  hasNamespace(namespace) {
36
47
  return this.namespaces.has(namespace);
37
48
  }
38
49
  getNamespaces() {
39
50
  return [...this.namespaces];
40
51
  }
41
- static async create(connection, platform, config, schemaName) {
42
- const schema = new DatabaseSchema(platform, schemaName ?? config.get('schema'));
52
+ static async create(connection, platform, config, schemaName, schemas) {
53
+ const schema = new DatabaseSchema(platform, schemaName ?? config.get('schema') ?? platform.getDefaultSchemaName());
43
54
  const allTables = await connection.execute(platform.getSchemaHelper().getListTablesSQL());
44
55
  const parts = config.get('migrations').tableName.split('.');
45
56
  const migrationsTableName = parts[1] ?? parts[0];
46
57
  const migrationsSchemaName = parts.length > 1 ? parts[0] : config.get('schema', platform.getDefaultSchemaName());
47
58
  const tables = allTables.filter(t => t.table_name !== migrationsTableName || (t.schema_name && t.schema_name !== migrationsSchemaName));
48
- await platform.getSchemaHelper().loadInformationSchema(schema, connection, tables);
59
+ await platform.getSchemaHelper().loadInformationSchema(schema, connection, tables, schemas && schemas.length > 0 ? schemas : undefined);
49
60
  return schema;
50
61
  }
51
62
  static fromMetadata(metadata, platform, config, schemaName) {
52
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);
53
71
  for (const meta of metadata) {
54
72
  const table = schema.addTable(meta.collection, this.getSchemaName(meta, config, schemaName));
55
73
  table.comment = meta.comment;
56
74
  meta.props
57
75
  .filter(prop => this.shouldHaveColumn(meta, prop))
58
- .forEach(prop => table.addColumnFromProperty(prop, meta));
76
+ .forEach(prop => table.addColumnFromProperty(prop, meta, config));
59
77
  meta.indexes.forEach(index => table.addIndex(meta, index, 'index'));
60
78
  meta.uniques.forEach(index => table.addIndex(meta, index, 'unique'));
61
79
  table.addIndex(meta, { properties: meta.props.filter(prop => prop.primary).map(prop => prop.name) }, 'primary');
@@ -75,18 +93,18 @@ class DatabaseSchema {
75
93
  return (meta.schema === '*' ? schema : meta.schema) ?? config.get('schema');
76
94
  }
77
95
  static shouldHaveColumn(meta, prop) {
78
- if (prop.persist === false || !prop.fieldNames) {
96
+ if (prop.persist === false || (prop.columnTypes?.length ?? 0) === 0) {
79
97
  return false;
80
98
  }
81
- if (meta.pivotTable || (core_1.ReferenceType.EMBEDDED && prop.object)) {
99
+ if (meta.pivotTable || (prop.kind === core_1.ReferenceKind.EMBEDDED && prop.object)) {
82
100
  return true;
83
101
  }
84
102
  const getRootProperty = (prop) => prop.embedded ? getRootProperty(meta.properties[prop.embedded[0]]) : prop;
85
103
  const rootProp = getRootProperty(prop);
86
- if (rootProp.reference === core_1.ReferenceType.EMBEDDED) {
104
+ if (rootProp.kind === core_1.ReferenceKind.EMBEDDED) {
87
105
  return prop === rootProp || !rootProp.object;
88
106
  }
89
- 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);
90
108
  }
91
109
  toJSON() {
92
110
  const { platform, namespaces, ...rest } = this;
@@ -1,4 +1,4 @@
1
- import type { Dictionary, EntityMetadata, EntityProperty, NamingStrategy } from '@mikro-orm/core';
1
+ import { type Configuration, type Dictionary, type EntityMetadata, type EntityProperty, type NamingStrategy } from '@mikro-orm/core';
2
2
  import type { SchemaHelper } from './SchemaHelper';
3
3
  import type { CheckDef, Column, ForeignKey, IndexDef } from '../typings';
4
4
  import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
@@ -13,17 +13,23 @@ 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;
21
+ removeColumn(name: string): void;
20
22
  getIndexes(): IndexDef[];
21
23
  getChecks(): CheckDef[];
22
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: 'always' | 'never' | 'smart'): 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
  */
@@ -36,10 +42,12 @@ export declare class DatabaseTable {
36
42
  hasCheck(checkName: string): boolean;
37
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[];