Prisma Client Python is an auto-generated and fully type-safe database client
Project description
What is Prisma Client Python?
Prisma Client Python is an unofficial implementation of Prisma which is a next-generation ORM that comes bundled with tools, such as Prisma Migrate, which make working with databases as easy as possible.
Prisma Client Python can be used in any Python backend application. This can be a REST API, a GraphQL API or anything else that needs a database.
Why should you use Prisma Client Python?
Unlike other Python ORMs, Prisma Client Python is fully type safe and offers native support for usage with and without async
. All you have to do is specify the type of client you would like to use for your project in the Prisma schema file.
Core features:
- Full type safety
- With / without async
- Recursive and pseudo-recursive types
- Atomic updates
- Complex cross-relational queries
- Partial type generation
- Batching write queries
Supported database providers:
- PostgreSQL
- MySQL
- SQLite
- MongoDB (experimental)
- SQL Server (experimental)
How does Prisma work?
This section provides a high-level overview of how Prisma works and its most important technical components. For a more thorough introduction, visit the documentation.
The Prisma schema
Every project that uses a tool from the Prisma toolkit starts with a Prisma schema file. The Prisma schema allows developers to define their application models in an intuitive data modeling language. It also contains the connection to a database and defines a generator:
// database
datasource db {
provider = "sqlite"
url = "file:database.db"
}
// generator
generator client {
provider = "prisma-client-py"
}
// data models
model Post {
id Int @id @default(autoincrement())
title String
content String?
views Int @default(0)
published Boolean @default(false)
author User? @relation(fields: [author_id], references: [id])
author_id Int?
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
In this schema, you configure three things:
- Data source: Specifies your database connection. In this case we use a local SQLite database however you can also use an environment variable.
- Generator: Indicates that you want to generate Prisma Client Python.
- Data models: Defines your application models.
On this page, the focus is on the generator as this is the only part of the schema that is specific to Prisma Client Python. You can learn more about Data sources and Data models on their respective documentation pages.
Prisma generator
A prisma schema can define one or more generators, defined by the generator
block.
A generator determines what assets are created when you run the prisma generate
command. The provider
value defines which Prisma Client will be created. In this case, as we want to generate Prisma Client Python, we use the prisma-client-py
value.
You can also define where the client will be generated to with the output
option. By default Prisma Client Python will be generated to the same location it was installed to, whether thats inside a virtual environment, the global python installation or anywhere else that python packages can be imported from.
For more options see configuring Prisma Client Python.
Accessing your database with Prisma Client Python
Just want to play around with Prisma Client Python and not worry about any setup? You can try it out online on gitpod.
Installing Prisma Client Python
The first step with any python project should be to setup a virtual environment to isolate installed packages from your other python projects, however that is out of the scope for this page.
In this example we'll install the dependencies required for an asynchronous client, if you would like to use a synchronous client see setting up a synchronous client.
pip install -U prisma-client[aiohttp]
Generating Prisma Client Python
Now that we have Prisma Client Python installed we need to actually generate the client to be able to access the database.
Copy the Prisma schema file shown above to a schema.prisma
file in the root directory of your project and run:
prisma db push
This command will add the data models to your database and generate the client, you should see something like this:
Prisma schema loaded from schema.prisma
Datasource "db": SQLite database "database.db" at "file:database.db"
SQLite database database.db created at file:database.db
🚀 Your database is now in sync with your schema. Done in 26ms
✔ Generated Prisma Client Python to ./.venv/lib/python3.9/site-packages/prisma in 265ms
It should be noted that whenever you make changes to your schema.prisma
file you will have to re-generate the client, you can do this automatically by running prisma generate --watch
.
The simplest asynchronous Prisma Client Python application looks something like this:
import asyncio
from prisma import Client
async def main() -> None:
client = Client()
await client.connect()
# write your queries here
await client.disconnect()
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())
Query examples
For a more complete list of queries you can perform with Prisma Client Python see the documentation.
All query methods return pydantic models.
Retrieve all User
records from the database
users = await client.user.find_many()
Include the posts
relation on each returned User
object
users = await client.user.find_many(
include={
'posts': True,
},
)
Retrieve all Post
records that contain "prisma"
posts = await client.post.find_many(
where={
'OR': [
{'title': {'contains': 'prisma'}},
{'content': {'contains': 'prisma'}},
]
}
)
Create a new User
and a new Post
record in the same query
user = await client.user.create(
data={
'name': 'Robert',
'email': 'robert@craigie.dev',
'posts': {
'create': {
'title': 'My first post from Prisma!',
},
},
},
)
Update an existing Post
record
post = await client.post.update(
where={
'id': 42,
},
data={
'views': {
'increment': 1,
},
},
)
Usage with static type checkers
All Prisma Client Python methods are fully statically typed, this means you can easily catch bugs in your code without having to run it!
For more details see the documentation.
Room for improvement
Prisma Client Python is a new project and as such there are some features that are missing or incomplete.
Auto completion for query arguments
As far as I am aware there is currently no Python IDE that will add the appropriate completions for the following code:
user = await client.user.find_first(
where={
'.'
}
)
Given the cursor is where the .
is, an IDE should suggest the following completions:
- id
- name
- posts
Performance
There has currently not been any work done on improving the performance of Prisma Client Python queries, this is something that will be worked on in the future and there is room for massive improvements.
Supported platforms
Only MacOS and Linux are officially supported, windows is not supported yet.
Version guarantees
Prisma Client Python is not stable, there will be documented breaking changes with new releases.
Contributing
We use conventional commits (also known as semantic commits) to ensure consistent and descriptive commit messages.
See the contributing documentation for more information.
Attributions
This project would not be possible without the work of the amazing folks over at prisma.
Massive h/t to @steebchen for his work on prisma-client-go which was incredibly helpful in the creation of this project.
This README is also heavily inspired by the README in the prisma/prisma repository.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file prisma-client-0.0.3.tar.gz
.
File metadata
- Download URL: prisma-client-0.0.3.tar.gz
- Upload date:
- Size: 52.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b376b0712f084ab191f46457959716083705016e492537293346ea32101651ee |
|
MD5 | e9209873d5cd128851053ed8f5e2c4e0 |
|
BLAKE2b-256 | ea20e2229b4c9897a8fd9a170a1e2e9d93bdb4339cd92c0df14a6e93561ff671 |
File details
Details for the file prisma_client-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: prisma_client-0.0.3-py3-none-any.whl
- Upload date:
- Size: 81.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf0700d391465424dba8ea6625b59cb8c6155751a2a779d30bb1a9126b790302 |
|
MD5 | 7f736c8619c4317a655b30df12d70242 |
|
BLAKE2b-256 | 0d495503cc7253a209a3c949391dc28405e882145595973a5ca8177141895a07 |