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.11.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for dataclassesdb-0.0.11.tar.gz
Algorithm Hash digest
SHA256 978c2137e6a58d43a40791d543170b213dd33c4d8218322c15da35f3e053d664
MD5 a20e6d5b4014740c9bf09d8bc0ff494b
BLAKE2b-256 696a8603b6ce811d946a4a16f7676e091beb9a682fcfa3636d553b4ff37ca00c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dataclassesdb-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 4bcdf612415bc9dfe964981491c1ca3c2be580554fadf5635af7658688603925
MD5 10fb013f04e94ef0c4ad0a07548c3f2d
BLAKE2b-256 e6b0e50b687329be5bc1dc792c456765cf4ebdcdc13ce46bbace7b851c2d6594

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