Skip to content

Commit

Permalink
Change line numbers in documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mekanix committed Feb 14, 2024
1 parent 217faae commit 961402d
Show file tree
Hide file tree
Showing 31 changed files with 413 additions and 659 deletions.
29 changes: 10 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,39 +172,31 @@ import ormar
import sqlalchemy

DATABASE_URL = "sqlite:///db.sqlite"
database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()


# note that this step is optional -> all ormar cares is a internal
# class with name Meta and proper parameters, but this way you do not
# have to repeat the same parameters if you use only one database
class BaseMeta(ormar.ModelMeta):
metadata = metadata
database = database
base_ormar_config = ormar.OrmarConfig(
database=databases.Database(DATABASE_URL),
metadata=sqlalchemy.MetaData(),
engine=sqlalchemy.create_engine(DATABASE_URL),
)


# Note that all type hints are optional
# below is a perfectly valid model declaration
# class Author(ormar.Model):
# class Meta(BaseMeta):
# tablename = "authors"
# ormar_config = base_ormar_config.copy()
#
# id = ormar.Integer(primary_key=True) # <= notice no field types
# name = ormar.String(max_length=100)


class Author(ormar.Model):
class Meta(BaseMeta):
tablename = "authors"
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)


class Book(ormar.Model):
class Meta(BaseMeta):
tablename = "books"
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
author: Optional[Author] = ormar.ForeignKey(Author)
Expand All @@ -215,10 +207,9 @@ class Book(ormar.Model):
# create the database
# note that in production you should use migrations
# note that this is not required if you connect to existing database
engine = sqlalchemy.create_engine(DATABASE_URL)
# just to be sure we clear the db before
metadata.drop_all(engine)
metadata.create_all(engine)
base_ormar_config.metadata.drop_all(engine)
base_ormar_config.metadata.create_all(engine)


# all functions below are divided into functionality categories
Expand Down
19 changes: 8 additions & 11 deletions docs/fastapi/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ from fastapi import FastAPI
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
if not config.database.is_connected:
await config.database.connect()
config.metadata.drop_all(config.engine)
config.metadata.create_all(config.engine)

yield

if config.database.is_connected:
config.metadata.drop_all(config.engine)
await config.database.disconnect()


Expand All @@ -72,21 +69,21 @@ Define ormar models with appropriate fields.
Those models will be used instead of pydantic ones.

```python
base_ormar_config = OrmarConfig(
metadata = metadata
database = database
)


class Category(ormar.Model):
class Meta:
tablename = "categories"
metadata = metadata
database = database
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)


class Item(ormar.Model):
class Meta:
tablename = "items"
metadata = metadata
database = database
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
Expand Down
12 changes: 7 additions & 5 deletions docs/fastapi/requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ Field is not required if (any/many/all) of following:

Example:
```python
base_ormar_config = ormar.OrmarConfig(
metadata=metadata
database=database
)

class User(ormar.Model):
class Meta:
tablename: str = "users"
metadata = metadata
database = database
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
email: str = ormar.String(max_length=255)
Expand Down Expand Up @@ -139,4 +141,4 @@ async def create_user3(user: UserCreate): # use pydantic model here
# note how now request param is a pydantic model and not the ormar one
# so you need to parse/convert it to ormar before you can use database
return await User(**user.model_dump()).save()
```
```
25 changes: 13 additions & 12 deletions docs/fastapi/response.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ Field is not required if (any/many/all) of following:

Example:
```python
base_ormar_config = ormar.OrmarConfig(
metadata=metadata
database=database
)

class User(ormar.Model):
class Meta:
tablename: str = "users"
metadata = metadata
database = database
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
email: str = ormar.String(max_length=255)
Expand Down Expand Up @@ -111,22 +113,21 @@ One is a dictionary with nested fields that represents the model tree structure,
Assume for a second that our user's category is a separate model:

```python
class BaseMeta(ormar.ModelMeta):
metadata = metadata
database = database
base_ormar_config = ormar.OrmarConfig(
metadata=metadata
database=database
)

class Category(ormar.Model):
class Meta(BaseMeta):
tablename: str = "categories"
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=255)
priority: int = ormar.Integer(nullable=True)


class User(ormar.Model):
class Meta(BaseMeta):
tablename: str = "users"
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
email: str = ormar.String(max_length=255)
Expand Down Expand Up @@ -239,4 +240,4 @@ class UserBase(pydantic.BaseModel):
@app.post("/users3/", response_model=UserBase) # use pydantic model here
async def create_user3(user: User): #use ormar model here (but of course you CAN use pydantic also here)
return await user.save()
```
```
13 changes: 8 additions & 5 deletions docs/fields/common-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Used in sql only.

Sample usage:

```Python hl_lines="21-23"
```Python hl_lines="20-22"
--8<-- "../docs_src/fields/docs004.py"
```

Expand Down Expand Up @@ -184,12 +184,15 @@ So it's on you as a user to provide a type that is valid in the context of given
As it's easy to break functionality of ormar the `overwrite_pydantic_type` argument is not available on relation fields!

```python
base_ormar_config = ormar.OrmarConfig(
metadata=metadata
database=database
)


# sample overwrites
class OverwriteTest(ormar.Model):
class Meta:
tablename = "overwrites"
metadata = metadata
database = database
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
my_int: str = ormar.Integer(overwrite_pydantic_type=PositiveInt)
Expand Down
21 changes: 11 additions & 10 deletions docs/fields/encryption.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ well as both-way encryption/decryption (`FERNET` backend).

To encrypt a field you need to pass at minimum `encrypt_secret` and `encrypt_backend` parameters.

```python hl_lines="7-8"
```python hl_lines="10-12"
base_ormar_config = ormar.OrmarConfig(
metadata=metadata
database=database
)

class Filter(ormar.Model):
class Meta(BaseMeta):
tablename = "filters"
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100,
Expand Down Expand Up @@ -59,8 +63,7 @@ Note that since this backend never decrypt the stored value it's only applicable

```python
class Hash(ormar.Model):
class Meta(BaseMeta):
tablename = "hashes"
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=128,
Expand Down Expand Up @@ -106,8 +109,7 @@ as the returned value is parsed to corresponding python type.

```python
class Filter(ormar.Model):
class Meta(BaseMeta):
tablename = "filters"
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100,
Expand Down Expand Up @@ -152,13 +154,12 @@ argument by `encrypt_custom_backend`.

```python
class Filter(ormar.Model):
class Meta(BaseMeta):
tablename = "filters"
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100,
encrypt_secret="secret123",
encrypt_backend=ormar.EncryptBackends.CUSTOM,
encrypt_custom_backend=DummyBackend
)
```
```
18 changes: 10 additions & 8 deletions docs/fields/field-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,16 @@ That way you can i.e. set the value by API, even if value is not `utf-8` compati
```python
import base64
... # other imports skipped for brevity


base_ormar_config = ormar.OrmarConfig(
metadata=metadata
database=database
)


class LargeBinaryStr(ormar.Model):
class Meta:
tablename = "my_str_blobs"
metadata = metadata
database = database
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
test_binary: str = ormar.LargeBinary(
Expand Down Expand Up @@ -233,10 +238,7 @@ class TestEnum(Enum):
val2 = 'Val2'

class TestModel(ormar.Model):
class Meta:
tablename = "org"
metadata = metadata
database = database
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
# pass list(Enum) to choices
Expand Down
Loading

0 comments on commit 961402d

Please sign in to comment.