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 typeJ - Boolean indicating if result is JSON (default: false).relation()Configure the insert to work with relation (edge) records instead of regular records.
Method SyntaxinsertPromise.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()Ignore records that already exist (skip duplicates without error).
Method SyntaxinsertPromise.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
.output()Specify which fields to return in the response.
Method SyntaxinsertPromise.output(fields)
| Parameter | Type | Description |
|---|---|---|
fields required | Output | ”NONE”, “AFTER”, or specific field list. |
InsertPromise<T, J> - Chainable promise
Return Specific Fieldsconst users = await db.insert(userData) .output('id', 'name'); // Returns only id and name
Return Nothingawait db.insert(logData) .output('NONE'); // No return value, useful for performance
.timeout()Set a timeout for the operation.
Method SyntaxinsertPromise.timeout(duration)
| Parameter | Type | Description |
|---|---|---|
duration required | Duration | Maximum time to wait. |
InsertPromise<T, J> - Chainable promise
.version()Insert at a specific version (for versioned storage engines).
Method SyntaxinsertPromise.version(timestamp)
| Parameter | Type | Description |
|---|---|---|
timestamp required | DateTime | The version timestamp. |
InsertPromise<T, J> - Chainable promise
.json()Return result as JSON string.
Method SyntaxinsertPromise.json()
InsertPromise<T, true> - Promise returning JSON string
.stream()Stream results as records are inserted.
Method SyntaxinsertPromise.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'));