nestjs-typegoose
Advanced tools
Comparing version 7.0.0 to 7.1.0
@@ -9,2 +9,17 @@ # Changelog | ||
## [7.1.0] - 2020-2-11 | ||
### Added | ||
- Mongoose Discriminators. | ||
### Changed | ||
- Updated peer dependency `@nestjs/common` from version `6.3.1` to `6.10.1` | ||
- Updated peer dependency `@nestjs/core` from version `6.3.1` to `6.10.1` | ||
- Updated peer dependency `@nestjs/core` from version `6.0.0` to `6.2.1` | ||
- Updated peer dependency `mongoose` from version `5.5.13` to `5.8.4` | ||
### Fixed | ||
- Fixed typos in the documentation. | ||
- Database shutdown behavior. (PR #54) | ||
- Clear cache of typegoose. (PR #40) | ||
## [7.0.0] - 2019-10-08 | ||
@@ -11,0 +26,0 @@ ### Changed |
@@ -0,0 +0,0 @@ # Contributing |
@@ -0,0 +0,0 @@ export * from './typegoose.decorators'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,3 +1,14 @@ | ||
import { SchemaOptions } from 'mongoose'; | ||
export interface TypegooseClass { | ||
new (...args: any[]): any; | ||
} | ||
export interface TypegooseClassWrapper { | ||
typegooseClass: TypegooseClass; | ||
} | ||
export interface TypegooseClassWithOptions extends TypegooseClassWrapper { | ||
schemaOptions?: SchemaOptions; | ||
discriminators?: (TypegooseClass | TypegooseDiscriminator)[]; | ||
} | ||
export interface TypegooseDiscriminator extends TypegooseClassWrapper { | ||
discriminatorId?: string; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -1,5 +0,5 @@ | ||
import { DynamicModule, OnModuleDestroy } from '@nestjs/common'; | ||
import { DynamicModule, OnApplicationShutdown } from '@nestjs/common'; | ||
import { ModuleRef } from '@nestjs/core'; | ||
import { TypegooseModuleAsyncOptions, TypegooseConnectionOptions } from './typegoose-options.interface'; | ||
export declare class TypegooseCoreModule implements OnModuleDestroy { | ||
export declare class TypegooseCoreModule implements OnApplicationShutdown { | ||
private readonly connectionName; | ||
@@ -12,3 +12,3 @@ private readonly moduleRef; | ||
private static createAsyncOptionsProvider; | ||
onModuleDestroy(): Promise<void>; | ||
onApplicationShutdown(): Promise<void>; | ||
} |
@@ -19,9 +19,12 @@ "use strict"; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) | ||
t[p[i]] = s[p[i]]; | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
var TypegooseCoreModule_1; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var TypegooseCoreModule_1; | ||
const mongoose = require("mongoose"); | ||
const data_1 = require("@typegoose/typegoose/lib/internal/data"); | ||
const common_1 = require("@nestjs/common"); | ||
@@ -31,2 +34,3 @@ const core_1 = require("@nestjs/core"); | ||
const typegoose_utils_1 = require("./typegoose.utils"); | ||
const typegoose_1 = require("@typegoose/typegoose"); | ||
let TypegooseCoreModule = TypegooseCoreModule_1 = class TypegooseCoreModule { | ||
@@ -105,6 +109,12 @@ constructor(connectionName, moduleRef) { | ||
} | ||
async onModuleDestroy() { | ||
async onApplicationShutdown() { | ||
const connection = this.moduleRef.get(this.connectionName); | ||
if (connection) { | ||
await connection.close(); | ||
[...data_1.models.entries()].reduce((array, [key, model]) => { | ||
if (model.db === connection) { | ||
array.push(key); | ||
} | ||
return array; | ||
}, []).forEach(typegoose_1.deleteModel); | ||
} | ||
@@ -111,0 +121,0 @@ } |
@@ -0,0 +0,0 @@ import { Type } from '@nestjs/common'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
export declare const DEFAULT_DB_CONNECTION_NAME = "DefaultTypegooseConnection"; | ||
export declare const TYPEGOOSE_CONNECTION_NAME = "TypegooseConnectionName"; | ||
export declare const TYPEGOOSE_MODULE_OPTIONS = "TypegooseModuleOptions"; |
@@ -0,0 +0,0 @@ "use strict"; |
import { TypegooseClass } from './typegoose-class.interface'; | ||
export declare const InjectModel: (model: TypegooseClass) => (target: Object, key: string | symbol, index?: number) => void; |
@@ -0,0 +0,0 @@ "use strict"; |
import { DynamicModule } from '@nestjs/common'; | ||
import { TypegooseClassWithOptions } from './typegoose.providers'; | ||
import { TypegooseClass } from './typegoose-class.interface'; | ||
import { TypegooseModuleAsyncOptions, TypegooseConnectionOptions } from './typegoose-options.interface'; | ||
import { TypegooseClass, TypegooseClassWithOptions } from './typegoose-class.interface'; | ||
import { TypegooseConnectionOptions, TypegooseModuleAsyncOptions } from './typegoose-options.interface'; | ||
export declare class TypegooseModule { | ||
@@ -6,0 +5,0 @@ static forRoot(uri: string, options?: TypegooseConnectionOptions): DynamicModule; |
@@ -8,4 +8,4 @@ "use strict"; | ||
}; | ||
var TypegooseModule_1; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var TypegooseModule_1; | ||
const common_1 = require("@nestjs/common"); | ||
@@ -12,0 +12,0 @@ const typegoose_core_module_1 = require("./typegoose-core.module"); |
@@ -1,13 +0,7 @@ | ||
import { TypegooseClass } from './typegoose-class.interface'; | ||
import { Connection, SchemaOptions } from 'mongoose'; | ||
export declare type TypegooseClassWithOptions = { | ||
typegooseClass: TypegooseClass; | ||
schemaOptions?: SchemaOptions; | ||
}; | ||
export declare const isTypegooseClassWithOptions: (item: any) => item is TypegooseClassWithOptions; | ||
export declare const convertToTypegooseClassWithOptions: (item: TypegooseClass | TypegooseClassWithOptions) => TypegooseClassWithOptions; | ||
export declare function createTypegooseProviders(connectionName: string, models?: TypegooseClassWithOptions[]): { | ||
provide: string; | ||
useFactory: (connection: Connection) => any; | ||
inject: string[]; | ||
}[]; | ||
import { FactoryProvider } from '@nestjs/common/interfaces'; | ||
import { TypegooseClass, TypegooseClassWithOptions, TypegooseDiscriminator } from './typegoose-class.interface'; | ||
export declare function createTypegooseProviders(connectionName: string, models?: TypegooseClassWithOptions[]): FactoryProvider[]; | ||
declare type ClassOrDiscriminator = TypegooseClassWithOptions | TypegooseDiscriminator; | ||
declare type TypegooseInput = TypegooseClass | ClassOrDiscriminator; | ||
export declare function convertToTypegooseClassWithOptions(item: TypegooseInput): TypegooseClassWithOptions; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const typegoose_1 = require("@typegoose/typegoose"); | ||
const is_class_1 = require("is-class"); | ||
const typegoose_utils_1 = require("./typegoose.utils"); | ||
const typegoose_1 = require("@typegoose/typegoose"); | ||
const isClass = require("is-class"); | ||
exports.isTypegooseClassWithOptions = (item) => isClass(item.typegooseClass); | ||
exports.convertToTypegooseClassWithOptions = (item) => { | ||
if (isClass(item)) { | ||
return { | ||
typegooseClass: item | ||
}; | ||
function createTypegooseProviders(connectionName, models = []) { | ||
const connectionToken = typegoose_utils_1.getConnectionToken(connectionName); | ||
const buildProvider = ({ name }, modelFactory) => ({ | ||
provide: typegoose_utils_1.getModelToken(name), | ||
useFactory: modelFactory, | ||
inject: [connectionToken], | ||
}); | ||
const createDiscriminatorFactoryFrom = (parentFactory) => (discriminatorDefinition) => { | ||
if (isTypegooseClass(discriminatorDefinition)) { | ||
return buildProvider(discriminatorDefinition, (connection) => typegoose_1.getDiscriminatorModelForClass(parentFactory(connection), discriminatorDefinition)); | ||
} | ||
const { typegooseClass, discriminatorId } = discriminatorDefinition; | ||
return buildProvider(typegooseClass, (connection) => typegoose_1.getDiscriminatorModelForClass(parentFactory(connection), typegooseClass, discriminatorId)); | ||
}; | ||
return models.reduce((providers, { typegooseClass, schemaOptions = {}, discriminators = [] }) => { | ||
const modelFactory = (connection) => typegoose_1.getModelForClass(typegooseClass, { existingConnection: connection, schemaOptions }); | ||
const modelProvider = buildProvider(typegooseClass, modelFactory); | ||
const discriminatorProviders = discriminators.map(createDiscriminatorFactoryFrom(modelFactory)); | ||
return [...providers, modelProvider, ...discriminatorProviders]; | ||
}, []); | ||
} | ||
exports.createTypegooseProviders = createTypegooseProviders; | ||
function convertToTypegooseClassWithOptions(item) { | ||
const tcwo = convertToOptions(item); | ||
if (tcwo) { | ||
if (tcwo.discriminators) { | ||
tcwo.discriminators = tcwo.discriminators.map(d => convertToOptions(d) || invalidObject('discriminator')); | ||
} | ||
return tcwo; | ||
} | ||
else if (exports.isTypegooseClassWithOptions(item)) { | ||
return invalidObject('model'); | ||
} | ||
exports.convertToTypegooseClassWithOptions = convertToTypegooseClassWithOptions; | ||
const isTypegooseClass = (item) => is_class_1.isClass(item); | ||
const isTypegooseClassWithOptions = (item) => isTypegooseClass(item.typegooseClass); | ||
function convertToOptions(item) { | ||
if (isTypegooseClass(item)) { | ||
return { typegooseClass: item }; | ||
} | ||
else if (isTypegooseClassWithOptions(item)) { | ||
return item; | ||
} | ||
throw new Error('Invalid model object'); | ||
}; | ||
function createTypegooseProviders(connectionName, models = []) { | ||
return models.map(({ typegooseClass, schemaOptions = {} }) => ({ | ||
provide: typegoose_utils_1.getModelToken(typegooseClass.name), | ||
useFactory: (connection) => typegoose_1.getModelForClass(typegooseClass, { | ||
existingConnection: connection, | ||
schemaOptions | ||
}), | ||
inject: [typegoose_utils_1.getConnectionToken(connectionName)] | ||
})); | ||
} | ||
exports.createTypegooseProviders = createTypegooseProviders; | ||
function invalidObject(type) { | ||
throw new Error(`Invalid ${type} object`); | ||
} |
export declare function getModelToken(model: string): string; | ||
export declare function getConnectionToken(name?: string): string; |
@@ -0,0 +0,0 @@ "use strict"; |
{ | ||
"name": "nestjs-typegoose", | ||
"version": "7.0.0", | ||
"version": "7.1.0", | ||
"description": "A nestjs module wrapper for typegoose", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"typings": "dist/index.d.ts", | ||
"engines": { | ||
"node": ">=8.10.0" | ||
}, | ||
"scripts": { | ||
"build": "gulp build", | ||
"build": "tsc -p tsconfig.build.json", | ||
"lint": "tslint --project tsconfig.json", | ||
"test": "jest", | ||
@@ -14,41 +17,37 @@ "test:ci": "jest --forceExit", | ||
"test:e2e": "jest --config ./test/jest-e2e.json", | ||
"coverage": "yarn run test:cov && cat ./coverage/lcov.info | coveralls" | ||
"test:watch": "jest --watch", | ||
"coverage": "yarn run test:cov && cat ./coverage/lcov.info | coveralls", | ||
"prepare": "npm run build" | ||
}, | ||
"author": "Kyle Pfromer", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/kpfromer/nestjs-typegoose" | ||
}, | ||
"repository": "github:kpfromer/nestjs-typegoose", | ||
"peerDependencies": { | ||
"@typegoose/typegoose": "^6.0.0", | ||
"@nestjs/common": "^6.3.1", | ||
"@nestjs/core": "^6.3.1", | ||
"mongoose": "^5.5.13" | ||
"@nestjs/common": "^6.10.1", | ||
"@nestjs/core": "^6.10.1", | ||
"@typegoose/typegoose": "^6.2.1", | ||
"mongoose": "^5.8.4" | ||
}, | ||
"dependencies": { | ||
"is-class": "^0.0.7", | ||
"is-class": "^0.0.9", | ||
"reflect-metadata": "^0.1.13" | ||
}, | ||
"devDependencies": { | ||
"@typegoose/typegoose": "^6.0.0", | ||
"@nestjs/common": "^6.7.2", | ||
"@nestjs/core": "^6.7.2", | ||
"@nestjs/platform-express": "^6.7.2", | ||
"@nestjs/testing": "^6.7.2", | ||
"@nestjs/common": "^6.10.13", | ||
"@nestjs/core": "^6.10.13", | ||
"@nestjs/platform-express": "^6.10.13", | ||
"@nestjs/testing": "^6.10.13", | ||
"@typegoose/typegoose": "^6.2.1", | ||
"@types/jest": "^24.0.18", | ||
"@types/mongoose": "^5.5.18", | ||
"@types/mongoose": "^5.5.38", | ||
"@types/supertest": "^2.0.8", | ||
"coveralls": "^3.0.6", | ||
"gulp": "^4.0.2", | ||
"gulp-typescript": "^5.0.1", | ||
"coveralls": "^3.0.9", | ||
"jest": "^24.9.0", | ||
"mockgoose": "latest", | ||
"mongoose": "^5.7.1", | ||
"rimraf": "^2.7.1", | ||
"rxjs": "^6.5.3", | ||
"mongodb-memory-server": "^6.2.1", | ||
"mongoose": "^5.8.4", | ||
"rxjs": "^6.5.4", | ||
"supertest": "latest", | ||
"ts-jest": "^24.1.0", | ||
"ts-jest": "^24.2.0", | ||
"tslint": "^5.20.0", | ||
"typescript": "3.4.3" | ||
"typescript": "3.7.4" | ||
}, | ||
@@ -58,3 +57,2 @@ "jest": { | ||
"js", | ||
"json", | ||
"ts" | ||
@@ -61,0 +59,0 @@ ], |
@@ -20,2 +20,4 @@ # nestjs-typegoose | ||
You can checkout the `example` project for more details. | ||
**app.module.ts** | ||
@@ -26,2 +28,3 @@ | ||
import { TypegooseModule } from "nestjs-typegoose"; | ||
import { CatsModule } from "./cat.module.ts"; | ||
@@ -39,3 +42,3 @@ @Module({ | ||
Create class that extends [Typegoose](https://github.com/szokodiakos/typegoose#motivation) | ||
Create class that describes your schema | ||
@@ -86,3 +89,5 @@ **cat.model.ts** | ||
export class CatsService { | ||
constructor(@InjectModel(Cat) private readonly catModel: ReturnModelType<typeof Cat>) {} | ||
constructor( | ||
@InjectModel(Cat) private readonly catModel: ReturnModelType<typeof Cat> | ||
) {} | ||
@@ -107,2 +112,3 @@ async create(createCatDto: { name: string }): Promise<Cat> { | ||
import { CatsService } from "./cats.service"; | ||
import { Cat } from "./cats.model.ts"; | ||
@@ -146,2 +152,39 @@ @Controller("cats") | ||
### Mongoose Discriminators | ||
To add [discriminators](https://mongoosejs.com/docs/discriminators.html) to a model, you may specify a `discriminators` array in the long-form options shown above. | ||
You may either add just the class, or if you need to override the [discriminator key](https://mongoosejs.com/docs/discriminators.html#discriminator-keys) value, an object with `typegooseClass` and `discriminatorId` property. | ||
For example: | ||
```typescript | ||
class Tabby extends Cat { | ||
@prop() | ||
spotted: boolean | ||
} | ||
class BlackCat extends Cat { | ||
@prop() | ||
unlucky: boolean | ||
} | ||
@Module({ | ||
imports: [ | ||
TypegooseModule.forFeature([ | ||
{ | ||
typegooseClass: Cat, | ||
discriminators: [ | ||
Tabby, | ||
{ | ||
typegooseClass: BlackCat, | ||
discriminatorId: 'Black' | ||
} | ||
] | ||
} | ||
]) | ||
] | ||
}) | ||
export class CatsModule {} | ||
``` | ||
### Async Mongoose Schema Options | ||
@@ -227,3 +270,3 @@ | ||
useNewUrlParser: true, | ||
connctionName: "other-mongodb" | ||
connectionName: "other-mongodb" | ||
}), | ||
@@ -257,3 +300,2 @@ CatsModule | ||
uri: configService.getString("MONGODB_URI"), | ||
connectionName: config | ||
// ...typegooseOptions (Note: config is spread with the uri) | ||
@@ -314,6 +356,6 @@ }), | ||
1. @typegoose/typegoose +6.0.0 | ||
2. @nestjs/common +5.0.0 | ||
3. @nestjs/core +5.0.0 | ||
4. mongoose (with typings `@types/mongoose`) +5.1.1 | ||
1. @typegoose/typegoose +6.1.5 | ||
2. @nestjs/common +6.10.1 | ||
3. @nestjs/core +6.10.1 | ||
4. mongoose (with typings `@types/mongoose`) +5.7.12 | ||
@@ -320,0 +362,0 @@ ## License |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
458491
17
356
26
332
1
1
+ Addedis-class@0.0.9(transitive)
- Removedis-class@0.0.7(transitive)
Updatedis-class@^0.0.9