Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/surrealdb/surrealdb.py
SurrealDB SDK for Python
https://github.com/surrealdb/surrealdb.py
database database-connector database-sdk iot-database python python-library python-module realtime-database surreal surrealdb
Last synced: 13 days ago
JSON representation
SurrealDB SDK for Python
- Host: GitHub
- URL: https://github.com/surrealdb/surrealdb.py
- Owner: surrealdb
- License: apache-2.0
- Created: 2022-04-07T16:16:39.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-09T16:56:30.000Z (2 months ago)
- Last Synced: 2024-09-09T20:54:14.778Z (2 months ago)
- Topics: database, database-connector, database-sdk, iot-database, python, python-library, python-module, realtime-database, surreal, surrealdb
- Language: Python
- Homepage: https://surrealdb.com
- Size: 376 KB
- Stars: 174
- Watchers: 21
- Forks: 52
- Open Issues: 26
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
- awesome-surreal - surrealdb.py - <a href="https://surrealdb.com#gh-dark-mode-only" target="_blank"><img src="/img/white/text.svg" height="12" alt="SurrealDB"></a> <a href="https://surrealdb.com#gh-light-mode-only" target="_blank"><img src="/img/black/text.svg" height="12" alt="SurrealDB"></a> official driver for Python. (Client libraries)
README
The official SurrealDB SDK for Python.
# surrealdb.py
The official SurrealDB SDK for Python.
## Documentation
View the SDK documentation [here](https://surrealdb.com/docs/integration/libraries/python).
## How to install
```sh
pip install surrealdb
```## Getting started
### Running within synchronous code
> This example requires SurrealDB to be [installed](https://surrealdb.com/install) and running on port 8000.
Import the SDK and create the database connection:
```python
from surrealdb import SurrealDBdb = SurrealDB("ws://localhost:8000/database/namespace")
```Here, we can see that we defined the connection protocol as WebSocket using `ws://`. We then defined the host as `localhost` and the port as `8000`.
Finally, we defined the database and namespace as `database` and `namespace`.
We need a database and namespace to connect to SurrealDB.Now that we have our connection we need to signin:
```python
db.signin({
"username": "root",
"password": "root",
})
```
We can now run our queries to create some users, select them and print the outcome.```python
db.query("CREATE user:tobie SET name = 'Tobie';")
db.query("CREATE user:jaime SET name = 'Jaime';")
outcome = db.query("SELECT * FROM user;")
print(outcome)
```### Running within asynchronous code
> This example requires SurrealDB to be [installed](https://surrealdb.com/install) and running on port 8000.
The async methods work in the same way, with two main differences:
- Inclusion of `async def / await`.
- You need to call the connect method before signing in.```python
import asyncio
from surrealdb import AsyncSurrealDBasync def main():
db = AsyncSurrealDB("ws://localhost:8000/database/namespace")
await db.connect()
await db.signin({
"username": "root",
"password": "root",
})
await db.query("CREATE user:tobie SET name = 'Tobie';")
await db.query("CREATE user:jaime SET name = 'Jaime';")
outcome = await db.query("SELECT * FROM user;")
print(outcome)# Run the main function
asyncio.run(main())
```### Using Jupyter Notebooks
The Python SDK currently only supports the `AsyncSurrealDB` methods.