Title
d

Dev__

08/13/2021, 2:38 PM
hi, I am still having some issues with the interactive transactions API. for some reason it says this
Argument of type '() => Promise<Integration[]>' is not assignable to parameter of type 'PrismaPromise<any>[]'
const [updatedIntegration] = await this.prisma.$transaction(async () => {
    const method = ...

	const updatedIntegration = await this.prisma.integration.update({
		data: {
			name,
			username,
			password: hashedPassword,
			descriptionCharacterLimit,
			integrationMethodId: method.id
		},
		where: {
			id: integration.id
		}
	});

	// some more queries...

	return [updatedIntegration];
});
v

Victor Iris

08/13/2021, 2:40 PM
Hi! transactions expect you to pass a Promise. But you are passing the result of a Promise inside a custom Promise.
Try creating an empty array and then push to it ONLY the prisma promises that you want to make (do not await their result)
d

Dev__

08/13/2021, 2:43 PM
@Victor Iris
const updatedIntegration = await this.prisma.$transaction(async () => {
    const method = ...

	const updatedIntegration = this.prisma.integration.update({
		data: {
			name,
			username,
			password: hashedPassword,
			descriptionCharacterLimit,
			integrationMethodId: method.id
		},
		where: {
			id: integration.id
		}
	});

	// some more queries...

	return updatedIntegration;
});
like this?
not sure I understand what you mean 😅
so I need to return all updates/deletions/inserts as a promise instead of returning/executing with await
v

Victor Iris

08/13/2021, 2:46 PM
let items = []
for (const integration of integrations) { const newPromise = this.prisma.integration.update({ data: { name, username, password: hashedPassword, descriptionCharacterLimit, integrationMethodId: method.id }, where: { id: integration.id } }); items.push(newPromise) } const itemsResults = await this.prisma.$transaction(items)
@Dev__ is this example helpful?
d

Dev__

08/13/2021, 2:52 PM
yes, I think so, let me try something
so it didnt work. i tried this simple transaction query (almost the same exampe as in the docs)
const trans = await this.prisma.$transaction(async () => {
	return this.prisma.integration.update({
		data: {
			name,
			username,
			password: hashedPassword,
			descriptionCharacterLimit,
			integrationMethodId: method.id
		},
		where: {
			id: integration.id
		}
	});
});
still:
Argument of type '() => Promise<Integration>' is not assignable to parameter of type 'PrismaPromise<any>[]'.
a normal array transaction works, but I need some more conditional insert which I can do with the interactive transaction and not with the array variant
v

Victor Iris

08/13/2021, 3:14 PM
OOOOH, thanks for pointing that out @Dev__. Thats called interactive transactions. Thats a preview feature, do you have it on your schema.prisma client?
d

Dev__

08/13/2021, 3:16 PM
yes, I do
previewFeatures = ["orderByRelation", "referentialActions", "interactiveTransactions"]
also did a generate
👍 1
v

Victor Iris

08/13/2021, 3:25 PM
seeing the same error from my side @Dev__. Would you mind opening an issue on Github?
d

Dev__

08/13/2021, 3:26 PM
yeah sure, i'll do it later today
🙌 1
just to be clear, whats happens if I do the following in an interactive transaction
try {
    await prisma.$transaction(async () => {
        await prisma.delete()
        await prisma.create(); // lets say this throws a prisma unique contraint error

        return prisma.update()
    });
} catch (err) {
    ....
}
does this rollback the
delete
and
create
as well? or do I need to return
delete
and
create
as a promise inside the interactive transation @Victor Iris
r

Ryan

08/16/2021, 8:01 AM
@Dev__ 👋 Works fine for me:
d

Dev__

08/16/2021, 8:03 AM
yeah, I put a comment in the github issue
👍 1
a

Ashrith Reddy

10/25/2021, 1:00 PM
Hello @Ryan,
async function main() {
  const [user] = await prisma.$transaction(async () => {
    // Should the prisma object here need to the argument from this transaction or it can be the global prisma object initialized during application start?
    const user = await prisma.user.create({ data: { name: 'name' }})
    return [user]
  })

  prettyPrint(user)
}
r

Ryan

10/25/2021, 1:03 PM
Argument from the transaction only.
a

Ashrith Reddy

10/25/2021, 1:04 PM
Ok
Does the queries run in parallel, when we use interactive transaction? Can i depend on the previous query data in the transaction?
r

Ryan

10/25/2021, 2:43 PM
Yes, it doesn’t run in parallel.
a

Ashrith Reddy

10/25/2021, 3:45 PM
But for the following example, it is returning false. but it should return true
const clearUser = async () => {
  return prisma.$transaction(async (p) => {
    const user = p.user.update({ where: { id: 1 }, data: { deleted: true }})
    const findUser = await p.user.fineOne({ where: { id: 1 }})
    return findUser.deleted
  })
}

const user =  await clearUser() // should return true, but in actual it is returning false.
r

Ryan

10/25/2021, 5:16 PM
You need to add
await
before the p.user.update call.
a

Ashrith Reddy

10/25/2021, 5:19 PM
Oh sorry. In my real example, it is present. But i found the problem. I had queries inside the functions that are using global prismaclient, instead of the transaction prisma client. that may be the cause of issue. what do you say?