@mikro-orm/knex 6.0.0-dev.61 → 6.0.0-dev.62

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.
@@ -846,7 +846,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
846
846
  ret.push((0, core_1.raw)(`${prop.formula(alias)} as ${aliased}`));
847
847
  });
848
848
  meta.props
849
- .filter(prop => prop.customType?.convertToDatabaseValueSQL || prop.customType?.convertToJSValueSQL)
849
+ .filter(prop => prop.hasConvertToDatabaseValueSQL || prop.hasConvertToJSValueSQL)
850
850
  .forEach(prop => ret.push(prop.name));
851
851
  }
852
852
  return ret.length > 0 ? core_1.Utils.unique(ret) : ['*'];
@@ -14,6 +14,7 @@ export declare abstract class AbstractSqlPlatform extends Platform {
14
14
  quoteValue(value: any): string;
15
15
  formatQuery(sql: string, params: readonly any[]): string;
16
16
  getSearchJsonPropertySQL(path: string, type: string, aliased: boolean): string;
17
+ getSearchJsonPropertyKey(path: string[], type: string, aliased: boolean): string;
17
18
  isRaw(value: any): boolean;
18
19
  supportsSchemas(): boolean;
19
20
  /** @inheritDoc */
@@ -76,6 +76,14 @@ class AbstractSqlPlatform extends core_1.Platform {
76
76
  getSearchJsonPropertySQL(path, type, aliased) {
77
77
  return this.getSearchJsonPropertyKey(path.split('->'), type, aliased);
78
78
  }
79
+ getSearchJsonPropertyKey(path, type, aliased) {
80
+ const [a, ...b] = path;
81
+ const quoteKey = (key) => key.match(/^[a-z]\w*$/i) ? key : `"${key}"`;
82
+ if (aliased) {
83
+ return (0, core_1.raw)(alias => `json_extract(${this.quoteIdentifier(`${alias}.${a}`)}, '$.${b.map(quoteKey).join('.')}')`);
84
+ }
85
+ return (0, core_1.raw)(`json_extract(${this.quoteIdentifier(a)}, '$.${b.map(quoteKey).join('.')}')`);
86
+ }
79
87
  isRaw(value) {
80
88
  return super.isRaw(value) || (typeof value === 'object' && value !== null && value.client && ['Ref', 'Raw'].includes(value.constructor.name));
81
89
  }
@@ -18,5 +18,12 @@ export declare class SqlEntityRepository<T extends object> extends EntityReposit
18
18
  * Returns configured knex instance.
19
19
  */
20
20
  getKnex(type?: ConnectionType): Knex;
21
+ /**
22
+ * @inheritDoc
23
+ */
24
+ getEntityManager(): SqlEntityManager;
25
+ /**
26
+ * @inheritDoc
27
+ */
21
28
  protected get em(): SqlEntityManager;
22
29
  }
@@ -11,7 +11,7 @@ class SqlEntityRepository extends core_1.EntityRepository {
11
11
  * Creates a QueryBuilder instance
12
12
  */
13
13
  createQueryBuilder(alias) {
14
- return this.em.createQueryBuilder(this.entityName, alias);
14
+ return this.getEntityManager().createQueryBuilder(this.entityName, alias);
15
15
  }
16
16
  /**
17
17
  * Shortcut for `createQueryBuilder()`
@@ -23,10 +23,19 @@ class SqlEntityRepository extends core_1.EntityRepository {
23
23
  * Returns configured knex instance.
24
24
  */
25
25
  getKnex(type) {
26
- return this.em.getConnection(type).getKnex();
26
+ return this.getEntityManager().getConnection(type).getKnex();
27
27
  }
28
+ /**
29
+ * @inheritDoc
30
+ */
31
+ getEntityManager() {
32
+ return this._em;
33
+ }
34
+ /**
35
+ * @inheritDoc
36
+ */
28
37
  get em() {
29
- return this._em.getContext(false);
38
+ return this._em;
30
39
  }
31
40
  }
32
41
  exports.SqlEntityRepository = SqlEntityRepository;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.0.0-dev.61",
3
+ "version": "6.0.0-dev.62",
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,9 +63,9 @@
63
63
  "sqlstring": "2.3.3"
64
64
  },
65
65
  "devDependencies": {
66
- "@mikro-orm/core": "^5.6.16"
66
+ "@mikro-orm/core": "^5.7.2"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "~6.0.0-dev.61"
69
+ "@mikro-orm/core": "~6.0.0-dev.62"
70
70
  }
71
71
  }
@@ -97,7 +97,7 @@ class CriteriaNode {
97
97
  [util_1.inspect.custom]() {
98
98
  const o = {};
99
99
  ['entityName', 'key', 'index', 'payload']
100
- .filter(k => this[k] != null)
100
+ .filter(k => this[k] !== undefined)
101
101
  .forEach(k => o[k] = this[k]);
102
102
  return `${this.constructor.name} ${(0, util_1.inspect)(o)}`;
103
103
  }
@@ -37,9 +37,6 @@ class CriteriaNodeFactory {
37
37
  }
38
38
  static createObjectNode(metadata, entityName, payload, parent, key) {
39
39
  const meta = metadata.find(entityName);
40
- if (!parent && Object.keys(payload).every(k => meta?.properties[k]?.kind === core_1.ReferenceKind.SCALAR)) {
41
- return this.createScalarNode(metadata, entityName, payload, parent, key);
42
- }
43
40
  const node = new ObjectCriteriaNode_1.ObjectCriteriaNode(metadata, entityName, parent, key);
44
41
  node.payload = Object.keys(payload).reduce((o, item) => {
45
42
  o[item] = this.createObjectItemNode(metadata, entityName, node, payload, item, meta);
@@ -370,6 +370,7 @@ class QueryBuilder {
370
370
  getKnexQuery() {
371
371
  this.finalize();
372
372
  const qb = this.getQueryBase();
373
+ qb.__raw = true; // tag it as there is now way to check via `instanceof`
373
374
  core_1.Utils.runIfNotEmpty(() => this.helper.appendQueryCondition(this.type ?? enums_1.QueryType.SELECT, this._cond, qb), this._cond && !this._onConflict);
374
375
  core_1.Utils.runIfNotEmpty(() => qb.groupBy(this.prepareFields(this._groupBy, 'groupBy')), this._groupBy);
375
376
  core_1.Utils.runIfNotEmpty(() => this.helper.appendQueryCondition(this.type ?? enums_1.QueryType.SELECT, this._having, qb, undefined, 'having'), this._having);
@@ -709,7 +710,7 @@ class QueryBuilder {
709
710
  });
710
711
  const meta = this.mainAlias.metadata;
711
712
  /* istanbul ignore next */
712
- const requiresSQLConversion = meta?.props.filter(p => p.customType?.convertToJSValueSQL) ?? [];
713
+ const requiresSQLConversion = meta?.props.filter(p => p.hasConvertToJSValueSQL) ?? [];
713
714
  if (this.flags.has(core_1.QueryFlag.CONVERT_CUSTOM_TYPES) && (fields.includes('*') || fields.includes(`${this.mainAlias.aliasName}.*`)) && requiresSQLConversion.length > 0) {
714
715
  requiresSQLConversion.forEach(p => ret.push(this.helper.mapper(p.name, this.type)));
715
716
  }
@@ -940,6 +941,7 @@ class QueryBuilder {
940
941
  // multiple sub-queries are needed to get around mysql limitations with order by + limit + where in + group by (o.O)
941
942
  // https://stackoverflow.com/questions/17892762/mysql-this-version-of-mysql-doesnt-yet-support-limit-in-all-any-some-subqu
942
943
  const subSubQuery = this.getKnex().select(pks).from(knexQuery);
944
+ subSubQuery.__raw = true; // tag it as there is now way to check via `instanceof`
943
945
  this._limit = undefined;
944
946
  this._offset = undefined;
945
947
  this.select(this._fields).where({ [core_1.Utils.getPrimaryKeyHash(meta.primaryKeys)]: { $in: subSubQuery } });
@@ -73,7 +73,7 @@ class QueryBuilderHelper {
73
73
  const as = alias === null ? '' : ` as ${aliased}`;
74
74
  return this.knex.raw(`${prop.formula(alias2)}${as}`);
75
75
  }
76
- if (prop?.customType?.convertToJSValueSQL) {
76
+ if (prop?.hasConvertToJSValueSQL) {
77
77
  const prefixed = this.prefix(field, isTableNameAliasRequired, true);
78
78
  const valueSQL = prop.customType.convertToJSValueSQL(prefixed, this.platform);
79
79
  if (alias === null) {
@@ -301,7 +301,7 @@ class QueryBuilderHelper {
301
301
  }
302
302
  appendOnConflictClause(type, onConflict, qb) {
303
303
  onConflict.forEach(item => {
304
- const sub = qb.onConflict(item.fields);
304
+ const sub = item.fields.length > 0 ? qb.onConflict(item.fields) : qb.onConflict();
305
305
  core_1.Utils.runIfNotEmpty(() => sub.ignore(), item.ignore);
306
306
  core_1.Utils.runIfNotEmpty(() => {
307
307
  let mergeParam = item.merge;
@@ -618,9 +618,10 @@ class QueryBuilderHelper {
618
618
  if (prop.customType && convertCustomTypes && !this.platform.isRaw(data[k])) {
619
619
  data[k] = prop.customType.convertToDatabaseValue(data[k], this.platform, { fromQuery: true, key: k, mode: 'query-data' });
620
620
  }
621
- if (prop.customType && 'convertToDatabaseValueSQL' in prop.customType && !this.platform.isRaw(data[k])) {
621
+ if (prop.hasConvertToDatabaseValueSQL && !this.platform.isRaw(data[k])) {
622
622
  const quoted = this.platform.quoteValue(data[k]);
623
- data[k] = this.knex.raw(prop.customType.convertToDatabaseValueSQL(quoted, this.platform).replace(/\?/, '\\?'));
623
+ const sql = prop.customType.convertToDatabaseValueSQL(quoted, this.platform);
624
+ data[k] = this.knex.raw(sql.replace(/\?/, '\\?'));
624
625
  }
625
626
  if (!prop.customType && (Array.isArray(data[k]) || core_1.Utils.isPlainObject(data[k]))) {
626
627
  data[k] = JSON.stringify(data[k]);
@@ -439,6 +439,11 @@ class SchemaComparator {
439
439
  const defaultValueTo = !['0', 'false', 'f', 'n', 'no', 'off'].includes('' + to.default);
440
440
  return defaultValueFrom === defaultValueTo;
441
441
  }
442
+ if (to.mappedType instanceof core_1.JsonType) {
443
+ const defaultValueFrom = (0, core_1.parseJsonSafe)(from.default.replace(/^'(.*)'$/, '$1'));
444
+ const defaultValueTo = (0, core_1.parseJsonSafe)(to.default?.replace(/^'(.*)'$/, '$1'));
445
+ return core_1.Utils.equals(defaultValueFrom, defaultValueTo);
446
+ }
442
447
  if (to.mappedType instanceof core_1.DateTimeType && from.default && to.default) {
443
448
  // normalize now/current_timestamp defaults, also remove `()` from the end of default expression
444
449
  const defaultValueFrom = from.default.toLowerCase().replace('current_timestamp', 'now').replace(/\(\)$/, '');