Skip to main content

upsert()

Updates a record if it matches the specified conditions, or creates a new one if it doesn't exist. Returns the resulting record.

Available Keys

KeyDescriptionOptionalNotes
whereSpecifies search conditionsRequired
createData for creation when not foundRequired
updateData for update when foundRequired
selectDisplay settings for return value columnsOptionalCannot be used with include
omitExclusion settings for return value columnsOptionalCannot be used with select
includeRetrieve related recordsOptionalDetails here

Example Sheet

Example Sheet

Description

Suppose you want to perform the following operation on the above example:

  • Set the age of akahoshi to 23
  • Create a new record if it doesn't exist

The code would be:

const gassma = new Gassma.GassmaClient();

// gassma.{{TARGET_SHEET_NAME}}.upsert
const result = gassma.sheet1.upsert({
where: {
name: "akahoshi",
},
update: {
age: 23,
},
create: {
name: "akahoshi",
age: 23,
pref: "Ibaraki",
postNumber: "310-8555",
},
});

When the record exists, the updated record is returned:

{
name: 'akahoshi',
age: 23,
pref: 'Ibaraki',
postNumber: '310-8555'
}

When the record doesn't exist, it's created with the create data and the created record is returned:

const result = gassma.sheet1.upsert({
where: { name: "newuser" },
update: { age: 30 },
create: {
name: "newuser",
age: 30,
pref: "Tokyo",
postNumber: "100-0001",
},
});
// => { name: "newuser", age: 30, pref: "Tokyo", postNumber: "100-0001" }

Nested Write

When relation definitions exist, Nested Write can be used within create / update:

The where specification follows findMany().