Creating a Basic Model
Here’s how you create a simple blog post model:BaseModel class, you automatically get access to all of Esix’s powerful features. Your model can now create and update documents and query your MongoDB collection.
Built-in Properties
Every Esix model automatically comes with some helpful built-in properties:id
Your model gets an id property that defaults to a MongoDB ObjectId. You can also provide your own custom ID if needed.
Timestamps
Two timestamp properties are automatically managed for you:createdAt- Set when the document is first createdupdatedAt- Updated every time you save or update the document
Collection Naming
Esix automatically determines which MongoDB collection to use based on your model’s class name. This follows our Convention over Configuration philosophy to keep things simple. Here’s how the naming works:
The class name is transformed by:
- Converting from PascalCase to kebab-case (dashes)
- Making it plural
Custom Collection Names
Sometimes the conventional name doesn’t work - you’re pointing Esix at an existing collection, sharing a database with another app, or your build tool minifies class names. Set the staticcollectionName property to override the convention:
app_users collection. Subclasses inherit the custom name unless they define their own.
Relationships
Models can declare relationships to one another withhasMany, hasOne, and
belongsTo. These helpers are documented in detail in the
Relationships guide.