Skip to main content

Opinionated persistence with SQLite

Project description

microcosm-sqlite

Opinionated data loading with SQLite.

While most distributed application runtimes will use a networked data store for mutable state, the usage patterns of data that is read-only at runtime are great fit for SQLite.

In particular, microcosm-sqlite assumes that applications will

  • Build data sets in advance and ship them as static artifacts (e.g. in source control)
  • Load data immutable sets at runtime without loading entire data sets into memory

Writing Models

Persistent data is expected to use SQLAlchemy's declarative base classes. Because different data sets may be shipped in different SQLite databases, each declarative base class needs to have a unique name and a separate engine configuration, which is achieved by adding DataSet as the base of the declarative base class:

Base = DataSet.create("some_name")


class SomeModel(Base):
    __tablename__ = "sometable"

    id = Column(Integer, primary_key=True)

Using Stores

Basic persistence operations are abstracted through a store:

class SomeStore(Store):

    @property
    def model_class(self):
        return SomeModel


 store = SomeStore()
 results = store.search()

Configuring SQLite

Each DataSet defaults to using :memory: storage, but can be customized in two ways:

  1. The SQLiteBindFactory can be configured with custom paths:

    loader = load_from_dict(
        sqlite=dict(
            paths={
                "some_name": "/path/to/database",
            },
        ),
    )
    graph = create_object_graph("example", loader=loader)
    
  2. The microcosm.sqlite entrypoint can contain a mapping from a data set name to a function that returns a path.

Project details


Download files

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

Source Distribution

microcosm-sqlite-1.0.9.tar.gz (20.9 kB view details)

Uploaded Source

File details

Details for the file microcosm-sqlite-1.0.9.tar.gz.

File metadata

  • Download URL: microcosm-sqlite-1.0.9.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/21.8.0 rfc3986/2.0.0 colorama/0.4.3 CPython/3.7.12

File hashes

Hashes for microcosm-sqlite-1.0.9.tar.gz
Algorithm Hash digest
SHA256 d7fbdd5caa2921c718255800a43c52533cf64f115ac65907e0d5d8bbe1a7b86a
MD5 b9098d78fbd2a56d61adc7755cb89e80
BLAKE2b-256 53f74f6338b68d0afbe8d689ae6b77525b3df2a4bad6866352b9a8b0c184db57

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page