Syntax

table.insert(document) → result
table.insert([document1, document2, ...]) → result

Arguments

List of arguments to provide.
document
object | array<object>
required
A single document object or an array of document objects to insert into the table. Each document should be a valid JSON object.

Optargs

No optional arguments.

Returns

result
object
Information about the operation.

Behavior

  • If no primary key (id) is provided, RuloDB automatically generates a unique ID.
  • When inserting multiple documents, the operation is performed in a single batch for efficiency.
  • On primary key conflict, the conflicting document will be replaced with the new one.

Notes & Caveats

  • Documents must be valid JSON objects (no functions, undefined values, etc.).
  • Auto-generated primary keys are ULID by default.
  • Primary key fields cannot be null or undefined.
  • Primary key must be a string.

Example

Insert a single document

Insert a new user into the users table.
const result = await r
  .table('users')
  .insert({
    name: 'Alice Johnson',
    email: 'alice@example.com',
    age: 28,
    createdAt: new Date().toISOString()
  })
  .run(client);

console.log(`Inserted ${result.inserted} user(s)`);

Insert multiple documents

Insert several users in a single batch operation.
const result = await r
  .table('users')
  .insert([
    { name: 'Bob Smith', email: 'bob@example.com', age: 32 },
    { name: 'Carol Davis', email: 'carol@example.com', age: 27 },
    { name: 'David Wilson', email: 'david@example.com', age: 35 }
  ])
  .run(client);

console.log(`Inserted ${result.inserted} user(s)`);
  • get - Get a document by primary key
  • getAll - Get multiple documents
  • update - Update existing documents
  • delete - Delete documents
  • table - Reference a table

Found a typo? Or maybe a broken link? RuloDB is open-source, help us fix it!