Possible to chain requests?

If I have a user table and a usersettings table, the way i create a user now is something like:
await db.transaction(async tx => {
const [{id: userId}] = await tx.insert(users).values(user).returning();
await tx.insert(userSettings).values({...settings, userId});
})
await db.transaction(async tx => {
const [{id: userId}] = await tx.insert(users).values(user).returning();
await tx.insert(userSettings).values({...settings, userId});
})
This takes 2 trips to the DB. Is it possible to somehow chain the requests together such that i send both SQL statements at once? Use case: my db is located in us-west-1 and my user and edge function is located in africa. If both sql statements could be made in one trip, it would lead to a faster response time. If this is not currently possible, is such a feature conceivably possible?
1 Reply
Angelelz
Angelelz12mo ago
This is actually taking 3 or 4 round trips depending on how the transaction is handled You need to send a begin transaction and a commit afterwards Your transaction is doing what it's supposed to do But there are some solutions that people have come up with, that may or may not fit your needs For example, you could have a proxy server, running probably in the same machine (or region) as your database, to which you could send the new user data in on request. This server could be responsible to create the transaction to the database, run the queries and send you back the response Another, possibly easier solution, depending on your business logic and what you need to store in userSettings, would be to use stored procedures
Want results from more Discord servers?
Add your server