Honovel Honovel Deno

Models in Honovel

Models represent your application’s data structure and interact with the database. Honovel models are inspired by Laravel’s Eloquent, supporting relationships, querying, and data manipulation.

Creating a Model

Use the CLI to generate a model:

deno task make:model User

Optional flags:

  • --migration — Generate a migration file along with the model
  • --factory — Generate a factory for the model
  • --controller — Generate a controller
  • --resource — Make the controller resourceful
  • --all — Generate migration, factory, and controller

Defining a Model

Example User model in App/Models/User.ts:

import { Model } from "Illuminate/Database/Eloquent/Model.ts";

export default class User extends Model {
    static table = "users"; // specify table if different from class name
    static fillable = ["name", "email", "password"]; // mass assignable fields
}

Relationships

Models can define relationships:

import Post from "./Post.ts";

export default class User extends Model {
    posts() {
        return this.hasMany(Post); // One-to-Many
    }

    roles() {
        return this.belongsToMany(Role); // Many-to-Many
    }
}

Querying Models

You can use the model to query the database:

const users = await User.where("active", true).get();
const user = await User.find(1);
const newUser = await User.create({ name: "Alice", email: "alice@example.com" });

Summary

  • Models are your primary way to interact with the database.
  • Relationships allow you to express complex data structures.
  • The CLI can generate models, migrations, factories, and controllers in one go.