InsertQueryOptions
Defined in: define/table/types/query.ts:309
Defines the options available for an INSERT query.
Example
Section titled “Example”// Single insertconst user = await User.insert({ data: { name: 'John', email: 'john@example.com' },});
// Bulk insertconst users = await User.insert({ data: [ { name: 'John', email: 'john@example.com' }, { name: 'Jane', email: 'jane@example.com' }, ],});
// With IGNORE (skip duplicates silently)const users = await User.insert({ data: { name: 'John', email: 'john@example.com' }, ignore: true,});
// With ON DUPLICATE KEY UPDATEconst users = await User.insert({ data: { name: 'John', email: 'john@example.com' }, onDuplicate: surql`visits += 1`,});
// With custom RETURN clauseconst names = await User.insert({ data: { name: 'John', email: 'john@example.com' }, return: { value: 'name' },});Type Parameters
Section titled “Type Parameters”TTable
Section titled “TTable”TTable
The data shape of the table being inserted into.
TData = TTable | TTable[]
The type of data to insert (single object or array).
Properties
Section titled “Properties”data:
TDataextendsunknown[] ?TData<TData>[number] &object[] :TData&object
Defined in: define/table/types/query.ts:329
The data to insert. Can be a single object or an array of objects.
Optionally includes an id field for specifying custom record IDs.
Example
Section titled “Example”// Single record{ data: { name: 'John', email: 'john@example.com' } }
// With custom ID{ data: { id: 'user:john', name: 'John', email: 'john@example.com' } }
// Multiple records{ data: [ { name: 'John', email: 'john@example.com' }, { name: 'Jane', email: 'jane@example.com' },]}ignore?
Section titled “ignore?”
optionalignore:boolean
Defined in: define/table/types/query.ts:352
If true, silently ignores duplicate record IDs instead of throwing an error.
Equivalent to INSERT IGNORE INTO.
Example
Section titled “Example”// Will not throw error if user:123 already existsawait User.insert({ data: { id: 'user:123', name: 'John' }, ignore: true,});onDuplicate?
Section titled “onDuplicate?”
optionalonDuplicate:BoundQuery<unknown[]> |Expr|Partial<TTable>
Defined in: define/table/types/query.ts:400
Specifies how to update existing records when a duplicate ID or unique index violation occurs. Can be:
- Object:
{ field: value }pairs to update - BoundQuery: Raw SurrealQL for complex updates
Use $input.field to reference the attempted insert data.
Example
Section titled “Example”// Simple field updatesawait User.insert({ data: { name: 'John', email: 'john@example.com' }, onDuplicate: { updatedAt: new Date() },});
// Increment/decrement with raw queryawait User.insert({ data: { name: 'John', email: 'john@example.com' }, onDuplicate: surql`visits += 1, lastSeen = time::now()`,});
// Reference input dataawait User.insert({ data: { name: 'John', email: 'john@example.com' }, onDuplicate: surql`name = $input.name, updatedAt = time::now()`,});relation?
Section titled “relation?”
optionalrelation:boolean
Defined in: define/table/types/query.ts:337
If true, uses INSERT RELATION syntax for relation tables. Automatically set for relation tables defined with Table.relation().
return?
Section titled “return?”
optionalreturn:ReturnType
Defined in: define/table/types/query.ts:369
Specifies what to return from the INSERT operation.
Example
Section titled “Example”// Return nothingawait User.insert({ data, return: 'NONE' });
// Return specific fieldsawait User.insert({ data, return: ['id', 'name'] });
// Return single field valueconst names = await User.insert({ data, return: { value: 'name' } });