InsertPromise<T, J>

The InsertPromise class provides a chainable interface for configuring INSERT operations for bulk record insertion. It extends Promise, allowing you to await it directly or chain configuration methods.

Returned by: SurrealQueryable.insert()

Source: query/insert.ts

  • T - The result type

  • J - Boolean indicating if result is JSON (default: false)

Configure the insert to work with relation (edge) records instead of regular records.

Method Syntax

insertPromise.relation()

InsertPromise<T, J> - Chainable promise

const edges = await db.insert([
{
id: new RecordId('likes', '1'),
in: new RecordId('users', 'john'),
out: new RecordId('posts', '1')
}
]).relation();

Ignore records that already exist (skip duplicates without error).

Method Syntax

insertPromise.ignore()

InsertPromise<T, J> - Chainable promise

const users = await db.insert([
{ id: new RecordId('users', 'john'), name: 'John' },
{ id: new RecordId('users', 'jane'), name: 'Jane' }
]).ignore();
// If 'john' exists, it's skipped; 'jane' is inserted

Specify which fields to return in the response.

Method Syntax

insertPromise.output(fields)
ParameterTypeDescription
fields Output"NONE", "AFTER", or specific field list.

InsertPromise<T, J> - Chainable promise

Return Specific Fields

const users = await db.insert(userData)
.output('id', 'name');
// Returns only id and name

Return Nothing

await db.insert(logData)
.output('NONE');
// No return value, useful for performance

Set a timeout for the operation.

Method Syntax

insertPromise.timeout(duration)
ParameterTypeDescription
duration DurationMaximum time to wait.

InsertPromise<T, J> - Chainable promise

Insert at a specific version (for versioned storage engines).

Method Syntax

insertPromise.version(timestamp)
ParameterTypeDescription
timestamp DateTimeThe version timestamp.

InsertPromise<T, J> - Chainable promise

Return result as JSON string.

Method Syntax

insertPromise.json()

InsertPromise<T, true> - Promise returning JSON string

Stream results as records are inserted.

Method Syntax

insertPromise.stream()

AsyncIterableIterator - Async iterator

import { Surreal, RecordId } from 'surrealdb';

const db = new Surreal();
await db.connect('ws://localhost:8000');

// Insert single record
const user = await db.insert({
id: new RecordId('users', 'alice'),
name: 'Alice',
email: 'alice@example.com'
});

// Insert multiple records
const users = await db.insert([
{ id: new RecordId('users', 'bob'), name: 'Bob' },
{ id: new RecordId('users', 'carol'), name: 'Carol' }
]);
// Let database generate IDs
const users = await db.insert(new Table('users'), [
{ name: 'Dave', email: 'dave@example.com' },
{ name: 'Eve', email: 'eve@example.com' }
]);
// Skip existing records without error
const users = await db.insert([
{ id: new RecordId('users', 'john'), name: 'John' },
{ id: new RecordId('users', 'jane'), name: 'Jane' }
]).ignore();

console.log(`Inserted ${users.length} new users`);
const largeDataset = generateThousandsOfRecords();

let count = 0;
for await (const record of db.insert(largeDataset).stream()) {
count++;
if (count % 100 === 0) {
console.log(`Inserted ${count} records`);
}
}
const likes = await db.insert([
{
id: new RecordId('likes', '1'),
in: new RecordId('users', 'john'),
out: new RecordId('posts', '1'),
created_at: DateTime.now()
},
{
id: new RecordId('likes', '2'),
in: new RecordId('users', 'jane'),
out: new RecordId('posts', '1'),
created_at: DateTime.now()
}
]).relation();
// Don't wait for return values
await db.insert(logEntries)
.output('NONE');
// Faster execution when you don't need the results
const users = await db.insert(largeDataset)
.timeout(Duration.parse('30s'));
try {
const users = await db.insert([
{ id: new RecordId('users', 'existing'), name: 'Test' }
]);
} catch (error) {
if (error instanceof ResponseError) {
console.error('Duplicate key error:', error.message);

// Retry with ignore
const users = await db.insert([
{ id: new RecordId('users', 'existing'), name: 'Test' }
]).ignore();
}
}
const BATCH_SIZE = 100;
const allUsers = [...]; // Large array

for (let i = 0; i < allUsers.length; i += BATCH_SIZE) {
const batch = allUsers.slice(i, i + BATCH_SIZE);
await db.insert(batch);
console.log(`Inserted batch ${i / BATCH_SIZE + 1}`);
}
// CREATE: For single records, with more configuration options
const user = await db.create(new RecordId('users', 'john'))
.content(userData);

// INSERT: For bulk operations, optimized for performance
const users = await db.insert([
{ id: new RecordId('users', 'alice'), ...data1 },
{ id: new RecordId('users', 'bob'), ...data2 },
{ id: new RecordId('users', 'carol'), ...data3 }
]);
const result = await db.insert(records)
.ignore()
.output('id', 'name')
.timeout(Duration.parse('10s'));

Was this page helpful?