'get the last record id using bookshelf.js/knex.js[mysql] + express.js

for example, if I have 2 tables(invoices , bills items which include the quantity ) connected to each other through a one to many relationships . and a post api gives me the data . the new bill id will be the last id+1 so if I can make a loop that will make the objects be inserted then run

  knex.insert(arrayPurchaseItems, 'id').into('purchaseRequests').then()

so I don't need to wait for the bill insertion to be resolved however, the risk of the purchase request insertion starting before bill inserting completion exist . any ideas ??



Solution 1:[1]

knex.select().from('table_name').orderBy([{column:"id",order:"desc"}]).limit(1)

Here we are taking id and sorting it in reverse order.

Then take the last result by limiting the result

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shah Vipul