Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC-325: Add Python Docs #1130

Merged
merged 25 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8944866
Removed golang updates
remade Jan 22, 2025
4d40d22
Updated with readme example
remade Jan 23, 2025
d03f6db
Fix broken build
Ekwuno Jan 23, 2025
4969e22
Merge branch 'main' into update-docs-py-and-go
remade Jan 24, 2025
b3ce667
add back api warning
Ekwuno Jan 26, 2025
a620772
rename setup
Ekwuno Jan 26, 2025
0da7299
connection engine options
Ekwuno Jan 26, 2025
00da229
fix build
Ekwuno Jan 26, 2025
a374162
reorder pages
Ekwuno Jan 26, 2025
5c8ad90
update first full draft of python docs - will need some adjustment
AlexFrid Jan 27, 2025
3f91920
Update src/content/doc-sdk-python/concepts/index.mdx
Ekwuno Jan 27, 2025
9310349
Merge branch 'main' into update-docs-py-and-go
remade Jan 28, 2025
9e036ff
take out embedded connection
AlexFrid Jan 29, 2025
e592cc5
Doc update
remade Jan 30, 2025
3c89c7b
Merge branch 'update-docs-py-and-go' of github.com:surrealdb/docs.sur…
remade Jan 30, 2025
6714112
update coming soon to available
Ekwuno Jan 30, 2025
0918b84
update examples
AlexFrid Jan 30, 2025
9d1e16f
Merge branch 'update-docs-py-and-go' of https://github.com/surrealdb/…
AlexFrid Jan 30, 2025
55f37ed
update data types
AlexFrid Jan 30, 2025
91456e5
Hopefully resolve conflicts
AlexFrid Jan 30, 2025
0eda6ca
updating examples
AlexFrid Jan 30, 2025
ec3e88f
Update src/content/doc-sdk-python/methods/connect.mdx
AlexFrid Jan 30, 2025
958e14b
Merge branch 'main' of https://github.com/surrealdb/docs.surrealdb.co…
AlexFrid Jan 30, 2025
253bbd3
add final updates
AlexFrid Jan 30, 2025
c0c1744
Update src/content/doc-sdk-python/start.mdx
AlexFrid Jan 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/content/doc-sdk-python/concepts/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"sidebar_label": "Concepts",
"sidebar_position": 6
}
133 changes: 133 additions & 0 deletions src/content/doc-sdk-python/concepts/create-a-new-connection.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
sidebar_position: 1
sidebar_label: Create a new connection
title: Python | SDK | Create a new connection
description: The SurrealDB SDK for Python enables simple and advanced querying of a remote or embedded database.
---

import Tabs from "@components/Tabs/Tabs.astro"
import TabItem from "@components/Tabs/TabItem.astro"
import Label from "@components/shared/Label.astro"

# Create a new connection

When creating a new connection to SurrealDB, you can choose to connect to a local or remote endpoint, specify a namespace and database pair to use, authenticate with an existing token, authenticate using a pair of credentials, or use advanced custom logic to prepare the connection to the database.

First, you need to initialize a new instance of the `Surreal` or `AsyncSurreal` class and connect it to a database endpoint.


## Related Methods and Properties

While the `Surreal` class is the primary method to connect to SurrealDB, there are other methods that you can use while managing your connection.

<table>
<thead>
<tr>
<th scope="col">Method</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td scope="row" data-label="Method"><a href="#connect"> <code> Surreal / AsyncSurreal </code></a></td>
<td scope="row" data-label="Description">Establishes a persistent connection to the database</td>
</tr>
<tr>
<td scope="row" data-label="Method"><a href="#close"> <code> db.close() </code></a></td>
<td scope="row" data-label="Description">Closes the persistent connection to the database</td>
</tr>
<tr>
<td scope="row" data-label="Method"><a href="#use"> <code> db.use()</code></a></td>
<td scope="row" data-label="Description">Switch to a specific namespace and database</td>
</tr>
</tbody>
</table>

## `Surreal / AsyncSurreal`

You can specify your connection protocol either as `http`, `https`, `ws`, or `wss`.

### Example usage
```python
# Connect to a local endpoint with http protocol
db = Surreal('http://127.0.0.1:8000')

# Connect to a remote endpoint with ws protocol
db = AsyncSurreal('wss://cloud.surrealdb.com')
```

### Effect of connection protocol on token & session duration

The connection protocol you choose affects how authentication tokens and sessions work:

With websockets connections (`ws://`, `wss://`) you open a single long-lived stateful connection where after the initial authentication, the session duration applies and if not specified, defaults to `NONE` meaning that the session never expires unless otherwise specified.

When you connect with a HTTP connection (`http://`, `https://`), every request you make is short-lived and stateless, requiring you to authenticate every request individually for which the token is used, creating a short lived session. Hence, the token duration which defaults to 1 hour applies.

You can extend the session duration of a token or a session by setting the `DURATION` clause when creating a new access method with the [`DEFINE ACCESS METHOD`](/docs/surrealql/statements/define/access) statement or when defining a new user with the [`DEFINE USER`](/docs/surrealql/statements/define/user) statement.

Learn more about token and session duration in our [security best practices](/docs/surrealdb/reference-guide/security-best-practices#expiration) documentation.

<br />

## `.use()`

Depending on the complexity of your use case, you can switch to a specific namespace and database using the `.use()` method. This is particularly useful if you want to switch to a different setup after connecting. You can also stay in the same namespace but switch to a different database.

Learn more about the `.use()` method [in the methods section](/docs/sdk/python/methods/use).

### Example usage
```python
db.use(namespace='surrealdb', database='docs')
```

<br />

## `.close()`

The `.close()` method closes the persistent connection to the database. You should call this method when you are done with the connection to free up resources.


### Example usage

```python
db.close()
```

## Context manager

You can also use Python's context manger to automatically open and close a connection when exiting the context.

## Putting it all together

```python
from surrealdb import Surreal

# Without using a context manager
db = Surreal('ws://localhost:8000')
db.use('namespace', 'database')
# Sign in and your code...
db.close()

# Using a context manager
with Surreal('ws://localhost:8000') as db:
db.use('namespace', 'database')
# Sign in and your code...
```

The same applies when using Async

```python
from surrealdb import AsyncSurreal

# Without using a context manager
db = AsyncSurreal('ws://localhost:8000')
await db.use('namespace', 'database')
# Sign in and your code...
await db.close()

# Using a context manager
async with AsyncSurreal('ws://localhost:8000') as db:
await db.use('namespace', 'database')
# Sign in and your code...
```
Loading