Skip to main content

/images/icons/xs_temp.svg Query All Records

Simple Query

Get all records from a table
   db.query table as $variable
ParameterPurposeExample
tableThe name of the table to queryuser
asThe variable to store the resultallUsers

Custom Queries

    db.query user {
      where = $db.user.id == 1
    } as $user1
ParameterPurposeExample
whereThe query condition to run
$db.user.id == 1

Joins

    db.query user {
      where = $db.user.id == 1
      join = {
        event_log: {
          table: "event_log"
          where: $db.user.id == $db.event_log.user_id
        }
      }
    } as $user1
ParameterPurposeExample
joinObject containing join definitionsEach join is defined as a key-value pair with table name and join config
tableThe name of the table being joined”event_log”
whereThe conditional query to perform when matching records in the join$db.user.id == $db.event_log.user_id

Sorting

    db.query user {
      where = $db.user.id == 1
      sort = {user.name: "asc"}
    } as $user1
ParameterPurposeExample
sortObject defining sort order for the query results{user.name: "asc"}

Evals

    db.query user {
      where = $db.user.id == 1
      eval = {user_action: $db.event_log.action}
    } as $user1
ParameterPurposeExample
evalObject defining computed fields to add to the response{user_action: $db.event_log.action}

Customizing the Response

    db.query user {
      where = $db.user.id == 1 
      output = ["id", "name", "email"]
    } as $user1
ParameterPurposeExample
outputArray of fields returned to display in the response[“id”, “name”, “email”]

Return Options

Return Type

Returns are defined using the return parameter with type and configuration options.
    db.query user {
      where = $db.user.id == 1
      return = {type: "list", paging: {page: 1, per_page: 25}}
    } as $user1
Return Types:
  • exists - Returns true/false if records exist
    • return = {type: "exists"}
      
  • count - Returns the number of records found
    • return = {type: "count"}
      
  • single - Returns the first record found
    • return = {type: "single"}
      
  • list - Returns an array of records
    • return = {type: "list"}
      
  • stream - Returns records for efficient iteration
    • return = {type: "stream"}
      
Paging Paging is configured within the return object:
return = {
  type: "list", 
  paging: {
    page: 1,
    per_page: 25,
    totals: true,
    offset: 0,
    metadata: true
  }
}
Comprehensive Example
db.query user {
  join = {
    event_log: {
      table: "event_log"
      where: $db.user.id == $db.event_log.user_id
    }
  }
  
  where = $db.user.name == $input.name && $db.user.created_at > 1 || $db.user.id == 1 && ($db.user.role == "member" && true) || ($db.user.role == "admin" && true)
  sort = {user.name: "asc"}
  eval = {user_action: $db.event_log.action}
  return = {type: "list", paging: {page: 1, per_page: 25}}
  output = [
    "itemsReceived",
    "curPage", 
    "nextPage",
    "prevPage",
    "offset",
    "itemsTotal",
    "pageTotal",
    "items.id",
    "items.created_at",
    "items.name",
    "items.email",
    "items.account_id",
    "items.role",
    "items.last_login_at",
    "items.password_reset.expiration",
    "items.password_reset.used"
  ]
} as $user1

Return any user records with an ID that matches the input userId

db.query user {
  where = $db.user.id == $input.userId
  return = {type: "exists"}
} as user1

Return any user records with an ID that matches the input userId that are also admins

db.query user {
  where = $db.user.id == $input.userId && $db.user.userRole == "admin"
  return = {type: "exists"}
} as user1

/images/icons/xs_temp.svg Get Record

Simple query

db.get test_data {
      field_name = "id"
      field_value = $input.id
    } as $foundRecord
ParameterDescriptionExample
field_nameUses the specified column to search againstid
field_valueUses the value to search the specified column to retrieve a found match1
asThe variable name that holds the record foundfoundRecord

Customizing the Response

db.get color_pair {
      field_name = "id"
      field_value = $input.id
      output = ["id", "created_at", "user"]
} as $color_pair1
ParameterPurposeExample
outputThe list of fields returned to display in the response.[“id”, “created_at”, “user”]

Get a record by ID

db.get test_data {
  field_name = "id"
  field_value = $input.id
} as foundRecord

/images/icons/xs_temp.svg Has Record

    db.has user {
      field_name = "id"
      field_value = $input.id
    } as $user1
ParameterPurposeExample
field_nameThe field to search for the recordid
field_valueThe value to match in the search field$input.id

Example

    security.create_rsa_key {
      bits = 1024
      format = "object"
    } as crypto1

/images/icons/xs_temp.svg Add Record

db.add user {
    data = {
        created_at: "now",
        name: $input.name,
        email: $input.email,
        password: $input.password
    }
} as $recordAdd
ParameterPurposeExample
dataObject containing the fields and values for the new record{name: "John", email: "john@example.com"}

Create a new user record

db.add user {
  data = {
    created_at: "now",
    name: "John Doe",
    email: "john@example.com",
    password: "hashedPassword123"
  }
} as newUser

/images/icons/xs_temp.svg Edit Record

Edits the contents of an existing record in the database by specifying the target record and the data to update.
db.edit user {
    field_name = "id"
    field_value = 1
    data = {
        name: $input.name,
        list: $listVar
    }
} as $user2
ParameterDescriptionExample
field_nameThe database field name to search withinid
field_valueThe value to match in the specified field1
dataObject containing the fields and values to update{name: 'John', list: ['item1']}

Update a user's name and list

db.edit user {
  field_name = "id"
  field_value = 1
  data = {
    name: $input.name,
    list: $listVar
  }
} as user2

Update specific fields in a product record

db.edit products {
  field_name = "sku"
  field_value = "PROD-123"
  data = {
    price: 29.99,
    stock: 100
  }
} as updatedProduct
💡 The function will only update the fields specified in the data object. Other fields in the record will remain unchanged.
Notes
  • You must specify which record to update using field_name and field_value
  • The data object should only include the fields you want to update
  • You can reference input variables ($input) or other variables in your data object
  • The updated record will be returned in the variable specified after as

/images/icons/xs_temp.svg Add or Edit Record

db.add_or_edit user {
    field_name = "id"
    field_value = $input.id
    data = {name: $input.name}
} as $recordAddOrEdit
ParameterDescriptionExample
field_nameField to search for the recordid
field_valueValue to match in the search field1
dataObject containing fields to update/create{name: 'John'}

Update or create user profile

db.add_or_edit user_profile {
  field_name = "user_id"
  field_value = 456
  data = {
    bio: "Software Developer",
    location: "San Francisco"
  }
} as userProfile

/images/icons/xs_temp.svg Delete Record

db.del user {
    field_name = "id"
    field_value = $input.id
}
ParameterPurposeExample
field_nameField to search for the record”id”
field_valueValue to match in the search field1

Delete inactive user

db.del user {
  field_name = "email"
  field_value = "old@example.com"
} as deletedUser

/images/icons/xs_temp.svg Patch Record

db.patch user {
    field_name = "id"
    field_value = $input.id
    data = {}|set:"name":$input.name
} as $patchRecord
ParameterDescriptionExample
field_nameField to search for the recordid
field_valueValue to match in the search field1
dataObject with fields to patch{name: 'John'}

Update only the last_login field

db.patch user {
  field_name = "id"
  field_value = 789
  data = {}|set:"last_login":"now"
} as updatedLoginTime

/images/icons/xs_temp.svg Bulk Operations

Bulk Add Users

db.bulk.add user {
  allow_id_field = false
  items = [
    {
      name: "John Doe",
      email: "john@example.com"
    },
    {
      name: "Jane Smith",
      email: "jane@example.com"
    }
  ]
} as newUsers
db.bulk.update user {
    items = $input.arrayData
} as $updateBulk
ParameterDescriptionExample
itemsArray of records to update[{id: 1, name: 'John'}, {id: 2, name: 'Jane'}]

Bulk Update Users

db.bulk.update user {
  items = [
    {id: 1, status: "active"},
    {id: 2, status: "inactive"},
    {id: 3, status: "pending"}
  ]
} as statusUpdates
db.bulk.patch user {
    items = $input.arrayData
} as $patchBulk
ParameterDescriptionExample
itemsArray of records to patch[{id: 1, name: 'John'}, {id: 2, name: 'Jane'}]

Bulk Patch Users

db.bulk.patch user {
  items = [
    {id: 1, data: {}|set:"role":"admin"},
    {id: 2, data: {}|set:"role":"moderator"}
  ]
} as roleUpdates
ParameterDescriptionExample
searchQuery condition for deletion$db.user.id >= 100

Bulk Delete Users

db.bulk.delete user {
  search = `$db.user.status == "inactive" && $db.user.last_login < "2023-01-01"`
} as inactiveUsersDeletion
💡 Bulk operations are more efficient than performing multiple individual operations when working with multiple records.
Notes
  • All operations return the affected record(s) in the variable specified after as
  • Bulk operations can significantly improve performance when working with multiple records
  • The field_name and field_value combination is used to identify specific records
  • Patch operations are useful when you want to update specific fields without affecting others
  • The |set: operator in patch operations allows you to update only specified fields, leaving others unchanged. For example, data = {}|set:"name":$input.name will only update the name field in the record.
  • Always use valid table and field names as defined in your Xano database.
  • You can reference input variables ($input) or other variables in your data object for dynamic updates.
  • For best performance, prefer bulk operations when working with many records instead of looping single operations.
I