Package: lymph-sqlalchemy
lymph-sqlalchemy================This is a library is a basic wrapper around sqlalchemy that provides an easyway to write an interface, integrate logging and helps in testing.Store-----The ``lymph.sqlalchemy.store.Store`` class is a basic wrapper around sqlalchemy,it configures the engine and gives an interface to sessions... code-block:: pythonfrom lymph.sqlalchemy.store import Storestore = Store('postgresql://postgres/lymph-test-sqlalchemy')# Access the engineengine = store.engineengine.execute('SELECT 1')# The sessions are context managerswith store.get_session() as session:session.add(user)You can pass all of the engine args to the ``Store``, they would be passed onwhen creating the engine.Defining Models--------------The recommended way of defining your models is by first creating an sqlalchemy declarative base class and thendefine your models like any normal sqlalchemy models:.. code-block:: pythonfrom sqlalchemy import Column, Integer, Stringfrom sqlalchemy.ext.declarative import declarative_baseBase = declarative_base()class User(Base):__tablename__ = 'users'id = Column(Integer, primary_key=True)name = Column(String)**IMPORTANT**: Please put the ``Base`` class in the same python module as the models, else you mayhave some import problems.Interface---------The ``lymph.sqlalchemy.interface.StoreInterface`` class uses the the lymph config toconfigure a store instance.An example config for a ``StoreInterface`` looks like this, it has a dependencyon store... code-block:: pythonimport lymphfrom lymph.sqlalchemy.interface import StoreInterfaceclass UserInterface(StoreInterface):@lymph.rpc()def create_user(self, data):user = User(**data)with self.store.get_session() as session:session.add(user).. code-block:: yamldependencies:store:class: lymph.sqlalchemy.store:Storeuri: postgresql://postgres/userspool_size: 5max_overflow: 10pool_timeout: 30base: dhh.users.models:Baseinterfaces:restaurants:class: UserInterfacestore: dep:storeTesting-------The library provides a ``lymph.sqlalchemy.testing.StorageInterfaceTest`` to helpyou write your unittests. You would extend this class by providing it your owndatabase config and your service interface.The class would create the database on ``setupClass`` and it would create thetables and the models that you provided as fixtures in ``setUp`` method of thetest case.In the ``teardown`` this class would drop all the tables... code-block:: pythonfrom lymph.sqlalchemy.testing import StorageInterfaceTestclass UserInterfaceTest(StorageInterfaceTest):def get_fixtures(self):self.user = User(name='Test')self.user_2 = User(name='Test 2')return [self.user, self.user_2]def test_get_user(self):user = self.client.get(pk=self.user.id)user_2 = self.client.get(pk=self.user_2.id)self.assertEqual(user.id, self.user.id)self.assertEqual(user_2.id, self.user_2.id)Command Line Interfaces-----------------------This library provides a couple of cli to make the workflow easiers.``init-db`` command would create all the tables for all models that are definedusing the library's `Base`.. code-block:: bash$lymph init-db -c <config>.yml.. code-block:: yamldependencies:store:class: lymph.sqlalchemy.store:Storebase: dhh.users.models:Base``sql-shell`` command would give you a shell with a `store` instance alreadyconfigured... code-block:: bash$LYMPH_NODE_CONFIG=`pwd`/.lymph.yml lymph sql-shell -c <config>.yml