Skip to content

Commit

Permalink
fix: improve the providing of discriminator models
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas committed Dec 10, 2020
1 parent 78fd9a0 commit cd11e48
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions lib/mongoose.providers.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
import { Provider } from '@nestjs/common';
import { Connection, Document, Model } from 'mongoose';
import { getConnectionToken, getModelToken } from './common/mongoose.utils';
import {
AsyncModelFactory,
DiscriminatorOptions,
ModelDefinition,
} from './interfaces';

function addDiscriminators(
model: Model<Document>,
discriminators: DiscriminatorOptions[] = [],
) {
discriminators.forEach(({ name, schema }) =>
model.discriminator(name, schema),
);
}
import { AsyncModelFactory, ModelDefinition } from './interfaces';

export function createMongooseProviders(
connectionName?: string,
Expand All @@ -23,7 +10,12 @@ export function createMongooseProviders(
return options.reduce(
(providers, option) => [
...providers,
...createMongooseProviders(connectionName, option.discriminators),
...(option.discriminators || []).map((d) => ({
provide: getModelToken(d.name),
useFactory: (model: Model<Document>) =>
model.discriminator(d.name, d.schema),
inject: [getModelToken(option.name)],
})),
{
provide: getModelToken(option.name),
useFactory: (connection: Connection) => {
Expand All @@ -32,7 +24,6 @@ export function createMongooseProviders(
option.schema,
option.collection,
);
addDiscriminators(model, option.discriminators);
return model;
},
inject: [getConnectionToken(connectionName)],
Expand All @@ -58,19 +49,16 @@ export function createMongooseAsyncProviders(
schema,
option.collection,
);
addDiscriminators(model, option.discriminators);
return model;
},
inject: [getConnectionToken(connectionName), ...(option.inject || [])],
},
// discriminators must convert to `AsyncModelFactory`.
// Otherwise, the discriminators will register as `Model` before `model.discriminator` and throw OverwriteModelError
...createMongooseAsyncProviders(
connectionName,
(option.discriminators || []).map<AsyncModelFactory>(
({ name, schema }) => ({ name, useFactory: async () => schema }),
),
),
...(option.discriminators || []).map((d) => ({
provide: getModelToken(d.name),
useFactory: (model: Model<Document>) =>
model.discriminator(d.name, d.schema),
inject: [getModelToken(option.name)],
})),
];
}, [] as Provider[]);
}

0 comments on commit cd11e48

Please sign in to comment.