https://github.com/jmsv/prisma-to-python
Parse prisma.schema files and output models as Python classes
https://github.com/jmsv/prisma-to-python
prisma python
Last synced: 26 days ago
JSON representation
Parse prisma.schema files and output models as Python classes
- Host: GitHub
- URL: https://github.com/jmsv/prisma-to-python
- Owner: jmsv
- License: mit
- Created: 2023-02-22T21:37:24.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-15T00:58:35.000Z (over 3 years ago)
- Last Synced: 2025-08-08T02:42:39.509Z (12 months ago)
- Topics: prisma, python
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/prisma-to-python
- Size: 49.8 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Prisma to Python
[](https://badge.fury.io/js/prisma-to-python)
Parse prisma.schema files and output enums, types and models as Python classes.
> ⚠️ prisma-to-python only supports a subset of the prisma schema syntax - feel free to contribute any other features you need!
This was built to set up models for use with [pymongo](https://pypi.org/project/pymongo), but could be applicable to other use cases.
prisma-to-python works by using the `getDMMF` function of [@prisma/internals](https://www.npmjs.com/package/@prisma/internals), which effectively parses the schema to an AST in JSON. This AST is then used to write Python code for the defined enums, types and models.
## Usage
```bash
npx prisma-to-python --input ./example/schema.prisma --output ./example/models.py
```
## Example
### Input
```prisma
// https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
enum FruitName {
apple
banana
orange
}
type Fruit {
fruit FruitName
price Int
description String?
dateSold DateTime
received Boolean
}
model FruitOrders {
id String @id @default(auto()) @map("_id") @db.ObjectId
fruit Fruit[]
}
```
### Output
```python
# Generated by prisma-to-python - https://github.com/jmsv/prisma-to-python#readme
# Do not edit this file directly!
from enum import Enum
from typing import TypedDict, Optional
from datetime import datetime
# Enums
class FruitName(str, Enum):
APPLE = "apple"
BANANA = "banana"
ORANGE = "orange"
# Types
class Fruit(TypedDict):
fruit: FruitName
price: int
description: Optional[str]
dateSold: datetime
received: bool
# Models
class FruitOrders(TypedDict):
_id: str
fruit: list[Fruit]
```