Skip to main content

Create Records

Three methods for writing flat records. For nested/graph data see Import Data.

Create a Record

db.records.create()

const movie = await db.records.create({
label: "MOVIE",
data: { title: "Inception", rating: 8.8, genre: "sci-fi" },
});
// → DBRecordInstance { __id, __label, title, rating, genre }

Precise type control (PropertyDraft)

await db.records.create({
label: "MOVIE",
data: [
{ name: "title", type: "string", value: "Inception" },
{ name: "rating", type: "number", value: 8.8 },
{
name: "genres",
type: "string",
value: "sci-fi,thriller",
valueSeparator: ",",
},
{ name: "releasedAt", type: "datetime", value: "2010-07-16T00:00:00Z" },
],
});
PropertyDraft fieldTypeDescription
namestringProperty name
typestringstring · number · boolean · datetime · null · vector
valueanyThe value
valueSeparatorstringSplit value string into an array on this separator

Create Multiple Records

db.records.createMany()

Flat rows only — no nested objects. For nested data use importJson.

const result = await db.records.createMany({
label: "ACTOR",
data: [
{ name: "Leonardo DiCaprio", country: "USA" },
{ name: "Ken Watanabe", country: "Japan" },
],
});
// → DBRecordsArrayInstance { data: [...], total: 2 }

Upsert

db.records.upsert()

Create-or-update based on matching criteria.

// Match on 'title'; update rating if found, create if not
const movie = await db.records.upsert({
label: "MOVIE",
data: { title: "Inception", rating: 9.0, genre: "sci-fi" },
options: { mergeBy: ["title"], mergeStrategy: "append" },
});

Merge strategies

StrategyBehaviour
append (default)Add / update incoming fields; preserve all other existing fields
rewriteReplace all fields with incoming data; unmentioned fields are removed

mergeBy behaviour

mergeBy valueMatch behaviour
['field']Match only on listed fields
[] or omittedMatch on ALL incoming property keys

Options

All three methods accept the same options object:

OptionDefaultDescription
suggestTypestrueInfer types automatically
convertNumericValuesToNumbersfalseConvert string numbers to number type
capitalizeLabelsfalseUppercase all inferred label names
relationshipType__RUSHDB__RELATION__DEFAULT__Relationship type used for nested links
returnResultfalseReturn created records in the response
mergeByFields to match on for upsert
mergeStrategyappendappend or rewrite

In a transaction

const tx = await db.tx.begin();
try {
const movie = await db.records.create(
{ label: "MOVIE", data: { title: "Dune" } },
tx,
);
const actor = await db.records.create(
{ label: "ACTOR", data: { name: "Timothée Chalamet" } },
tx,
);
await db.records.attach(
{ source: movie, target: actor, options: { type: "STARS" } },
tx,
);
await tx.commit();
} catch (e) {
await tx.rollback();
throw e;
}

Via Model

const MovieModel = new Model("MOVIE", {
title: { type: "string" },
rating: { type: "number" },
});

const movie = await MovieModel.create({ title: "Inception", rating: 8.8 });
const movies = await MovieModel.createMany([
{ title: "Dune" },
{ title: "Arrival" },
]);

See also