Skip to content

Collections

Collections define the data models in your CMS. Each collection represents a type of content that can be created, read, updated, and deleted.

Creating a Collection

You can scaffold a new collection using the CLI:

bash
lucent collection Posts

This creates a new file in src/collections/Posts.ts with a default structure. Alternatively, you can define it manually:

Manual Definition

typescript
import { defineCollection } from "@codesordinatestudio/lucent-cms";

export const Posts = defineCollection({
  slug: "posts",
  fields: [
    { name: "title", type: "text", required: true },
    { name: "slug", type: "text", required: true, unique: true },
    { name: "content", type: "richtext" },
  ],
  access: {
    read: () => true,
    create: ({ user }) => !!user,
  },
  timestamps: true,
});

Collection Options

OptionTypeDescription
namestringUnique name for the collection
fieldsobjectField definitions
apiobjectAPI access settings
hooksobjectCollection-specific hooks
accessobjectAccess control rules

API Options

typescript
api: {
  // Enable/disable operations
  read: true,
  create: true,
  update: true,
  delete: true,

  // Pagination
  perPage: 20,
  maxPerPage: 100,

  // Filtering
  filterable: ['status', 'author', 'createdAt'],
  sortable: ['createdAt', 'updatedAt', 'title']
}

Collection Hooks

typescript
export const posts = defineCollection({
  name: "posts",
  fields: {
    /* ... */
  },
  hooks: {
    beforeCreate: async (data, { db }) => {
      // Modify data before creation
      data.slug = slugify(data.title);
      return data;
    },
    afterCreate: async (record, { db }) => {
      // Perform actions after creation
      await notifyAuthor(record);
    },
  },
});

Released under the MIT License.