# nestjs-papr

> Papr integration for NestJS

Latest version **1.2.1** (published 2024-01-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-papr
pnpm add nestjs-papr
yarn add nestjs-papr
bun add nestjs-papr
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2024-01-24 |
| First published | 2023-05-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=16.0.0 |
| Dependencies | 4 |
| Unpacked size | 47.4 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Vitaliy Grusha |
| Maintainers | grushav |
| Keywords | nestjs, mongodb, database, model, schema, json, json-schema, validation, typescript, types |

## Links

- npm: https://www.npmjs.com/package/nestjs-papr
- Repository: https://github.com/vitaliy-grusha/nestjs-papr
- Issues: https://github.com/vitaliy-grusha/nestjs-papr/issues/new
- npm.io page: https://npm.io/package/nestjs-papr

## Dependencies (4)

- [papr](https://npm.io/package/papr.md) ^15.1.0
- [rxjs](https://npm.io/package/rxjs.md) ^7.8.1
- [@nestjs/core](https://npm.io/package/@nestjs/core.md) ^10.3.1
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^10.3.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.2.1 (latest) — 2024-01-24
- 1.2.0 — 2023-09-14
- 1.1.1 — 2023-08-21
- 1.1.0 — 2023-08-21
- 1.0.0 — 2023-07-30
- 0.0.6 — 2023-07-18
- 0.0.5-a — 2023-06-19
- 0.0.4-a — 2023-06-19
- 0.0.3-a — 2023-06-19
- 0.0.2-a — 2023-05-23

## README

# nestjs-papr

## Description

nestjs-papr is a module that integrates the [Papr](https://github.com/plexinc/papr) library with the Nest Framework.

It provides a way to define Papr models and use them in your Nest modules through dependency injection. This integration allows you to easily work with Papr models in your Nest application.

Supports:

* single MongoDB connection
* multiple MongoDD connections
* passing custom mongo client options
* passing custom database options
* passing custom collection options
* sync and async configuration
* automatic indexes creation

## Getting started

### Instalation

```sh
# npm
npm install nestjs-papr

# pnpm
pnpm add nestjs-papr

# yarn
yarn install nestjs-papr
```

### Usage

1. Import ParpModule for root

```ts
@Module({
  imports: [
    PaprModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => {
        const { MONGODB_URI, MONGODB_DATABASE_NAME } =
          configService.getConfig();
        return {
          uri: MONGODB_URI,
          databaseName: MONGODB_DATABASE_NAME,
          autoIndex: true, // Create indexes automatically
          autoSchema: true, // Create schema validation automatically
        };
      },
      inject: [ConfigService],
    }),
  ]
})
```

2. Define your model

```ts
// user.model.ts

import { model, schema, types } from 'nestjs-papr';

export const UserSchema = schema(
  {
    _id: types.objectId({ required: true }),
    firstName: types.string({ required: true }),
  },
  {
    timestamps: true,
  },
);
export type UserDoc = (typeof UserSchema)[0];
export type UserProps = Omit<UserDoc, '_id' | 'createdAt' | 'updatedAt'>;
export type UserCollection = 'users'
export const UserModelDef = model(UserCollection, UserSchema, { 
  indexes: [
    // Optional indexes here (IndexDescription[] type in mongodb);
  ],
  collectionOptions: {
    // Optional collectionOptions here (CollectionOptions type in mongodb);
  }
});

```

3. Provide your model for feature

```ts
@Module({
  imports: [
    PaprModule.forFeature([
      UserModelDef,
    ]),
  ]
})
```

4. Inject model into service and use it's power

```ts
// module.service.ts

import { Injectable } from '@nestjs/common';
import { InjectModel } from 'nestjs-papr';
import { UserModelDef, UserDoc, UserProps } from './models/user.model';

@Injectable()
export class ModuleService {
  constructor(
    @InjectModel(UserModelDef)
    private readonly userModel: typeof UserModelDef.model,
  ) {}
  
  async createUser(userProps: UserProps): Promise<UserDoc> {
    return await this.userModel.insertOne({ ...userProps });
  }
}

```

### Decorators

`InjectModel = (modelDef: ModelDef, connectionName?: string)` - inject model

`InjectPapr = (connectionName: string)` - inject Papr instance

`InjectConnection = (name: string)` - inject MongoClient instance

### Configuration

```ts
export interface PaprModuleOptions {

  // MongoDB connection uri
  uri?: string;
  
  // Connection name 
  connectionName?: string;
  
  // MongoDB database name
  databaseName?: string;
  
  // Custom MongoClient options
  mongoClientOptions?: MongoClientOptions;
  
  // Custom database options
  databaseOptions?: DbOptions;
  
  // Custom options for papr instance
  paprOptions?: ModelOptions;
  
  // Number of attempts to connect to database
  retryAttempts?: number;
  
  // Number of delay between attempts
  retryDelay?: number;
  
  // Whether to create indexes automatically (default: false)
  autoIndex?: boolean;
  
  // Whether to schema validation automatically  (default: false)
  autoSchema?: boolean;
  
  connectionFactory?: (connection: any, name: string) => any;
  connectionErrorFactory?: (error: Error) => Error;
}
```

## License

nestjs-papr is [MIT licensed](LICENSE).

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