JCC Express

JCC Eloquent ORM

JCC-Eloquent Introduction

Introduction

JCC-Eloquent is the framework’s ActiveRecord-style ORM layer (jcc-express-mvc/Eloquent).

It gives you:

  • Model classes mapped to tables
  • Static query API on models (query, where, select, find, all, etc.)
  • Relationships (hasOne, hasMany, belongsTo, belongsToMany, morph relations)
  • Model events and observer support
  • Casting, hidden attributes, accessors/mutators
  • Soft delete support and pagination helpers
  • JWT token creation via createToken() on model instances (opt-in with hasToken)

Imports

TypeScript
import { Model } from "jcc-express-mvc/Eloquent";
import { DB } from "jcc-express-mvc/Eloquent";

Basic model example

TypeScript
import { Model } from "jcc-express-mvc/Eloquent";

export class User extends Model {
  protected hidden = ["password"];
}

Table name defaults to plural snake_case of class name (users for User), unless overridden.


Basic model usage

TypeScript
const users = await User.all();
const one = await User.find(1);

const created = await User.create({
  name: "Abdou",
  email: "abdou@example.com",
});

await created.update({ name: "Abdou J" });
await created.delete();

Querying through model static methods

Model inherits static query methods through QueryBuilder, so you can do:

TypeScript
const active = await User.where("active", true)
  .orderBy("created_at", "desc")
  .limit(20)
  .get();

const exists = await User.where("email", "admin@example.com").exists();

Relationships

Inside models, available relationship builders include:

  • hasOne(...)
  • hasMany(...)
  • belongsTo(...)
  • belongsToMany(...)
  • morphOne(...)
  • morphMany(...)
  • morphTo(...)

Example:

TypeScript
export class User extends Model {
  posts() {
    return this.hasMany(Post, "author");
  }
}

Useful model features

  • hidden -> hide fields from JSON (toJSON)
  • accessors -> getXxxAttribute()
  • mutators -> setXxxAttribute()
  • updateQuietly(...), deleteQuietly(), executeWithoutEvents(...)
  • soft delete behavior when model enables soft deletes
  • createToken(payload?) — issue a JWT for the model instance when protected hasToken = true (see Defining Model)

Pagination

Model-level pagination helper exists:

TypeScript
const page = await User.paginate(15);

This returns data plus pagination metadata/links.


How it fits with DB/query builder

  • Use User model methods when working with domain entities.
  • Use DB.table(...) for table-centric or cross-model query patterns.

You can mix both approaches in the same project.


Summary

  • JCC-Eloquent is the default ORM path in JCC mode.
  • It combines ActiveRecord models + fluent query builder + relationships.
  • Start with class User extends Model, then query using static model methods.