dehaze

Update

An update request consists of two parts - a where clause and update operators for new values. The where clause has the same filtering options as queries.

Note: At least any one of the update operations is required to update the rows.

Update operationslink

You can perform different types of update operations like set, inc, push, etc. on your data. Following are the different types of update operations:

Set operationlink

set operator is used to set a field’s value with the given value.

Example: Update the name of your pokemon:

mutation {
  update_caught_pokemons(
    where: {id: {_eq: 1}},
    set: {name: "My Cool Pikachu"}
  ) @postgres {
    status
  }
}
const whereClause = cond("id", "==", 1)

const { status } = await db.update("caught_pokemons")
  .where(whereClause)
  .set({name: "My Cool Pikachu"})
  .apply()

Increment operationlink

inc operator is used to increment/decrement a field’s value by the provided value.

Example: Increment the combat power of your pokemon by 50:

mutation {
  update_caught_pokemons(
    where: {id: 1},
    inc: {combat_power: 50}
  ) @postgres {
    status
  }
}
const whereClause = cond("id", "==", 1)

const { status } = await db.update("caught_pokemons")
  .where(whereClause)
  .inc({combat_power: 50})
  .apply()

Example: Decrement the hit points of your pokemon by 10:

mutation {
  update_caught_pokemons(
    where: {id: 1},
    inc: {hit_points: -10}
  ) @postgres {
    status
  }
}
const whereClause = cond("id", "==", 1)

const { status } = await db.update("caught_pokemons")
  .where(whereClause)
  .inc({hit_points: -10})
  .apply()

Multiply operationlink

mul operator multiplies a field’s value by the given value.

Note: The multiplier can be float as well to achieve division.

Example: Your pokemon has evolved, and you want to multiply its combat power by 2:

mutation {
  update_caught_pokemons(
    where: {id: 1},
    mul: {combat_power: 2}
  ) @postgres {
    status
  }
}
const whereClause = cond("id", "==", 1)

const { status } = await db.update("caught_pokemons")
  .where(whereClause)
  .mul({combat_power: 2})
  .apply()

Min operationlink

min operator updates a field’s value with the least (minimum) value amongst the specified value and the current value.

Example: Update the trainer’s lowest score after a battle:

mutation {
  update_trainers(
    where: {id: 1},
    min: {lowest_sore: 50}
  ) @postgres {
    status
  }
}
const whereClause = cond("id", "==", 1)

const { status } = await db.update("trainers")
  .where(whereClause)
  .min({lowest_score: 50})
  .apply()

Note: In the above example, the lowest_score is updated, only if it’s current value is greater than 50.

Max operationlink

max operator updates a field’s value with the maximum value amongst the specified value and the current value.

Example: Update the trainer’s highest score after a battle:

mutation {
  update_trainers(
    where: {id: 1},
    max: {highest_score: 200}
  ) @postgres {
    status
  }
}
const whereClause = cond("id", "==", 1)

const { status } = await db.update("trainers")
  .where(whereClause)
  .max({highest_score: 200})
  .apply()

Note: In the above example the highest_score is updated, only if it’s current value is lesser than 200.

Current date operationlink

currentDate operator updates a field’s value with the current date/timestamp value.

Example: Update last_battled field of a pokemon to current date and last_modified field with the current timestamp:

mutation {
  update_caught_pokemons(
    where: {id: 1},
    currentDate: {
      last_battled: true,
      last_modified: { $type: "timestamp" }
    }
  ) @postgres {
    status
  }
}
const whereClause = cond("id", "==", 1)

const { status } = await db.update("caught_pokemons")
  .where(whereClause)
  .currentDate("last_battled")
  .currentTimestamp("last_modified")
  .apply()

Push operationlink

Note: This is only available for MongoDB.

push operator can be used to push a given value to an array in a document.

Example: Push thunderbolt to your pokemon’s list of attacks:

mutation {
  update_caught_pokemons(
    where: {_id: "1"},
    push: {attacks: "thunderbolt"}
  ) @mongo {
    status
  }
}
const whereClause = cond("id", "==", 1)

const { status } = await db.update("caught_pokemons")
  .where(whereClause)
  .push({attacks: "thunderbolt"})
  .apply()

Unset operationlink

Note: This is only available for MongoDB.

unset operator deletes the value of a particular field.

Example: Delete is_favourite field from your pokemons:

mutation {
  update_caught_pokemons(
    where: {_id: "1"},
    unset: {is_favourite: ""}
  ) @mongo {
    status
  }
}
const whereClause = cond("_id", "==", "1")

const { status } = await db.update("caught_pokemons")
  .where(whereClause)
  .remove("caught_pokemons")
  .apply()

Note: The specified value in the unset object (i.e. “") does not impact the operation.

Rename operationlink

Note: This is only available for MongoDB.

rename operator rename the field’s name.

Example: Rename is_favourite field to favourite:

mutation {
  update_caught_pokemons(
    where: {_id: "1"},
    rename: {is_favourite: "favourite"}
  ) @mongo {
    status
  }
}
const whereClause = cond("_id", "==", "1")

const { status } = await db.update("caught_pokemons")
  .where(whereClause)
  .rename({is_favourite: "favourite"})
  .apply()

Have a technical question?

Improve the docs!