typed-knex icon indicating copy to clipboard operation
typed-knex copied to clipboard

Get ID after inserting row

Open Meldiron opened this issue 3 years ago • 13 comments

Issue type:

[X] Question [ ] Bug report [X] 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)

Is it possible to get ID of the last inserted row? Knex itself has .returning('id') but typedKnex always returns void with insertItem so I could not find a way to achieve this.

Meldiron avatar May 24 '21 12:05 Meldiron

https://github.com/wwwouter/typed-knex/pull/36

Looks awesome :O I will try to implement it tomorrow.

Meldiron avatar May 27 '21 17:05 Meldiron

Thanks! Available in 4.3.0 https://github.com/wwwouter/typed-knex#insertItemWithReturning

wwwouter avatar May 27 '21 18:05 wwwouter

I have tested it out and got 2 problems. My console log is:

(in warning colors) .returning() is not supported by mysql and will not have any effect.
TEST 4

My code is:

const userData = await SQLManager.typedKnex
  .query(TableCustomers)
  .insertItemWithReturning(
    new TableCustomers({
      email: body.email,
      password: encryptedPass,
      agreeNewsletter: body.agreeNewsletter,
    }),
    ['id']
  );

console.log('TEST', userData);

Meanwhile, the autocomplete is: image

  • Why does it say .returning() is not supported? Is this function deprecated, should we be using something else?
  • Why does autocomplete suggest .id as a number if the object itself seems to be the number?

Meldiron avatar May 31 '21 08:05 Meldiron

  • It seems that returning is only supported by PostgreSQL, MSSQL, and Oracle databases: https://knexjs.org/#Builder-returning

  • The function returns Pick<TableCustomers, 'id'> and not number.

wwwouter avatar Jun 05 '21 13:06 wwwouter

@wwwouter You should also implement this for update in addition to insert. It's been very useful for us so far, but we've been having to make 2 queries on update. updateItemWithReturning would be huge!

bgilman-nyk avatar Jan 06 '22 21:01 bgilman-nyk

@bgilman-nyk I added this to v4.5.0. Can you check if this is what you actually need?

wwwouter avatar Jan 12 '22 10:01 wwwouter

@wwwouter I see the change on your repo. Exactly what we need! But when I grabbed latest from npm, it looks like while the src of the package has been updated, the changes are still not reflected in dist

bgilman-nyk avatar Jan 12 '22 12:01 bgilman-nyk

Not sure what went wrong. Can you try v4.5.1?

wwwouter avatar Jan 12 '22 13:01 wwwouter

Fantastic, thank you!

bgilman-nyk avatar Jan 12 '22 13:01 bgilman-nyk

@Meldiron Can this be closed?

wwwouter avatar Jan 12 '22 18:01 wwwouter

@wwwouter I think the types of these functions don't match what they actually return. For example, if I have some table with an aliased ID field:

@Table("foo")
class Foo {
    @Column({ primary: true, name: "foo_id" })
    id: number
    
    // ...
}

If I do typedKnex.query(Foo).insertItemWithReturning({ ... }, ["id"]), the type on that is something close to Pick<Foo, "id"> but what I actually get is something of type { foo_id: number }.

Seems like either the types should be changed to reflect that, or the functions should be changed to alias the returned columns back to their property names

bam365 avatar Feb 23 '22 08:02 bam365

I see the alias mapping is omitted. Will fix.

wwwouter avatar Mar 02 '22 13:03 wwwouter

@bam365 Could you check to see if it works in v4.7.0?

wwwouter avatar Mar 15 '22 20:03 wwwouter