@mikro-orm/knex 6.5.8-dev.8 → 6.5.8

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.
@@ -52,6 +52,7 @@ export declare abstract class AbstractSqlDriver<Connection extends AbstractSqlCo
52
52
  * @internal
53
53
  */
54
54
  mergeJoinedResult<T extends object>(rawResults: EntityData<T>[], meta: EntityMetadata<T>, joinedProps: PopulateOptions<T>[]): EntityData<T>[];
55
+ protected shouldHaveColumn<T, U>(meta: EntityMetadata<T>, prop: EntityProperty<U>, populate: PopulateOptions<U>[], fields?: Field<U>[], exclude?: Field<U>[]): boolean;
55
56
  protected getFieldsForJoinedLoad<T extends object>(qb: QueryBuilder<T, any, any, any>, meta: EntityMetadata<T>, explicitFields?: Field<T>[], exclude?: Field<T>[], populate?: PopulateOptions<T>[], options?: {
56
57
  strategy?: Options['loadStrategy'];
57
58
  populateWhere?: FindOptions<any>['populateWhere'];
@@ -60,7 +61,7 @@ export declare abstract class AbstractSqlDriver<Connection extends AbstractSqlCo
60
61
  /**
61
62
  * @internal
62
63
  */
63
- mapPropToFieldNames<T extends object>(qb: QueryBuilder<T, any, any, any>, prop: EntityProperty<T>, tableAlias?: string): Field<T>[];
64
+ mapPropToFieldNames<T extends object>(qb: QueryBuilder<T, any, any, any>, prop: EntityProperty<T>, tableAlias?: string, explicitFields?: Field<T>[]): Field<T>[];
64
65
  /** @internal */
65
66
  createQueryBuilder<T extends object>(entityName: EntityName<T> | QueryBuilder<T, any, any, any>, ctx?: Transaction<Knex.Transaction>, preferredConnectionType?: ConnectionType, convertCustomTypes?: boolean, loggerContext?: LoggingOptions, alias?: string, em?: SqlEntityManager): QueryBuilder<T, any, any, any>;
66
67
  protected resolveConnectionType(args: {
@@ -922,25 +922,25 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
922
922
  }
923
923
  return res;
924
924
  }
925
+ shouldHaveColumn(meta, prop, populate, fields, exclude) {
926
+ if (!this.platform.shouldHaveColumn(prop, populate, exclude)) {
927
+ return false;
928
+ }
929
+ if (!fields || fields.includes('*') || prop.primary || meta.root.discriminatorColumn === prop.name) {
930
+ return true;
931
+ }
932
+ return fields.some(f => f === prop.name || f.toString().startsWith(prop.name + '.'));
933
+ }
925
934
  getFieldsForJoinedLoad(qb, meta, explicitFields, exclude, populate = [], options, parentTableAlias, parentJoinPath) {
926
935
  const fields = [];
927
936
  const joinedProps = this.joinedProps(meta, populate, options);
928
- const shouldHaveColumn = (meta, prop, populate, fields) => {
929
- if (!this.platform.shouldHaveColumn(prop, populate, exclude)) {
930
- return false;
931
- }
932
- if (!fields || fields.includes('*') || prop.primary || meta.root.discriminatorColumn === prop.name) {
933
- return true;
934
- }
935
- return fields.some(f => f === prop.name || f.toString().startsWith(prop.name + '.'));
936
- };
937
937
  const populateWhereAll = options?._populateWhere === 'all' || core_1.Utils.isEmpty(options?._populateWhere);
938
938
  // root entity is already handled, skip that
939
939
  if (parentJoinPath) {
940
940
  // alias all fields in the primary table
941
941
  meta.props
942
- .filter(prop => shouldHaveColumn(meta, prop, populate, explicitFields))
943
- .forEach(prop => fields.push(...this.mapPropToFieldNames(qb, prop, parentTableAlias)));
942
+ .filter(prop => this.shouldHaveColumn(meta, prop, populate, explicitFields, exclude))
943
+ .forEach(prop => fields.push(...this.mapPropToFieldNames(qb, prop, parentTableAlias, explicitFields)));
944
944
  }
945
945
  for (const hint of joinedProps) {
946
946
  const [propName, ref] = hint.field.split(':', 2);
@@ -991,10 +991,14 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
991
991
  /**
992
992
  * @internal
993
993
  */
994
- mapPropToFieldNames(qb, prop, tableAlias) {
994
+ mapPropToFieldNames(qb, prop, tableAlias, explicitFields) {
995
995
  if (prop.kind === core_1.ReferenceKind.EMBEDDED && !prop.object) {
996
- return Object.values(prop.embeddedProps).flatMap(childProp => {
997
- return this.mapPropToFieldNames(qb, childProp, tableAlias);
996
+ return Object.entries(prop.embeddedProps).flatMap(([name, childProp]) => {
997
+ const childFields = explicitFields ? core_1.Utils.extractChildElements(explicitFields, prop.name) : [];
998
+ if (childFields.length > 0 && !this.shouldHaveColumn(prop.targetMeta, { ...childProp, name }, [], childFields)) {
999
+ return [];
1000
+ }
1001
+ return this.mapPropToFieldNames(qb, childProp, tableAlias, childFields);
998
1002
  });
999
1003
  }
1000
1004
  const aliased = this.platform.quoteIdentifier(tableAlias ? `${tableAlias}__${prop.fieldNames[0]}` : prop.fieldNames[0]);
@@ -1286,7 +1290,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
1286
1290
  }
1287
1291
  }
1288
1292
  else if (!core_1.Utils.isEmpty(options.exclude) || lazyProps.some(p => !p.formula && (p.kind !== '1:1' || p.owner))) {
1289
- const props = meta.props.filter(prop => this.platform.shouldHaveColumn(prop, populate, options.exclude, false));
1293
+ const props = meta.props.filter(prop => this.platform.shouldHaveColumn(prop, populate, options.exclude, false, false));
1290
1294
  ret.push(...props.filter(p => !lazyProps.includes(p)).map(p => p.name));
1291
1295
  addFormulas = true;
1292
1296
  }
package/index.mjs CHANGED
@@ -241,6 +241,7 @@ export const expandDotPaths = mod.expandDotPaths;
241
241
  export const getLoadingStrategy = mod.getLoadingStrategy;
242
242
  export const getOnConflictFields = mod.getOnConflictFields;
243
243
  export const getOnConflictReturningFields = mod.getOnConflictReturningFields;
244
+ export const getWhereCondition = mod.getWhereCondition;
244
245
  export const helper = mod.helper;
245
246
  export const knex = mod.knex;
246
247
  export const parseJsonSafe = mod.parseJsonSafe;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.5.8-dev.8",
3
+ "version": "6.5.8",
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",
@@ -63,10 +63,10 @@
63
63
  "sqlstring": "2.3.3"
64
64
  },
65
65
  "devDependencies": {
66
- "@mikro-orm/core": "^6.5.7"
66
+ "@mikro-orm/core": "^6.5.8"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "6.5.8-dev.8",
69
+ "@mikro-orm/core": "^6.0.0",
70
70
  "better-sqlite3": "*",
71
71
  "libsql": "*",
72
72
  "mariadb": "*"
@@ -109,33 +109,34 @@ class DatabaseTable {
109
109
  if (prop.referencedTableName.includes('.')) {
110
110
  schema = undefined;
111
111
  }
112
- this.foreignKeys[constraintName] = {
113
- constraintName,
114
- columnNames: prop.fieldNames,
115
- localTableName: this.getShortestName(),
116
- referencedColumnNames: prop.referencedColumnNames,
117
- referencedTableName: schema ? `${schema}.${prop.referencedTableName}` : prop.referencedTableName,
118
- createForeignKeyConstraint: prop.createForeignKeyConstraint,
119
- };
120
- const cascade = prop.cascade.includes(core_1.Cascade.REMOVE) || prop.cascade.includes(core_1.Cascade.ALL);
121
- if (prop.deleteRule || cascade || prop.nullable) {
122
- this.foreignKeys[constraintName].deleteRule = prop.deleteRule || (cascade ? 'cascade' : 'set null');
123
- }
124
- if (prop.updateRule) {
125
- this.foreignKeys[constraintName].updateRule = prop.updateRule || 'cascade';
126
- }
127
- if ((prop.cascade.includes(core_1.Cascade.PERSIST) || prop.cascade.includes(core_1.Cascade.ALL))) {
128
- const hasCascadePath = Object.values(this.foreignKeys).some(fk => {
129
- return fk.constraintName !== constraintName
130
- && ((fk.updateRule && fk.updateRule !== 'no action') || (fk.deleteRule && fk.deleteRule !== 'no action'))
131
- && fk.referencedTableName === this.foreignKeys[constraintName].referencedTableName;
132
- });
133
- if (!hasCascadePath || this.platform.supportsMultipleCascadePaths()) {
134
- this.foreignKeys[constraintName].updateRule ??= 'cascade';
112
+ if (prop.createForeignKeyConstraint) {
113
+ this.foreignKeys[constraintName] = {
114
+ constraintName,
115
+ columnNames: prop.fieldNames,
116
+ localTableName: this.getShortestName(),
117
+ referencedColumnNames: prop.referencedColumnNames,
118
+ referencedTableName: schema ? `${schema}.${prop.referencedTableName}` : prop.referencedTableName,
119
+ };
120
+ const cascade = prop.cascade.includes(core_1.Cascade.REMOVE) || prop.cascade.includes(core_1.Cascade.ALL);
121
+ if (prop.deleteRule || cascade || prop.nullable) {
122
+ this.foreignKeys[constraintName].deleteRule = prop.deleteRule || (cascade ? 'cascade' : 'set null');
123
+ }
124
+ if (prop.updateRule) {
125
+ this.foreignKeys[constraintName].updateRule = prop.updateRule || 'cascade';
126
+ }
127
+ if ((prop.cascade.includes(core_1.Cascade.PERSIST) || prop.cascade.includes(core_1.Cascade.ALL))) {
128
+ const hasCascadePath = Object.values(this.foreignKeys).some(fk => {
129
+ return fk.constraintName !== constraintName
130
+ && ((fk.updateRule && fk.updateRule !== 'no action') || (fk.deleteRule && fk.deleteRule !== 'no action'))
131
+ && fk.referencedTableName === this.foreignKeys[constraintName].referencedTableName;
132
+ });
133
+ if (!hasCascadePath || this.platform.supportsMultipleCascadePaths()) {
134
+ this.foreignKeys[constraintName].updateRule ??= 'cascade';
135
+ }
136
+ }
137
+ if (prop.deferMode) {
138
+ this.foreignKeys[constraintName].deferMode = prop.deferMode;
135
139
  }
136
- }
137
- if (prop.deferMode) {
138
- this.foreignKeys[constraintName].deferMode = prop.deferMode;
139
140
  }
140
141
  }
141
142
  if (prop.index) {
@@ -268,7 +268,7 @@ class SchemaComparator {
268
268
  }
269
269
  for (const toConstraint of Object.values(toForeignKeys)) {
270
270
  tableDifferences.addedForeignKeys[toConstraint.constraintName] = toConstraint;
271
- this.log(`FK constraint ${toConstraint.constraintName} added from table ${tableDifferences.name}`, { constraint: toConstraint });
271
+ this.log(`FK constraint ${toConstraint.constraintName} added to table ${tableDifferences.name}`, { constraint: toConstraint });
272
272
  changes++;
273
273
  }
274
274
  return changes ? tableDifferences : false;
@@ -321,7 +321,7 @@ class SchemaHelper {
321
321
  });
322
322
  }
323
323
  createForeignKey(table, foreignKey, schema) {
324
- if (!this.options.createForeignKeyConstraints || !foreignKey.createForeignKeyConstraint) {
324
+ if (!this.options.createForeignKeyConstraints) {
325
325
  return;
326
326
  }
327
327
  const builder = table
package/typings.d.ts CHANGED
@@ -60,7 +60,6 @@ export interface ForeignKey {
60
60
  updateRule?: string;
61
61
  deleteRule?: string;
62
62
  deferMode?: DeferMode;
63
- createForeignKeyConstraint: boolean;
64
63
  }
65
64
  export interface IndexDef {
66
65
  columnNames: string[];