Skip to content

add option to use SQLite instead of PostgreSQL #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,4 @@ cython_debug/
# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,linux,python

# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)
test.db
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- [Installation](#installation)
- [Usage](#usage)
- [Local Postgres server using Docker](#local-postgres-server-using-docker)
- [Use SQLite instead of PostgreSQL](#use-sqlite-instead-of-postgresql)
- [License](#license)

## Introduction
Expand Down Expand Up @@ -84,6 +85,17 @@ docker exec -it postgres psql -U postgres

This will allow you to edit or delete the database or records.

### Use SQLite instead of PostgreSQL

For testing purposes, you can also use SQLite instead of PostgreSQL. To do so,
open the [dp.py](db.py) file and comment out the PostgreSQL database in the
`DATABASE_URL` environment variable and uncomment the SQLite database.

```python
# DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost/postgres"
DATABASE_URL = "sqlite+aiosqlite:///./test.db"
```

## License

This project is licensed under the terms of the MIT license.
Expand Down
4 changes: 4 additions & 0 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from sqlalchemy.orm import declarative_base

DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost/postgres"
# DATABASE_URL = "sqlite+aiosqlite:///./test.db"
# Note that (as far as I can tell from the docs and searching) there is no need
# to add 'check_same_thread=False' to the sqlite connection string, as
# SQLAlchemy version 1.4+ will automatically add it for you when using SQLite.

engine = create_async_engine(DATABASE_URL, echo=False)
Base = declarative_base()
Expand Down
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fastapi = "^0.97.0"
asyncpg = "^0.27.0"
sqlalchemy = {extras = ["asyncio"], version = "^2.0.16"}
uvicorn = {extras = ["standard"], version = "^0.22.0"}
aiosqlite = "^0.19.0"

[tool.poetry.group.dev.dependencies]
# linting and formatting tools
Expand Down