wwwouter / typed-knex

A TypeScript wrapper for Knex.js
MIT License
113 stars 13 forks source link

findByPrimaryKey Not accepting all columns #19

Closed Meldiron closed 4 years ago

Meldiron commented 4 years ago

Issue type:

[ ] Question [X] Bug report [ ] Feature request [ ] Documentation issue

Database system/driver:

[ ] Postgres [ ] MSSQL [X] MySQL [ ] MariaDB [ ] SQLite3 [ ] Oracle [ ] Amazon Redshift

typed-knex version:

[X] latest [ ] @next [ ] 0.x.x (or put your version here)

Description:

My code:

const companyData = await SQLManager.typedKnex.query(TableCompany)
      .findByPrimaryKey(companyId, (r) => r.subscription);

My definitions:

@Table('company')
export class TableCompany {
  @Column({ name: 'id', primary: true })
  id: number;
  @Column({ name: 'tag' })
  tag: string | null;
  @Column({ name: 'name' })
  name: string | null;
  @Column({ name: 'email' })
  email: string | null;
  @Column({ name: 'password' })
  password: string | null;
  @Column({ name: 'activation_code' })
  activationCode: string | null;
  @Column({ name: 'resetpass_code' })
  resetpassCode: string | null;
  @Column({ name: 'google_data' })
  googleData: string | null;
  @Column({ name: 'facebook_data' })
  facebookData: string | null;
  @Column({ name: 'subscription' })
  subscription: Date;
  @Column({ name: 'created_at' })
  createdAt: Date;
}

Error on line .findByPrimaryKey(companyId, (r) => r.subscription);, error is: Property 'subscription' does not exist on type 'TransformPropsToFunctionsOnlyLevel1<TableCompany>'.ts(2339)

I noticed that both subscription and createdAt are of type Date and both of them throws this typescript error. rest of properties are working fine (see screenshot).

image

Solution would be to use .select() and .where() - this solution works perfectly with these Date-typed properties.

wwwouter commented 4 years ago

In my code I've always used string for dates, because most of the time the input and output isn't a real Date object. Nevertheless, it should be possible to have it in your table definition. I updated the code and added some testcases.

Can you try v2.16.0?

Meldiron commented 4 years ago

Tried with 2.17, works perfectly.