Skip to main content

Set

Sets a value in the database with an optional expiration.

  • Parameters:
    • key: The key under which to store the value.
    • value: The value to store.
    • expiresAt: Optional expiration date as a Date object or number of milliseconds.
  • Returns:
    • The result of the operation, includes a boolean indicating whether the operation was successful or an error if the operation failed.
// Full example with result type handling
const result = db.set<User>("user:1234", { name: "Ahmad" });
if (result.success) {
console.log("Key set successfully");
} else {
console.log(result.error.message);
}

// Set a value with expiration in milliseconds
db.set("key", "value", 90000);

// Set a value with Date object expiration
db.set("key", "value", new Date("2030-12-31"));