SurrealDB Docs Logo

Enter a search query

.insert()

Inserts one or multiple records in the database.

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

Arguments

ArgumentsDescription
thing required

The table name or RecordId to insert to.

data optional

Either a single document/record or an array of documents/records to insert

Example usage

type Person = { id: string; name: string; settings: { active: boolean; marketing: boolean; }; }; // Insert a single record const [person] = await db.insert<Person>('person', { name: 'Tobie', settings: { active: true, marketing: true, }, }); // Insert multiple records const people = await db.insert<Person>('person', [ { name: 'Tobie', settings: { active: true, marketing: true, }, }, { name: 'Jaime', settings: { active: true, marketing: true, }, }, ]); // The content you are creating the record with might differ from the return type const people = await db.insert< Person, Pick<Person, 'name'> >('person', [ { name: 'Tobie' }, { name: 'Jaime' }, ]);

Translated query

This function will run the following query in the database.

INSERT INTO $thing $data;

On this page

© SurrealDB GitHub Discord Community Cloud Features Releases Install