Skip to content

REST Collections

The @chimpbase/rest-collections plugin exposes Chimpbase collections as REST APIs without writing route handlers.

Installation

bash
bun add @chimpbase/rest-collections

Quick Start

ts
import { restCollections } from "@chimpbase/rest-collections";

const rest = restCollections({
  basePath: "/api",
  collections: {
    notes: {
      collection: "todo_notes",
      writableFields: ["body", "todoId"],
      filterableFields: { todoId: "number" },
    },
  },
});

This creates:

MethodPathDescription
GET/api/notesList notes
GET/api/notes/:idGet a note
POST/api/notesCreate a note
PATCH/api/notes/:idUpdate a note
DELETE/api/notes/:idDelete a note

Configuration

ts
restCollections({
  // URL prefix for all collection routes
  basePath: "/api",

  // Default pagination limit
  defaultLimit: 50,

  // Maximum pagination limit
  maxLimit: 100,

  collections: {
    collectionKey: {
      // Actual collection name in storage (defaults to key)
      collection: "my_collection",

      // URL path segment (defaults to key)
      path: "/my-items",

      // Which HTTP methods to expose
      methods: ["list", "get", "create", "update", "delete"],

      // Fields that can be written via POST/PATCH
      writableFields: ["title", "body", "status"],

      // Fields that can be used as query filters
      filterableFields: ["status", "priority"],
      // or with type parsing:
      filterableFields: {
        status: "string",
        priority: "string",
        todoId: "number",
        active: "boolean",
      },

      // Per-collection pagination
      defaultLimit: 20,
      maxLimit: 50,

      // Transform documents on read
      onRead: (context) => {
        return { ...context.document, displayName: context.document.name.toUpperCase() };
      },

      // Transform documents on write
      onWrite: (context) => {
        return { ...context.input, updatedAt: new Date().toISOString() };
      },
    },
  },
});

Querying

Filtering

GET /api/notes?todoId=42&status=active

Only fields listed in filterableFields can be used as filters.

Pagination

GET /api/notes?limit=20

Schema Versioning

Collections support schema versioning for data migrations:

ts
restCollections({
  collections: {
    notes: {
      schemaVersion: 2,
      onRead: (context) => {
        if (context.schemaVersion === 1) {
          // migrate v1 document shape on read
          return { ...context.document, body: context.document.content };
        }
        return context.document;
      },
    },
  },
});

Registration

ts
export default {
  registrations: [
    restCollections({ basePath: "/api", collections: { /* ... */ } }),
  ],
};