Schema & property types
Define a bucket's properties, validation, defaults, relations and translations, and every bucket-level setting.
A bucket's schema is a JSON Schema document with a few Spica-specific keywords. You can edit it in the panel, over the API (POST /bucket, PUT /bucket/:id, PATCH /bucket/:id) or as YAML with the CLI.
Bucket settings
| Field | Type | Description |
|---|---|---|
title | string, 4–100 chars | Required. Name shown in the panel and used by the CLI as the folder name. |
description | string, 5–250 chars | Required. Shown under the title in the panel. |
properties | object | Required. The properties of a document, at least one. |
primary | string | The property that represents a document, for example in relation pickers. On PostgreSQL it is also the primary key. |
required | string[] | Properties that every document must have. |
icon | string | Material icon name shown in the panel. Default view_stream. |
category | string | Groups buckets in the panel sidebar. |
order | number | Position of the bucket in the panel. |
readOnly | boolean | Documents can't be edited from the panel's data pages. |
history | boolean | Keep the previous versions of each document. Default false. |
acl | {read, write} | Rules evaluated for USER requests. Default true==true for both. |
indexes | array | Database indexes for the bucket. |
documentSettings | object | countLimit and limitExceedBehaviour (prevent or remove). See Document limits. |
Property names must start with a letter or _, contain only letters, digits and _, and can't be _id.
Property types
| Type | Stored as | Panel input | Notes |
|---|---|---|---|
string | string | Text field | Supports enum, pattern, minLength, maxLength. |
textarea | string | Multi-line text | |
richtext | string (HTML) | Rich text editor | |
number | number | Number field | Supports minimum, maximum, enum. |
boolean | boolean | Toggle | |
date | date | Date-time picker | Send ISO 8601 strings. Supports the :created_at and :updated_at defaults. |
color | string | Color picker | Hex color such as #36369c. |
array | array | List editor | Describe the items with items. Supports minItems, maxItems, uniqueItems. |
multiselect | array | Multi select | An array of values chosen from items.enum. |
object | object | Nested form | Describe the fields with properties and required. |
json | any JSON | JSON editor | No inner validation. |
storage | string (URL) | File picker | Stores the URL of a storage object. |
relation | id or id[] | Document picker | References documents in another bucket. See Relations. |
location | GeoJSON Point | Map picker | {"type": "Point", "coordinates": [longitude, latitude]} |
hash | string | Text field | Stored as a one-way keyed hash. Can be matched in filters but never read back. |
encrypted | object | Text field | Encrypted at rest, decrypted in responses. |
uuid | string | Text field | PostgreSQL only. |
integer | integer | Number field | PostgreSQL only. |
bigint | string | Number field | PostgreSQL only. Returned as a string to keep its precision. |
You can ask an instance which types it supports:
curl https://api.example.com/api/bucket/capabilities -H "Authorization: APIKEY <key>"{"propertyTypes": ["array", "multiselect", "boolean", "number", "object", "string", "storage", "richtext", "date", "textarea", "color", "relation", "location", "json", "hash", "encrypted"]}Common property keywords
Every property accepts these keywords in addition to type:
| Keyword | Description |
|---|---|
title | Label shown in the panel. |
description | Help text shown in the panel. |
default | Value used when the property is missing on insert. |
readOnly | The value isn't editable in the panel. Combined with default, the server always writes the default and ignores the value the client sends. |
enum | Allowed values. |
pattern | Regular expression a string must match. |
minLength, maxLength | String length limits. |
minimum, maximum | Number limits. |
items, minItems, maxItems, uniqueItems | Array item schema and limits. |
properties, required | Fields of an object property. |
options.translate | Store one value per language. See Translations. |
acl | A field-level rule. See Field rules. |
Example
{
"slug": {
"type": "string",
"title": "Slug",
"description": "URL-safe identifier",
"pattern": "^[a-z0-9-]+$"
},
"status": {
"type": "string",
"title": "Status",
"enum": ["draft", "published"],
"default": "draft"
},
"tags": {
"type": "array",
"title": "Tags",
"items": {"type": "string"},
"maxItems": 10,
"uniqueItems": true
},
"views": {"type": "number", "title": "Views", "minimum": 0, "default": 0}
}A document that breaks the schema is rejected with 400:
{"message": ".slug must match pattern \"^[a-z0-9-]+$\"", "error": "validation failed", "statusCode": 400}Server-set dates
Use the predefined defaults to let Spica fill in dates:
| Default | Value |
|---|---|
:created_at | The time of the write that sets it. |
:updated_at | The time of the write that sets it. |
{
"created_at": {"type": "date", "title": "Created at", "default": ":created_at", "readOnly": true},
"updated_at": {"type": "date", "title": "Updated at", "default": ":updated_at"}
}Defaults are applied on insert and on replace (PUT), not on patch (PATCH). Two consequences:
- With
readOnly,created_atis written again on everyPUT. Update documents withPATCHwhen you want to keep the original creation time. updated_atis only refreshed byPUT. If you patch documents and need a reliable modification time, set it yourself or from a bucket trigger.
GET /bucket/predefineddefaults lists the available defaults.
Relations
A relation property stores references to documents in another bucket:
{
"author": {
"type": "relation",
"title": "Author",
"bucketId": "<authorsBucketId>",
"relationType": "onetoone",
"dependent": false
}
}| Field | Description |
|---|---|
bucketId | Required. The referenced bucket. |
relationType | Required. onetoone stores a single id, onetomany stores an array of ids. |
dependent | When true, deleting a document also deletes the documents it references through this property. Default false. |
Write relations as ids, and use the relation query parameter to resolve them when you read.
Translations
Set options.translate to store one value per language:
{"title": {"type": "string", "title": "Title", "options": {"translate": true}}}Write all languages as an object keyed by locale:
{"title": {"en_US": "Hello Spica", "tr_TR": "Merhaba Spica"}}When you read, Spica returns the value in the language of the Accept-Language header and falls back to the default language. The available and default languages are stored in the bucket preferences:
curl https://api.example.com/api/preference/bucket -H "Authorization: IDENTITY <token>"{"scope": "bucket", "language": {"available": {"tr_TR": "Turkish", "en_US": "English"}, "default": "en_US"}}Change them with PUT /preference/bucket or in the panel. See Localization for reading options.
Locations
A location property stores a GeoJSON point. Coordinates are [longitude, latitude]:
{"location": {"type": "Point", "coordinates": [28.97, 41.01]}}Add a 2dsphere index when you filter by distance.
Hashed and encrypted properties
Both types protect sensitive values. They need the bucket-data-hash-secret and bucket-data-encryption-secret API options, which are derived from the master key when you don't set them.
hash | encrypted | |
|---|---|---|
| Stored | A keyed hash (HMAC) | Ciphertext plus a hash |
| Returned | The hash | The original value |
| Filter with equality | Yes, send the plain value | Yes, send the plain value |
| Use for | National ids, values you only compare | Phone numbers, addresses, values you must display |
{
"national_id": {"type": "hash", "title": "National ID"},
"phone": {"type": "encrypted", "title": "Phone"}
}Primary keys on PostgreSQL
On PostgreSQL, primary names the bucket's primary key, and documents are identified by that property instead of _id. The primary property must be of type uuid, integer, bigint or string, can't be translated, and can set options.generated to let the database create values:
{
"title": "Authors",
"description": "People who write posts",
"primary": "id",
"properties": {
"id": {"type": "uuid", "title": "ID", "options": {"generated": true}},
"name": {"type": "string", "title": "Name"}
}
}Only the primary property can be generated. More differences between the engines are listed in MongoDB and PostgreSQL.
Changing a schema
You can change a bucket at any time. Keep in mind:
- New required properties and stricter validation only apply to documents you write from now on.
- Removing a property doesn't remove existing values from stored documents.
- Changing a property's type can make old values fail validation on the next write.
- Deleting a bucket deletes all of its documents and history.
Review schema changes before they reach production with spica plan.