Skip to main content

Easy local SQL databases for no-sweat persistence

Project description

ezlocaldb

Version 0.0.1

Easy, local databases for no-sweat persistence. This convinience package wraps small parts of appdirs, sqlite3, and sqlalchemy, to easily create databases for local applications. The package saves about 15 lines of code, but it's 15 lines I find myself writting at the start of many projects, so maybe it will save you some time too.

Installation

PIP

The easiest way to install ezlocaldb is with pip

  pip install ezlocaldb

Usage/Examples

Quick Start

To work with sqlalchemy use get_engine to get an engine object for database access. The first argument to get_engine is the name of your application, which is used to uniquely specify the database.

from ezlocaldb import get_engine
from sqlalchemy import Session

# Get an sqlalchemy engine to use with sqlalchemy.Session
engine = get_engine(app_name="MyAmazingApp")

with Session(engine) as session:
    # Now work with session to initalize database,
    # or execute other CRUD operations.
    pass

When you call get_engine, ezlocaldb checks to see if your database was created previously. If it wasn't it creates a new sqlite database. Either way, it returns an engine which can be used to access the database.

Removing or refreshing databases

If you want to remove or refresh the database, use

from ezlocaldb import remove_database, get_engine

remove_database("MyAmazingApp")

# To recreate the database (with no data in it) use
engine = get_engine("MyAmazingApp")

Initializing the Schema

Usually, getting tables and such setup is just a little tricky. When should you emit CREATE TABLE commands? Is everything in place? Did you just overwrite your data?

With ezlocaldb you can pass a sqlalchemy.MetaData instance as a keyword argument to get_engine. If you're using the ORM, it's as simple as this:

from sqlalchemy.orm import DeclarativeBase
from ezlocaldb import get_engine

class Base(DeclarativeBase):
   pass

# More ORM classes derived from Base
# ...

engine = get_engine('MyAmazingApp',metadata=Base.metadata)

If your database file hasn't been created, get_engine will create_all the tables, etc. represented in your ORM tree. If the file was previously created, the metadata argument is ignored. This way you can quickly get a database setup for orm operations, and not worry about accidentally overwritting all your data.

If you make changes to your ORM structure, you can use remove_database from a repl or other function, and call get_engine with the metadata argument again, and the new database will reflect all your new structure.

A note on migrations. Database migrations are a complicated subject. If you want to keep your data while you revise your ORM structure or Database Schema, you're probably ready to graduate to using full SQLAlchemy, and look at Alembic.

Thank You!

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

ezlocaldb-0.0.1.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ezlocaldb-0.0.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file ezlocaldb-0.0.1.tar.gz.

File metadata

  • Download URL: ezlocaldb-0.0.1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for ezlocaldb-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d99c30c4310361bcbf030f1937c6d3dc969fc11385d8b94fbff0e6959e38a56c
MD5 091d24d978e3f16c2bf24f8db903e5a0
BLAKE2b-256 efc7d4c9a527b8e758ceeb516ba034c24673c5e7ed5bedd7e97c635179b2db8f

See more details on using hashes here.

File details

Details for the file ezlocaldb-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: ezlocaldb-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for ezlocaldb-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 696217c2b7fa83496fc9d6e6e0ede9a8ee129fff695fdb1022e31f666a2095b4
MD5 eda21964a52da660658dd32360f4646d
BLAKE2b-256 bb7a1cadc379ffd6697ba6ed10e9f69a2833f7d0a0ee7f3e0088ec48d31f574d

See more details on using hashes here.

Supported by

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