A small, schemaless ORM that is backed by SQLite.
Project description
Description
goatfish
is a small, schemaless ORM that is backed by SQLite.
It's also this:
Its usage is very simple, just have your classes inherit from goatfish.Model
and and specify a connection, and the goatfish methods are available to you.
goatfish also supports querying for arbitrary properties in your models, as
well as indexing on arbitrary properties. It does not enforce a schema of any
kind.
It appears that this method is identical to what FriendFeed used to implement a schemaless layer over MySQL, which is pretty significant validation:
http://backchannel.org/blog/friendfeed-schemaless-mysql
Usage
To use goatfish
, all you need to do is create a class that inherits from
goatfish.Model
:
import goatfish
import sqlite3
db_connection = sqlite3.connect(":memory:")
class Test(goatfish.Model):
class Meta:
# This is so we know where to connect.
connection = db_connection
indexes = (
("foo",),
("foo", "bar"),
)
# Create the necessary tables. If they exist, do nothing.
Test.initialize()
foo = Test()
foo.foo = "hi"
foo.bar = "hello"
foo.save()
# Retrieve all elements.
>>> [test.bar for test in Test.all()]
['hello']
# Count the number of elements.
>>> Test.count(foo="hi")
1
# Run a query with parameters (slow, loads every item from the DB to check it).
>>> [test.bar for test in Test.find(bar="hello")]
['hello']
# This uses an index, so it's fast.
>>> [test.foo for test in Test.find(foo="hi"})]
['hi']
# Run a query with a parameter that doesn't exist in the dataset.
>>> [test.bar for test in Test.find({bar="hello", baz="hi"})]
[]
>>> Test.find_one(bar="hello").foo
"hi"
>>> print(Test.find_one(bar="doesn't exist"))
None
# Delete the element.
>>> foo.delete()
# Try to retrieve all elements again.
>>> [test.bar for test in Test.find()]
[]
Indexes
What sets goatfish
apart from other modules such as shelve
, zodb
,
etc is its ability to query random attributes, and make those queries faster
by using SQLite indexes.
The way this is achieved is by creating an intermediate table for each index we specify. The index tables consist of the uuid column, and one column for every field in the index. This way, we can store the value itself in these index tables and query them quickly, as the rows have SQLite indexes themselves.
The find() method uses these indexes automatically, if they exist, to avoid
sequential scans. It will automatically use the largest index that contains
the data we want to query on, so a query of {"foo": 3, "bar": 2}
when only
foo
is indexed will use the index on foo
to return the data, and do a
sequential scan to match bar
.
Right now, new indexes are only populated with data on save(), so you might miss rows when querying on indexes that are not ready yet. To populate indexes, go through the objects in your model and perform a save() in each of them. Convenience functions to populate single indexes will be provided shortly.
Installation
To install goatfish
you need:
- Python 3.4 or later.
You have multiple options for installation:
- With pip (preferred), do
pip install goatfish
. - With setuptools, do
easy_install goatfish
. - To install from source, download it from
https://github.com/stochastic-technologies/goatfish/ and do
python setup.py install
.
License
goatfish
is distributed under the BSD license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file goatfish-2.0.0a0.tar.gz
.
File metadata
- Download URL: goatfish-2.0.0a0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.9 CPython/3.8.5 Linux/5.4.0-53-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 313eb267c347c847831389a04942a68b9a1bc43485a73c5c7f69a2127f050a62 |
|
MD5 | f8bc309d8a463ce0e104752689fd9256 |
|
BLAKE2b-256 | 845a5ba8e215e0c922fa26f01977ada994fc1adc3dc3cb998afe65dac1eb486e |
File details
Details for the file goatfish-2.0.0a0-py3-none-any.whl
.
File metadata
- Download URL: goatfish-2.0.0a0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.9 CPython/3.8.5 Linux/5.4.0-53-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25b8c3088aae6e1caf4fe5d034f4e5a345391b1e987dd1e945740945f448e41f |
|
MD5 | 19a699a41e9a6068b47cb37569e2a9e8 |
|
BLAKE2b-256 | 1a3f08ed6e197676ad6091d111cb443f5a16d16bbe446040c8d630b426ad2667 |