Database

Initialize the client with your API key

Updates records in the specified WorqDB table based on the provided where conditions. This endpoint allows you to modify existing records in your database by specifying which records to update and what data to change.

PUT https://api.worqhat.com/db/update

What Does This Endpoint Do?

Imagine you have a database table with user information, and you need to change a user's status from "inactive" to "active" or update their name. This endpoint lets you do exactly that!

When to Use Update Data

You'll find this endpoint useful when you need to:

  • Update user profiles: Change names, emails, or other profile information
  • Change status values: Mark orders as "shipped" or tasks as "completed"
  • Update inventory: Adjust product quantities or prices
  • Modify settings: Change configuration values in your application
  • Mark notifications: Set notifications as "read" or "unread"
  • Process workflows: Update records as they move through approval stages

How It Works

  1. You specify the table name where your data is stored
  2. You provide where conditions to identify which records to update
  3. You include the new data you want to set for those records
  4. The API updates the matching records and returns the updated data

Code Examples

Example 1: Multiple Where Conditions

This example shows how to update records that match multiple conditions (both ID and email).

JavaScript

update-records.js

import Worqhat from 'worqhat';// Initialize the client with your API keyconst client = new Worqhat({apiKey: process.env.WORQHAT_API_KEY, // Always use environment variables for API keys});async function updateUserStatus() {try {  // Call the updateRecords method  const response = await client.db.updateRecords({    table: 'users',                // The table to update    where: {                       // Which records to update      id: '123',                   // Find records with id = 123      email: 'user@example.com'    // AND email = user@example.com    },    data: {                        // New values to set      status: 'active',            // Change status to active      name: 'Updated Name'         // Update the name    },    environment: process.env.WORQHAT_ENVIRONMENT || 'production' // Defaults to production  });    // Handle the successful response  console.log(`Updated ${response.count} records`);  console.log('Updated records:', response.data);  return response;} catch (error) {  // Handle any errors  console.error('Error updating records:', error.message);}}// Call the functionupdateUserStatus();

Example 2: Single Where Condition

This example shows how to update all records that match a single condition (status = "inactive").

JavaScript

update-single-condition.js

import Worqhat from 'worqhat';// Initialize the client with your API keyconst client = new Worqhat({apiKey: process.env.WORQHAT_API_KEY,});async function updateInactiveUsers() {try {  // Update all inactive users to active  const response = await client.db.updateRecords({    table: 'users',          // The table to update    where: {                 // Which records to update      status: 'inactive'     // Find ALL records with status = inactive    },    data: {                  // New values to set      status: 'active',      // Change status to active      updatedBy: 'system'    // Add audit information    },    environment: process.env.WORQHAT_ENVIRONMENT || 'production' // Defaults to production  });    // Handle the successful response  console.log(`Updated ${response.count} inactive users to active status`);  return response;} catch (error) {  console.error('Error updating users:', error.message);}}updateInactiveUsers();

Request Body Explained

tablestringbodyrequired

The name of the table where you want to update records. For example, users, products, or orders.

whereobjectbodyrequired

Conditions to identify which records to update. This works like a filter - only records matching ALL conditions will be updated.

For example: {"id": "123", "status": "inactive"} will update only records where both the ID is "123" AND the status is "inactive".

dataobjectbodyrequired

The new values you want to set for the matching records. Each key-value pair represents a field name and its new value.

For example: {"status": "active", "last_login": "2025-08-01T12:00:00Z"} will set the status field to "active" and the last_login field to the specified timestamp.

Response Fields Explained

successbooleanrequired

true if the update was successful, false otherwise.

dataarrayrequired

An array containing the updated records with their new values. This shows you exactly what was changed in the database.

countintegerrequired

The number of records that were updated. If this is 0, it means no records matched your where conditions.

executionTimeintegerrequired

How long the update operation took to complete, measured in milliseconds.

messagestringrequired

A human-readable message describing the result of the operation.

Example Response

response.json

{"success": true,"data": [  {    "documentId": "doc_123456789",    "name": "Updated Name",    "email": "user@example.com",    "status": "active",    "organization_id": "org_id",    "createdAt": "2023-06-15 10:00:00",    "updatedAt": "2023-06-15 10:30:00"  }],"count": 1,"executionTime": 52,"message": "1 record(s) updated successfully in users"}

Common Errors and How to Fix Them

ErrorCauseSolution
"Table not found"The specified table doesn't existCheck your table name for typos
"No records matched the update criteria"Your where conditions didn't match any recordsVerify your where conditions are correct
"Missing required field"You didn't provide a required parameterMake sure you include table, where, and data fields
"Unauthorized"Invalid or missing API keyCheck that you're using a valid API key

Tips for Successful Updates

  • Always use specific where conditions to avoid updating too many records by accident
  • Consider fetching the records first to verify which ones will be updated
  • Remember that updates are permanent - consider backing up important data before updates
  • For large tables, updates might take longer to complete