What is a Model?
Think of a model as a blueprint for your data. If you have a “users” table in your database, you create a “User” model to work with user data in your JavaScript code.Real-world analogy: A model is like a form at the doctor’s office. The form has specific fields (name, age, symptoms), and each filled-out form represents one patient record. The model defines what fields exist, and each instance represents one record.
Creating Your First Model
Using the CLI (Recommended)
Manual Creation
Model Configuration
Table Settings
UUID Primary Keys
SetkeyType = 'uuid' and incrementing = false — IlanaORM generates a UUID automatically on create:
ULID Primary Keys
ULIDs are 26-character sortable IDs — they’re URL-safe, lexicographically ordered, and include a millisecond timestamp prefix so they sort naturally by creation time:char(26) for the column type in migrations:
Timestamps
Control automatic timestamp handling:Soft Deletes
Mark records as deleted without actually removing them:Mass Assignment Protection
Control which fields can be set when creating or updating records:Attribute Casting
Automatically convert database values to JavaScript types:Available Cast Types
Cast Class Instances
You can also pass cast class instances (objects withget(value) / set(value) methods). They are called automatically the same way string casts are:
Default Values
Set default values for new model instances:Hidden Attributes
Hide sensitive fields from JSON output:Mutators and Accessors
Transform data when setting or getting attributes:Mutators (Setters)
Mutators are called automatically whenever an attribute is set — on direct assignment (user.email = x), via fill(), and via update(). They must return the transformed value.
Accessors (Getters)
Accessors are called automatically on direct property access. List the key inappends to include it in toJSON() output.
Model Events
Hook into the model lifecycle:Available Events:
saving/saved- Before/after create or updatecreating/created- Before/after createupdating/updated- Before/after updatedeleting/deleted- Before/after deleterestoring/restored- Before/after soft delete restore
Muting Events
Sometimes you need to save records without triggering events — for example in seeders, migrations, or bulk imports. UsewithoutEvents():
Query Scopes
Create reusable query constraints:Working with Model Instances
Creating Records
Reading Records
Updating Records
Deleting Records
Pruning Models
Pruning lets you automatically delete stale records (e.g. old logs, expired tokens) by defining what “prunable” means on the model:prune() on a schedule (e.g. a daily cron job) to keep your tables clean.
Model Utilities
Reload and Compare Instances
Replicating Models
replicate() creates an unsaved copy of a model, excluding the primary key and timestamps so it can be inserted as a new row:
Table Helpers
seed() requires a factory to be defined for the model with defineFactory. See Factories for setup details.Enum Helpers
Define the possible values for an enum column once, and IlanaORM generatesisX() / makeX() helpers automatically on every instance:
Check Model State
Convert to Different Formats
Best Practices
1. Use Descriptive Names
2. Always Use Mass Assignment Protection
3. Hide Sensitive Data
4. Use Appropriate Casting
5. Organize Related Logic
Next Steps
Learn Queries
Discover how to find and filter your data
Set Up Relationships
Connect different models together
