Prisma schemas and plugins
Hello everyone,
So here's my issue - We're working on a project that has a plugin architecture. Basically there's a main program and different plugins that run on top of it (similar to Wordpress). Plugins are loaded dynamically using "away import". It should be noted that we use MongoDB as a backend. There is a collection with a list of plugins to be loaded.
There's a main schema with general collections, and plugins can create their own plugin-specific collections as well. A plugin may also access collections in the general schema, but not the other way around.
Installing a plugin is as simple as copying its js file into a plugins subdirectory (if it's written in TypeScript it has to be precompiled), and running a plugin specific install script. The main program is not rebuilt when a plugin is installed. It should be noted that plugins do not have their own copy of node_modules, since they're not considered as separate apps.
So, I think you see where I'm going with this - How do I deal with various plugins, that are loaded dynamically, with each having its own schema without having them overwrite each other? And how do we allow a plugin to access the main schema?
Any thoughts would be greatly appreciated.
5 Replies
You're in no rush, so we'll let a dev step in. Enjoy your coffee, or drop into
#ask-ai
if you get antsy for a second opinion!- have each plugin register its schema with a central registry in the main app. this registry can handle schema validation and prevent conflicts. use unique prefixes or suffixes for plugin collections to maintain isolation.
- implement a migration system for plugin schemas. when a plugin is installed or updated, run migrations to keep the database structure current. tools like
migrate-mongo
can automate this process. (afaik)
- create a permissions layer to define what parts of the main schema plugins can access. use role-based access control to manage these permissions dynamically, ensuring plugins only interact with allowed data.
- develop a lifecycle management system for plugins, including hooks for installation, updates, and removal. this allows you to manage schema changes and data migrations seamlessly.
- load and validate schemas at runtime using a schema validation library to enforce structure and constraints dynamically.
- set up a sandbox environment for each plugin to test its interactions with the main schema. which will help catch potential issues before they affect the main application.
i'm sure the Prisma team has some great insights too, but these are the key points i'd focus on based on your setup.Thank you for your message.
The main issue we have is that running "prisma generate" writes files into node_modules, and since that directory is shared between plugins and the main program, each plugin installed overwrites those files.
i see, Prisma team might have some hacks
Hey 👋
Did you consider specifying output path for each of your schema files?
By default, Prisma writes the generated client to
node_modules/@prisma/client
, so if multiple plugins run prisma generate without customization, they will indeed overwrite each other. But you can change that behaviour by specifying different locations for each of your schema file by specifying output pathGenerating Prisma Client | Prisma Documentation
This page explains how to generate Prisma Client. It also provides additional context on the generated client, typical workflows and Node.js configuration.