Skip to main content

Generates ***SQL*** and ***NoSQL*** Database Models from @dataclass

Project description

dataclassesdb

dataclassesdb

Generates SQL and NoSQL database models from @dataclass


Documentation: https://dutradda.github.io/dataclassesdb

Source Code: https://github.com/dutradda/dataclassesdb


Key Features

  • Fast start data modeling with persistence

  • Supports from simple database schemas to complex one

  • Integrate with:

    • SQLALchemy
    • aioredis (soon)
    • google-datastore (soon)
    • mongodb (planned)
    • elasticsearch (planned)
    • aws-dynamodb (planned)
  • Same interface as sqlalchemy.orm.session.Session

  • Easy integration with other data sources

  • Supports redis data structure (like hashs, sets, etc) to store objects

Requirements

Python 3.7+

Instalation

$ pip install dataclassesdb 

Basic SQLAlchemy example

import asyncio

from dataclassesdb import DataSourceType, SessionFactory
from dataclasses import dataclass


@dataclass
class Music:
    name: str


@dataclass
class Person:
    name: str
    age: int
    music: Music


session = SessionFactory.make(
    Music,
    Person,
    data_source=DataSourceType.RELATIONAL_SQLALCHEMY,
    data_source_args=dict(
        db_url='sqlite://',
        backrefs=True,
        create_tables=True,
    )
)

person = Person(
    name='John',
    age=40,
    music=Music('Imagine')
)
session.add(person)  # commit=True by default

musicQuery = session.query(Address)
musics = musicQuery.filter(name='Imagine').all()

loop = asyncio.get_event_loop()
print(loop.run_until_complete(musics))
[
  Music(
    name='Imagine',
    _id=1,
    backrefs=Backrefs(
       person=[Person(age=40, name=John, _id=1)]
    )
  )
]

Basic aioredis with hash data type example

import asyncio

from dataclassesdb import DataSourceType, SessionFactoryAsync
from dataclasses import dataclass


@dataclass
class Music:
    name: str


@dataclass
class Person:
    name: str
    age: int
    music: Music


async def get_musics():
    session = await SessionFactoryAsync.make(
        Address,
        Person,
        data_source=DataSourceType.MEMORY_AIOREDIS,
        data_source_args=dict(
            db_url='redis://',
            backrefs=True,
        )
    )

    person = Person(
        name='John',
        age=40,
        music=Music('Imagine')
    )
    await session.add(person)

    musicQuery = await session.query(Address)
    return await musicQuery.filter(name='Imagine').all()

loop = asyncio.get_event_loop()
print(loop.run_until_complete(musics))
[
  Music(
    name='Imagine',
    _id=1,
    backrefs=Backrefs(
       person=[Person(age=40, name=John, _id=1)]
    )
  )
]

Basic aioredis with sorted set data type example

from dataclassesdb import (
    DataSourceType,
    MemorySortedSetRanked,
    ModelKey,
    SessionFactoryAsync,
    SortedValue,
)
from dataclasses import dataclass


@dataclass
class Music:
    id: ModelKey(str)
    name: str


class Playlist(MemorySortedSetRanked):
    value_type = Music


@dataclass
class Person:
    name: str
    age: int
    playlist: Playlist


session = await SessionFactoryAsync.make(
    Music,
    Playlist,
    Person,
    data_source=DataSourceType.MEMORY_AIOREDIS,
    data_source_args=dict(
        db_url='redis://',
        backrefs=True,
    )
)

person = Person(
    name='John',
    age=40,
    playlist=Playlist(
        Music(id='imagine', name='Imagine'),
        Music(id='come-together', name='Come Together'),
    )
)
await session.add(person)

playlistQuery = await session.query(Playlist)
playlist = await playlistQuery.filter(Person.name=='John').one(withrank=True)

print(playlist)
[
  SortedValue(
    rank=1,
    key='imagine'
  ),
  SortedValue(
    rank=2,
    key='come-together'
  )
]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dataclassesdb-0.0.10.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

dataclassesdb-0.0.10-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file dataclassesdb-0.0.10.tar.gz.

File metadata

  • Download URL: dataclassesdb-0.0.10.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.22.0

File hashes

Hashes for dataclassesdb-0.0.10.tar.gz
Algorithm Hash digest
SHA256 6bd41bebcd8f388fd450035eb378c0b93ce1f43ab2dad4a67738ebcd5521900f
MD5 c0f4dd05d88a895b67bb6a02fc260a6c
BLAKE2b-256 e9ced6c824b26d3d2b06f4e2a93c533328962585c0fcebb6bfb36b39d8bf95e0

See more details on using hashes here.

File details

Details for the file dataclassesdb-0.0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for dataclassesdb-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 e33270b8785457fa55559c243f45c9b22a6c41da422ed4b5cdf035ce400a57df
MD5 40a4c7d51435ee31975048998ac63d32
BLAKE2b-256 f7b1da29063e51d0caf02fbb1f9720bf088766fac4170e088c6c862d0f43a8c4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page