A simple BSON database written in Python
Project description
BysonDB
A serverless keyvalue and document based database using BSON.
What the heck is BSON?
BSON is short for Binary JavaScript Object Notation. It is the binary encoding of JSON, created by MongoDB, and is used in their database. It allows for more datatypes, such as dates and bytes, that JSON does not allow. You can read more about it's purpose, and it's similarites and differences to JSON here.
Usage:
Installation:
pip install bysondb
Key -> Value database:
A database just like normal JSON, with each key coresponding to a value
import datetime
from bysondb import BysonDB
# Create a database with a path to a file. The suffix
# will be changed to .bson, even if there is no suffix
my_db = BysonDB('path/to/database/file.bson')
# Set values like a normal dictionary
my_db["name"] = "John"
my_db["age"] = 30
# Get values in the same way
print(f"My name is {my_db['name']}. I am {my_db['age']} years old.")
# Some objects like datetimes can be used as well
# Not all objects can be serialized
my_db["date"] = datetime.datetime.now()
print(f"Today's date is {my_db['date'].strftime('%x')}")
# Deleteing a key. Can't be undone, so be careful.
my_db.remove("test")
Document based database
A database structure similar to MongoDB, with a single collection of objects
from bysondb import BysonDocumentDB
# Creating a database is the same with the BysonDB,
# but using BysonDocumentDB
my_db = BysonDocumentDB('path/to/database/file.bson')
# Insert a single object to the database
person = {
"name": "John",
"age": 30
}
my_db.insert_one(person)
# Insert multiuple objects into the database
people = [
{"name": "Joe"},
{"name": "Steve"},
{"key": "value"}
]
my_db.insert_many(people)
# Get objects as dicts from the database using a query
# Get all objects
all_objects = my_db.find({})
# Get an object where the key 'debug' is True
debug_objects = my_db.find({"debug": True})
# Find a maximum of 5 objects where the key 'key' is 'value'
objects = my_db.find({"key": "value"}, limit=5)
# Removing values is like finding them, with a search query and a limit
my_db.remove({"remove": True})
my_db.remove({"random_number": 5}, limit=1})
This package uses bson, a package for the BSON codec that does not rely on MongoDB.
This package was formatted using black.
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 bysondb-0.1.0.tar.gz
.
File metadata
- Download URL: bysondb-0.1.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.9.6 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d143d529fdd43ac65c8617bea55a9da7eaf2f3cb5d61c6bbf8d22e7c8f3f982 |
|
MD5 | eefa5b1a90e9374eb631d50740ff6c19 |
|
BLAKE2b-256 | bfa713335ec40c9a4f7bf87f75eac776c3d29b03ddbf50778122aba85c0cdad0 |
File details
Details for the file bysondb-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: bysondb-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.9.6 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f49a61ab7ae5e88fac2d410d10a72c21e09571fc3bc653a3a511458eae8ba587 |
|
MD5 | 9d088a41fd08510ad68ae890dd0ce515 |
|
BLAKE2b-256 | 97bfad3eb3d6ae4f8e67ff9374ba461cb70b4498da92ccbab895af839e42e373 |