LLaraNode
Database Package

Models

Models in LaraNode provide an expressive, Eloquent-inspired way to interact with your database.

Models

Models in LaraNode provide an expressive, Eloquent-inspired way to interact with your database.

Defining Models

Extend the Model class:

import { Model } from "@lara-node/db";

class User extends Model {
  static table = "users";
  static primaryKey = "id";
  static fillable = ["name", "email", "password"];
  static hidden = ["password"];
  static timestamps = true;
}

Model Properties

PropertyDefaultDescription
tablePluralized class nameDatabase table name
primaryKey'id'Primary key column
fillable[]Mass-assignable attributes
guarded[]Non-assignable attributes
hidden[]Hidden from JSON
appends[]Appended to JSON
casts{}Attribute type casting
timestampstrueAuto-manage timestamps
softDeletesfalseEnable soft deletes

Creating Records

// Create and save
const user = await User.create({
  name: "John",
  email: "[email protected]",
});

// Create instance and save
const user = new User();
user.name = "John";
user.email = "[email protected]";
await user.save();

// First or create
const user = await User.firstOrCreate({ email: "[email protected]" }, { name: "John" });

// Update or create
const user = await User.updateOrCreate({ email: "[email protected]" }, { name: "John" });

Retrieving Records

// Find by primary key
const user = await User.find(1);

// All records
const users = await User.all();

// First record
const user = await User.first();

// With conditions
const users = await User.where("active", true).get();

// Multiple conditions
const users = await User.where("active", true).where("role", "admin").get();

Updating Records

// Find and update
const user = await User.find(1);
await user.update({ name: "Jane" });

// Update by query
await User.where("role", "user").update({ status: "active" });

// Increment/decrement
await User.find(1).increment("login_count");
await User.find(1).decrement("credits", 10);

Deleting Records

// Delete instance
const user = await User.find(1);
await user.delete();

// Delete by query
await User.where("inactive", true).delete();

Accessors and Mutators

Define getters and setters for attributes:

class User extends Model {
  static table = "users";

  // Accessor
  getFullName() {
    return `${this.first_name} ${this.last_name}`;
  }

  // Mutator
  setPasswordAttribute(value: string) {
    this.attributes.password = hashPassword(value);
  }
}

Attribute Casting

class User extends Model {
  static casts = {
    is_admin: "boolean",
    email_verified_at: "date",
    settings: "json",
    age: "integer",
    balance: "float",
  };
}

Serialization

const user = await User.find(1);

// To JSON (respects hidden/appends)
const json = user.toJSON();

// To plain object
const data = user.toObject();

Next Steps