SurrealDB Docs Logo

Enter a search query

.upsert()

This method can be used to insert records into the database, or to update them if they exist.

Method Syntax
db.upsert<T,U>(thing, data)

Arguments

ArgumentsDescription
thing required

The table name or the specific RecordId to upsert.

data optional

The document / record data to update.

Example usage

type Person = { id: string; name: string; settings: { active: boolean; marketing: boolean; }; }; // Upsert all records in a table const multiple = await db.upsert<Person>( 'person', { name: 'Mary Doe', settings: { active: true, marketing: false, }, } ); // Upsert a record with a specific ID const single = await db.upsert<Person>( new RecordId('person', 'john-doe'), { name: 'John Doe', settings: { active: true, marketing: true, }, } ); // The content you upsert might differ from the return type const partial = await db.upsert< Person, Pick<Person, 'name'> >( new RecordId('person', 'jane-doe'), { name: 'Jane Doe', } );