Model configuration
This is the documentation for the latest version of Next Admin. If you are using an older version (<5.0.0
), please refer to the documentation
To configure your models, you can use the model
property of the NextAdmin options parameter.
This property allows you to configure everything about how your models, their fields, their relations are displayed and edited.
Example for a schema with a User
, Post
and Category
model:
import { NextAdminOptions } from "@premieroctet/next-admin";
export const options: NextAdminOptions = {
/* Your other options here */
model: {
User: {
toString: (user) => `${user.name} (${user.email})`,
title: "Users",
icon: "UsersIcon",
list: {
display: ["id", "name", "email", "posts", "role", "birthDate"],
search: ["name", "email"],
filters: [
{
name: "is Admin",
active: false,
value: {
role: {
equals: "ADMIN",
},
},
},
],
},
edit: {
display: [
"id",
"name",
"email",
"posts",
"role",
"birthDate",
"avatar",
],
},
},
Post: {
toString: (post) => `${post.title}`,
},
Category: {
title: "Categories",
icon: "InboxStackIcon",
toString: (category) => `${category.name}`,
list: {
display: ["name", "posts"],
},
edit: {
display: ["name", "posts"],
},
},
},
};
It takes as key a model name of your schema as value an object to configure it.
By default, if no models are defined, they will all be displayed in the admin. If you want more control, you have to define each model individually as empty objects or with the following properties:
Name | Description | Default Value |
---|---|---|
toString | a function that is used to display your record in related list | id field |
aliases | an object containing the aliases of the model fields as keys, and the field name | - |
title | a string used to display the model name in the sidebar and in the section title | Model name |
list | an object containing the list options (see list property) | - |
edit | an object containing the edit options (see edit property) | - |
actions | an array of actions (see actions property) | - |
icon | the outline HeroIcon name displayed in the sidebar and pages title | - |
permissions | an array to specify restricted permissions on model | [`create`, `edit`, `delete`] |
You can customize the following for each model:
list
property
This property determines how your data is displayed in the list View
Name | Type | Description | Default Value |
---|---|---|---|
search | Array | an array of searchable fields. You can search on nested fields by concatenating them with a dot, for example author.name | All scalar fields are searchable by default |
display | Array | an array of fields that are displayed in the list | All scalar fields are displayed by default |
fields | Object | an object containing the model fields as keys, and customization values (see fields property) | - |
copy | Array | an array of fields that are copyable into the clipboard | undefined - no field is copyable by default |
defaultSort | Object | an optional object to determine the default sort to apply on the list | - |
defaultSort.field | String | the model's field name to which the sort is applied. It is mandatory | - |
defaultSort.direction | String | the sort direction to apply. It is optional | - |
filters | Array | define a set of Prisma filters that user can choose in list (see filters) | - |
exports | Object | an object or array of export - containing export url (see exports) | - |
The search
property is only available for scalar
fields.
list.fields
property
The fields
property is an object that can have the following properties:
Name | Type | Description |
---|---|---|
formatter | Function | a function that takes the field value as a parameter, and returns a JSX node. It also accepts a second argument which is the NextAdmin context |
sortBy | String | available only on many-to-one models. The name of a field in the related model to apply the sort to. Defaults to the id field of the related model |
list.filters
property
The filters
property allow you to define a set of Prisma filters that user can apply on list page. It’s an array of the type below:
Name | Type | Description | Default Value |
---|---|---|---|
name | String | a unique name for the filter | - |
active | Boolean | a boolean to set the filter active by default | false |
value | String | a where clause Prisma filter of the related model (e.g. Prisma operators) | - |
group | String | an id that will be used to give filters with the same group name a radio like behavior | - |
It can also be an async function that returns the above type so that you can have a dynamic list of filters.
list.exports
property
The exports
property is available in the list
property. It’s an object or an array of objects that can have the following properties:
Name | Type | Description | Default Value |
---|---|---|---|
format | String | a string defining the format of the export. It's used to label the export button | undefined |
url | String | a string defining the URL of the export action | undefined |
The exports
property does not account for active filters. If you want to
export filtered data, you need to add the filters to the URL or in your export
action.
edit
property
This property determines how your data is displayed in the edit view
Name | Type | Description | Default Value |
---|---|---|---|
display | Array | an array of fields that are displayed in the form. It can also be an object that will be displayed in the form of a notice (see notice) | all scalar fields are displayed |
styles | Object | an object containing the styles of the form (see styles) | - |
fields | Object | an object containing the model fields as keys, and customization values (see fields property) | - |
submissionErrorMessage | String | a message displayed if an error occurs during the form submission | 'Submission error' |
edit.styles
property
The styles
property is available in the edit
property.
If your options are defined in a separate file, make sure to add the path to
the content
property of the tailwind.config.js
file (see TailwindCSS
configuration).
Name | Type | Description |
---|---|---|
_form | String | a string defining the classname of the form |
... | String | all fields of the model, wwith the field name as the key and the classname as the value |
Here is an example of using styles
property:
styles: {
_form: "form-classname",
... // all fields
};
edit.fields
property
This property can be defined for each field of the corresponding model. When you define a field, use the field’s name as the key and the following object as the value:
Name | Type | Description | Default Value |
---|---|---|---|
validate | Function | a function that takes the field value as a parameter, and returns a boolean | - |
format | String | a string defining an OpenAPI field format, overriding the one set in the generator. An extra file format can be used to be able to have a file input | - |
input | React Element | a React Element that should receive CustomInputProps. For App Router, this element must be a client component | - |
handler | Object | - | |
handler.get | Function | a function that takes the field value as a parameter and returns a transformed value displayed in the form | - |
handler.upload | Function | an async function that is used only for formats file and data-url . It takes a Buffer object and an information object containing name and type properties as parameters and must return a string. It has context as a parameter, which holds record information such as the resource ID, which does not exist during record creation. It can be useful to upload a file to a remote provider | - |
handler.uploadErrorMessage | String | an optional string displayed in the input field as an error message in case of a failure during the upload handler | - |
optionFormatter | Function | only for relation fields, a function that takes the field values as a parameter and returns a string. Useful to display your record in related list | - |
tooltip | String | A tooltip content to display for the field | - |
helperText | String | a helper text that is displayed underneath the input | - |
disabled | Boolean | a boolean to indicate that the field is read only | - |
display | String | only for relation fields, indicate which display format to use between list , table or select | select |
required | Boolean | a true value to force a field to be required in the form, note that if the field is required by the Prisma schema, you cannot set required to false | - |
relationOptionFormatter | Function | same as optionFormatter , but used to format data that comes from an explicit many-to-many relationship | - |
orderField | String | the field to use for relationship sorting. This allows to drag and drop the related records in the list display | - |
relationshipSearchField | String | a field name of the explicit many-to-many relation table to apply the search on. See handling explicit many-to-many | - |
visible | Function | a function that takes the resource value as a parameter, and returns a boolean that shows or hides the field in the edit form | - |
edit.hooks
property
The hooks
property allows you to intercept the values of a form, before and after its insertion in the database.
It is an object that can have the following properties:
Name | Type | Description |
---|---|---|
beforeDb | Function | a function that takes the form values, the insertion mode (create or edit) and the request object as parameters. It must return a promise with the form values. You can throw a [HookError]() in case of an error, which will cause an early return in the handler with the appropriate response status and error message. |
afterDb | Function | a function that takes the insertion response, the insertion mode (create or edit) and the request as parameters. It returns the same data as the insertion response. |
actions
property
The actions
property is an array of objects that allows you to define a set of actions that can be executed on one or more records of the resource. On the list view, there is a default action for deletion. The object can have the following properties:
Name | Type | Description |
---|---|---|
title | String | action title that will be shown in the action dropdown |
id | String | mandatory, action's unique identifier |
type | String | action type for client side actions, possible value is 'dialog' | 'server'. By default action with no type is executed on the server |
component | ReactElement | a React component that will be displayed in a dialog when the action is triggered. Its mandatory if the action type `dialog` is specified. See the ClientActionDialogContentProps for the props passed to the component. |
className | string | class name applied to the dialog displayed when the action type is set to 'dialog'. |
action | Function | an async function that will be triggered when selecting the action in the dropdown. Its mandatory if the action type is not specified. Can return a message object to display a message after the action is done or throw an error to display a message |
canExecute | Function | a function that takes a record as a parameter and returns a boolean. It is used to determine if the action can be executed on the record |
successMessage | String | a message that will be displayed when the action is successful if action doesn't return a message object |
errorMessage | String | a message that will be displayed when the action fails if action doesn't return a message object or throw an error with a message |
depth | Number | a number that defines the depth of the relations to select in the resource. Use this with caution, a number too high can potentially cause slower queries. Defaults to 2. |
NextAdmin Context
The NextAdmin
context is an object containing the following properties:
Name | Type | Description |
---|---|---|
locale | String | The locale used by the calling page. (refers to the accept-language header) |
row | Object | The current row of the list view |
Notice
The edit page’s form can display notice alerts. To do so, you can pass objects in the display
array of the edit
property of a model.
This can be useful to display alerts or information to the user when editing a record.
This object takes the following properties :
Name | Type | Description |
---|---|---|
title | String | The title of the notice. This is mandatory |
id | String | A unique identifier for the notice that can be used to style it with the styles property. This is mandatory |
description | String | The description of the notice. This is optional |
Here is a quick example of usage.
Considering you have a model User
with the following configuration:
export const options: NextAdminOptions = {
basePath: "/admin",
title: "⚡️ My Admin",
model: {
User: {
/**
...some configuration
**/
edit: {
display: [
"id",
"name",
{
title: "Email is mandatory",
id: "email-notice",
description: "You must add an email from now on",
} as const,
"email",
"posts",
"role",
"birthDate",
"avatar",
"metadata",
],
/** ... some configuration */
},
},
},
};
In this example, the email-notice
notice will be displayed in the form before the email
field.
CustomInputProps
Represents the props that are passed to the custom input component.
Name | Type | Description |
---|---|---|
name | String | the field name |
value | any | the field value |
onChange | Function | a function taking a ChangeEvent as a parameter |
readonly | Boolean | boolean indicating if the field is editable or not |
rawErrors | Array | array of strings containing the field errors |
disabled | Boolean | boolean indicating if the field is disabled |
HookError
The HookError
is an error that can be thrown in the beforeDb
hook of the edit
property. It takes the following arguments in its constructor:
Name | Type | Description |
---|---|---|
status | Number | the HTTP status code |
data | Object | an error object that must contain at least an error property which is a string |
ClientActionDialogContentProps
Represents the props that are passed to the custom dialog component.
Name | Type | Description |
---|---|---|
resource | String | the current Prisma model name |
resourceId | String | Number | the selected record id |
data | Record<string, ListDataFieldValue> | A record of row's properties with ListDataFieldValue as value |
onClose | Function | a function to close the dialog, it can receive a message object to display a message after the dialog is closed |
ListDataFieldValue
Represents a formatted value used by Next-Admin. It will have different shapes depending of the data type.
It will always have the following :
Name | Type | Description |
---|---|---|
__nextadmin_formatted | ReactNode | A React Node used to have a custom rendering of the field, if needed |
Scalar
Name | Type | Description |
---|---|---|
type | `scalar` | The type of data. It is always `scalar` |
value | String | Number | boolean | the value coming from the database |
Count
Name | Type | Description |
---|---|---|
type | `count` | The type of data. It is always `count` |
value | Number | the value coming from the database |
Link
Name | Type | Description |
---|---|---|
type | `link` | The type of data. It is always `link` |
value | object | the link data displayed in the link CTA |
value.label | String | the link CTA label |
value.url | String | the link CTA href |
Date
Name | Type | Description |
---|---|---|
type | `date` | The type of data. It is always `date` |
value | Date | the value coming from the database |