Skip to content

Commit

Permalink
All tests pass. Compliance with request /alex-ppg/fastify-mongoose/is…
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoBruni committed Feb 6, 2024
1 parent 641958d commit d47a8e2
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { join } from 'path';
import fp from 'fastify-plugin';
import mongoose from 'mongoose';

let decorator: TFMPPlugin;
let decoratorPlugin: TFMPPlugin;

const initPlugin: FastifyPluginAsync<TFMPOptions> = async (
fastify: FastifyInstance,
Expand All @@ -23,14 +23,14 @@ const initPlugin: FastifyPluginAsync<TFMPOptions> = async (
}: TFMPOptions
) => {
await mongoose.connect(uri, settings);
decorator = { instance: mongoose } as unknown as TFMPPlugin;
decoratorPlugin = { instance: mongoose } as unknown as TFMPPlugin;

if (modelDirPath)
models = [...(await loadModelsFromPath(modelDirPath)), ...models];

if (models.length !== 0) {
models.forEach(model => {
fixReferences(decorator, model.schema);
fixReferences(decoratorPlugin, model.schema);

const schema = new mongoose.Schema(model.schema, model.options);

Expand All @@ -43,13 +43,13 @@ const initPlugin: FastifyPluginAsync<TFMPOptions> = async (
if (model.alias === undefined)
throw new Error(`No alias defined for ${model.name}`);

decorator[model.alias] = mongoose.model(
decoratorPlugin[model.alias] = mongoose.model(
model.alias,
schema,
model.name
);
} else {
decorator[
decoratorPlugin[
model.alias
? model.alias
: model.name.charAt(0).toUpperCase() +
Expand All @@ -65,7 +65,7 @@ const initPlugin: FastifyPluginAsync<TFMPOptions> = async (
app.mongoose.instance.connection.close();
});

fastify.decorate('mongoose', decorator);
fastify.decorate('mongoose', decoratorPlugin);
};

const loadModelsFromPath = async (
Expand Down Expand Up @@ -147,3 +147,7 @@ const plugin = fp(initPlugin, {
});

export default plugin;

const decorator = () => decoratorPlugin;

export { decorator };

0 comments on commit d47a8e2

Please sign in to comment.