Creating and Inserting Records
UnrealORM exposes two static methods for adding data: create and insert.
create
Section titled “create”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
Section titled “insert”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' }, ],});Ignore duplicates
Section titled “Ignore duplicates”await User.insert(db, { data: { id: new RecordId('user', 'alice'), name: 'Alice' }, ignore: true,});On duplicate
Section titled “On duplicate”await User.insert(db, { data: { id: new RecordId('user', 'alice'), name: 'Alice' }, onDuplicate: { updatedAt: new Date() },});
// Or raw SurrealQLawait User.insert(db, { data: { id: new RecordId('user', 'alice'), name: 'Alice' }, onDuplicate: surql`visits += 1, lastSeen = time::now()`,});Relation inserts
Section titled “Relation inserts”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 },});Return clause
Section titled “Return clause”Both methods support a return option:
const names = await User.insert(db, { data: { name: 'Alice' }, return: { value: 'name' },});