khmm12 Github contribution chart
khmm12 Github Stats
khmm12 Most Used Languages

Activity

09 Sep 2022

Khmm12

started

Started On 09 Sep 2022 at 09:46:04

Khmm12

started

Started On 09 Sep 2022 at 09:45:54
Issue Comment

Khmm12

Unable to use nested properties in SELECT clause

Description

When trying to use nested properties in a QueryBuilder.select() to retrieve a single or all embedded fields at once, QueryBuilder throws an error:

Stack trace

TypeError: Cannot read property '0' of undefined
  at QueryBuilderHelper.fieldName (node_modules/@mikro-orm/knex/query/QueryBuilderHelper.js:520:31)
  at QueryBuilderHelper.prefix (node_modules/@mikro-orm/knex/query/QueryBuilderHelper.js:468:36)
  at QueryBuilderHelper.mapper (node_modules/@mikro-orm/knex/query/QueryBuilderHelper.js:65:24)
  at node_modules/@mikro-orm/knex/query/QueryBuilder.js:538:34
  at Array.forEach (<anonymous>)
  at QueryBuilder.prepareFields (node_modules/@mikro-orm/knex/query/QueryBuilder.js:525:16)
  at QueryBuilder.getQueryBase (node_modules/@mikro-orm/knex/query/QueryBuilder.js:591:32)
  at QueryBuilder.getKnexQuery (node_modules/@mikro-orm/knex/query/QueryBuilder.js:298:25)
  at QueryBuilder.execute (node_modules/@mikro-orm/knex/query/QueryBuilder.js:360:28)
  at QueryBuilder.getResultList (node_modules/@mikro-orm/knex/query/QueryBuilder.js:393:30)
  at processTicksAndRejections (internal/process/task_queues.js:95:5)
  at async QueryBuilder.getSingleResult (node_modules/@mikro-orm/knex/query/QueryBuilder.js:403:21) 

To Reproduce Steps to reproduce the behavior:

  1. Declare an embeddable entity
@Embeddable()
export class Address {
  @Property()
  city: string;

  constructor(city: string) {
    this.city = city;
  }
} 
  1. Declare an entity, embedding the previously implemented Address as follows:
@Entity()
export class User {
  @PrimaryKey()
  id!: number;

  @Embedded(() => Address)
  address: Address;

  constructor(address: Address) {
    this.address = address;
  }
} 
  1. Create a QueryBuilder to retrieve address properties:
const user = await em.createQueryBuilder(User)
  .select(['address'])
  .where({
    address: {
      city: 'Paris'
    }
  })
  .getSingleResult(); 

or

const user = await em.createQueryBuilder(User)
  .select(['address'])
  .where({
    address: {
      city: 'Paris'
    }
  })
  .execute('get', false) 

or

const user = await em.createQueryBuilder(User)
  .select(['address.city'])
  .where({
    address: {
      city: 'Paris'
    }
  })
  .execute('get', false) 

Workaround You still can receive a raw result using column names instead of fields as follows:

const user = await em.createQueryBuilder(User)
  .select(['id', 'address_city'])
  .where({
    address: {
      city: 'Paris'
    }
  })
  .execute('get', false);

console.log(em.map(User, user)); 

Actual behavior QueryBuilder throws an error trying to generate a SQL query.

Expected behavior QueryBuilder is able to generate an expected SQL query in accordance with the naming strategy: select "u0"."address_city" from "user" as "u0"

Additional context There is repro-repo here: https://github.com/derevnjuk/mikro-orm-3365

Versions

| Dependency | Version | | - | - | | node | >= 14 | | typescript | >=4 | | mikro-orm | >=5 |

Forked On 03 Sep 2022 at 09:51:43

Khmm12

The same thing when using insert(/*records*/).onConflict(/* fields */).merge(/* fields */).

@Entity()
export class User {
  @PrimaryKey()
  id!: number;

  @Property({ unique: true, length: 255 })
  email!: string

  @Property({ length: 255 })
  fullName!: string

  @Embedded(() => Address)
  address: Address;
}

@Embeddable()
export class Address {
  @Property()
  city: string;
} 
const records = [{ email: 'test@test.test', fullName: 'John Smith', address: { city: 'Los Angeles' } }]

const query = this.em
  .qb(User)
  .insert(records)
  .onConflict(['email'])
  .merge(['fullName', 'address.city'])
  .getQuery() 

Expected:

nsert into "users" ("email", "full_name", "address_city") values ($1, $2, $3) on conflict ("email") do update set "full_name" = excluded."full_name", "address_city" = excluded."address_city" returning "id"; 

Actual:

Commented On 03 Sep 2022 at 09:51:43
Pull Request

Khmm12

fix: add missing type declarations in conditional exports

Created On 23 Aug 2022 at 01:39:12
Create Branch
Khmm12 In khmm12/ms Create Branchfix/types-in-conditional-exports

Khmm12

Tiny millisecond conversion utility

On 23 Aug 2022 at 01:38:56
Forked

Khmm12

Tiny millisecond conversion utility

Forked On 23 Aug 2022 at 01:15:23

Khmm12

started

Started On 14 Jul 2022 at 01:36:06

Khmm12

started

Started On 01 Jul 2022 at 05:34:46