SurrealDB supports a number of methods for interacting with the database and performing CRUD operations.
Method | Description |
---|---|
db.select() | Selects all records in a table, or a specific record, from the database |
db.create() | Creates a record in the database |
db.insert() | Inserts one or multiple records in the database |
db.insert_relation() | Inserts one or multiple relations in the database |
db.update() | Updates all records in a table, or a specific record, in the database |
db.merge() | Modifies all records in a table, or a specific record, in the database |
db.patch() | Applies JSON Patch changes to all records, or a specific record, in the database |
db.delete() | Deletes all records in a table, or a specific record, from the database |
.select()
Selects all records in a table, or a specific record, from the database.
Method Syntaxdb.select(thing)
Arguments | Description | ||
---|---|---|---|
thing required | The table name or a |
# Select all records from a table people = db.select('person') # Select a specific record from a table person = db.select(RecordID('person', 'h5wxrf2ewk8xjxosxtyc'))
This function will run the following query in the database.
SELECT * FROM $thing;
.create()
Creates a record in the database.
Method Syntaxdb.create(thing, data)
Arguments | Description | ||
---|---|---|---|
thing required | The table name or a | ||
data optional | The document / record data to create. |
# Create a record with a random ID person = db.create('person') # Create a record with a specific ID person = db.create(RecordID('person', 'tobie'), { "name": 'Tobie', "settings": { "active": True, "marketing": True, } })
This function will run the following query in the database.
CREATE $thing CONTENT $data;
.insert()
Inserts one or multiple records in the database.
Method Syntaxdb.insert(table, data)
Arguments | Description | ||
---|---|---|---|
thing required | The table name to insert to. | ||
data required | Either a single document/record or an array of documents/records to insert |
# Insert a single record db.insert('person', { "name": 'Tobie', "settings": { "active": True, "marketing": True, }, }) # Insert multiple records db.insert('person', [ { "name": 'Tobie', "settings": { "active": True, "marketing": True, }, }, { "name": 'Jaime', "settings": { "active": True, "marketing": True, }, }, ])
This function will run the following query in the database.
INSERT INTO $table $data;
.insert_relation()
Inserts one or multiple relations in the database.
Method Syntaxdb.insert_relation(table, data)
Arguments | Description | ||
---|---|---|---|
table required | The table name to insert to. | ||
data required | Either a single document/record or an array of documents/records to insert |
# Insert a single record db.insert_relation('likes', { "in": RecordID('person', 'tobie'), "out": RecordID('post', 123) }) # Insert multiple records across tables people = db.insert_relation('likes', [ { "in": RecordID('person', 'tobie'), "out": RecordID('post', 123), }, { "in": RecordID('person', 'jaime'), "out": RecordID('post', 456), } ])
This function will run the following query in the database.
INSERT RELATION INTO $table $data;
.update()
Updates all records in a table, or a specific record, in the database.
Method Syntaxdb.update(thing, data)
NoteThis function replaces the current document / record data with the specified data.
Arguments | Description | ||
---|---|---|---|
thing required | The table name or the specific | ||
data optional | The document / record data to update. |
# Update all records in a table db.update('person', {name: "Jaime"}) # Update a record with a specific ID db.update(RecordID('person', 'tobie'), { "name": 'Tobie', "settings": { "active": True, "marketing": True, } })
This function will run the following query in the database.
UPDATE $thing CONTENT $data;
.merge()
Modifies all records in a table, or a specific record, in the database.
Method Syntaxdb.merge(thing, data)
NoteThis function merges the current document / record data with the specified data. If no merge data is passed it will simply trigger an update.
Arguments | Description | ||
---|---|---|---|
thing required | The table name or the specific | ||
data optional | The document / record data to merge. |
# Update all records in a table db.merge('person', { "updated_at": datetime.datetime.now().isoformat() }) # Update a record with a specific ID db.merge(RecordID('person', 'tobie'), { "updated_at": datetime.datetime.now().isoformat(), "settings": { "active": True, } })
This function will run the following query in the database.
UPDATE $thing MERGE $data;
.patch()
Applies JSON Patch changes to all records, or a specific record, in the database.
Method Syntaxdb.patch(thing, data)
NoteThis function patches the current document / record data with the specified JSON Patch data.
Arguments | Description | ||
---|---|---|---|
thing required | The table name or the specific | ||
data required | The JSON Patch data with which to patch the records. |
# Update all records in a table db.patch('person', [ { "op": 'replace', "path": '/created_at', "value": datetime.datetime.now().isoformat() }, ]) # Update a record with a specific ID db.patch(RecordID('person', 'tobie'), [ { "op": 'replace', "path": '/settings/active', "value": False }, { "op": 'add', "path": '/tags', "value": ['developer', 'engineer'] }, { "op": 'remove', "path": '/temp' }, ])
This function will run the following query in the database.
UPDATE $thing PATCH $data;
.delete()
Deletes all records in a table, or a specific record, from the database.
Method Syntaxdb.delete(thing,data)
Arguments | Description | ||
---|---|---|---|
thing required | The table name or a |
# Delete all records from a table db.delete('person') # Delete a specific record from a table db.delete(RecordID('person', 'h5wxrf2ewk8xjxosxtyc'))
This function will run the following query in the database.
DELETE $thing;