Skip to main content

Introduction

dbbase implements a light-weight wrapper around SQLAlchemy in the style of Flask-SQLAlchemy, but without the requirements of Flask. So the same models and classes used in a Flask project can be used in other contexts than web applications.

The package focuses on three areas of interest.

  • Convenience functions as part of the Model class with easy access to the session and query object, as well as simple functions for saving, etc. within the Model class.

  • Serialization functions enable expressing model data as both JSON and dictionary objects. This feature facilitates easy access to views. The views can be scaled up to include objects created by relationships with explicit control over the content, or show only the bare minimum. These functions can be used as part of an API.

  • Document dictionaries introspect the model classes you have built and present the data objects in a format similar to Swagger / OpenAPI. This enables a method for communicating the details of the models. In addition, the functions could be wrapped into parsing functions that evaluate query strings and form data, directly from the table characteristics. This avoids the extra work of defining tables, and then coding a separate schema just to evaluate incoming and outgoing data. Finally, the doc functions could be used as a basis for unit/integration tests to ensure that all the requirements for the data have been met.

Characteristics

Engine Behavior

Integration of session / query with table classes.

Just as with Flask-SQLAlchemy, the db object carries a lot of the SQLAlchemy functionality. The db object has session as well as the other standard SQLAlchemy features.

Model Behavior

Model Creation

Below is typical example of a table class with dbbase. Like Flask-SQLAlchemy, the models are created with db.Model that is an enhanced version of SQLAlchemy's declarative base.

    class Job(db.Model):
        __tablename__ = 'jobs'

        id = db.Column(db.Integer, primary_key=True)
        name = db.Column(db.String, nullable=False)
        another_id = db.Column(db.SmallInteger)
        start_date = db.Column(db.Date, default=date.today)
        update_at = db.Column(db.DateTime, onupdate=datetime.now)
        completed_at = db.Column(db.DateTime)
        completion_status = db.Column(db.SmallInteger, default=0)

Record Creation

Record creation uses the standard methods.

    job = Job(
        name='model build process',
        another_id=4
        # letting defaults through
    )

    db.session.add(job)
    db.session.commit()

    # alternatively
    job.save()

Queries

The Model class also holds the query object.

Using SQLAlchemy, you would have a session object and do something along the lines of:

    session.query(Job).filter(Job.start_date > '2020-04-01').all()

With Flask-SQLAlchemy and dbbase you would do:

    Job.query.filter(Job.start_date > '2020-04-01').all()

Serialization

To accommodate differing formatting standards between JavaScript and Python when outputting JSON formatted data, the following conversion is available.

Start with a record:

job = Job(
    id=123303,
    name='model build process',
    another_id=4,
    start_date='2020-04-15',
    update_at=datetime.datetime(2020, 4, 20, 3, 2, 1)
    completed_at = datetime.date(2020, 4, 30)
    completion_status = 0
).save()

job.serialize()

{
    "id": 123303,
    "name": "model build process",
    "anotherId": 4,
    "startDate": "2020-04-15",
    "updateAt": "2020-04-20 03:02:01"
    "endDate" = "2020-4-30"
    "completionStatus" = 0
}

Or, job.serialize(to_camel_case=False) would output it without any camel case conversion.

Incoming data could also be formatted as serialized above, and deserialied via

    Job.deserialize(data, from_camel_case=True)

Note that this does not update the record directly (the job in this example). Rather, the output is in the form of a dict. This gives an opportunity to evaluate the data prior to updating the record and database.

For example, suppose you also use parser = reqparse.RequestParser() from Flask-Restful on the Flask side of things:

    data = Job.deserialize(
        JobResource.parser.parse_args())
    job = Job(id, **data)

Finally, the serialize / deserialize functions can always be subclassed for special requirements of that particular model.

Document Dictionaries

The same model, Job, can be expressed with details similarly to the OpenAPI specification for objects. It is a little different because SQLAlchemy has a more nuanced approach to defaults and onupdate functions, and foreign keys. Just as serialization mentioned above of objects can control what columns to include, the documentation function, db.doc_table enables control of what fields to include and what column properties to include.

The following command

    db.doc_table(Job, to_camel_case=True, serial_fields=None, column_props=None)

produces this output.

{
    "Job": {
        "type": "object",
        "properties": {
            "id": {
                "type": "integer",
                "format": "int32",
                "primary_key": true,
                "nullable": false,
                "info": {}
            },
            "name": {
                "type": "string",
                "nullable": false,
                "info": {}
            },
            "anotherId": {
                "type": "integer",
                "format": "int8",
                "nullable": true,
                "info": {}
            },
            "startDate": {
                "type": "date",
                "nullable": true,
                "default": {
                    "for_update": false,
                    "arg": "date.today"
                },
                "info": {}
            },
            "updateAt": {
                "type": "date-time",
                "nullable": true,
                "onupdate": {
                    "for_update": true,
                    "arg": "datetime.now"
                },
                "info": {}
            },
            "completedAt": {
                "type": "date-time",
                "nullable": true,
                "info": {}
            },
            "completionStatus": {
                "type": "integer",
                "format": "int8",
                "nullable": true,
                "default": {
                    "for_update": false,
                    "arg": 0
                },
                "info": {}
            }
        },
        "xml": "Job"
    }
}

More

Additional documentation can be found at https://sidorof.github.io/dbbase/

Metadata

Release files for dbbase 0.3.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dbbase 0.3.10
File Size Uploaded
dbbase-0.3.10.tar.gz 22.5 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for dbbase 0.3.10
File Interpreter ABI Platform
dbbase-0.3.10-py3.9.egg Legacy Egg format - - Details
dbbase-0.3.10-py3-none-any.whl Python 3 none any Details

Total release size: 89.2 kB

Release files / dbbase-0.3.10.tar.gz

Download URL dbbase-0.3.10.tar.gz
Size 22.5 kB
Tags Source
SHA-256 checksum
How to use checksums
dd509d6a74e86785230c15b0487608bb82254c4a711498954fc1d50ffa4dd13a
BLAKE2b-256 checksum
How to use checksums
b6b86cfe8796a8aa84f84658c79b2c458574ea10883e1ad2f6bd29384657a64b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.6

Release files / dbbase-0.3.10-py3.9.egg

Download URL dbbase-0.3.10-py3.9.egg
Size 44.5 kB
Tags Egg
SHA-256 checksum
How to use checksums
61d6807a285bda080285aa163ce9488aad003363315ab9cb92bcde103d2fbfef
BLAKE2b-256 checksum
How to use checksums
4b4604030407befb1fe816b56163ff970ef09f9e4e814ef85d36c527c4c4f39e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.6

Release files / dbbase-0.3.10-py3-none-any.whl

Download URL dbbase-0.3.10-py3-none-any.whl
Size 22.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6bdd4fcf507b3c6632a85acab322e254094f5af1622ce4471bc6805e9797dbbe
BLAKE2b-256 checksum
How to use checksums
cd300ea0bdf1c93edc6de8c10d9054d876335e3c2ad5c41cc30e02ac43edc094
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.3.10 This release

3 release files

0.3.9

3 release files

0.3.8

3 release files

0.3.7

3 release files

0.3.6

3 release files

0.3.5

3 release files

0.3.4

3 release files

0.3.3

3 release files

0.3.2

3 release files

0.3.1

3 release files

0.3.0

3 release files

0.2.8

3 release files

0.2.7

3 release files

0.2.6

3 release files

0.2.5

3 release files

0.2.4

3 release files

0.2.3

3 release files

0.2.2

3 release files

0.2.1

3 release files

0.2.0

1 release file

0.1.14

3 release files

0.1.13

3 release files

0.1.12

3 release files

0.1.11

3 release files

0.1.10

3 release files

0.1.9

3 release files

0.1.8

3 release files

0.1.7

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page