A serverless Mongo-like database backed with SQLite.
Project description
Monty, Mongo tinified. A literally serverless, Mongo-like database in Python
:construction: Not Ready For Prime Time :construction:
Inspired by TinyDB and the extension TinyMongo.
MontyDB is:
- A serverless version of MongoDB, against to MongoDB 3.6.4
- Document oriented, of course
- Storage engine pluggable
- Write in pure Python, works on Python 2.7, 3.4, 3.5, 3.6
Install
pip install montydb
Requirements
pyyaml
jsonschema
pymongo
(forbson
)
Example Code
>>> from montydb import MontyClient
>>> col = MontyClient(":memory:").db.test
>>> col.insert_many([{"stock": "A", "qty": 6}, {"stock": "A", "qty": 2}])
>>> cur = col.find({"stock": "A", "qty": {"$gt": 4}})
>>> next(cur)
{'_id': ObjectId('5ad34e537e8dd45d9c61a456'), 'stock': 'A', 'qty': 6}
Develop Status
See Projects' TODO
Storage Engine Configurations
The configuration process only required on repository creation or modification.
Currently, one repository can only assign one storage engine.
- Memory
Memory storage does not need nor have any configuration, nothing saved to disk.
>>> client = MontyClient(":memory:")
- SQLite
SQLite is the default on-disk storage engine, you can skip the configuration if the default setting is okay.
>>> client = MontyClient("/db/repo")
SQLite default settings, they are infact SQLite pragmas:
connection:
journal_mode: WAL
write_concern:
synchronous: 1
automatic_index: OFF
busy_timeout: 5000
If you are not happy with the default, use MontyConfigure
before get client.
>>> from montydb import MontyClient, MontyConfigure, storage
>>> with MontyConfigure("/db/repo") as cf: # Auto save config when exit
... cf.load(storage.SQLiteConfig) # Load sqlite config
... cf.config.connection.journal_mode = "DELETE"
... cf.config.write_concern.busy_timeout = 8000
...
>>> client = MontyClient("/db/repo") # Running tweaked sqlite storage now
- FlatFile
Not default storage engine, need configuration first before get client.
>>> from montydb import MontyClient, MontyConfigure, storage
>>> with MontyConfigure("/db/repo") as cf: # Auto save config when exit
... cf.load(storage.FlatFileConfig) # Load flatfile config
...
>>> client = MontyClient("/db/repo") # Running on flatfile storage now
FlatFile default settings:
connection:
cache_modified: 0 # how many document CRUD cached before flush to disk.
Change storage engine
MontyConfigure
will ignore load()
if conf.yaml
exists, you need to drop()
first before changing the storage engine. The documents will remain on disk and conf.yaml
will be deleted.
>>> with MontyConfigure("/db/repo") as cf:
... cf.drop()
... cf.load(storage.WhateverConfig)
Reload configuration
MontyClient
will reload conf.yaml
at the operation right after client.close()
.
>>> client.close()
>>> col.insert_one({"doc": 1}) # client auto re-open and reload config
After storage engine configuration, you should feel like using MongoDB's Python driver, unless it's not implemented.
Utilities
-
monty_dump
Write documents to disk, able to load by
monty_load
ormongoimport
>>> from montydb.utils import monty_dump >>> documents = [{"a": 1}, {"doc": "some doc"}] >>> monty_dump("/path/dump.json", documents)
-
monty_load
Read documents from disk, able to read from
monty_dump
ormongoexport
>>> from montydb.utils import monty_load >>> monty_load("/path/dump.json") [{"a": 1}, {"doc": "some doc"}]
-
MontyList
Experimental, a subclass of
list
, combined the common CRUD methods from Mongo's Collection and Cursor.>>> from montydb.utils import MontyList >>> mtl = MontyList([1, 2, {"a": 1}, {"a": 5}, {"a": 8}]) >>> mtl.find({"a": {"$gt": 3}}) MontyList([{'a': 5}, {'a': 8}])
You can dump it with
monty_dump
or read frommonty_load
>>> monty_dump("/path/dump.json", mtl) >>> MontyList(monty_load("/path/dump.json")) MontyList([1, 2, {'a': 1}, {'a': 5}, {'a': 8}])
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 montydb-0.0.5.tar.gz
.
File metadata
- Download URL: montydb-0.0.5.tar.gz
- Upload date:
- Size: 36.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 680dd2d2bb413022692c11bf42ab88d73614aa51ec0ad6810c0f729ade567782 |
|
MD5 | 9b35db726bd14b82280fde50e3ab562c |
|
BLAKE2b-256 | 0efc65cc83896616508ad806a383901b43b5297b23f9edd35ec724d874cb77d7 |
Provenance
File details
Details for the file montydb-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: montydb-0.0.5-py3-none-any.whl
- Upload date:
- Size: 45.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a9540efd6c211572e0fea92abd6c58c13bb91f4f296fcf247410a59c7fb24d0 |
|
MD5 | d931399ebda77663550f9fe4857468b5 |
|
BLAKE2b-256 | 74b8d2940cf31f42702695f613c2c6e7c8733ea5a49b2ada57873d00dee1b8d7 |