Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install rebecca.repository

How to install rebecca.repository

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install rebecca.repository
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
Windows (64-bit)
Mac OS X (10.5+)
Linux (32-bit)
Linux (64-bit)
0.3 Available View build log
 
License
MIT
Dependencies
Lastest release
version 0.3 on Sep 20th, 2013
https://travis-ci.org/rebeccaframework/rebecca.repository.png?branch=master

rebecca.repository

An implementation of repository pattern for SQLAlchemy.

Getting Started

install by pip:

$ pip install rebecca.repository

Implement your model by SQLAlchemy:

from sqlalchemy import Column, Integer, Unicode
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
DBSession = scoped_session(sessionmaker())

class Person(Base):
    __tablename__ = "person"
    id = Column(Integer, primary_key=True)
    name = Column(Unicode(255))
    age = Column(Integer, default=0)
    job = Column(Unicode(255))

Get repository:

from rebecca.repository.sqla import SQLARepository

person_repository = SQALRepository(Person, Person.id, DBSession())

this repository for Person model. To get person, use Person.id as key.

repository interface

create object for demonstration:

person1 = Person(name=u"person1")
DBSession.add(person1)
DBSession.flush() # to generate person.id

A repository has dict like interface:

person_repository[person.id]
person_repository.get(person.id)

and utility methods:

person_repository.get_many([1, 2, 3])
new_person = person_repository.new_item()
conditional repository

repository can configure to set condition:

person_repository = SQALRepository(Person, Person.id, DBSession(), condition=Person.age>30)
pyramid integration

rebecca.repository provides directive for pyramid registry.:

config.include('rebecca.repository')
config.add_repository(person_repository, 'person')

or using repository_config decorator:

@repository_config(name="person", args=(DBSession,))
class PersonRepository(SQLARepository):
    def __init__(self, dbsession):
        super(PersonRepository, self).__init__(Person, Person.id, dbsession)

To get registered repositories, use get_repository:

get_repository(request, 'person')
repository factory

If you pass the parameters during request time, use factory.

class JobPersonRepository(SQLARepository):
    def __init__(self, db_session, job):
        super(JobPersonRepository, self).__init__(Person, Person.id, dbsession,
                                                  condition=Person.job==job)

The parameter job will be passed from request attribute.

To register repository factory, add_repository_factory directive:

config.add_repository_factory(JobPersonRepository, "job-person", args=(DBSession,))

or repository_factory_config decorator:

@repository_factory_config("job-person", args=(DBSession,))
class JobPersonRepository(SQLARepository):
    ....

To create repository from registered factory, call create_repository API:

job = request.matchdict["job"]
repository = create_repository("person", args=(job,))

System Message: WARNING/2 (<string>, line 122)

Literal block ends without a blank line; unexpected unindent.

Contributors

  • Atsushi Odagiri, Original Author

Changelog

0.3 (2013-08-29)
  • added repository factory API
  • added get_many method
0.2 (2013-08-25)
  • added pyramid config directive and venusian decorator
0.1.1 (2013-08-24)
  • fix packaging bug
0.1 (2013-08-24)
  • first release

Subscribe to package updates

Last updated Sep 20th, 2013

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.