Skip to content

Concepts

Data manipulation

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.

MethodDescription
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

Selects all records in a table, or a specific record. This is a convenience wrapper that runs SELECT * FROM <thing>;.

Method Syntax
client.select(thing, session, txn)
ArgumentDescription
thingThe table or specific record to select.
sessionAn optional session id.
txnAn optional transaction id.
# Select every record in a table
var people = client.select("person")

# Select a specific record
var chiru = client.select("person:chiru")

Creates a record in the database. This is a convenience wrapper that runs CREATE <thing> CONTENT <content_json>;.

Method Syntax
client.create(thing, content_json, session, txn)
ArgumentDescription
thingThe table or specific record to create.
content_jsonThe record content as a JSON string.
sessionAn optional session id.
txnAn optional transaction id.
var resp = client.create("person", '{ "name": "Chiru", "age": 30 }')

Inserts one or more records into a table. This is a convenience wrapper that runs INSERT INTO <table> <data_json>;.

Method Syntax
client.insert(table, data_json, session, txn)
ArgumentDescription
tableThe table to insert into.
data_jsonA single record or an array of records, as a JSON string.
sessionAn optional session id.
txnAn optional transaction id.
var resp = client.insert("person", '[{ "name": "Alice" }, { "name": "Bob" }]')

Inserts one or more relation records into a table. This is a convenience wrapper that runs INSERT RELATION INTO <table> <data_json>;.

Method Syntax
client.insert_relation(table, data_json, session, txn)
ArgumentDescription
tableThe relation table to insert into.
data_jsonA single relation or an array of relations, as a JSON string. Each carries an in and an out record.
sessionAn optional session id.
txnAn optional transaction id.
var resp = client.insert_relation(
    "likes",
    '[{ "in": "person:alice", "out": "person:bob" }]',
)

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>;.

Method Syntax
client.update(thing, content_json, session, txn)
ArgumentDescription
thingThe table or specific record to update.
content_jsonThe replacement content as a JSON string.
sessionAn optional session id.
txnAn optional transaction id.
var resp = client.update("person:chiru", '{ "age": 31 }')

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>;.

Method Syntax
client.upsert(thing, content_json, session, txn)
ArgumentDescription
thingThe table or specific record to upsert.
content_jsonThe record content as a JSON string.
sessionAn optional session id.
txnAn optional transaction id.
var resp = client.upsert("person:chiru", '{ "name": "Chiru", "age": 31 }')

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>;.

Method Syntax
client.merge(thing, data_json, session, txn)
ArgumentDescription
thingThe table or specific record to merge into.
data_jsonThe data to merge, as a JSON string.
sessionAn optional session id.
txnAn optional transaction id.
var resp = client.merge("person:chiru", '{ "age": 31 }')

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>;.

Method Syntax
client.patch(thing, patch_json, session, txn)
ArgumentDescription
thingThe table or specific record to patch.
patch_jsonA JSON Patch array as a JSON string.
sessionAn optional session id.
txnAn optional transaction id.
var resp = client.patch(
    "person:chiru",
    '[{ "op": "replace", "path": "/age", "value": 31 }]',
)

Deletes all records in a table, or a specific record. This is a convenience wrapper that runs DELETE <thing>;.

Method Syntax
client.delete(thing, session, txn)
ArgumentDescription
thingThe table or specific record to delete.
sessionAn optional session id.
txnAn optional transaction id.
# Delete a specific record
var resp = client.delete("person:chiru")

# Delete every record in a table
var cleared = client.delete("person")

Was this page helpful?

Last updated