Skip to main content

FastAPI-GENE

A lightweight CLI tool to scaffold fastapi projects, run migrations and migrate migration files.

Installation

Create and activate a virtual environment and then install fastapi-gene:

$ pip install fastapi-gene

Usage

Here are some examples of what fastapi_gene can do:

fastapi_gene createproject main

It creates a fastapi project named main along with it's subfolder models,schema,api and file app.py inside it.

main/
├── app.py
└── main/
    ├── __init__.py
    ├── api/
    │   ├── __init__.py
    │   └── item.py
    ├── models/
    │   └── __init__.py
    └── schema/
        ├── __init__.py
        └── item.py

To run the project just change path to main and run fastapi dev.now, you can navigate to http://127.0.0.1:8000/docs/ to see autogenerated items endpoints but make sure to install fastapi on your enviroment.😊

fastapi_gene createuser fastapi-users[sqlalchemy]

It creates file and add user table on it as well as map endpoint into your project automatically from fastapi-users[sqlalchemy] module but project structure should be maintained to work as smoothly.

  • Supported modules are:
    • fastapi-users[sqlalchemy]
    • fastapi-users[sqlalchemy,oauth]
    • fastapi-users[beanie]
    • fastapi-users[beanie,oauth]

Now, File structure changed to

main/
├── app.py
└── main/
    ├── __init__.py
    ├── api/
    │   ├── __init__.py
    │   |── item.py
    |   └── users.py    #created
    |
    ├── models/
    │   |── __init__.py
    |   |── db.py       #created
    |   └── users.py    #created
    |
    └── schema/
        ├── __init__.py
        |── item.py
        └── users.py    #created

// To reflect users endpoint on project include `user_router` in `app.py` as
# app.py

from fastapi import FastAPI
from main.api import item_router,user_router

app = FastAPI()


app.include_router(item_router)
app.include_router(user_router)

fastapi_gene makemigrations

fastapi_gene makemigrations init

Migrations directory wasnot created to store migrations file. To create that, you should run above command in your terminal and it creates migrations directory named alembic. It works same as alembic init alembic but you can also provide custom name as fastapi_gene makemigrations init --name=migrations

main/
├── app.py
├── alembic/...  #created
├── alembic.ini  #created
└── main/...

// some manual setup

# main/models/__init__.py
from .db import Base

# alembic/env.py
from main.models import Base

...

target_metadata = Base.metadata
config.set_main_option("sqlalchemy.url","sqlite:///./fastgen.db")

fastapi_gene makemigrations -m "create user tables"

After importing Base from user model and sqlalchemy_url inside env.py of migrations directory, you can run this command which will create the migration file with named randomnumber_create_user_tables.py inside version directory of migrations. It works same as alembic revision --autogenerate -m "create user tables".

fastapi_gene migrate up

It applies all migrations upto latest version. It works same as alembic upgrade head. By default, it passes head revision but you can provide desire revision as fastapi_gene migrate up revision_id

fastapi_gene migrate down

It reverts all migrations, bringing the database back to its initial empty state. It works same as alembic downgrade base. By default, it passes base revision but you can provide desire revision as fastapi_gene migrate down revision_id

Notes

  • command: To know all about the commands fastapi_gene --help
  • specific-command: For the information of specific commands fastapi_gene "command_name" --help

Metadata

Release files for fastapi-gene 0.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-gene 0.0.8
File Size Uploaded
fastapi_gene-0.0.8.tar.gz 12.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-gene 0.0.8
File Interpreter ABI Platform
fastapi_gene-0.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 26.8 kB

Release files / fastapi_gene-0.0.8.tar.gz

Download URL fastapi_gene-0.0.8.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
cbaef925d5070ee8ab49320e1522086e02b7b0c5b623617dd811d57fd54bc5fb
BLAKE2b-256 checksum
How to use checksums
0b090aad916bd12e8b0a05232d5896adb2415ae935935715618ee438f027540c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.1

Release files / fastapi_gene-0.0.8-py3-none-any.whl

Download URL fastapi_gene-0.0.8-py3-none-any.whl
Size 14.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f7416176a6ac2e4ff22df88d83f4d4ead3107165b8b36a5471f658a34d00d68c
BLAKE2b-256 checksum
How to use checksums
9259246505eccadabe85ba410d6827a95e002e0a5800a3659d106b4e94312b18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.1

Release history Release notifications | RSS feed

This release

0.0.8 This release

2 release files

0.0.7

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page