lite surrealDB client that only supports websocket raw queries and async pooled connections
Project description
surreal-lite-py
An unofficial Python API for surrealDB that only has one dependency (websockets) and has a very simple interface. One interface is a blocking isolated query interface, and the other is an async connection pool interface.
Async Connection Pool Interface
You can spin up an async connection pool and make requests using the code below:
import asyncio
from sblpy.pool.connection_pool import execute_pooled_query, client_pool, shutdown_pool
from sblpy.query import Query
async def main():
# Create a pool of 5 clients
asyncio.create_task(client_pool(
host="localhost",
port=8000,
user="root",
password="root",
number_of_clients=5
))
# make 400 requests
for _ in range(100):
_ = await execute_pooled_query(Query("CREATE user:tobie SET name = 'Tobie';"))
_ = await execute_pooled_query(Query("CREATE user:jaime SET name = 'Jaime';"))
response = await execute_pooled_query(Query("SELECT * FROM user;"))
print(response)
_ = await execute_pooled_query(Query("DELETE user;"))
# Shutdown the pool
await shutdown_pool(number_of_clients=5)
if __name__ == "__main__":
asyncio.run(main())
Here we can see that we pass in a Query
object that defines the query and the params if they are also passed into the Query
object constructor. If you print this you can also see that the response is raw. In the integration tests you can see how to parse this response using response["result"][0]["result"]
This is because we do not want any serialization errors happening in the connection pool. You have control over how you handle the response. This can also help isolate against breaking changes in the future.
Basic Blocking Interface
We can create a basic blocking interface using the code below:
from sblpy.connection import SurrealSyncConnection
connection = SurrealSyncConnection(
host="localhost",
port=8000,
user="root",
password="root"
)
_ = connection.query("CREATE user:tobie SET name = 'Tobie';")
_ = connection.query("CREATE user:jaime SET name = 'Jaime';")
outcome = connection.query("SELECT * FROM user;")
print(outcome)
Here you will see that the response is a lot smoother. This is because if there are any errors or issue with parsing, we can directly throw them as the connection is going to close anyway once the connection goes out of scope. The python garbage collector will take care of cleaning up the connection but this will be delayed. If you want to ensure that the connection is closed, you can call connection.socket.close()
to close the connection.
Future Plans
There isn't much, this is just a super simple API. The less moving parts the less that can go wrong. I want to keep the dependencies to a minimum and the codebase as simple as possible. However, I do want to add the following features:
- Migration tool with command line interface
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 sdblpy-0.1.0.tar.gz
.
File metadata
- Download URL: sdblpy-0.1.0.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80fe1005132cbaef8c6382e82b6b8d2ff0994284583fcabac2fc64b57f14be63 |
|
MD5 | ef4f7e3b12c90c9f86a2e7375c77ece6 |
|
BLAKE2b-256 | 973df53ec9c38544d77e53b95d6fb702ce03f7b4acccc2c909baf283d13d7d4d |
File details
Details for the file sdblpy-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: sdblpy-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f776a81cfe32e9621a5ee269f998f94245a1f918f8b5959df285c18dec46f79 |
|
MD5 | aa76a9d61856366748165b6621c9f85b |
|
BLAKE2b-256 | 5248fe852408503cd2fadc03d5c775af7bb6c643c8360afc26e4615719e77140 |