@@ -89,76 +89,166 @@ Below are examples of how you can connect to your Instance using the SurrealDB S
89
89
<TabItem value = " Rust" label = " Rust" default >
90
90
91
91
``` rust
92
- use surrealdb :: engine :: remote :: ws :: Wss ;
92
+ use serde :: {Deserialize , Serialize };
93
+ use surrealdb :: engine :: any;
93
94
use surrealdb :: opt :: auth :: Root ;
94
- use surrealdb :: Surreal ;
95
+ use tokio;
96
+
97
+ #[derive(Debug , Serialize , Deserialize )]
98
+ struct Person {
99
+ name : String ,
100
+ }
95
101
96
102
#[tokio:: main]
97
- async fn main () -> surrealdb :: Result <()> {
98
- // Connect to the server
99
- let db = Surreal :: new :: <Wss >(" cloud-docs-068rp16e0hsnl62vgooa7omjks.aws-euw1.staging.surrealdb.cloud" ). await ? ;
103
+ async fn main () -> Result <(), Box <dyn std :: error :: Error >> {
104
+ // Open a connection
105
+ let db = any :: connect (" wss://cloud-docs-068rp16e0hsnl62vgooa7omjks.aws-euw1.staging.surrealdb.cloud" ). await ? ;
106
+
107
+ // Select a namespace and database
108
+ db . use_ns (" Cloud Namespace" ). use_db (" Cloud Database" ). await ? ;
100
109
101
- // Signin as a namespace, database, or root user
102
- db . signin (Root {
103
- username : " <created root user>" ,
104
- password : " <created root password>" ,
105
- })
106
- . await ? ;
110
+ // Authenticate
111
+ db . signin (Root {
112
+ username : " <created root user>" ,
113
+ password : " <created root password>" ,
114
+ }). await ? ;
107
115
108
- // Select a specific namespace / database
109
- db . use_ns (" Cloud Namespace" ). use_db (" Cloud Database" ). await ? ;
116
+ db . query (" CREATE person:john SET name = 'John Doe', age = 25" ). await ? . check ()? ;
110
117
111
- Ok (())
118
+ // Query that person
119
+ let john : Option <Person > = db . select ((" person" , " john" )). await ? ;
120
+ dbg! (john );
121
+
122
+ Ok (())
112
123
}
113
124
```
114
125
</TabItem >
115
126
<TabItem value = " JavaScript" label = " JavaScript" >
116
127
117
128
``` js
118
- import { Surreal } from " surrealdb" ;
129
+ import { Surreal , RecordID } from " surrealdb" ;
119
130
const db = new Surreal ();
120
-
121
- // Connect to the server
122
- await db .connect (' wss://cloud-docs-068rp16e0hsnl62vgooa7omjks.aws-euw1.staging.surrealdb.cloud' );
123
-
124
- // Signin as a namespace, database, or root user
125
- await db .signin ({
126
- username: ' <created root user>' ,
127
- password: ' <created root password>' ,
128
- });
129
-
130
- // Select a specific namespace / database
131
- await db .use ({ ns: ' Cloud Namespace' , db: ' Cloud Database' });
131
+ // Open a connection and authenticate
132
+ await db .connect (" wss://cloud-docs-068rp16e0hsnl62vgooa7omjks.aws-euw1.staging.surrealdb.cloud" , {
133
+ // Declare the namespace and database in the connection string there is no need to use the `use` method
134
+ namespace: " Cloud Namespace" ,
135
+ database: " Cloud Database" ,
136
+ auth: {
137
+ username: " <created root user>" ,
138
+ password: " <created root password>" ,
139
+ }
140
+ });
141
+ // If you want to select a specific namespace / database outside the connection string, you can use the `use` method
142
+ // await db.use({ ns: 'Cloud Namespace', db: 'Cloud Database' });
143
+ // Create record
144
+ await db .create (new RecordID (" person" ), {
145
+ title: " Founder & CEO" ,
146
+ name: {
147
+ first: " Tobie" ,
148
+ last: " Morgan Hitchcock" ,
149
+ },
150
+ tags: [" python" , " documentation" ],
151
+ });
152
+ // Select all records in person table
153
+ console .log (await db .select (" person" ));
154
+ await db .close ();
132
155
```
133
156
</TabItem >
134
157
<TabItem value = " Python" label = " Python" >
135
158
136
159
``` py
160
+
161
+ from surrealdb import Surreal
162
+ from surrealdb.classes import RecordID
137
163
# Open a connection
138
- async with Surreal(url = " wss://cloud-demo-06a98cg0u1qs3d4mnl774mlq8k.aws-use1.surreal.cloud" ) as db:
164
+ with Surreal(url = " wss://cloud-demo-06a98cg0u1qs3d4mnl774mlq8k.aws-use1.surreal.cloud" ) as db:
139
165
# Select a namespace and database
140
- await db.use(' Cloud Namespace' , ' Cloud Database' )
166
+ db.use(" Cloud Namespace" , " Cloud Database" )
141
167
# Authenticate
142
- await db.signin({" username" =" <created root user>" , " password" =" <created root password>" })
168
+ db.signin(username = " <created root user>" , password = " <created root password>" )
169
+ # Create a record
170
+ db.create(RecordID(" grocery" , " 1" ), {
171
+ " name" : " Banana" ,
172
+ " quantity" : 10 ,
173
+ })
174
+ # Select a specific record
175
+ print (db.select(RecordID(" grocery" , " 1" )))
143
176
```
144
177
</TabItem >
145
178
<TabItem value = " .NET" label = " .NET" >
146
179
147
180
``` csharp
181
+
148
182
using SurrealDb .Net ;
183
+ using SurrealDb .Net .Models ;
149
184
using SurrealDb .Net .Models .Auth ;
185
+ using System .Text .Json ;
150
186
151
- using var db = new SurrealDbClient ( " https://cloud-docs-068rp16e0hsnl62vgooa7omjks.aws-euw1.staging.surrealdb.cloud/rpc " ) ;
187
+ const string TABLE = " person " ;
152
188
153
- // Select a namespace and database
189
+ using var db = new SurrealDbClient (" wss://cloud-docs-068rp16e0hsnl62vgooa7omjks.aws-euw1.staging.surrealdb.cloud/rpc" );
190
+
191
+ await db .SignIn (new RootAuth { Username = " <created root user>" , Password = " <created root password>" });
154
192
await db .Use (" Cloud Namespace" , " Cloud Database" );
155
193
156
- // Authenticate
157
- await db .SignIn (new RootAuth
194
+ // Create a new instance of a person with nested properties
195
+ var person = new Person
196
+ {
197
+ Title = " Founder & CEO" ,
198
+ Name = new () { FirstName = " Tobie" , LastName = " Morgan Hitchcock" },
199
+ Marketing = true
200
+ };
201
+
202
+ // Create a new record in the database and store the result
203
+ var created = await db .Create (TABLE , person );
204
+ Console .WriteLine (ToJsonString (created ));
205
+
206
+ // Merge (update) an existing record with new marketing value
207
+ // Uses generic types to specify the merge data and return type
208
+ var updated = await db .Merge <ResponsibilityMerge , Person >(
209
+ new () { Id = (TABLE , " jaime" ), Marketing = true }
210
+ );
211
+ Console .WriteLine (ToJsonString (updated ));
212
+
213
+ // Select all records from the person table
214
+ var people = await db .Select <Person >(TABLE );
215
+ Console .WriteLine (ToJsonString (people ));
216
+
217
+ // Perform a grouped query to count records by marketing status
218
+ var queryResponse = await db .Query (
219
+ $" SELECT Marketing, count() AS Count FROM type::table({TABLE }) GROUP BY Marketing"
220
+ );
221
+ var groups = queryResponse .GetValue <List <Group >>(0 );
222
+ Console .WriteLine (ToJsonString (groups ));
223
+
224
+ static string ToJsonString (object ? o )
225
+ {
226
+ return JsonSerializer .Serialize (o , new JsonSerializerOptions { WriteIndented = true , });
227
+ }
228
+
229
+ public class Person : Record
158
230
{
159
- Username = " <created root user>" ,
160
- Password = " <created root password>" ,
161
- });
231
+ public string ? Title { get ; set ; }
232
+ public Name ? Name { get ; set ; }
233
+ public bool Marketing { get ; set ; }
234
+ }
235
+ public class Name
236
+ {
237
+ public string ? FirstName { get ; set ; }
238
+ public string ? LastName { get ; set ; }
239
+ }
240
+ public class ResponsibilityMerge : Record
241
+ {
242
+ public bool Marketing { get ; set ; }
243
+ }
244
+ public class Group
245
+ {
246
+ public bool Marketing { get ; set ; }
247
+ public int Count { get ; set ; }
248
+ }
249
+
250
+ // Run
251
+ dotnet run
162
252
```
163
253
</TabItem >
164
254
<TabItem value = " PHP" label = " PHP" >
0 commit comments