Skip to main content

Documentation Index

Fetch the complete documentation index at: https://www.esixorm.com/llms.txt

Use this file to discover all available pages before exploring further.

Getting Started with Esix

Esix is a slick ORM for MongoDB that makes working with your database in TypeScript a breeze! 🥧 Inspired by ActiveRecord and Eloquent, Esix uses a Convention over Configuration approach. This means you can define your models as normal TypeScript classes with minimal boilerplate.

Installation

Getting Esix up and running is simple! Just add the package using your favorite package manager: Using Yarn:
yarn add esix mongodb
Using NPM:
npm install esix mongodb
Note: You’ll need both esix and mongodb packages to get started.

Creating Your First Model

Let’s start by defining your first model. Here’s how you can create a simple Book model:
import { BaseModel } from 'esix'

export default class Book extends BaseModel {
  public isbn = ''
  public title = ''
}
That’s it! The BaseModel automatically provides id and timestamp fields (createdAt and updatedAt), so you don’t have to worry about those.

Creating Records

Now you’re ready to create some data! Here’s how you can add books to your database:
import Book from './book'

async function createBooks(): Promise<void> {
  await Book.create({
    isbn: '978-0525536291',
    title: 'The Vanishing Half'
  })
  
  await Book.create({
    isbn: '978-0525521143',
    title: 'The Glass Hotel'
  })
}

Querying Your Data

Once you have some data, querying is just as simple! Here’s how you can retrieve books:
import { Request, Response } from 'express'
import Book from './book'

async function showBook(request: Request, response: Response): Promise<void> {
  const book = await Book.find(request.params.id)

  response.json({
    book
  })
}

What’s Next?

Congratulations! You’ve just created your first Esix model and learned the basics of creating and querying data. Here are some next steps to explore: