nestjs-papr
Advanced tools
Comparing version 0.0.6 to 1.0.0
{ | ||
"name": "nestjs-papr", | ||
"version": "0.0.6", | ||
"version": "1.0.0", | ||
"description": "Papr integration for NestJS", | ||
@@ -33,4 +33,4 @@ "author": "Vitaliy Grusha", | ||
"dependencies": { | ||
"@nestjs/common": "^10.1.0", | ||
"@nestjs/core": "^10.1.0", | ||
"@nestjs/common": "^10.1.2", | ||
"@nestjs/core": "^10.1.2", | ||
"papr": "^14.0.0", | ||
@@ -40,2 +40,4 @@ "rxjs": "^7.8.1" | ||
"devDependencies": { | ||
"@nestjs/platform-express": "^10.1.2", | ||
"@nestjs/testing": "^10.1.2", | ||
"@types/jest": "29.5.1", | ||
@@ -46,4 +48,4 @@ "@types/node": "18.16.12", | ||
"@typescript-eslint/parser": "^5.62.0", | ||
"eslint": "^8.45.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"eslint": "^8.46.0", | ||
"eslint-config-prettier": "^8.9.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
@@ -50,0 +52,0 @@ "jest": "29.5.0", |
169
README.md
@@ -1,5 +0,172 @@ | ||
# :warning: The project is under development (not for production) | ||
# 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 | ||
## 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 const UserModelDef = model(UserCollection, UserSchema, { | ||
indexes: [ | ||
// Optional indexes here (IndexDescription[] type); | ||
], | ||
collectionOptions: [ | ||
// Optional collectionOptions here (CollectionOptions type); | ||
] | ||
}); | ||
``` | ||
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 attepts 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). |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
48370
36
507
0
173
19
Updated@nestjs/common@^10.1.2
Updated@nestjs/core@^10.1.2