defineCollection
The defineCollection
function helps you define collections property regarding the APIs and Admin Panel. You can create multiple collections in your project to manage different types of data. This function takes an object of type CollectionConfig
. You can define the collection's schema, access control, admin settings, and more. Here's an example of how to define a collection -
const collection = defineCollection({
slug: "todos",
schema: schema.todos,
access: () => true,
admin: {
label: { singular: "Todo", plural: "Todos" },
fields: ["message", { name: "status", label: "Status", required: false }],
},
pagination: {
defaultLimit: 10,
},
});
You can use the access
property to define access control for the collection. The access property can be a function that returns a boolean or a promise that resolves to a boolean.
API
Property | Description | Type | Default |
---|---|---|---|
slug | The collection slug | string | Table's Name |
queryKey | The key to use for querying | Table Column | Table's PK |
schema* | The schema for the collection | Drizzle Table Schema | - |
access | Access control | (c) => boolean | Promise<boolean> | - |
defaultSort | Default sort order | string | - |
listSearchableFields | Fields to be searched via full text search | TableColumns[] | - |
admin | Collection admin options | CollectionAdminProps | - |
pagination | Pagination options | defaultLimit and maxLimit | |
hooks | Hooks to modify HonoHub functionality | CollectionHooks | - |
plugins | Collection plugins | CollectionPlugin[] | - |
Collection Admin Options
The admin
property takes an object of type CollectionAdminProps
. You can define the label for the collection, the fields to be displayed in the admin panel, and more.
These properties are used to build the Admin Panel for the collection. These actions will run on the server. Here's an example of how to define admin settings for a collection -
const adminOptions: CollectionAdminProps = {
label: { singular: "Todo", plural: "Todos" },
fields: ["message", { name: "status", label: "Status", required: false }],
actions: [
{
name: "sendResetPasswordEmail",
label: "Send Reset Password Email",
icon: "AtSymbolIcon",
action: (options) => {
// Your action logic here
},
},
],
};
By default the actions are disabled, you can enable them by passing true
or an array of actions. It also gets enabled when you pass any value in the admin
property, to prevent this you can pass false
in the actions
property. When enabled it will create a /actions/{action_name}
endpoint for each action, which will be used to run the action on the server. Here's a default action that is added when enabled -
{
name: "bulk_delete",
label: "Delete",
icon: "TrashIcon",
action: async ({ ids }) => {
// Your action logic here
},
}
API
Property | Description | Type | Default |
---|---|---|---|
label | Collection label | CollectionLabel | - |
fields | Fields to be displayed in the admin panel | AdminField[] | - |
actions | Actions to be displayed in the admin panel | boolean | AdminAction[] | false | [bulk_delete] |
Collection Hooks
The hooks
property takes an object of type CollectionHooks
. You can define hooks to run before or after creating, updating, or deleting a record. Here's an example of how to define hooks for a collection -
const collection = defineCollection({
// ...
hooks: {
afterChange: [
async ({ doc }) => {
console.log(doc);
return undefined;
},
],
},
});
API
Property | Description |
---|---|
beforeCreate | Hook to run before creating a record |
afterCreate | Hook to run after creating a record |
beforeUpdate | Hook to run before updating a record |
afterUpdate | Hook to run after updating a record |
beforeDelete | Hook to run before deleting a record |
afterDelete | Hook to run after deleting a record |
Collection Plugins
The plugins
property takes an array of type CollectionPlugin
. You can define plugins to add more functionalities to the collection. Here's an example of how to define plugins for a collection -
const collection = defineCollection({
// ...
plugins: [
{
name: "myPlugin",
register: (config) => {
console.log("Plugin registered", config);
},
bootstrap: async ({ app }) => {
// Your plugin logic here
},
},
],
});
API
Property | Description | Type |
---|---|---|
name | Plugin name | string |
register | Runs before the app is created | (config) => SanitizedCollection | undefined; |
bootstrap | Runs when bootstraping the app | ({ app, config }) => Hono | undefined |