Skip to content
🚀 This documentation is for unreal-orm 1.0.0 alpha which requires SurrealDB 2.0 SDK. For use with version 1.x, see here.

Creating and Inserting Records

UnrealORM exposes two static methods for adding data: create and insert.

create maps to SurrealDB CREATE ... RETURN AFTER and returns a hydrated model instance.

const user = await User.create(db, {
name: 'Alice',
email: 'alice@example.com',
});
console.log(user.id); // RecordId<'user'>

You can pass a custom id:

await User.create(db, {
id: new RecordId('user', 'alice'),
name: 'Alice',
});

insert maps to INSERT INTO and is useful for bulk loading or when you need IGNORE or ON DUPLICATE KEY UPDATE behavior.

const users = await User.insert(db, {
data: [
{ name: 'Alice', email: 'alice@example.com' },
{ name: 'Bob', email: 'bob@example.com' },
],
});
await User.insert(db, {
data: { id: new RecordId('user', 'alice'), name: 'Alice' },
ignore: true,
});
await User.insert(db, {
data: { id: new RecordId('user', 'alice'), name: 'Alice' },
onDuplicate: { updatedAt: new Date() },
});
// Or raw SurrealQL
await User.insert(db, {
data: { id: new RecordId('user', 'alice'), name: 'Alice' },
onDuplicate: surql`visits += 1, lastSeen = time::now()`,
});

For relation tables insert can emit INSERT RELATION:

await Follow.insert(db, {
data: { id: new RecordId('follow', 'a-b'), in: userA.id, out: userB.id },
});

Both methods support a return option:

const names = await User.insert(db, {
data: { name: 'Alice' },
return: { value: 'name' },
});