Replies: 1 comment 4 replies
-
You can also specify where you want the generator py {
provider = "prisma-client-py"
output = "my_local_dir"
} Upon generation this will copy the entire
The fact that the query engine is internally implemented as a web server shouldn't matter to end users, the internal web server is not intended to be exposed publicly and is merely an implementation detail. However it should be noted that I am working on removing the web server and instead switching to using native FFI bindings instead, see #27.
Including the auto-generated prisma code in a packaged application should be possible today, albeit with some workarounds. The biggest challenges I can see with using Prisma in say a packaged CLI application is handling of the engine binaries and migrations. Here's how I'd get around these issues: from pathlib import Path
import click
# this assumes you're generating prisma to a local directory within
# the package named _prisma, e.g. pkg/_prisma
from ._prisma import Prisma
from ._prisma.cli import prisma as prisma_cli
@click.command()
def main() -> None:
# use the packaged schema file
schema = Path(__file__).parent / 'schema.prisma'
# ensure the database is in the correct state
# and ensure the engine binaries are downloaded
prisma_cli.run(['db', 'push', '--skip-generate', f'--schema={schema}'])
# as you've already packaged the generated prisma code you can
# make any queries like you normally would
client = Prisma()
client.connect()
if __name__ == '__main__':
main() It should be noted that the application of migrations in this solution is naive and may cause issues down the line, a better solution would be to also package migration files as well and apply them serially although I do not know how difficult this would be in practice. Prisma Client Python was designed with backend use cases in mind, where the developer is in control of the database and the machine. As such, use cases outside of that are more difficult to work with although I agree that it's a valid use case and we should figure out how to make it easy for people who want to use prisma within a packaged application as well. Another use case I've considered before is using |
Beta Was this translation helpful? Give feedback.
-
I just starting looking at Prisma as an ORM for a new project. It took a bit of digging through the docs and playing with the model generation to come to the conclusion that Prisma isn't suitable outside of a backend application (yes, I realise it's stated on the docs' home page -- I managed to gloss over "any backend application" ruling out non-backend use cases).
As it stands, with models generated directly into site-packages, Prisma acts more like a deployment tool and only works with a single project's database. That said, what would it take to support packaging and distribution of models in a project package?
Naively, one can copy the
prisma
package into their project, and it seems to Just Work as internal references already use relative imports to stay within the package.I'm aware from the architecture docs page that a local query engine web server is involved, which is certainly overkill for, say, a small CLI application, but it would be neat to expand the scope beyond just server applications.
Suggested solution
Models, actions, migrations, and other project-specific files would be generated to a (user-selected) location within the current Python project (e.g. for the
spam
project,spam.prisma
containingmodels.py
,enums.py
etc.). This could be configured via an option of thegenerator
section of schema.prisma.In theory, this location could be ignored by version control of the project, with the schema file remaining the single source of truth and developers still needing to run the likes of
prisma db pull
to get a working local set of models. It would however be included in packaged releases of the project, so that end users obtain a copy of the models without having to interface with Prisma themselves.Depending on the compatibility of models with the rest of the Prisma framework, there would probabl need to be a note on how strict a version requirement for the
prisma
package should be set once a set of models is generated.Beta Was this translation helpful? Give feedback.
All reactions