Skip to content

Commit 3d7d6a7

Browse files
authored
Update readme and examples (#125)
1 parent d5bcfeb commit 3d7d6a7

24 files changed

+517
-157
lines changed

README.md

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,49 @@ await db.connect()
8989
await db.use("namespace", "database_name")
9090
```
9191

92+
### Example Usage
93+
```python
94+
from surrealdb import SurrealDB, GeometryPoint, Table
95+
96+
with SurrealDB(url="ws://localhost:8000") as db:
97+
db.use("test_ns", "test_db")
98+
auth_token = db.sign_in(username="root", password="root")
99+
100+
# Check token validity. This is not necessary if you called `sign_in` before. This authenticates the
101+
# `db` instance too if `sign_in` was not previously called
102+
db.authenticate(auth_token)
103+
104+
# Create an entry
105+
person = db.create(Table("persons"), {
106+
"Name": "John",
107+
"Surname": "Doe",
108+
"Location": GeometryPoint(-0.11, 22.00),
109+
})
110+
111+
print("Created person with a map:", person)
112+
113+
# Get entry by Record ID
114+
person = db.select(person.get("id"))
115+
print("Selected a person by record id: ", person)
116+
117+
# Or retrieve the entire table
118+
persons = db.select(Table("persons"))
119+
print("Selected all in persons table: ", persons)
120+
121+
# Delete an entry by ID
122+
_ = db.delete(persons[0].get("id"))
123+
124+
# Delete all entries
125+
_ = db.delete(Table("persons"))
126+
127+
# Confirm empty table
128+
persons = db.select(Table("persons"))
129+
print("No Selected person: ", persons)
130+
131+
# And later, we can invalidate the token
132+
db.invalidate(auth_token)
133+
```
134+
92135
## Connection Engines
93136
There are 3 available connection engines you can use to connect to SurrealDb backend. It can be via Websocket, HTTP or
94137
through embedded database connections. The connection types are simply determined by the url scheme provided in
@@ -122,14 +165,15 @@ db = SurrealDB("mem://")
122165
db = SurrealDB("memory://")
123166
```
124167

125-
## Usage Examples
168+
## Additional Examples
126169
### Insert and Retrieve Data
127170
```python
128171
from surrealdb import SurrealDB
129172

130-
db = SurrealDB("ws://localhost:8080")
173+
db = SurrealDB("ws://localhost:8000")
131174
db.connect()
132175
db.use("example_ns", "example_db")
176+
db.sign_in("root", "root")
133177

134178
# Insert a record
135179
new_user = {"name": "Alice", "age": 30}
@@ -147,12 +191,18 @@ db.close()
147191
```python
148192
from surrealdb import AsyncSurrealDB
149193

150-
async with AsyncSurrealDB(url="ws://localhost:8080") as db:
151-
query = "SELECT * FROM users WHERE age > $min_age"
152-
variables = {"min_age": 25}
194+
async def main():
195+
async with AsyncSurrealDB(url="ws://localhost:8000") as db:
196+
await db.sign_in("root", "root")
197+
await db.use("test", "test")
198+
199+
query = "SELECT * FROM users WHERE age > min_age;"
200+
variables = {"min_age": 25}
201+
202+
results = await db.query(query, variables)
203+
print(f"Query Results: {results}")
153204

154-
results = await db.query(query, variables)
155-
print(f"Query Results: {results}")
205+
asyncio.run(main())
156206
```
157207

158208
### Manage Authentication
@@ -201,15 +251,13 @@ asyncio.run(handle_notifications())
201251
```python
202252
from surrealdb.surrealdb import SurrealDB
203253

204-
db = SurrealDB("ws://localhost:8080")
205-
db.connect()
206-
db.use("example_ns", "example_db")
254+
with SurrealDB("ws://localhost:8000") as db:
255+
db.sign_in("root", "root")
256+
db.use("example_ns", "example_db")
207257

208-
upsert_data = {"id": "user:123", "name": "Charlie", "age": 35}
209-
result = db.upsert("users", upsert_data)
210-
print(f"Upsert Result: {result}")
211-
212-
db.close()
258+
upsert_data = { "name": "Charlie", "age": 35}
259+
result = db.upsert("users", upsert_data)
260+
print(f"Upsert Result: {result}")
213261
```
214262

215263
## Contributing

examples/async/basic_auth_example.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import asyncio
2+
3+
from surrealdb import AsyncSurrealDB
4+
5+
6+
async def main():
7+
async with AsyncSurrealDB(url="ws://localhost:8000") as db:
8+
# Sign up a new user
9+
token = await db.sign_up(username="new_user", password="secure_password")
10+
print(f"New User Token: {token}")
11+
12+
# Sign in as an existing user
13+
token = await db.sign_in(username="existing_user", password="secure_password")
14+
print(f"Signed In Token: {token}")
15+
16+
# Authenticate using a token
17+
await db.authenticate(token=token)
18+
print("Authentication successful!")
19+
20+
21+
if __name__ == '__main__':
22+
asyncio.run(main())

examples/basic_async_example.py renamed to examples/async/basic_example.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,31 @@ async def main():
88
await db.sign_in("root", "root")
99

1010
print("Using methods")
11-
print("create: ", await db.create(
12-
"person",
13-
{
14-
"user": "me",
15-
"pass": "safe",
16-
"marketing": True,
17-
"tags": ["python", "documentation"],
18-
},
19-
))
11+
print(
12+
"create: ",
13+
await db.create(
14+
"person",
15+
{
16+
"user": "me",
17+
"pass": "safe",
18+
"marketing": True,
19+
"tags": ["python", "documentation"],
20+
},
21+
),
22+
)
2023
print("read: ", await db.select("person"))
21-
print("update: ", await db.update("person", {
22-
"user": "you",
23-
"pass": "very_safe",
24-
"marketing": False,
25-
"tags": ["Awesome"]
26-
}))
24+
print(
25+
"update: ",
26+
await db.update(
27+
"person",
28+
{
29+
"user": "you",
30+
"pass": "very_safe",
31+
"marketing": False,
32+
"tags": ["Awesome"],
33+
},
34+
),
35+
)
2736
print("delete: ", await db.delete("person"))
2837

2938
# You can also use the query method
@@ -32,24 +41,34 @@ async def main():
3241
# In SurrealQL you can do a direct insert
3342
# and the table will be created if it doesn't exist
3443
print("Using justawait db.query")
35-
print("create: ", await db.query("""
44+
print(
45+
"create: ",
46+
await db.query(
47+
"""
3648
insert into person {
3749
user: 'me',
3850
pass: 'very_safe',
3951
tags: ['python', 'documentation']
4052
};
4153
42-
"""))
54+
"""
55+
),
56+
)
4357
print("read: ", await db.query("select * from person"))
4458

45-
print("update: ", await db.query("""
59+
print(
60+
"update: ",
61+
await db.query(
62+
"""
4663
update person content {
4764
user: 'you',
4865
pass: 'more_safe',
4966
tags: ['awesome']
5067
};
5168
52-
"""))
69+
"""
70+
),
71+
)
5372
print("delete: ", await db.query("delete person"))
5473

5574

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import asyncio
2+
3+
from surrealdb import AsyncSurrealDB
4+
5+
6+
async def main():
7+
db = AsyncSurrealDB("ws://localhost:8000")
8+
await db.connect()
9+
await db.use("example_ns", "example_db")
10+
await db.sign_in("root", "root")
11+
12+
# Insert a record
13+
new_user = {"name": "Alice", "age": 30}
14+
inserted_record = await db.insert("users", new_user)
15+
print(f"Inserted Record: {inserted_record}")
16+
17+
# Retrieve the record
18+
retrieved_users = await db.select("users")
19+
print(f"Retrieved Users: {retrieved_users}")
20+
21+
await db.close()
22+
23+
24+
if __name__ == '__main__':
25+
asyncio.run(main())

examples/async/basic_query_example.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import asyncio
2+
from surrealdb import AsyncSurrealDB
3+
4+
5+
async def main():
6+
async with AsyncSurrealDB(url="ws://localhost:8000") as db:
7+
await db.sign_in("root", "root")
8+
await db.use("test", "test")
9+
10+
query = "SELECT * FROM users WHERE age > min_age;"
11+
variables = {"min_age": 25}
12+
13+
results = await db.query(query, variables)
14+
print(f"Query Results: {results}")
15+
16+
17+
asyncio.run(main())
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import asyncio
2+
3+
from surrealdb import AsyncSurrealDB
4+
5+
async def main():
6+
async with AsyncSurrealDB("ws://localhost:8000") as db:
7+
db.sign_in("root", "root")
8+
db.use("example_ns", "example_db")
9+
10+
upsert_data = {"name": "Charlie", "age": 35}
11+
result = db.upsert("users", upsert_data)
12+
print(f"Upsert Result: {result}")
13+
14+
if __name__ == '__main__':
15+
asyncio.run(main())

examples/basic_example.py

Lines changed: 0 additions & 50 deletions
This file was deleted.

examples/notebook_example.ipynb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,10 @@
5757
"metadata": {},
5858
"outputs": [],
5959
"source": [
60-
"await db.update(\"person\", {\n",
61-
" \"user\":\"you\",\n",
62-
" \"pass\":\"very_safe\",\n",
63-
" \"marketing\": False,\n",
64-
" \"tags\": [\"Awesome\"]\n",
65-
"})"
60+
"await db.update(\n",
61+
" \"person\",\n",
62+
" {\"user\": \"you\", \"pass\": \"very_safe\", \"marketing\": False, \"tags\": [\"Awesome\"]},\n",
63+
")"
6664
]
6765
},
6866
{

examples/sync/basic_auth_example.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from surrealdb import SurrealDB
2+
3+
4+
def main():
5+
with SurrealDB(url="ws://localhost:8000") as db:
6+
# Sign up a new user
7+
token = db.sign_up(username="new_user", password="secure_password")
8+
print(f"New User Token: {token}")
9+
10+
# Sign in as an existing user
11+
token = db.sign_in(username="existing_user", password="secure_password")
12+
print(f"Signed In Token: {token}")
13+
14+
# Authenticate using a token
15+
db.authenticate(token=token)
16+
print("Authentication successful!")
17+
18+
19+
if __name__ == '__main__':
20+
main()

0 commit comments

Comments
 (0)