I have a general question that might be more of a ...
# orm-help
b
I have a general question that might be more of a SQL question. I want to create a row in my Transaction table, but my 3rd party API asks for the ID of the Transaction before they create an entry, but I want to do it in parallel. So my general solution would be to create a Transaction, send it to the third party API, wait for a response or error, delete the row with the ID if there is an error, or correlate it if it's successful. Is there a way I can just create the ID for the Transaction table in Prisma and conditionally discard it if there is an error with built in solutions?
l
Something like...
Copy code
goToApi: async (_, args, ctx, info) => {
   const transaction = await ctx.db.mutation.createTransaction(data: {... })

   const api = await fetch(`third party`)

   if (!validated api) ctx.db.mutation.deleteTransaction(where: { id: transaction.id })

   else ctx.db.mutation.updateTransaction(where: {id: transaction.id}, data: {... api.data}})
}
b
Hehe seems like what I was going to do. Wanted to see if there was some way of just creating an ID without the whole row ๐Ÿ™‚
Thank you!
l
If you want to be really efficient, you can use a UUID. Are you familiar with that?
b
@lawjolla I will definitely look at that now ๐Ÿ™‚
l
I can explain it quickly..
b
If you have time. I can always read the manual.
l
A UUID is a unique, randomly generated identifier. You can ask the server to generate one. Then you make a unique field with the UUID on the type, e.g.
Copy code
type Transaction {
   ...
   uuid: ID! @unique
}
Basically, it's a server generated unique ID. So you can later query by its actual ID or its UUID. This is a good pattern for keeping track of things on the client without getting the server id back. The client generates a uuid and can then query the database with that uuid instead of getting the server id
b
I've used @unique, but I didn't think it was 128-bit?
A theoretical question, UUID assumes that it would be unique regardless of which table it is. does @unique also provide that same guarantee?
or will it be unique for that specific table
interesting. I'm going to look at impllementations!
l
"A sample of 3.26*10ยนโถ UUIDs has a 99.99% chance of not having any duplicates." https://towardsdatascience.com/are-uuids-really-unique-57eb80fc2a87
b
I do a lot of crypto development so It's up my alley ๐Ÿ™‚
l
In other words, you can safely assume that a UUID will not have a unique collision, though it's theoretically possible
Something like...
Copy code
goToApi: async (_, args, ctx, info) => {
   const transactionUUID = uuid()

   const api = await fetch(`third party`, transactionUUID)

   if (validated api) ctx.db.mutation.createTransaction(data: { uuid: transactionUUID, ...api.data })

}
Then your API and server data are correlated without jumping through creates and deletes. The downside is you have another field to manage
b
Have you heard of the birthday Paradox?
l
If the concern is UUID collisions, mathematicians a lot smarter than us came up with it and the pattern is used all over computer science for nearly a decade.
b
I'm going to try that implementation you just posted that's super interesting
๐Ÿ‘ 1
Exactly what I was looking for!