Skip to content

Commit

Permalink
Version 0.2.0 (#64)
Browse files Browse the repository at this point in the history
Co-authored-by: Amin Alaee <mohammadamin.alaee@gmail.com>
  • Loading branch information
tomchristie and aminalaee committed Sep 17, 2021
1 parent 588951b commit 7f22e1a
Show file tree
Hide file tree
Showing 15 changed files with 696 additions and 530 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,9 @@ jobs:
env:
TEST_DATABASE_URL: "mysql://username:password@localhost:3306/testsuite"
run: "scripts/test"
- name: "Run tests with SQLite"
env:
TEST_DATABASE_URL: "sqlite:///testsuite"
run: "scripts/test"
- name: "Enforce coverage"
run: "scripts/coverage"
25 changes: 10 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ $ pip install orm[sqlite]
```

Driver support is provided using one of [asyncpg][asyncpg], [aiomysql][aiomysql], or [aiosqlite][aiosqlite].
Note that if you are using any synchronous SQLAlchemy functions such as `engine.create_all()` or [alembic][alembic] migrations then you still have to install a synchronous DB driver: [psycopg2][psycopg2] for PostgreSQL and [pymysql][pymysql] for MySQL.

---

Expand All @@ -52,23 +51,22 @@ Note that if you are using any synchronous SQLAlchemy functions such as `engine.
```python
import databases
import orm
import sqlalchemy

database = databases.Database("sqlite:///db.sqlite")
metadata = sqlalchemy.MetaData()
models = orm.ModelRegistry(database=database)


class Note(orm.Model):
__tablename__ = "notes"
__database__ = database
__metadata__ = metadata
id = orm.Integer(primary_key=True)
text = orm.String(max_length=100)
completed = orm.Boolean(default=False)
tablename = "notes"
registry = models
fields = {
"id": orm.Integer(primary_key=True),
"text": orm.String(max_length=100),
"completed": orm.Boolean(default=False),
}

# Create the database and tables
engine = sqlalchemy.create_engine(str(database.url))
metadata.create_all(engine)
# Create the tables
models.create_all()

await Note.objects.create(text="Buy the groceries.", completed=False)

Expand All @@ -78,9 +76,6 @@ print(note)
```

[sqlalchemy-core]: https://docs.sqlalchemy.org/en/latest/core/
[alembic]: https://alembic.sqlalchemy.org/en/latest/
[psycopg2]: https://www.psycopg.org/
[pymysql]: https://github.com/PyMySQL/PyMySQL
[asyncpg]: https://github.com/MagicStack/asyncpg
[aiomysql]: https://github.com/aio-libs/aiomysql
[aiosqlite]: https://github.com/jreese/aiosqlite
Expand Down
37 changes: 16 additions & 21 deletions docs/declaring_models.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
## Declaring models

You can define models by inheriting from `orm.Model` and
defining model fields as attributes in the class.
defining model fields in the `fields` attribute.
For each defined model you need to set two special variables:

* `__database__` for database connection.
* `__metadata__` for `SQLAlchemy` functions and migrations.
* `registry` an instance of `orm.ModelRegistry`
* `fields` a `dict` of `orm` fields

You can also specify the table name in database by setting `__tablename__` attribute.
You can also specify the table name in database by setting `tablename` attribute.

```python
import databases
import orm
import sqlalchemy

database = databases.Database("sqlite:///db.sqlite")
metadata = sqlalchemy.MetaData()
models = orm.ModelRegistry(database=database)


class Note(orm.Model):
__tablename__ = "notes"
__database__ = database
__metadata__ = metadata

id = orm.Integer(primary_key=True)
text = orm.String(max_length=100)
completed = orm.Boolean(default=False)
tablename = "notes"
registry = models
fields = {
"id": orm.Integer(primary_key=True),
"text": orm.String(max_length=100),
"completed": orm.Boolean(default=False),
}
```

ORM can create or drop database and tables from models using SQLAlchemy.
For using these functions or `Alembic` migrations, you still have to
install a synchronous DB driver: [psycopg2][psycopg2] for PostgreSQL and [pymysql][pymysql] for MySQL.

Afer installing a synchronous DB driver, you can create tables for the models using:
You can use the following methods:

```python
engine = sqlalchemy.create_engine(str(database.url))
metadata.create_all(engine)
models.create_all()

models.drop_all()
```

## Data types
Expand Down Expand Up @@ -72,6 +69,4 @@ See `TypeSystem` for [type-specific validation keyword arguments][typesystem-fie
* `orm.UUID()`
* `orm.JSON()`

[psycopg2]: https://www.psycopg.org/
[pymysql]: https://github.com/PyMySQL/PyMySQL
[typesystem-fields]: https://www.encode.io/typesystem/fields/
23 changes: 9 additions & 14 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ $ pip install orm[sqlite]
```

Driver support is provided using one of [asyncpg][asyncpg], [aiomysql][aiomysql], or [aiosqlite][aiosqlite].
Note that if you are using any synchronous SQLAlchemy functions such as `engine.create_all()` or [alembic][alembic] migrations then you still have to install a synchronous DB driver: [psycopg2][psycopg2] for PostgreSQL and [pymysql][pymysql] for MySQL.

---

Expand All @@ -52,23 +51,22 @@ Note that if you are using any synchronous SQLAlchemy functions such as `engine.
```python
import databases
import orm
import sqlalchemy

database = databases.Database("sqlite:///db.sqlite")
metadata = sqlalchemy.MetaData()
models = orm.ModelRegistry(database=database)


class Note(orm.Model):
__tablename__ = "notes"
__database__ = database
__metadata__ = metadata
id = orm.Integer(primary_key=True)
text = orm.String(max_length=100)
completed = orm.Boolean(default=False)
tablename = "notes"
registry = models
fields = {
"id": orm.Integer(primary_key=True),
"text": orm.String(max_length=100),
"completed": orm.Boolean(default=False),
}

# Create the database and tables
engine = sqlalchemy.create_engine(str(database.url))
metadata.create_all(engine)
models.create_all()

await Note.objects.create(text="Buy the groceries.", completed=False)

Expand All @@ -78,9 +76,6 @@ print(note)
```

[sqlalchemy-core]: https://docs.sqlalchemy.org/en/latest/core/
[alembic]: https://alembic.sqlalchemy.org/en/latest/
[psycopg2]: https://www.psycopg.org/
[pymysql]: https://github.com/PyMySQL/PyMySQL
[asyncpg]: https://github.com/MagicStack/asyncpg
[aiomysql]: https://github.com/aio-libs/aiomysql
[aiosqlite]: https://github.com/jreese/aiosqlite
Expand Down
17 changes: 8 additions & 9 deletions docs/making_queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ Let's say you have the following model defined:
```python
import databases
import orm
import sqlalchemy

database = databases.Database("sqlite:///db.sqlite")
metadata = sqlalchemy.MetaData()
models = orm.ModelRegistry(database=database)


class Note(orm.Model):
__tablename__ = "notes"
__database__ = database
__metadata__ = metadata

id = orm.Integer(primary_key=True)
text = orm.String(max_length=100)
completed = orm.Boolean(default=False)
tablename = "notes"
registry = models
fields = {
"id": orm.Integer(primary_key=True),
"text": orm.String(max_length=100),
"completed": orm.Boolean(default=False),
}
```

You can use the following queryset methods:
Expand Down
31 changes: 15 additions & 16 deletions docs/relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,29 @@ Let's say you have the following models defined:
```python
import databases
import orm
import sqlalchemy

database = databases.Database("sqlite:///db.sqlite")
metadata = sqlalchemy.MetaData()
models = orm.ModelRegistry(database=database)


class Album(orm.Model):
__tablename__ = "album"
__metadata__ = metadata
__database__ = database

id = orm.Integer(primary_key=True)
name = orm.String(max_length=100)
tablename = "albums"
registry = models
fields = {
"id": orm.Integer(primary_key=True),
"name": orm.String(max_length=100),
}


class Track(orm.Model):
__tablename__ = "track"
__metadata__ = metadata
__database__ = database

id = orm.Integer(primary_key=True)
album = orm.ForeignKey(Album)
title = orm.String(max_length=100)
position = orm.Integer()
tablename = "tracks"
registry = models
fields = {
"id": orm.Integer(primary_key=True),
"album": orm.ForeignKey(Album),
"title": orm.String(max_length=100),
"position": orm.Integer(),
}
```

You can create some `Album` and `Track` instances:
Expand Down
5 changes: 3 additions & 2 deletions orm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
Text,
Time,
)
from orm.models import Model
from orm.models import Model, ModelRegistry

__version__ = "0.1.9"
__version__ = "0.2.0"
__all__ = [
"NoMatch",
"MultipleMatches",
Expand All @@ -36,4 +36,5 @@
"UUID",
"ForeignKey",
"Model",
"ModelRegistry",
]
Loading

0 comments on commit 7f22e1a

Please sign in to comment.