Skip to main content

🏷️ Database Decorators

Decorators are a way to add metadata to a class, property, or method declaration, making them more expressive and functional.

🌀 Sluggable

The Sluggable decorator is used to generate a slug from a string property. It utilizes the sluggable utility function under the hood.

Example

src/app/products/models/product.ts
import { castModel, Casts, Model } from "@warlock.js/cascade";
import { Sluggable, uploadable } from "@warlock.js/core";

@Sluggable("title")
export default class Post extends Model {
// ...

/**
* {@inheritDoc}
*/
protected casts: Casts = {
title: "localized",
description: "localized",
shortDescription: "localized",
isActive: "boolean",
};
}

This will create a slug field whenever the post is saved (created or updated). The slug will be generated from the title field.

You can generate the slug from another column by passing the column name to the Sluggable decorator.

src/app/products/models/product.ts
import { castModel, Casts, Model } from "@warlock.js/cascade";
import { Sluggable, uploadable } from "@warlock.js/core";

@Sluggable("name")
export default class Post extends Model {
// ...

/**
* {@inheritDoc}
*/
protected casts: Casts = {
name: "localized",
description: "localized",
shortDescription: "localized",
isActive: "boolean",
};
}