Skip to content
VibORM
Esc
navigateopen⌘Jpreview
On this page

createMany

Create multiple records in a single operation

Create multiple records in one call:

const result = await client.user.createMany({
  data: [
    { email: "alice@example.com", name: "Alice" },
    { email: "bob@example.com", name: "Bob" },
    { email: "carol@example.com", name: "Carol" },
  ],
});
// Result: { count: 3 }

createMany returns a { count }. It doesn’t accept nested relations — for that, use create or nested writes.

Skip duplicates

const result = await client.user.createMany({
  data: [
    { email: "alice@example.com", name: "Alice" },
    { email: "alice@example.com", name: "Alice Duplicate" }, // Skipped
  ],
  skipDuplicates: true,
});
// Result: { count: 1 }

skipDuplicates skips unique/primary-key conflicts only. Foreign-key, NOT NULL, validation, conversion, and other integrity failures still reject the whole operation on every supported database.

A row containing only database-owned defaults cannot use skipDuplicates. VibORM rejects that shape before writing because no supported database shares a duplicate-only DEFAULT VALUES primitive.

Getting the rows back

Add a select and createMany returns the created rows instead of a count. The return type follows: with select it is an array of the projected rows, without it, { count }.

const users = await client.user.createMany({
  data: [
    { email: "alice@example.com", name: "Alice" },
    { email: "bob@example.com", name: "Bob" },
  ],
  select: { id: true, email: true },
});
// Type: { id: string; email: string }[]

select and skipDuplicates can only be combined on a database with RETURNING (PostgreSQL, SQLite, LibSQL, D1). On MySQL the combination is refused with a typed error rather than guessed at, because a skipped insert cannot be told apart from a fresh one — drop select to get { count }, or drop skipDuplicates.

Options

await client.user.createMany({
  data: [{ ... }],      // Required: array of records
  skipDuplicates: true, // Optional: skip duplicate-key conflicts
  select: { ... },      // Optional (scalar fields only): return the created rows
                        // instead of { count }
});

Was this page helpful?