# nestjs-type-mongodb

> A nest.js module for type-mongodb

Latest version **2.0.0-beta.4** (published 2022-12-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-type-mongodb
pnpm add nestjs-type-mongodb
yarn add nestjs-type-mongodb
bun add nestjs-type-mongodb
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0-beta.4 |
| Published | 2022-12-01 |
| First published | 2021-01-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 36.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Jordan Stout |
| Maintainers | jrdn |
| Keywords | orm, odm, mongo, mongodb, ts, typescript, javascript, entity, ddd, data-mapper, type-mongodb, nestjs, nest, nest.js |

## Links

- npm: https://www.npmjs.com/package/nestjs-type-mongodb
- Repository: https://github.com/j/nestjs-type-mongodb
- Homepage: https://github.com/j/nestjs-type-mongodb#readme
- Issues: https://github.com/j/nestjs-type-mongodb/issues
- npm.io page: https://npm.io/package/nestjs-type-mongodb

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 2.0.0-beta.4 (latest) — 2022-12-01
- 2.0.0-beta.3 — 2022-12-01
- 2.0.0-beta.2 — 2022-12-01
- 2.0.0-beta.1 — 2021-06-01
- 2.0.0-beta.0 — 2021-05-19
- 1.0.0-beta.1 — 2021-01-26
- 1.0.0-beta.0 — 2021-01-25

## README

<h1 align="center" style="border-bottom: none;">nestjs-type-mongodb</h1>
<p align="center">
    A <a href="https://github.com/j/type-mongodb">type-mongodb</a> module for <a href="https://github.com/nestjs/nest">Nest.js</a>.
</p>

<p align="center">
    <a href="https://www.npmjs.com/~jrdn" target="_blank"><img src="https://img.shields.io/npm/v/nestjs-type-mongodb.svg" alt="NPM Version" /></a>
    <a href="https://www.npmjs.com/~jrdn" target="_blank"><img src="https://img.shields.io/npm/l/nestjs-type-mongodb.svg" alt="Package License" /></a>
    <a href="https://www.npmjs.com/~jrdn" target="_blank"><img src="https://img.shields.io/npm/dm/nestjs-type-mongodb.svg" alt="NPM Downloads" /></a>
</p>

## Installation

```bash
$ yarn add nestjs-type-mongodb
```

## Basic usage

**app.module.ts**

```typescript
import { Module } from "@nestjs/common";
import { TypeMongoDBModule } from 'nestjs-type-mongodb';
import { DogsModule } from "./dog.module.ts";

@Module({
  imports: [
    TypeMongoDBModule.forRoot({
      connection: {
        uri: 'mongodb://localhost:31000/animals?replicaSet=test',
        database: 'animals'
      }
    }),
    DogsModule
  ]
})
export class AppModule {}
```

Create class that describes your schema

**dog.model.ts**

```typescript
import { Document, Id, Field } from 'type-mongodb';
import { ObjectId } from 'mongodb';

@Document()
export class Dog {
  @Id()
  _id: ObjectId;

  @Field()
  name: string;

  @Field()
  age: number;

  @Field()
  breed: string;
}
```

Inject Dog for `DogsModule`

**dog.module.ts**

```typescript
import { Module } from '@nestjs/common';
import { TypeMongoDBModule } from 'nestjs-type-mongodb';
import { DogsController } from './dogs.controller';
import { DogsService } from './dogs.service';
import { Dog } from './models/dog';

@Module({
    imports: [
        TypeMongoDBModule.forFeature([Dog])
    ],
    controllers: [DogsController],
    providers: [DogsService]
})
export class DogsModule { }
```

Get the dog repository in a service

**dogs.service.ts**

```typescript
import { Injectable } from '@nestjs/common';
import { InjectRepository } from 'nestjs-type-mongodb';
import { Repository } from 'type-mongodb';
import { CreateDogDto } from './dto/create-dog.dto';
import { Dog } from './models/dog';

@Injectable()
export class DogsService {
    constructor(
        @InjectRepository(Dog)
        private readonly repository: Repository<Dog>
    ) { }

    async create(dog: CreateDogDto): Promise<Dog> {
        return this.repository.create(dog);
    }

    async findAll(): Promise<Dog[]> {
        return this.repository.find().toArray();
    }
}
```


## Test

```bash
# e2e tests
$ npm run test:e2e
```

## Stay in touch

- Author - [Jordan Stout](https://github.com/j)

## License

`nestjs-type-mongodb` is [MIT licensed](LICENSE).

---
_Source: https://npm.io/package/nestjs-type-mongodb · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
