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

PropertyDescriptionTypeDefault
slugThe collection slugstringTable's Name
queryKeyThe key to use for queryingTable ColumnTable's PK
schema*The schema for the collectionDrizzle Table Schema-
accessAccess control(c) => boolean | Promise<boolean>-
defaultSortDefault sort orderstring-
listSearchableFieldsFields to be searched via full text searchTableColumns[]-
adminCollection admin optionsCollectionAdminProps-
paginationPagination optionsdefaultLimit and maxLimit
hooksHooks to modify HonoHub functionalityCollectionHooks-
pluginsCollection pluginsCollectionPlugin[]-

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

PropertyDescriptionTypeDefault
labelCollection labelCollectionLabel-
fieldsFields to be displayed in the admin panelAdminField[]-
actionsActions to be displayed in the admin panelboolean | 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

PropertyDescription
beforeCreateHook to run before creating a record
afterCreateHook to run after creating a record
beforeUpdateHook to run before updating a record
afterUpdateHook to run after updating a record
beforeDeleteHook to run before deleting a record
afterDeleteHook 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

PropertyDescriptionType
namePlugin namestring
registerRuns before the app is created(config) => SanitizedCollection | undefined;
bootstrapRuns when bootstraping the app({ app, config }) => Hono | undefined