Overview
Esix provides two primary ways to create new models: using the staticcreate() method for immediate persistence, or instantiating a model and calling save() later.
Using create()
Thecreate() method is the recommended way to create and persist a model in a single operation. It automatically generates an ID if none is provided and sets the createdAt timestamp.
Method Signature
packages/esix/src/base-model.ts
Basic Usage
Default Values
Esix automatically includes default values defined in your model class when creating new instances.Model Definition
isAvailable, it will default to true:
Timestamps
Every model created withcreate() automatically receives:
createdAt- Set to the current timestampupdatedAt- Initially set tonullid- Auto-generated MongoDB ObjectId (unless provided)
The
create() method returns the fully populated model instance fetched from the database, ensuring all default values and auto-generated fields are included.Error Handling
Thecreate() method throws an error if the model fails to persist: