CreatePromise<T, I, J>The CreatePromise class provides a chainable interface for configuring CREATE operations before execution. It extends Promise, allowing you to await it directly or chain configuration methods.
Returned by: SurrealQueryable.create()
Source: query/create.ts
T - The result typeI - The input type for record dataJ - Boolean indicating if result is JSON (default: false).content()Set the complete content for the new record.
Method SyntaxcreatePromise.content(data)
| Parameter | Type | Description |
|---|---|---|
data required | Values<I> | The record data (excluding id field). |
CreatePromise<T, I, J> - Chainable promise
Create with Specific IDconst user = await db.create(new RecordId('users', 'john')) .content({ name: 'John Doe', email: 'john@example.com', age: 30 });
Create with Auto-Generated IDconst user = await db.create(new Table('users')) .content({ name: 'Jane Doe', email: 'jane@example.com', age: 28 });
.patch()Apply JSON Patch operations to set record data.
Method SyntaxcreatePromise.patch(operations)
| Parameter | Type | Description |
|---|---|---|
operations required | Values<I> | JSON Patch operations to apply. |
CreatePromise<T, I, J> - Chainable promise
const user = await db.create(new Table('users')) .patch([ { op: 'add', path: '/name', value: 'John' }, { op: 'add', path: '/email', value: 'john@example.com' } ]);
.output()Specify which fields to return in the response.
Method SyntaxcreatePromise.output(fields)
| Parameter | Type | Description |
|---|---|---|
fields required | Output | Output specification: “NONE”, “BEFORE”, “AFTER”, “DIFF”, or field list. |
CreatePromise<T, I, J> - Chainable promise
Return Specific Fieldsconst user = await db.create(new Table('users')) .content(userData) .output('id', 'name'); // Returns only id and name
Return Full Recordconst user = await db.create(new Table('users')) .content(userData) .output('AFTER'); // Returns complete created record
Return Nothingawait db.create(new Table('logs')) .content(logData) .output('NONE'); // Returns undefined, useful for fire-and-forget
.timeout()Set a timeout for the operation.
Method SyntaxcreatePromise.timeout(duration)
| Parameter | Type | Description |
|---|---|---|
duration required | Duration | Maximum time to wait for operation completion. |
CreatePromise<T, I, J> - Chainable promise
const user = await db.create(new Table('users')) .content(userData) .timeout(Duration.parse('5s'));
.version()Create the record at a specific version (for versioned storage engines).
Method SyntaxcreatePromise.version(timestamp)
| Parameter | Type | Description |
|---|---|---|
timestamp required | DateTime | The version timestamp. |
CreatePromise<T, I, J> - Chainable promise
const user = await db.create(new Table('users')) .content(userData) .version(DateTime.now());
.json()Return result as JSON string instead of parsed object.
Method SyntaxcreatePromise.json()
CreatePromise<T, I, true> - Promise returning JSON string
const jsonString = await db.create(new Table('users')) .content(userData) .json();
.stream()Stream the operation result (useful when creating multiple records).
Method SyntaxcreatePromise.stream()
AsyncIterableIterator - Async iterator
const results = db.create(new Table('users')) .content(multipleUsers); for await (const user of results.stream()) { console.log('Created:', user); }
import { Surreal, RecordId, Table } from 'surrealdb'; const db = new Surreal(); await db.connect('ws://localhost:8000'); // Create with specific ID const user = await db.create(new RecordId('users', 'john')) .content({ name: 'John Doe', email: 'john@example.com', role: 'admin' }); // Create with auto-generated ID const post = await db.create(new Table('posts')) .content({ title: 'Hello World', content: 'My first post', author: new RecordId('users', 'john') });
// Only return the ID const { id } = await db.create(new Table('users')) .content(userData) .output('id'); // Return specific fields const summary = await db.create(new Table('users')) .content(userData) .output('id', 'name', 'created_at');
const users = [ { name: 'Alice', email: 'alice@example.com' }, { name: 'Bob', email: 'bob@example.com' }, { name: 'Carol', email: 'carol@example.com' } ]; for await (const user of db.create(new Table('users')).content(users).stream()) { console.log(`Created user: ${user.name} with ID: ${user.id}`); }
const post = await db.create(new Table('posts')) .content({ title: 'New Post', content: 'Post content here', author: new RecordId('users', 'john'), tags: [ new RecordId('tags', 'javascript'), new RecordId('tags', 'tutorial') ], created_at: DateTime.now() });
try { const user = await db.create(new RecordId('users', 'existing')) .content(userData); } catch (error) { if (error instanceof ResponseError) { console.error('User already exists:', error.message); } }
const user = await db.create(new Table('users')) .content(complexUserData) .timeout(Duration.parse('10s'));
All configuration methods return a new CreatePromise, allowing method chaining:
const result = await db.create(new Table('users')) .content(userData) .output('id', 'name', 'email') .timeout(Duration.parse('5s'));