Esix provides several intuitive methods for retrieving models from your MongoDB database. These methods are chainable and type-safe, making it easy to fetch the data you need.
Retrieving All Models
Use the all() method to retrieve all records from a collection:
const posts = await BlogPost.all();
This returns an array of all BlogPost instances in the database.
Finding by ID
The find() method retrieves a single model by its ID:
const post = await BlogPost.find('5f5a41cc3eb990709eafda43');
The find() method returns null if no model with the given ID exists.
Method Signature
static async find<T extends BaseModel>(
this: ObjectType<T>,
id: string
): Promise<T | null>
Source: packages/esix/src/base-model.ts:129
Finding by Attribute
The findBy() method retrieves the first model where a specific attribute matches a value:
const user = await User.findBy('email', 'john.smith@company.com');
Method Signature
static async findBy<T extends BaseModel, K extends keyof T | '_id'>(
this: ObjectType<T>,
key: K,
value: K extends keyof T ? T[K] : any
): Promise<T | null>
Source: packages/esix/src/base-model.ts:147
Use findBy() for unique attributes like email addresses or usernames.
Getting the First Result
When you need the first result from a query, use the first() method:
const firstPost = await BlogPost.where('status', 'published').first();
This is particularly useful when combined with filtering and ordering:
const latestPost = await BlogPost
.where('status', 'published')
.orderBy('createdAt', 'desc')
.first();
The first() method returns null if no matching models are found.
Executing Queries
Most query builder methods return a QueryBuilder instance, allowing you to chain multiple conditions. Call get() to execute the query and retrieve the results:
const posts = await BlogPost
.where('authorId', 'author-123')
.orderBy('createdAt', 'desc')
.get();
Example: Complete Retrieval Workflow
Here’s a practical example showing different ways to retrieve models:
class User extends BaseModel {
public name = '';
public email = '';
public status = 'active';
}
// Get all users
const allUsers = await User.all();
// Find a specific user by ID
const user = await User.find('5f5a41cc3eb990709eafda43');
if (user) {
console.log(`Found user: ${user.name}`);
}
// Find user by email
const userByEmail = await User.findBy('email', 'admin@example.com');
// Get first active user
const firstActiveUser = await User.where('status', 'active').first();
// Get all active users
const activeUsers = await User.where('status', 'active').get();
Next Steps