The official Python SDK for Contiguity Base
MIT License
You can install the SDK using pip:
$ pip install contiguity_base
Then, import & initialize it like this:
from contiguity_base import connect
db = connect("your-api-key", "your-project-id")
You can get an API key by fetching it in the dashboard, and a project ID is given to you when creating a project.
Contiguity Base is a one-to-one replacement for the old Deta Base API, Deta Base JavaScript SDK, Deta Base Python SDK, and Deta Base Go SDK. The only thing that has changed is initialization.
Instead of deta = Deta(project_key)
, you'll use db = connect(api_key, project_id)
The rest stays the same, because at Contiguity, we think it's crazy for a cloud provider to give you 45 days to move dozens of apps from their proprietary database.
If you're transitioning from Deta Space to Contiguity, welcome!
To start working with a base, you can create a Base instance:
my_base = db.Base("my-awesome-base")
Now you're ready to perform some cool database operations!
To add an item to your base, use the put
method:
item = {
"name": "Contiguity",
"is_awesome": True,
"coolness_level": 9000
}
my_base.put(item)
You can also specify a key for your item:
my_base.put(item, "unique-key-1")
Need to add multiple items at once? No problem! Just pass a list of items:
items = [
{"name": "Item 1", "value": 100},
{"name": "Item 2", "value": 200},
{"name": "Item 3", "value": 300, "key": "some-unique-key"}
]
my_base.put(items)
To insert an item into your base, use the insert method. This is useful when you want to ensure you're not overwriting existing data:
new_item = {
"name": "New Product",
"price": 49.99
}
my_base.insert(new_item, "product-1")
If an item with the same key already exists, the insert operation will fail, preventing accidental overwrites.
To retrieve an item, use the get
method:
my_item = my_base.get("unique-key-1")
print(my_item["name"]) # Outputs: Contiguity
Need to update an item? Use the update
method:
my_base.update({"coolness_level": 9001}, "unique-key-1")
To remove an item, use the delete
method:
my_base.delete("unique-key-1")
You can perform complex queries using the fetch
method like so:
results = my_base.fetch({
"is_awesome": True,
"profile.name?contains": "John"
})
{
"age": 22,
"name": "Sarah"
}
{
"user.profile.age": 22,
"user.profile.name": "Sarah"
}
{
"fav_numbers": [2, 4, 8]
}
{
"time": {
"day": "Tuesday",
"hour": "08:00"
}
}
{
"user.profile.age?ne": 22
}
{
"user.profile.age?lt": 22
}
{
"user.profile.age?gt": 22
}
{
"user.profile.age?lte": 22
}
{
"user.profile.age?gte": 22
}
{
"user.id?pfx": "afdk"
}
{
"user.age?r": [22, 30]
}
{
"user.email?contains": "@contiguity.co"
}
{
"user.places_lived_list?contains": "Miami"
}
{
"user.email?not_contains": "@contiguity.co"
}
{
"user.places_lived_list?not_contains": "Miami"
}
Contiguity provides some cool utility operations for updating your data:
my_base.update({"views": my_base.util.increment(1)}, "blog-post-1")
my_base.update({"days": my_base.util.increment(-1)}, "countdown")
my_base.update({"tags": my_base.util.append("awesome")}, "product-1")
my_base.update({"recent_visitors": my_base.util.prepend("Alice")}, "website-stats")
my_base.update({"description": my_base.util.trim()}, "user-bio")
If you enable debug mode during initialization, the SDK will log detailed information about your requests. This can be super helpful for troubleshooting!
db = connect("your-api-key", "your-project-id", debug=True)
The SDK won't throw errors when things don't go as planned. Instead, it will return None in most cases, like if you attempt to GET a non-existent key. However, it is always recommended to put database calls in a try/except block:
try:
my_base.get("non-existent-key")
except Exception as error:
print("Oops!", str(error))