Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 7f22e1a

Browse files
Version 0.2.0 (#64)
Co-authored-by: Amin Alaee <[email protected]>
1 parent 588951b commit 7f22e1a

File tree

15 files changed

+696
-530
lines changed

15 files changed

+696
-530
lines changed

.github/workflows/test-suite.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,9 @@ jobs:
5757
env:
5858
TEST_DATABASE_URL: "mysql://username:password@localhost:3306/testsuite"
5959
run: "scripts/test"
60+
- name: "Run tests with SQLite"
61+
env:
62+
TEST_DATABASE_URL: "sqlite:///testsuite"
63+
run: "scripts/test"
6064
- name: "Enforce coverage"
6165
run: "scripts/coverage"

README.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ $ pip install orm[sqlite]
4141
```
4242

4343
Driver support is provided using one of [asyncpg][asyncpg], [aiomysql][aiomysql], or [aiosqlite][aiosqlite].
44-
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.
4544

4645
---
4746

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

5755
database = databases.Database("sqlite:///db.sqlite")
58-
metadata = sqlalchemy.MetaData()
56+
models = orm.ModelRegistry(database=database)
5957

6058

6159
class Note(orm.Model):
62-
__tablename__ = "notes"
63-
__database__ = database
64-
__metadata__ = metadata
65-
id = orm.Integer(primary_key=True)
66-
text = orm.String(max_length=100)
67-
completed = orm.Boolean(default=False)
60+
tablename = "notes"
61+
registry = models
62+
fields = {
63+
"id": orm.Integer(primary_key=True),
64+
"text": orm.String(max_length=100),
65+
"completed": orm.Boolean(default=False),
66+
}
6867

69-
# Create the database and tables
70-
engine = sqlalchemy.create_engine(str(database.url))
71-
metadata.create_all(engine)
68+
# Create the tables
69+
models.create_all()
7270

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

@@ -78,9 +76,6 @@ print(note)
7876
```
7977

8078
[sqlalchemy-core]: https://docs.sqlalchemy.org/en/latest/core/
81-
[alembic]: https://alembic.sqlalchemy.org/en/latest/
82-
[psycopg2]: https://www.psycopg.org/
83-
[pymysql]: https://github.com/PyMySQL/PyMySQL
8479
[asyncpg]: https://github.com/MagicStack/asyncpg
8580
[aiomysql]: https://github.com/aio-libs/aiomysql
8681
[aiosqlite]: https://github.com/jreese/aiosqlite

docs/declaring_models.md

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,39 @@
11
## Declaring models
22

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

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

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

1212
```python
1313
import databases
1414
import orm
15-
import sqlalchemy
1615

1716
database = databases.Database("sqlite:///db.sqlite")
18-
metadata = sqlalchemy.MetaData()
17+
models = orm.ModelRegistry(database=database)
1918

2019

2120
class Note(orm.Model):
22-
__tablename__ = "notes"
23-
__database__ = database
24-
__metadata__ = metadata
25-
26-
id = orm.Integer(primary_key=True)
27-
text = orm.String(max_length=100)
28-
completed = orm.Boolean(default=False)
21+
tablename = "notes"
22+
registry = models
23+
fields = {
24+
"id": orm.Integer(primary_key=True),
25+
"text": orm.String(max_length=100),
26+
"completed": orm.Boolean(default=False),
27+
}
2928
```
3029

3130
ORM can create or drop database and tables from models using SQLAlchemy.
32-
For using these functions or `Alembic` migrations, you still have to
33-
install a synchronous DB driver: [psycopg2][psycopg2] for PostgreSQL and [pymysql][pymysql] for MySQL.
34-
35-
Afer installing a synchronous DB driver, you can create tables for the models using:
31+
You can use the following methods:
3632

3733
```python
38-
engine = sqlalchemy.create_engine(str(database.url))
39-
metadata.create_all(engine)
34+
models.create_all()
35+
36+
models.drop_all()
4037
```
4138

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

75-
[psycopg2]: https://www.psycopg.org/
76-
[pymysql]: https://github.com/PyMySQL/PyMySQL
7772
[typesystem-fields]: https://www.encode.io/typesystem/fields/

docs/index.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ $ pip install orm[sqlite]
4141
```
4242

4343
Driver support is provided using one of [asyncpg][asyncpg], [aiomysql][aiomysql], or [aiosqlite][aiosqlite].
44-
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.
4544

4645
---
4746

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

5755
database = databases.Database("sqlite:///db.sqlite")
58-
metadata = sqlalchemy.MetaData()
56+
models = orm.ModelRegistry(database=database)
5957

6058

6159
class Note(orm.Model):
62-
__tablename__ = "notes"
63-
__database__ = database
64-
__metadata__ = metadata
65-
id = orm.Integer(primary_key=True)
66-
text = orm.String(max_length=100)
67-
completed = orm.Boolean(default=False)
60+
tablename = "notes"
61+
registry = models
62+
fields = {
63+
"id": orm.Integer(primary_key=True),
64+
"text": orm.String(max_length=100),
65+
"completed": orm.Boolean(default=False),
66+
}
6867

6968
# Create the database and tables
70-
engine = sqlalchemy.create_engine(str(database.url))
71-
metadata.create_all(engine)
69+
models.create_all()
7270

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

@@ -78,9 +76,6 @@ print(note)
7876
```
7977

8078
[sqlalchemy-core]: https://docs.sqlalchemy.org/en/latest/core/
81-
[alembic]: https://alembic.sqlalchemy.org/en/latest/
82-
[psycopg2]: https://www.psycopg.org/
83-
[pymysql]: https://github.com/PyMySQL/PyMySQL
8479
[asyncpg]: https://github.com/MagicStack/asyncpg
8580
[aiomysql]: https://github.com/aio-libs/aiomysql
8681
[aiosqlite]: https://github.com/jreese/aiosqlite

docs/making_queries.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@ Let's say you have the following model defined:
77
```python
88
import databases
99
import orm
10-
import sqlalchemy
1110

1211
database = databases.Database("sqlite:///db.sqlite")
13-
metadata = sqlalchemy.MetaData()
12+
models = orm.ModelRegistry(database=database)
1413

1514

1615
class Note(orm.Model):
17-
__tablename__ = "notes"
18-
__database__ = database
19-
__metadata__ = metadata
20-
21-
id = orm.Integer(primary_key=True)
22-
text = orm.String(max_length=100)
23-
completed = orm.Boolean(default=False)
16+
tablename = "notes"
17+
registry = models
18+
fields = {
19+
"id": orm.Integer(primary_key=True),
20+
"text": orm.String(max_length=100),
21+
"completed": orm.Boolean(default=False),
22+
}
2423
```
2524

2625
You can use the following queryset methods:

docs/relationships.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,29 @@ Let's say you have the following models defined:
77
```python
88
import databases
99
import orm
10-
import sqlalchemy
1110

1211
database = databases.Database("sqlite:///db.sqlite")
13-
metadata = sqlalchemy.MetaData()
12+
models = orm.ModelRegistry(database=database)
1413

1514

1615
class Album(orm.Model):
17-
__tablename__ = "album"
18-
__metadata__ = metadata
19-
__database__ = database
20-
21-
id = orm.Integer(primary_key=True)
22-
name = orm.String(max_length=100)
16+
tablename = "albums"
17+
registry = models
18+
fields = {
19+
"id": orm.Integer(primary_key=True),
20+
"name": orm.String(max_length=100),
21+
}
2322

2423

2524
class Track(orm.Model):
26-
__tablename__ = "track"
27-
__metadata__ = metadata
28-
__database__ = database
29-
30-
id = orm.Integer(primary_key=True)
31-
album = orm.ForeignKey(Album)
32-
title = orm.String(max_length=100)
33-
position = orm.Integer()
25+
tablename = "tracks"
26+
registry = models
27+
fields = {
28+
"id": orm.Integer(primary_key=True),
29+
"album": orm.ForeignKey(Album),
30+
"title": orm.String(max_length=100),
31+
"position": orm.Integer(),
32+
}
3433
```
3534

3635
You can create some `Album` and `Track` instances:

orm/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
Text,
1616
Time,
1717
)
18-
from orm.models import Model
18+
from orm.models import Model, ModelRegistry
1919

20-
__version__ = "0.1.9"
20+
__version__ = "0.2.0"
2121
__all__ = [
2222
"NoMatch",
2323
"MultipleMatches",
@@ -36,4 +36,5 @@
3636
"UUID",
3737
"ForeignKey",
3838
"Model",
39+
"ModelRegistry",
3940
]

0 commit comments

Comments
 (0)