Skip to content

Commit

Permalink
Remove some Meta classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mekanix committed Feb 13, 2024
1 parent 9d76c98 commit 6407697
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 107 deletions.
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()
```
```
11 changes: 7 additions & 4 deletions docs/fields/common-parameters.md
Original file line number Diff line number Diff line change
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
52 changes: 21 additions & 31 deletions docs/fields/pydantic-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@ If you set a field as `Optional`, it defaults to `None` if not provided and that
exactly what's going to happen during loading from database.

```python
database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()
base_ormar_config = ormar.OrmarConfig(
metadata=sqlalchemy.MetaData(),
database=databases.Database(DATABASE_URL),
)


class BaseMeta(ormar.ModelMeta):
metadata = metadata
database = database

class ModelTest(ormar.Model):
class Meta(BaseMeta):
pass
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=200)
Expand All @@ -57,17 +54,14 @@ By setting a default value, this value will be set on initialization and databas
Note that setting a default to `None` is the same as setting the field to `Optional`.

```python
database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()
base_ormar_config = ormar.OrmarConfig(
metadata=sqlalchemy.MetaData(),
database=databases.Database(DATABASE_URL),
)


class BaseMeta(ormar.ModelMeta):
metadata = metadata
database = database

class ModelTest(ormar.Model):
class Meta(BaseMeta):
pass
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=200)
Expand Down Expand Up @@ -97,13 +91,12 @@ on initialization and each database load.
from pydantic import Field, PaymentCardNumber
# ...

database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()

base_ormar_config = ormar.OrmarConfig(
metadata=sqlalchemy.MetaData(),
database=databases.Database(DATABASE_URL),
)

class BaseMeta(ormar.ModelMeta):
metadata = metadata
database = database

CARD_NUMBERS = [
"123456789007",
Expand All @@ -119,8 +112,7 @@ def get_number():


class ModelTest2(ormar.Model):
class Meta(BaseMeta):
pass
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=200)
Expand Down Expand Up @@ -149,22 +141,20 @@ You can provide a value for the field in your `__init__()` method before calling
from pydantic import BaseModel
# ...

database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()

base_ormar_config = ormar.OrmarConfig(
metadata=sqlalchemy.MetaData(),
database=databases.Database(DATABASE_URL),
)

class BaseMeta(ormar.ModelMeta):
metadata = metadata
database = database

class PydanticTest(BaseModel):
aa: str
bb: int


class ModelTest3(ormar.Model):
class Meta(BaseMeta):
pass
ormar_config = base_ormar_config.copy()

# provide your custom init function
def __init__(self, **kwargs):
Expand Down Expand Up @@ -192,4 +182,4 @@ assert test_check.pydantic_test.aa == "random"
```

!!!warning
If you do not provide a value in one of the above ways `ValidationError` will be raised on load from database.
If you do not provide a value in one of the above ways `ValidationError` will be raised on load from database.
Loading

0 comments on commit 6407697

Please sign in to comment.