@@ -46,71 +46,173 @@ View the SDK documentation [here](https://surrealdb.com/docs/integration/librari
46
46
pip install surrealdb
47
47
```
48
48
49
- ## Getting started
49
+ ## Basic Usage
50
+ > All examples assume SurrealDB is [ installed] ( https://surrealdb.com/install ) and running on port 8000.
51
+ ### Initialization
52
+ To start using the database, create an instance of SurrealDB, connect to your SurrealDB server, and specify the
53
+ namespace and database you wish to work with.
54
+ ``` python
55
+ from surrealdb import SurrealDB
50
56
51
- ### Running within synchronous code
57
+ # Connect to the database
58
+ db = SurrealDB(url = " ws://localhost:8080" )
59
+ db.connect()
60
+ db.use(" namespace" , " database_name" )
61
+ ```
52
62
53
- > This example requires SurrealDB to be [ installed] ( https://surrealdb.com/install ) and running on port 8000.
63
+ ### Using context manager
64
+ The library supports Python’s context manager to manage connections automatically.
65
+ This ensures that connections are properly closed when the block of code finishes executing.
66
+ ``` python
67
+ from surrealdb import SurrealDB
54
68
55
- Import the SDK and create the database connection:
69
+ with SurrealDB(url = " ws://localhost:8080" ) as db:
70
+ db.use(" namespace" , " database_name" )
71
+ ```
56
72
73
+ ### Using Async
74
+ The AsyncSurrealDB supports asynchronous operations while retaining compatibility with synchronous workflows,
75
+ ensuring flexibility for any range of use cases. The APIs do not differ
57
76
``` python
58
- from surrealdb import SurrealDB
77
+ from surrealdb import AsyncSurrealDB
78
+
79
+ async with AsyncSurrealDB(url = " ws://localhost:8080" ) as db:
80
+ await db.use(" namespace" , " database_name" )
81
+ ```
82
+ Without context manager:
83
+ ``` python
84
+ from surrealdb import AsyncSurrealDB
59
85
60
- db = SurrealDB(" ws://localhost:8000/database/namespace" )
86
+ # Connect to the database
87
+ db = AsyncSurrealDB(url = " ws://localhost:8080" )
88
+ await db.connect()
89
+ await db.use(" namespace" , " database_name" )
61
90
```
62
91
63
- 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 ` .
92
+ ## Connection Engines
93
+ There are 3 available connection engines you can use to connect to SurrealDb backend. It can be via Websocket, HTTP or
94
+ through embedded database connections. The connection types are simply determined by the url scheme provided in
95
+ the connection url.
96
+
97
+ ### Via Websocket
98
+ Websocket url can be ` ws ` or ` wss ` for secure connection. For example ` ws://localhost:8000 ` and ` wss://localhost:8000 ` .
99
+ All functionalities are available via websockets.
100
+
101
+ ### Via HTTP
102
+ HTTP url can be ` http ` or ` https ` for secure connection. For example ` http://localhost:8000 ` and ` https://localhost:8000 ` .
103
+ There are some functions that are not available on RPC when using HTTP but are on Websocket. This includes all
104
+ live query/notification methods.
64
105
65
- Finally, we defined the database and namespace as ` database ` and ` namespace ` .
66
- We need a database and namespace to connect to SurrealDB.
67
106
68
- Now that we have our connection we need to signin:
107
+ ### Using SurrealKV and Memory
108
+ SurrealKV and In-Memory also do not support live notifications at this time. This would be updated in a later
109
+ release.
69
110
111
+ For Surreal KV
70
112
``` python
71
- db.signin({
72
- " username" : " root" ,
73
- " password" : " root" ,
74
- })
113
+ from surrealdb import SurrealDB
114
+
115
+ db = SurrealDB(" surrealkv://path/to/dbfile.kv" )
116
+ ```
117
+ For Memory
118
+ ``` python
119
+ from surrealdb import SurrealDB
120
+
121
+ db = SurrealDB(" mem://" )
122
+ db = SurrealDB(" memory://" )
75
123
```
76
- We can now run our queries to create some users, select them and print the outcome.
77
124
125
+ ## Usage Examples
126
+ ### Insert and Retrieve Data
78
127
``` python
79
- db.query(" CREATE user:tobie SET name = 'Tobie';" )
80
- db.query(" CREATE user:jaime SET name = 'Jaime';" )
81
- outcome = db.query(" SELECT * FROM user;" )
82
- print (outcome)
128
+ from surrealdb import SurrealDB
129
+
130
+ db = SurrealDB(" ws://localhost:8080" )
131
+ db.connect()
132
+ db.use(" example_ns" , " example_db" )
133
+
134
+ # Insert a record
135
+ new_user = {" name" : " Alice" , " age" : 30 }
136
+ inserted_record = db.insert(" users" , new_user)
137
+ print (f " Inserted Record: { inserted_record} " )
138
+
139
+ # Retrieve the record
140
+ retrieved_users = db.select(" users" )
141
+ print (f " Retrieved Users: { retrieved_users} " )
142
+
143
+ db.close()
83
144
```
84
145
85
- ### Running within asynchronous code
146
+ ### Perform a Custom Query
147
+ ``` python
148
+ from surrealdb import AsyncSurrealDB
86
149
87
- > This example requires SurrealDB to be [ installed] ( https://surrealdb.com/install ) and running on port 8000.
150
+ async with AsyncSurrealDB(url = " ws://localhost:8080" ) as db:
151
+ query = " SELECT * FROM users WHERE age > $min_age"
152
+ variables = {" min_age" : 25 }
88
153
89
- The async methods work in the same way, with two main differences:
90
- - Inclusion of ` async def / await ` .
91
- - You need to call the connect method before signing in.
154
+ results = await db.query(query, variables)
155
+ print (f " Query Results: { results} " )
156
+ ```
157
+
158
+ ### Manage Authentication
159
+ ``` python
160
+ from surrealdb import SurrealDB
161
+
162
+ with SurrealDB(url = " ws://localhost:8080" ) as db:
163
+ # Sign up a new user
164
+ token = db.sign_up(username = " new_user" , password = " secure_password" )
165
+ print (f " New User Token: { token} " )
166
+
167
+ # Sign in as an existing user
168
+ token = db.sign_in(username = " existing_user" , password = " secure_password" )
169
+ print (f " Signed In Token: { token} " )
170
+
171
+ # Authenticate using a token
172
+ db.authenticate(token = token)
173
+ print (" Authentication successful!" )
174
+ ```
92
175
176
+ ### Live Query Notifications
93
177
``` python
94
178
import asyncio
95
- from surrealdb import AsyncSurrealDB
179
+ from surrealdb.surrealdb import SurrealDB
180
+
181
+ db = SurrealDB(" ws://localhost:8080" )
182
+ db.connect()
183
+ db.use(" example_ns" , " example_db" )
184
+
185
+ # Start a live query
186
+ live_id = db.live(" users" )
187
+
188
+ # Process live notifications
189
+ notifications = db.live_notifications(live_id)
190
+
191
+ async def handle_notifications ():
192
+ while True :
193
+ notification = await notifications.get()
194
+ print (f " Live Notification: { notification} " )
195
+
196
+ # Run the notification handler
197
+ asyncio.run(handle_notifications())
198
+ ```
199
+
200
+ ### Upserting Records
201
+ ``` python
202
+ from surrealdb.surrealdb import SurrealDB
203
+
204
+ db = SurrealDB(" ws://localhost:8080" )
205
+ db.connect()
206
+ db.use(" example_ns" , " example_db" )
207
+
208
+ upsert_data = {" id" : " user:123" , " name" : " Charlie" , " age" : 35 }
209
+ result = db.upsert(" users" , upsert_data)
210
+ print (f " Upsert Result: { result} " )
96
211
97
- async def main ():
98
- db = AsyncSurrealDB(" ws://localhost:8000/database/namespace" )
99
- await db.connect()
100
- await db.signin({
101
- " username" : " root" ,
102
- " password" : " root" ,
103
- })
104
- await db.query(" CREATE user:tobie SET name = 'Tobie';" )
105
- await db.query(" CREATE user:jaime SET name = 'Jaime';" )
106
- outcome = await db.query(" SELECT * FROM user;" )
107
- print (outcome)
108
-
109
-
110
- # Run the main function
111
- asyncio.run(main())
212
+ db.close()
112
213
```
113
214
114
- ### Using Jupyter Notebooks
215
+ ## Contributing
216
+ Contributions to this library are welcome! If you encounter issues, have feature requests, or
217
+ want to make improvements, feel free to open issues or submit pull requests.
115
218
116
- The Python SDK currently only supports the ` AsyncSurrealDB ` methods.
0 commit comments