Skip to main content

asyncio sqlalchemy support

Project description

asyncsa

Downloads PyPI version

asyncio sqlalchemy support,with async/await

add asyncio sqlalchemy support

Install


pip install asyncsa

Usage examples


import asyncio
from sqlalchemy import Integer, Column, String, ForeignKey
from asyncsa.manager.async_pg import PostgresManager
from asyncsa.model.mixin import Mixin
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.sql import select

dsn = "postgres://postgres:test@localhost:2345/test"

conf = {'dsn': dsn}

Base = declarative_base()


Manager = PostgresManager(conf=conf)


class User(Mixin, Base):

    _manager = Manager

    __tablename__ = 'test'
    id = Column(Integer, autoincrement=True,  primary_key=True)
    name = Column(String(50))

    def __repr__(self):
        return "<User(name='%s', id='%s')>" % (
            self.name, self.id)


class Parent(Mixin, Base):
    __tablename__ = 'parent'
    _manager = Manager
    id = Column(Integer, primary_key=True)
    child = relationship("Child", lazy='joined')


class Child(Mixin, Base):
    _manager = Manager
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))
    parent = relationship("Parent", back_populates="child")


if __name__ == '__main__':

    loop = asyncio.get_event_loop()
    loop.run_until_complete(Manager.connect())

    result = loop.run_until_complete(Manager.get(
        User.objects([User.id]).join(
            Parent.__table__,
            Parent.id == User.id)))
    print(result)

    # result = loop.run_until_complete(Child.create_table())
    # print(result)
    # u = loop.run_until_complete(User.create(name='test'))
    # print('create', u)
    # print('get', loop.run_until_complete(User.get(name='test')))
    # print('all', loop.run_until_complete(User.all()))
    # u.name = 'aaa'
    # print('save', loop.run_until_complete(u.save()))

License

The asyncnsq is offered under APGLV3 license.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

asyncsa-2.0.2-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file asyncsa-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: asyncsa-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.9

File hashes

Hashes for asyncsa-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 deda784190be02c249fcd428baa2188e05c3e878b72fe872c96ba6139370b836
MD5 28f84e9420e86a8b71560aef11e49f79
BLAKE2b-256 8b5ada34a440c93479464ad04f8bb1491826b525a4c42a5d6389053b22e7660a

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