ForbesLindesay/atdatabases

documentation on insert ID

Opened this issue · 2 comments

Seems like an obvious feature, but I couldn't find anything in documentation.
Once I do INSERT either with query() or with insert() via mysql typed, how do I get last inserted id?

I looked at the code and looks like insert returns void..
I tried to use LAST_INSERT_ID() but that didn't work.

Looks like if I add SELECT LAST_INSERT_ID(); directly into the same query within INSERT then I can get the value..
https://github.com/ForbesLindesay/atdatabases/blob/master/packages/mysql-typed/src/index.ts#L283

but I'm not sure

  1. how much do we want mysql-typed interface to be compatible with other packages
  2. what to do in case of bulk insert

I think it's ok for mysql-typed to be different from pg-typed. Much of the point of having separate packages is to avoid the "lowest common denominator" problem that many ORMs suffer from. E.g. in Postgres we return the entire inserted row as that's fairly easy to do. In MySQL we should probably return just the LAST_INSERT_ID() as you suggest. I'd be happy to review/accept a PR to add this feature.