npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

monjoi

v0.0.2

Published

MongoDB + Joi

Downloads

9

Readme

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'])
      }
    ]
  }
})