Lightweight file based NoSQL DB
Project description
LunaDB
LunaDB is a document oriented file based database written in Python. It's designed on similar scenarios as TinyDB, but with a better scaling with more documents and (hopefully) less memory usage.
Introduction
LunaDB reads only one document at a time and doesn't save the whole database in one giant JSON. Instead it saves every document as an own JSON and seperates them by line breaks.
Main features from LunaDB:
- Document based: Like TinyDB, you can store documents as
dict
- Written in pure Python: LunaDB doesn't require any other dependency
- Even more tiny: LunaDB has less than 500 lines of code, without unittests
Important: LunaDB stores the data in multiple files located in one folder!
Supported Python Versions
LunaDB supports only Python 3, there is no Python 2 support and there won't be any. As it is using os.replace()
it needs at least Python 3.3 to run. Newer versions should be no problem. I use this module on Windows 10 and Debian Strech, it should run on any OS, if not feel free to open an issue.
Example Usage
from LunaDB import LunaDB
db = LunaDB("relative/path/to/database")
# If you are not giving an specific field, which will be unique, an automatic id_field will be used
character = db.table("character")
city = db.table("city", id_field="name")
# If strict is True it will throw an error if there is duplicated entry. Otherwise it will just skip the entry.
character.insert({"name": "Tristan", "age": 21}, strict = True)
character.insert({"name": "Isolt", "age": 19}, strict = False)
# To insert multiple entries at once, use insert_multiple
city.insert_multiple([{"name":"London"}, {"name": "Manchester"}])
Searching for entries
Searching works with filter functions. The search method takes a function which returns True
when the entry should be in the response. This filter function can be defined seperatly or with the lambda statement.
res = character.search(lambda entry: entry["age"] == 21)
# res = [{"name":"Tristan", "age": 21, "_id": 0}]
res = character.search(lambda entry: entry["age"] < 22)
# res = [{"name": "Tristan", "age": 21, "_id": 0}, {"Isolt", "age":19, "_id":1}]
# The "_id" field is the auto_increment identifier
res = city.search(lambda entry: entry["name"] == "London")
# res = [{"name": "London"}]
Update
The update uses the id field to identify the document and will replace it.
res = character.search(lambda entry: entry["name"] == "Tristan")
# res = [{"name":"Tristan", "age": 21, "_id": 0}]
res["age"] = 22
character.update(res, auto_clean = True)
res = character.search(lambda entry: entry["name"] == "Tristan")
# res = [{"name":"Tristan", "age": 22, "_id": 0}]
The auto_clean
parameter is present in every method that modifies or deletes existing data. Without auto_clean LunaDB does not delete the data from the database, in fact it is just disabling them. Only if the database is cleaned afterwards, the disabled entries will get deleted. If auto_clean
is False
you need to clean the table with the table.clean()
method manually if you want to save space. If auto_clean
is True
, the table will automatically cleared when the disabled lines exceed the auto_clean_buffer
, which is per default 5MB per table. auto_clean_buffer
can be set with the creation of the database object. Per default auto_clean
is True
.
Delete
The delete works the same way as search, every row which returns at the filter function True
will be deleted. The auto_clean
is the same as with update.
res = character.search(lambda entry: entry["name"] == "Tristan")
# res = [{"name":"Tristan", "age": 21, "_id": 0}]
character.delete(lambda entry: entry["name"] == "Tristan", auto_clean = True)
res = character.search(lambda entry: entry["name"] == "Tristan")
# res = []
Additional concepts
Insert or update
row = {"name":"Tristan", "age": 17}
character.upsert(row)
Insert or skip
row = {"name":"Tristan", "age": 17}
# With strict = False, duplicated entries won't throw an exception
character.insert(row, strict = False)
Additional documentation
Look at the module reference.
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 LunaDB-0.5.2.tar.gz
.
File metadata
- Download URL: LunaDB-0.5.2.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b80ebddd6cf0c5d546545ba6d7fe8ba07918d926ffb16b34cab10d30a85a3c5 |
|
MD5 | e6bcb6911ae59c0cb97ca4e526b25cbe |
|
BLAKE2b-256 | e0f8e8e1096e5add662a7ce4914957bdfe31b8de7894f5fe897f78f63ca2a757 |
File details
Details for the file LunaDB-0.5.2-py3-none-any.whl
.
File metadata
- Download URL: LunaDB-0.5.2-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c54d188e14211b6639ff463bc2545025d09f2f17a8783bf85fdc428c266f8a62 |
|
MD5 | cc062439a6ed114f7b6ca593e7c60a81 |
|
BLAKE2b-256 | 0390b4d24784b77951338394a85ad336cf2f6c94185b6a066f3cdbefc76a54ad |