Skip to main content

Fields

Every column is represented as a single field. Field name will by default be the same as the column name, but you can change it. Every field is tied to single collection (as column is to the table). If collection is disabled, all it's fields are unaccessible. Data about fields in stored in zmaj_field_metadata.

Zmaj tries to leverage database as much as possible. There is no config in Zmaj for default value, unique or required, since database provides those features. Zmaj will simply provide inputs for database config when creating field.

caution

Field configuration will not do anything if you are making changes manually to the database, e.g. by running raw SQL query directly.

Disable reading field value

Every fields has canRead. If value is false, this field's value will never be returned from the database. This is useful for passwords, when you don't want them returned, but you will read them manually form database.

Disable setting field value when updating record

It is possible to disable field value from updating. Set canUpdate to false. This will ensure that value cannot be updated. Value can still be set when creating record see. For example, createdBy field should never be changed.

Disable setting field value when creating record

If you do not want field value to be set when creating record, you can set canCreate value to false. This is useful if value is auto generated by database, or should only be set at the later date, for example, in todos collection, finishedAt field should never be set when creating record.

Disable sorting

If you want to forbid sorting by current field, you can set sortable column to false. This will prevent sorting by that column when accessing API. By default you can sort by any field.

Timestamps

Field can act as a createdAt and updatedAt timestamp by setting isCreatedAt or isUpdatedAt. Only one field in collection can be set to be timestamp (1 for created, 1 for updated). This is done on ORM level, so whenever record is changed within an app, timestamp will be updated.

There is unique constraint in Postgres database that prevents you from having multiple timestamps in same collection.

caution

If you are changing record outside of app, like with raw SQL query, isCreatedAt and isUpdatedAt have no effect. If both isCreatedAt and isUpdatedAt are true, isUpdatedAt has priority.

Label

You can set field's label by setting label property. This label is used in admin panel instead of field name.

Description

You can add description to field. That value will be used as a helper text bellow input field to explain to users what this field does.

Admin Panel Component

You can customize what component should be rendered in the admin panel for current field by specifying componentName. This way you can provide better UX to users of your panel. For example, set to email to display email input, password to set password input where value is hidden. Possible components will be shown in dropdown when creating/updating field.

By default admin panel will render generic component based on your column type (e.g. int input for integer column).

Additional Field Config

Fields can have additional configuration that is stored in fieldConfig field as a single JSON in database. Some properties are constant, and some are dependent on type of column (and selected component). Validation provided here is only used in client, so make sure to validate data on the server as well. Best way to edit field config is trough admin panel, since configuration is different depending on component, and component can provide it's own inputs (e.g. number input can provide min and max input).

Changing default collection name

Zmaj will generate collections for existing tables, and create names for them. By default, Zmaj will try to reuse table name. You can customize how name will look like with options.infra.defaultCase = "camel". This will not impact collections that already exist.

import { runServer } from "zmaj"

// "camel" | "snake" | "none", defaults to "none"
await runServer({ infra: { defaultCase: "camel" } })