New in version 1.1.
Object Relational Mapper (ORM), normally backed by a RDBMS like SQLite, MySQL or PostgreSQL.
Based on Elixir, which in turn is based on SQLAlchemy thus requiring Elixir to be installed. If Elixir or SQLAlchemy is not installed or broken, importing smisk.mvc.model will not fail, but rather issue a warning and export nothing.
This module inherits & exports all members of elixir and exports sqlalchemy as sql.
Normally, you import smisk.mvc.model in your module where the entities are defined.
Let’s look at one of the example applications; “kittens_orm”, which examplifies the basics of CRUD
kittens_orm/ app.py kittens.conf lighttpd.conf
Contents of app.py:
#!/usr/bin/env python
# encoding: utf-8
from smisk.mvc import *
from smisk.mvc.model import *
class Kitten(Entity):
name = Field(Unicode(255), primary_key=True)
color = Field(Unicode(30))
year_born = Field(Integer)
class root(Controller):
def __call__(self, *args, **params):
kittens = [dict(k) for k in Kitten.query.all()]
return {'kittens': kittens}
def create(self, **params):
kitten = Kitten(**params)
redirect_to(self.read, kitten)
def read(self, **params):
kitten = Kitten.get_by(**params)
return kitten.to_dict()
def update(self, name, **params):
kitten = Kitten.get_by(name=name)
kitten.from_dict(params)
redirect_to(self.read, kitten)
def delete(self, name):
kitten = Kitten.get_by(name=name)
kitten.delete()
redirect_to(self)
if __name__ == '__main__':
main(config='kittens')
Contents of kittens.conf:
..code-block:: javascript
“smisk.mvc.model”: { “url”: “sqlite:///tmp/kittens.sqlite” }
Try running the server and see a kitten getting born: http://localhost:8080/create?name=Busta+Rhymes&color=red
See also
Configure the underlying Elixir module and SQLAlchemy engine.
If defined, it’s actively used to setup a database engine. This parameter is not set by default.
Example:
"smisk.mvc.model": {
"url": "mysql://user@localhost/database",
"pool_recycle": 14400,
"elixir.shortnames": false
}
| Type: | dict |
|---|---|
| Default: | None |
Parameters:
The dictionary defined for smisk.mvc.model must contain "url" and can contain several optional parameters.
Indicate the appropriate database dialect and connection arguments.
The URL is a string in the form dialect://user:password@host/dbname[?key=value..], where dialect is a name such as mysql, oracle, postgres, etc.
This parameter must be defined.
| Type: | string |
|---|
The encoding to be used when encoding/decoding Unicode strings.
| Type: | string |
|---|---|
| Default: | "utf-8" |
True if unicode conversion should be applied to all str types.
| Type: | bool |
|---|---|
| Default: | False |
Allows alternate Engine implementations to take effect.
| Type: | string |
|---|---|
| Default: | "plain" |
| See: | SQLAlchemy create_engine() |
The size of the pool to be maintained.
This is the largest number of connections that will be kept persistently in the pool. Note that the pool begins with no connections; once this number of connections is requested, that number of connections will remain.
| Type: | int |
|---|---|
| Default: | 1 |
This setting causes the pool to recycle connections after the given number of seconds has passed.
It defaults to -1, or no timeout. For example, setting to 3600 means connections will be recycled after one hour.
Note
MySQL in particular will disconnect automatically if no activity is detected on a connection for eight hours (although this is configurable with the MySQLDB connection itself and the server configuration as well). In the case of a MySQL backend, the default value is instead 3600.
| Type: | int |
|---|---|
| Default: | -1 or 3600 if the dialect is MySQL |
The number of seconds to wait before giving up on returning a connection.
| Type: | int |
|---|---|
| Default: | 30 |
The maximum overflow size of the pool.
When the number of checked-out connections reaches the size set in pool_size, additional connections will be returned up to this limit. When those additional connections are returned to the pool, they are disconnected and discarded. It follows then that the total number of simultaneous connections the pool will allow is pool_size + max_overflow, and the total number of “sleeping” connections the pool will allow is pool_size. max_overflow can be set to -1 to indicate no overflow limit; no limit will be placed on the total number of concurrent connections.
| Type: | int |
|---|---|
| Default: | 10 |
If False, table names are deduced only from both module name and entity name.
| Type: | bool |
|---|---|
| Default: | True |
See also
The sqlalchemy module,
Exported for reasons of convenience:
from smisk.mvc.model import *
MyEntity.query().order_by(sql.desc(MyEntity.some_field))
Default options for the SQLAlchemy create_engine() call.
| Type: | dict |
|---|