Skip to main content

Overview

Esix provides two primary ways to create new models: using the static create() method for immediate persistence, or instantiating a model and calling save() later.

Using create()

The create() 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
static async create<T extends BaseModel>(
  this: ObjectType<T>,
  attributes: Partial<T>
): Promise<T>

Basic Usage

const post = await BlogPost.create({ 
  title: 'My First Blog Post!' 
});

Default Values

Esix automatically includes default values defined in your model class when creating new instances.
Model Definition
class Book extends BaseModel {
  public isAvailable = true;  // Default value
  public isbn = '';
  public title = '';
  public pages = 0;
}
When you create a book without specifying isAvailable, it will default to true:
const book = await Book.create({
  title: 'Jane Eyre',
  isbn: '9780141439518',
  pages: 544
});

// book.isAvailable is true by default

Timestamps

Every model created with create() automatically receives:
  • createdAt - Set to the current timestamp
  • updatedAt - Initially set to null
  • id - Auto-generated MongoDB ObjectId (unless provided)
const post = await BlogPost.create({ title: 'New Post' });

console.log(post.id);        // '5f3568f2a0cdd1c9ba411c43'
console.log(post.createdAt); // 1672574400000
console.log(post.updatedAt); // null
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

The create() method throws an error if the model fails to persist:
try {
  const post = await BlogPost.create({ 
    title: 'My Post' 
  });
} catch (error) {
  console.error('Failed to create model:', error);
}