I’m using Prisma python client for my mysql database. I’m wondering if its possible to do a single global connection instead of having to open and close them whenever I make a query?
@commands.has_permissions(manage_guild=True) @commands.slash_command(description="Sets up the server for verification") async def help(self, interaction: disnake.GuildCommandInteraction): await self.client.prisma.connect() response = await self.client.prisma.user.find_first(where={ "name": "benn", }) print(dict(response)) await self.client.prisma.disconnect()
I tried doing the following:
client.prisma = Prisma(auto_register=True) async def connect_to_db(): await client.prisma.connect() asyncio.run(connect_to_db())
However, I get an error:
disnake.ext.commands.errors.CommandInvokeError: Command raised an exception: RuntimeError: Event loop is closed```
Advertisement
Answer
It is recommended that you create one instance of PrismaClient
and reuse it across your application and you should only set it to a global variable in the development environment only and you do not need to explicitly $disconnect
. You can learn more about Prisma connection management in the docs. Also, I’ll encourage you to ask your Prisma Python client questions in prisma-client-py repositories GitHub Discussion