diff --git a/docs/fastapi/index.md b/docs/fastapi/index.md index 25521a73c..7095f0d95 100644 --- a/docs/fastapi/index.md +++ b/docs/fastapi/index.md @@ -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() @@ -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) diff --git a/docs/fastapi/requests.md b/docs/fastapi/requests.md index 361cbbea4..9e24fd091 100644 --- a/docs/fastapi/requests.md +++ b/docs/fastapi/requests.md @@ -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) @@ -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() -``` \ No newline at end of file +``` diff --git a/docs/fastapi/response.md b/docs/fastapi/response.md index 66e0591a8..b74c2a76a 100644 --- a/docs/fastapi/response.md +++ b/docs/fastapi/response.md @@ -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) @@ -111,13 +113,13 @@ 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) @@ -125,8 +127,7 @@ class Category(ormar.Model): 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) @@ -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() -``` \ No newline at end of file +``` diff --git a/docs/fields/common-parameters.md b/docs/fields/common-parameters.md index c7c7f906d..c74ea7601 100644 --- a/docs/fields/common-parameters.md +++ b/docs/fields/common-parameters.md @@ -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) diff --git a/docs/fields/encryption.md b/docs/fields/encryption.md index 9cc06b05d..ba644e76d 100644 --- a/docs/fields/encryption.md +++ b/docs/fields/encryption.md @@ -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, @@ -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, @@ -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, @@ -152,8 +154,7 @@ 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, @@ -161,4 +162,4 @@ class Filter(ormar.Model): encrypt_backend=ormar.EncryptBackends.CUSTOM, encrypt_custom_backend=DummyBackend ) -``` \ No newline at end of file +``` diff --git a/docs/fields/field-types.md b/docs/fields/field-types.md index 37be286c4..1d7df3991 100644 --- a/docs/fields/field-types.md +++ b/docs/fields/field-types.md @@ -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( @@ -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 diff --git a/docs/fields/pydantic-fields.md b/docs/fields/pydantic-fields.md index 042bd3654..800e349ee 100644 --- a/docs/fields/pydantic-fields.md +++ b/docs/fields/pydantic-fields.md @@ -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) @@ -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) @@ -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", @@ -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) @@ -149,13 +141,12 @@ 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 @@ -163,8 +154,7 @@ class PydanticTest(BaseModel): class ModelTest3(ormar.Model): - class Meta(BaseMeta): - pass + ormar_config = base_ormar_config.copy() # provide your custom init function def __init__(self, **kwargs): @@ -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. \ No newline at end of file + If you do not provide a value in one of the above ways `ValidationError` will be raised on load from database. diff --git a/docs/index.md b/docs/index.md index bb75b91aa..12a3de722 100644 --- a/docs/index.md +++ b/docs/index.md @@ -128,17 +128,20 @@ For tests and basic applications the `sqlalchemy` is more than enough: # 1. Imports import sqlalchemy import databases +import ormar # 2. Initialization DATABASE_URL = "sqlite:///db.sqlite" -database = databases.Database(DATABASE_URL) -metadata = sqlalchemy.MetaData() +base_ormar_config = ormar.OrmarConfig( + metadata=sqlalchemy.MetaData(), + database=databases.Database(DATABASE_URL), + engine=sqlalchemy.create_engine(DATABASE_URL), +) # Define models here # 3. Database creation and tables creation -engine = sqlalchemy.create_engine(DATABASE_URL) -metadata.create_all(engine) +base_ormar_config.metadata.create_all(engine) ``` For a sample configuration of alembic and more information regarding migrations and @@ -175,45 +178,39 @@ Note that you can find the same script in examples folder on github. from typing import Optional import databases -import pydantic 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( + metadata=sqlalchemy.MetaData(), + database=databases.Database(DATABASE_URL), + engine = sqlalchemy.create_engine(DATABASE_URL), +) + +# note that this step is optional -> all ormar cares is a field with name +# ormar_config # and proper parameters, but this way you do not have to repeat +# the same parameters if you use only one database +# # 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) @@ -224,10 +221,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