Topi Pihko
03/29/2022, 2:49 PMPrisma.service:
async auth(func: any): Promise<any> {
[ignore, result] = await this.$transaction([
this.$executeRaw`SET local "auth.principal" = "1234567890"`,
func,
]);
return result;
}
Usage:
async getUsers(): Promise<User[]>{
return this.prisma.auth(this.prisma.user.findMany());
}
Tryout with $use, but queries are not run inside the same transaction -> auth.principal is not set when actual query is ran resulting empty array.
Prisma.service:
this.$use(async (params, next) => {
// Set auth variable in a transaction
const setTransActionResult = await next({
args: {
query: 'SET local "auth.principal" = "1234567890"',
parameters: {
values: '[]',
__prismaRawParamaters__: true,
},
},
dataPath: [],
runInTransaction: true,
action: 'executeRaw',
model: undefined,
});
params.runInTransaction = true;
// Run original query here
return await next(params);
});
Usage:
async getUsers(): Promise<User[]>{
return this.prisma.user.findMany();
}
Nurul
04/06/2022, 10:13 AMTopi Pihko
04/06/2022, 10:25 AMTopi Pihko
04/06/2022, 11:03 AMNurul
04/06/2022, 11:25 AMTopi Pihko
04/08/2022, 11:20 AM