Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

Commit

Permalink
Use existingMongoose in buildSchema
Browse files Browse the repository at this point in the history
Not doing it makes my project with Mongoose v5 crash. The fix can be useful to anybody
that doesn't use Typegoose's mongoose version
  • Loading branch information
Florian Simon committed Jan 26, 2018
1 parent 3e0fe65 commit 1fba30d
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/typegoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ export class Typegoose {
const name = this.constructor.name;

// get schema of current model
let sch = this.buildSchema(name, schemaOptions);
let sch = this.buildSchema(existingMongoose || mongoose, name, schemaOptions);
// get parents class name
let parentCtor = Object.getPrototypeOf(this.constructor.prototype).constructor;
// iterate trough all parents
while (parentCtor && parentCtor.name !== 'Typegoose' && parentCtor.name !== 'Object') {
// extend schema
sch = this.buildSchema(parentCtor.name, schemaOptions, sch);
sch = this.buildSchema(existingMongoose || mongoose, parentCtor.name, schemaOptions, sch);
// next parent
parentCtor = Object.getPrototypeOf(parentCtor.prototype).constructor;
}
Expand All @@ -57,8 +57,8 @@ export class Typegoose {
return models[name] as ModelType<this> & T;
}

private buildSchema(name: string, schemaOptions, sch?: mongoose.Schema) {
const Schema = mongoose.Schema;
private buildSchema(mongooseInstance, name: string, schemaOptions, sch?: mongoose.Schema) {
const Schema = mongooseInstance.Schema;

if (!sch) {
sch = schemaOptions ?
Expand Down

0 comments on commit 1fba30d

Please sign in to comment.