from Hacker News

Notanorm: Lightweight Db Layer for Python

by earonesty on 4/7/21, 11:34 PM with 1 comments

  • by earonesty on 4/7/21, 11:34 PM

    I find full-blown orm's both restrictive and clumsy for smaller projects. But also i like to use a base layer that allows me to connect to different db's with a unified syntax.

    The syntax/structure below seems pretty clean (suggestions welcome), and because it doesn't do that much, it seems unlikely to stop working randomly.

        db = notanorm.SqliteDb(":memory:")
        db.query("create table foo(bar)")
        db.insert("foo", bar=22)
        db.select("foo")
        db.select("foo", bar=[22,33,44])
    
    (that last statement is a placeholder-generated IN clause)

    It's left as an exercise for the reader to structure create statements properly (mysql and sqlite syntax is actually pretty compatible).

    But realistically, your DBA doesn't want you running create statements from python anyway.

    Many libs I write wind up being discarded eventually or replaced with something better. This one has stuck around for 4 years now, and seems to be pretty useful.

    It currently only has mysql/python drivers but writing a new driver is pretty simple.