root / elixir / trunk / elixir / __init__.py @ 45

Revision 45, 2.4 kB (checked in by ged, 6 years ago)

- added a couple of hyperlinks to SQLAlchemy documentation. I tried to be
as precise as possible, so that a user will not have to search through
SQLAlchemy documentation, but that implies that we are more vulnerable to
changes of URLs
- documented some more technical facts of relations (like extra kwargs,

creation of foreign key and intermediary tables)

- documented order_by kwarg for relations and tablename for hnb2many

Line 
1'''
2Elixir package
3   
4A declarative layer on top of SQLAlchemy, which is intended to replace the
5ActiveMapper SQLAlchemy extension, and the TurboEntity project.  Elixir is a
6fairly thin wrapper around SQLAlchemy, which provides the ability to define
7model objects following the Active Record design pattern, and using a DSL
8syntax similar to that of the Ruby on Rails ActiveRecord system.
9
10Elixir does not intend to replace SQLAlchemy's core features, but instead
11focuses on providing a simpler syntax for defining model objects when you do
12not need the full expressiveness of SQLAlchemy's manual mapper definitions.
13
14For an example of how to use Elixir, please refer to the examples directory
15and the unit tests. The examples directory includes a TurboGears application
16with full identity support called 'videostore'.
17'''
18
19import sqlalchemy
20
21from sqlalchemy.ext.sessioncontext  import SessionContext
22from sqlalchemy.types               import *
23from elixir.options                 import *
24from elixir.entity                  import Entity, EntityDescriptor
25from elixir.fields                  import Field, has_field, with_fields
26from elixir.relationships           import belongs_to, has_one, has_many, \
27                                           has_and_belongs_to_many
28
29__all__ = ['Entity', 'Field', 'has_field', 'with_fields', 
30           'belongs_to', 'has_one', 'has_many', 'has_and_belongs_to_many', 
31           'using_options', 'using_table_options', 'using_mapper_options',
32           'create_all', 'drop_all', 'metadata', 'objectstore'] + \
33          sqlalchemy.types.__all__
34
35__pudge_all__ = ['create_all', 'drop_all', 'metadata', 'objectstore']
36
37# connect
38metadata = sqlalchemy.DynamicMetaData('elixir')
39
40try:
41    objectstore = sqlalchemy.objectstore
42except AttributeError:
43    # thread local SessionContext
44    class Objectstore(object):
45        def __init__(self, *args, **kwargs):
46            self.context = SessionContext(*args, **kwargs)
47        def __getattr__(self, name):
48            return getattr(self.context.current, name)
49        session = property(lambda s:s.context.current)
50   
51    objectstore = Objectstore(sqlalchemy.create_session)
52
53metadatas = set()
54
55def create_all():
56    'Create all necessary tables for all declared entities'
57    for md in metadatas:
58        md.create_all()
59
60def drop_all():
61    'Drop all tables for all declared entities'
62    for md in metadatas:
63        md.drop_all()
Note: See TracBrowser for help on using the browser.