BaasixBaasix
Reference

Utils Routes

← Back to Documentation Home

Table of Contents

  1. Sort Items

Sort Items

Move an item to a specific position in a sortable collection.

  • URL: /utils/sort/:collection
  • Method: POST
  • Auth required: Yes
  • Permissions required: Update permission for the collection's "sort" field

URL Parameters

ParameterTypeRequiredDescription
collectionstringYesName of the collection

Request Body

FieldTypeRequiredDescription
itemstring/numberYesID of the item to move
tostring/numberYesID of the target item (position to move to)
modestringNoPosition mode: 'before' (default) or 'after' target
{
  "item": "550e8400-e29b-41d4-a716-446655440000",
  "to": "550e8400-e29b-41d4-a716-446655440001",
  "mode": "after"
}

Mode Behavior

ModeDescription
before(Default) Places the item before the target. Item takes the target's sort value, target and subsequent items shift down.
afterPlaces the item after the target. Item gets target's sort value + 1, subsequent items shift down.

Success Response

  • Code: 200 OK
  • Content:
{
  "data": {
    "item": "550e8400-e29b-41d4-a716-446655440000",
    "collection": "products"
  }
}

Error Responses

  • Code: 400 Bad Request

    • Content: { "error": { "message": "Missing item ID to sort" } }
    • Content: { "error": { "message": "Missing target ID to sort to" } }
    • Content: { "error": { "message": "Collection 'collection_name' does not have a sort field" } }
  • Code: 401 Unauthorized

    • Content: { "error": { "message": "Authentication required" } }
  • Code: 403 Forbidden

    • Content: { "error": { "message": "You don't have permission to sort items in 'collection_name'" } }
  • Code: 404 Not Found

    • Content: { "error": { "message": "Collection 'collection_name' does not exist" } }
    • Content: { "error": { "message": "Item with ID {id} not found" } }
    • Content: { "error": { "message": "Target item with ID {id} not found" } }
  • Code: 500 Internal Server Error

    • Content: { "error": { "message": "Error message" } }

Notes

  • The collection must have a sort field defined in its schema.
  • To enable sorting functionality for a collection, set sortEnabled: true in the collection's schema.
  • When an item is moved, all items with sort values greater than or equal to the target position are shifted to make room for the moved item.
  • The sort endpoint uses database transactions to ensure consistency.

Example Usage

// Move product with ID 123 BEFORE product with ID 456 (default behavior)
fetch('/utils/sort/products', {
  method: 'POST',
  headers: {
    Authorization: 'Bearer <token>',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    item: 123,
    to: 456,
  }),
})
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((error) => console.error('Error:', error));

// Move product with ID 123 AFTER product with ID 456
fetch('/utils/sort/products', {
  method: 'POST',
  headers: {
    Authorization: 'Bearer <token>',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    item: 123,
    to: 456,
    mode: 'after',
  }),
})
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((error) => console.error('Error:', error));

← Back to Documentation Home

On this page