The Mojo SDK wraps the most common record statements so you do not have to write the SurrealQL by hand. Each method takes the table or record to act on and, for methods that write, the data as a JSON string. The SDK builds the SurrealQL statement internally, and the description of each method states the statement it runs. Every method returns an RpcResponse, described under Reading the response.
| Method | Description |
|---|---|
client.select() | Selects all records in a table, or a specific record |
client.create() | Creates a record in the database |
client.insert() | Inserts one or more records into a table |
client.insert_relation() | Inserts one or more relation records into a table |
client.update() | Updates all records in a table, or a specific record, replacing their content |
client.upsert() | Creates a record if it does not exist, or updates it if it does |
client.merge() | Merges data into all records in a table, or a specific record |
client.patch() | Applies a JSON Patch to all records in a table, or a specific record |
client.delete() | Deletes all records in a table, or a specific record |
.select()
Selects all records in a table, or a specific record. This is a convenience wrapper that runs SELECT * FROM <thing>;.
client.select(thing, session, txn)Arguments
| Argument | Description |
|---|---|
thing | The table or specific record to select. |
session | An optional session id. |
txn | An optional transaction id. |
Example usage
# Select every record in a table
var people = client.select("person")
# Select a specific record
var chiru = client.select("person:chiru") .create()
Creates a record in the database. This is a convenience wrapper that runs CREATE <thing> CONTENT <content_json>;.
client.create(thing, content_json, session, txn)Arguments
| Argument | Description |
|---|---|
thing | The table or specific record to create. |
content_json | The record content as a JSON string. |
session | An optional session id. |
txn | An optional transaction id. |
Example usage
var resp = client.create("person", '{ "name": "Chiru", "age": 30 }') .insert()
Inserts one or more records into a table. This is a convenience wrapper that runs INSERT INTO <table> <data_json>;.
client.insert(table, data_json, session, txn)Arguments
| Argument | Description |
|---|---|
table | The table to insert into. |
data_json | A single record or an array of records, as a JSON string. |
session | An optional session id. |
txn | An optional transaction id. |
Example usage
var resp = client.insert("person", '[{ "name": "Alice" }, { "name": "Bob" }]') .insert_relation()
Inserts one or more relation records into a table. This is a convenience wrapper that runs INSERT RELATION INTO <table> <data_json>;.
client.insert_relation(table, data_json, session, txn)Arguments
| Argument | Description |
|---|---|
table | The relation table to insert into. |
data_json | A single relation or an array of relations, as a JSON string. Each carries an in and an out record. |
session | An optional session id. |
txn | An optional transaction id. |
Example usage
var resp = client.insert_relation(
"likes",
'[{ "in": "person:alice", "out": "person:bob" }]',
) .update()
Updates all records in a table, or a specific record, replacing their content. This is a convenience wrapper that runs UPDATE <thing> CONTENT <content_json>;.
client.update(thing, content_json, session, txn)Arguments
| Argument | Description |
|---|---|
thing | The table or specific record to update. |
content_json | The replacement content as a JSON string. |
session | An optional session id. |
txn | An optional transaction id. |
Example usage
var resp = client.update("person:chiru", '{ "age": 31 }') .upsert()
Creates a record if it does not exist, or updates it if it does. This is a convenience wrapper that runs UPSERT <thing> CONTENT <content_json>;.
client.upsert(thing, content_json, session, txn)Arguments
| Argument | Description |
|---|---|
thing | The table or specific record to upsert. |
content_json | The record content as a JSON string. |
session | An optional session id. |
txn | An optional transaction id. |
Example usage
var resp = client.upsert("person:chiru", '{ "name": "Chiru", "age": 31 }') .merge()
Merges data into all records in a table, or a specific record, leaving unspecified fields untouched. This is a convenience wrapper that runs UPDATE <thing> MERGE <data_json>;.
client.merge(thing, data_json, session, txn)Arguments
| Argument | Description |
|---|---|
thing | The table or specific record to merge into. |
data_json | The data to merge, as a JSON string. |
session | An optional session id. |
txn | An optional transaction id. |
Example usage
var resp = client.merge("person:chiru", '{ "age": 31 }') .patch()
Applies a JSON Patch to all records in a table, or a specific record. This is a convenience wrapper that runs UPDATE <thing> PATCH <patch_json>;.
client.patch(thing, patch_json, session, txn)Arguments
| Argument | Description |
|---|---|
thing | The table or specific record to patch. |
patch_json | A JSON Patch array as a JSON string. |
session | An optional session id. |
txn | An optional transaction id. |
Example usage
var resp = client.patch(
"person:chiru",
'[{ "op": "replace", "path": "/age", "value": 31 }]',
) .delete()
Deletes all records in a table, or a specific record. This is a convenience wrapper that runs DELETE <thing>;.
client.delete(thing, session, txn)Arguments
| Argument | Description |
|---|---|
thing | The table or specific record to delete. |
session | An optional session id. |
txn | An optional transaction id. |
Example usage
# Delete a specific record
var resp = client.delete("person:chiru")
# Delete every record in a table
var cleared = client.delete("person")