Github|...

Mutate Data

Mutating data in Spooky is performant and secure. Changes are instantly reflected in your local UI (optimistic updates) and synchronized with the server.

Creating Records

To create a new record, use the db.create method. You need to specify the record ID (or let Spooky generate one) and the data.

TypeScript
import { Uuid } from 'surrealdb';

// Generate a unique ID
const genId = Uuid.v4().toString().replace(/-/g, '');
const threadId = `thread:${genId}`;

// Create the record
await db.create(threadId, {
title: 'My New Thread',
content: 'This is the content of the thread.',
author: 'user:current_user_id', // Record ID as string
active: true,
});
Warning

Ensure you adhere to the PERMISSIONS defined in your schema. If a user does not have permission to create a record, the operation will fail.

Updating Records

Use db.update() to update existing records. This performs a partial update (merge) by default.

TypeScript
// Partial update
await db.update('thread', 'thread:123', {
title: 'Updated Title'
});

// Update with debouncing (useful for real-time typing)
await db.update('thread', 'thread:123', {
content: 'Updated content...'
}, {
debounced: {
  key: 'recordId_x_fields',
  delay: 500
}
});

Deleting Records

To delete a record, call db.delete with the table name and record ID.

TypeScript
await db.delete('thread', 'thread:123');