-
Hi, I wonder if there are any best practices for integrating prisma with an electron app, connecting to a sqlite db. I was trying but found some problems. a) Prisma client will detect its runtime environment and throws an error if it's been ran on a browser (fair enough, but is there any way to bypass/disable this?) b) Calling prisma instance methods through IPC is definitely a solution, but could be a little bit inconvenient. (e.g using auto-generated types could be difficult) |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 14 replies
-
Option |
Beta Was this translation helpful? Give feedback.
-
Hey @kirkcola! I don't have a list of best practices for Prisma+Electron, but I can help you get set up if you can tell me what you're trying to do! Studio's electron app ships Prisma, so this is definitely possible. The most relevant thing I could find was this discussion a while ago: #5200 Might I ask why you're trying to do Let me know if this is unclear! |
Beta Was this translation helpful? Give feedback.
-
Hi @kirkcola
You can expose prisma client to window object and use it right in renderer process. Electron security policy allow to do this without any danger, like this: import { PrismaClient } from '@prisma/client'
import { contextBridge } from 'electron'
const prisma = new PrismaClient()
contextBridge.exposeInMainWorld('electronAPI', {
prisma: () => prisma
}) and in the renderer process window.electronAPI.prisma().user.create({
data: { ... }
}) You also may would like some thing like this // prisma.ts
const prisma = window.electronAPI.prisma()
export {
prisma
} and in case of that import { prisma } from '.../prisma.ts'
window.electronAPI.prisma.user.create({
data: { ... }
}) it's just to disclosure prisma dependency for other devs and improve readability. Also I think you may stop thinking as "frontend" and "backend" during electron development and develop your app like an usual desktop app, in another words you may need do all you app stuff at "frontend", but electron js files leave alone and use it only for electron api management like windows or hardware. It's all about i think, hope it helps, I just a newbie to electron development right now But I still don't know how to generate prisma client during Electron build. |
Beta Was this translation helpful? Give feedback.
-
Try the edge runtime maybe? // prisma.ts
import { PrismaClient } from '@prisma/client/edge'
export default new PrismaClient() |
Beta Was this translation helpful? Give feedback.
-
Where to store the sqlite database file? |
Beta Was this translation helpful? Give feedback.
Hey @kirkcola! I don't have a list of best practices for Prisma+Electron, but I can help you get set up if you can tell me what you're trying to do! Studio's electron app ships Prisma, so this is definitely possible.
The most relevant thing I could find was this discussion a while ago: #5200
Might I ask why you're trying to do
(a)
? You'll definitely need to run Prisma on the main process and then use Electron's IPC APIs to pass your data to your renderer process, since Prisma currently only runs on Node (this might not be 100% straight-forward because some Prisma types are not serializable, but I can help you figure this out). If your intention is to reuse types on your frontend, then you…