{"id":18483194,"url":"https://github.com/swimlane/mongtype","last_synced_at":"2025-04-08T18:32:27.230Z","repository":{"id":19893814,"uuid":"88194310","full_name":"swimlane/mongtype","owner":"swimlane","description":"🚀  MongoDB Repository Pattern for Node written in TypeScript","archived":false,"fork":false,"pushed_at":"2023-04-18T09:55:19.000Z","size":1242,"stargazers_count":65,"open_issues_count":8,"forks_count":10,"subscribers_count":31,"default_branch":"master","last_synced_at":"2024-04-14T20:06:40.548Z","etag":null,"topics":["hacktoberfest","mongo","mongodb","nodejs","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/swimlane.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-13T18:23:52.000Z","updated_at":"2024-06-20T00:08:26.150Z","dependencies_parsed_at":"2024-06-20T00:08:07.623Z","dependency_job_id":"5446c00e-a46f-499c-90fe-621f79ffba4a","html_url":"https://github.com/swimlane/mongtype","commit_stats":{"total_commits":97,"total_committers":11,"mean_commits":8.818181818181818,"dds":"0.44329896907216493","last_synced_commit":"87861c79230c6eb94f0c02cc5e989024c310d365"},"previous_names":[],"tags_count":35,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swimlane%2Fmongtype","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swimlane%2Fmongtype/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swimlane%2Fmongtype/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swimlane%2Fmongtype/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/swimlane","download_url":"https://codeload.github.com/swimlane/mongtype/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247902455,"owners_count":21015451,"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":["hacktoberfest","mongo","mongodb","nodejs","typescript"],"created_at":"2024-11-06T12:34:49.887Z","updated_at":"2025-04-08T18:32:22.220Z","avatar_url":"https://github.com/swimlane.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# MongType\n\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/4435e55cddf24b0f98831c3ae34c960d)](https://www.codacy.com/app/Swimlane/mongtype?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=swimlane/mongtype\u0026amp;utm_campaign=Badge_Grade) [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/4435e55cddf24b0f98831c3ae34c960d)](https://www.codacy.com/app/Swimlane/mongtype?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=swimlane/mongtype\u0026utm_campaign=Badge_Coverage) [![Build Status](https://travis-ci.org/swimlane/mongtype.svg?branch=master)](https://travis-ci.org/swimlane/mongtype) [![npm version](https://badge.fury.io/js/mongtype.svg)](https://badge.fury.io/js/mongtype)\n\nMongoDB Repository pattern for NodeJS written in TypeScript.\n\n## Install\n\n`npm i mongtype --S`\n\n## Building\n\n`npm run build`\n\n## Usage\n\n[Migrating from 2.X to 3.X](UPGRADE.md)\n[Migrating from 1.X to 2.X](UPGRADE.md)\n\n### DatabaseClient\n\nThe `DatabaseClient` class provides a wrapper around a mongodb connection\nIt has a single method called `connect` that allows you to provide a uri to a MongoDB instance and optionally include an already established `MongoClient`.\n\n```typescript\nimport { DatabaseClient } from 'mongtype';\n\nconst uri = 'mongodb://localhost/Foo';\nconst dbc = new DatabaseClient();\n\n// DatabaseClient manages the connection\ndbc.connect(uri);\n\n// DatabaseClient reuses an existing connection\ndbc.connect(uri, mongoClient);\n```\n\n### Using DI\n\n```javascript\nimport { MongoRepository, Collection, Pre, Post } from 'mongtype';\nimport { Injectable } from 'injection-js';\n\ninterface User {\n  name: string;\n}\n\n@Injectable()\n@Collection({\n  name: 'user',\n  capped: true,\n  size: 10000\n})\nexport class UserRepository extends MongoRepository\u003cUser\u003e {\n  @Before('save')\n  doSomethingBeforeSave() {}\n\n  @After('save')\n  doSomethingAfterSave() { }\n}\n\n@Injectable()\nexport class App {\n  constructor(private userRepo: UserRepository) { }\n\n  async findUsers() {\n    const one = await this.userRepo.findById('3434-34-34343-3434');\n    const many = await this.userRepo.find({ conditions: { name: 'foo' } });\n    const newOne = await this.userRepo.create({ foo: true });\n    const updated = await this.userRepo.save(newOne);\n  }\n}\n```\n\n### Without DI\n\n```javascript\nconst dbc = new DatabaseClient();\ndbc.connect('mongodb://your.mongo.url'); // optional existing connection as second arg\nconst svc = new UserRepository(dbc);\n\nconst one = await svc.findById('3434-34-34343-3434');\nconst many = await svc.find({ conditions: { name: 'foo' } });\nconst newOne = await svc.create({ foo: true });\nconst updated = await svc.save(newOne);\n```\n\n## Similar\n\n- [ts-mongo](https://github.com/joesonw/ts-mongo/)\n\n## Credits\n\nMongType is a [Swimlane](http://swimlane.com) open-source project; we believe in giving back to the open-source community by sharing some of the projects we build for our application. Swimlane is an automated cyber security operations and incident response platform that enables cyber security teams to leverage threat intelligence, speed up incident response and automate security operations.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswimlane%2Fmongtype","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswimlane%2Fmongtype","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswimlane%2Fmongtype/lists"}