Skip to main content
Models are the heart of Esix! They provide a beautiful, simple ActiveRecord-style implementation for working with your MongoDB collections. Each collection in your database has a corresponding model class that makes it easy to query, create, and update your data. Think of models as the bridge between your TypeScript code and your MongoDB collections. Let’s create your first model and see how simple it is!

Creating a Basic Model

Here’s how you create a simple blog post model:
That’s it! By extending the 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 created
  • updatedAt - Updated every time you save or update the document
Both timestamps use JavaScript’s Date.now() function, storing the time in milliseconds since January 1st, 1970.

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:
  1. Converting from PascalCase to kebab-case (dashes)
  2. Making it plural
This means you don’t need to configure collection names manually - just name your model classes descriptively and Esix handles the rest! And when the convention doesn’t fit, there’s an escape hatch.

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 static collectionName property to override the convention:
All queries, writes, and relationships for the model now use the app_users collection. Subclasses inherit the custom name unless they define their own.

Relationships

Models can declare relationships to one another with hasMany, hasOne, and belongsTo. These helpers are documented in detail in the Relationships guide.