monjoi
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

npm

🐦 Monjoi

Monjoi - A lightweight alternative to Mongoose.

Features

  • Natively written in TypeScript
  • Schema's with Joi
  • Pre and Post Hooks (a.k.a Plugins)
  • Pagination // TODO
  • Partial Schema Validation for Updates // TODO
  • Automatic Timestamp insertion / updating // TODO

Install

npm i monjoi

# or with Yarn

yarn add monjoi

* Monjoi has peer dependencies on Joi and node mongodb driver.

Examples

Creating a collection.

  1. First declare the Schema and Type, then export the Data Access Object (DAO) and Type.
// person.model.ts
import Joi from 'joi'
import { collection } from 'monjoi'

export type Person = {
  name: string
  age?: number
}

const PersonSchema = Joi.object<Person>({
  name: Joi.string().required(),
  age: Joi.number()
})

// Person - Data Access Object
export const PersonDAO = collection('person', PersonSchema)
  1. Connect DAO's to the DB
// TODO: Example needed
  1. Dependency Inject your new DAO into any service to get access to the collection.
// person.service.ts
import { Person, PersonDAO } from './person.model.ts'

class PersonService {
  constructor(
    // Dependency Injected `personDAO`
    private personDAO: PersonDAO
  ) {}

  public async createPerson(person: Person): Promise<Person> {
    // Automatically validates `person` against Joi schema
    // and returns the newly inserted mongo document with _id
    return this.personDAO.insertOne(person)
  }
}

Hooks (Plugins)

There are 2 types of hooks available:

  1. Pre Hooks - Functions that execute before running the db access
  2. Post Hooks - Functions that execute after running the db access

Hooks can return regular values or even Promises.

Pre hooks

Useful for triggering other operations before accessing the db. If a hook returns a rejected promise, then the main db operation will not execute, neither will the Post hooks.

Post hooks

Useful for formatting db responses or triggering other operations. Post hooks should generally return objects because they will be passed to the next hook.

Any hook that returns a rejected promise will cancel the hook chain.

Hook Examples

Hooks are declared when defining your DAO. You can hook into any standard mongodb collection operation.

export const PersonDAO = collection('person', PersonSchema, {
  insertOne: {
    pre: [
      () => console.log('I will run before any insertOne operation for person collection'),
      () => console.log('me too!')
    ],
    post: [
      (insertedDoc) => {
        console.log(`Document ${insertedDoc._id} created!`)
        return insertedDoc // <-- Don't forget to return objects from post hooks
      },
      (insertedDoc) => {
        // formatting example, removing mongo __v field
        return omit(insertedDoc, ['__v'])
      }
    ]
  }
})

Readme

Keywords

Package Sidebar

Install

npm i monjoi

Weekly Downloads

0

Version

0.0.2

License

BSD-3-Clause

Unpacked Size

14.9 kB

Total Files

11

Last publish

Collaborators

  • rametta