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 PostsThis 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
| Option | Type | Description |
|---|---|---|
name | string | Unique name for the collection |
fields | object | Field definitions |
api | object | API access settings |
hooks | object | Collection-specific hooks |
access | object | Access 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);
},
},
});