Closed kryptus36 closed 1 month ago
I like to use destructuring:
const [{id: gtId}] = (await this.db`INSERT INTO game_types ${this.db(gameType)} RETURNING id`);
gameType.id = gtId;
This takes the first element of the result set, pulls the id
value into gtId
that you can then assign to your gameType var.
Thanks, that's what I wanted but couldn't find the right syntax. Ultimately I settled on returning * and getting the whole object back. Same idea though, destructuring it works well.
I'm just checking to see if there's a better way to attach a postgres-generated ID to an existing object?
gameType.id = (await this.db
INSERT INTO game_types ${this.db(gameType)} RETURNING id
)[0].id;This works, but doesn't feel like it's the best way to do it.