{"id":22744238,"url":"https://github.com/rametta/monjoi","last_synced_at":"2025-03-30T04:43:40.475Z","repository":{"id":57302445,"uuid":"344952409","full_name":"rametta/monjoi","owner":"rametta","description":"MongoDB + Joi","archived":false,"fork":false,"pushed_at":"2021-03-08T14:28:50.000Z","size":24,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-28T23:53:01.449Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rametta.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-05T22:41:38.000Z","updated_at":"2021-03-11T13:53:20.000Z","dependencies_parsed_at":"2022-09-20T13:38:11.747Z","dependency_job_id":null,"html_url":"https://github.com/rametta/monjoi","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rametta%2Fmonjoi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rametta%2Fmonjoi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rametta%2Fmonjoi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rametta%2Fmonjoi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rametta","download_url":"https://codeload.github.com/rametta/monjoi/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246277351,"owners_count":20751548,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-11T01:41:18.188Z","updated_at":"2025-03-30T04:43:40.455Z","avatar_url":"https://github.com/rametta.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm](https://img.shields.io/npm/v/monjoi.svg)](http://npm.im/monjoi)\n\n# 🐦 Monjoi\n\n\u003e Monjoi - A lightweight alternative to [Mongoose](https://mongoosejs.com/).\n\n## Features\n\n- Natively written in [TypeScript](https://www.typescriptlang.org/)\n- Schema's with [Joi](https://joi.dev)\n- Pre and Post Hooks (a.k.a Plugins)\n- Pagination _// TODO_\n- Partial Schema Validation for Updates _// TODO_\n- Automatic Timestamp insertion / updating _// TODO_\n\n## Install\n\n```sh\nnpm i monjoi\n\n# or with Yarn\n\nyarn add monjoi\n```\n\n\\* Monjoi has peer dependencies on [Joi](https://joi.dev) and [node mongodb driver](https://www.npmjs.com/package/mongodb).\n\n## Examples\n\n### Creating a collection.\n\n1. First declare the **Schema** and **Type**, then export the **Data Access Object** _(DAO)_ and **Type**.\n\n```ts\n// person.model.ts\nimport Joi from 'joi'\nimport { collection } from 'monjoi'\n\nexport type Person = {\n  name: string\n  age?: number\n}\n\nconst PersonSchema = Joi.object\u003cPerson\u003e({\n  name: Joi.string().required(),\n  age: Joi.number()\n})\n\n// Person - Data Access Object\nexport const PersonDAO = collection('person', PersonSchema)\n```\n\n2. Connect DAO's to the DB\n\n```ts\n// TODO: Example needed\n```\n\n3. Dependency Inject your new DAO into any service to get access to the collection.\n\n```ts\n// person.service.ts\nimport { Person, PersonDAO } from './person.model.ts'\n\nclass PersonService {\n  constructor(\n    // Dependency Injected `personDAO`\n    private personDAO: PersonDAO\n  ) {}\n\n  public async createPerson(person: Person): Promise\u003cPerson\u003e {\n    // Automatically validates `person` against Joi schema\n    // and returns the newly inserted mongo document with _id\n    return this.personDAO.insertOne(person)\n  }\n}\n```\n\n### Hooks (Plugins)\n\nThere are 2 types of hooks available:\n\n1. Pre Hooks - Functions that execute **before** running the db access\n2. Post Hooks - Functions that execute **after** running the db access\n\nHooks can return regular values or even Promises.\n\n#### Pre hooks\n\nUseful 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.\n\n#### Post hooks\n\nUseful for formatting db responses or triggering other operations. Post hooks should generally **return** objects because they will be passed to the next hook.\n\nAny hook that **returns a rejected promise** will cancel the hook chain.\n\n#### Hook Examples\n\nHooks are declared when defining your DAO. You can hook into any standard mongodb collection [operation](http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html).\n\n```ts\nexport const PersonDAO = collection('person', PersonSchema, {\n  insertOne: {\n    pre: [\n      () =\u003e console.log('I will run before any insertOne operation for person collection'),\n      () =\u003e console.log('me too!')\n    ],\n    post: [\n      (insertedDoc) =\u003e {\n        console.log(`Document ${insertedDoc._id} created!`)\n        return insertedDoc // \u003c-- Don't forget to return objects from post hooks\n      },\n      (insertedDoc) =\u003e {\n        // formatting example, removing mongo __v field\n        return omit(insertedDoc, ['__v'])\n      }\n    ]\n  }\n})\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frametta%2Fmonjoi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frametta%2Fmonjoi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frametta%2Fmonjoi/lists"}