Litestone guide / Advanced
Extensible columns
@@extensible lets a customer of your app declare their own fields at runtime, without a migration. A Json column holds the values, and a model of declarations says what a form offers.
enum CustomFieldType { text number }
model CustomField {
id Int
model String (1, 40)
key String (1, 40)
type CustomFieldType
([model, key])
}
model Product {
id Int
name String
fields Json ("{}")
(fields, declaredBy: CustomField)
}
await db.customField.create({ data: { model: 'Product', key: 'color', type: 'text' } })
await db.customField.create({ data: { model: 'Other', key: 'size', type: 'text' } })
await db.product.create({ data: { name: 'Mug', fields: { color: 'red' } } })
const declared = await db.product.$declaredFields()
if (declared.length !== 1 || declared[0].key !== 'color') throw new Error('expected only the Product declaration')
The app writes no hook and no allocator, and the declaring model takes derived CRUD like any other.
What else it can do
- An undeclared key is still accepted: declaring says what a form offers, not what may be written
max: { text: 8, number: 4 }buys a pool of indexed slots, so a declared key can be filtered without a scan- The slot is allocated at the Data boundary, first free of the matching kind
- A key declared once the pool is full still stores and displays, and only a query naming it scans
$declaredFields()is scoped to the model and read under the caller's own access- Under
strategy rowthe declarations and the pool are per tenant scope: databaseIdkeys declarations per parent row, for a table that owns its own columns- The column cannot also carry
@type; a closed, typed column is a second column
In depth: Extensible columns § Declaring is not a whitelist, § Reading the declarations, § Making a declared key filterable, § Per parent row