Hanzo Base

Collections

Collections are the core data model in Base. They define your schema and generate REST APIs automatically.

Collections are typed data stores that auto-generate REST APIs. Think of them as database tables with built-in CRUD endpoints.

Collection Types

TypeDescriptionUse Case
BaseStandard data collectionPosts, tasks, products
AuthUser collection with auth methodsUsers, admins, teams
ViewRead-only computed collectionDashboards, aggregates

Creating Collections

Via Admin UI

Navigate to /_/ and click New Collection. Define fields visually with the schema editor.

Via Migration

// hz_migrations/1709000001_create_posts.js
migrate((app) => {
  const collection = new Collection({
    name: 'posts',
    type: 'base',
    fields: [
      { name: 'title', type: 'text', required: true, max: 200 },
      { name: 'body', type: 'editor' },
      { name: 'slug', type: 'text', unique: true },
      { name: 'status', type: 'select', values: ['draft', 'published'] },
      { name: 'author', type: 'relation', collection: 'users', required: true },
      { name: 'tags', type: 'json' },
      { name: 'cover', type: 'file', maxSize: 5242880, mimeTypes: ['image/*'] },
    ],
    indexes: [
      'CREATE UNIQUE INDEX idx_posts_slug ON posts (slug)',
    ],
  })
  return app.save(collection)
})

Field Types

TypeGo TypeDescription
textstringPlain text with optional min/max/pattern
editorstringRich text (HTML)
numberfloat64Integer or decimal
boolboolTrue/false
emailstringValidated email
urlstringValidated URL
datestringISO 8601 datetime
selectstringSingle or multi-select from defined values
jsonanyArbitrary JSON
filestringFile upload (single or multiple)
relationstringForeign key to another collection
autodatestringAuto-set on create/update

API Rules

Control access per operation with filter expressions:

// Only authenticated users can create
@request.auth.id != ""

// Only the author can update
@request.auth.id = author

// Public read access
(leave empty)

// Admin only
@request.auth.role = "admin"

// Complex filter
@request.auth.id != "" && status = "published"

Querying

REST API

# List records
GET /api/collections/posts/records?page=1&perPage=20&sort=-created

# Filter
GET /api/collections/posts/records?filter=(status='published')

# Expand relations
GET /api/collections/posts/records?expand=author

# Get single record
GET /api/collections/posts/records/RECORD_ID

JavaScript SDK

// List with filters
const posts = await base.collection('posts').getList(1, 20, {
  filter: 'status = "published"',
  sort: '-created',
  expand: 'author',
})

// Get one
const post = await base.collection('posts').getOne(id, {
  expand: 'author',
})

// Full list (auto-paginated)
const allPosts = await base.collection('posts').getFullList({
  sort: 'title',
})

Last updated on

On this page