fastify-mongoose-driver
Advanced tools
Comparing version 1.0.0 to 1.0.1
81
index.js
@@ -6,3 +6,59 @@ "use strict"; | ||
async function mongooseConnector(fastify, { uri, settings, models = [] }) { | ||
const fixReferences = (fastify, schema) => { | ||
Object.keys(schema).forEach(key => { | ||
if (schema[key].type === "ObjectId") { | ||
schema[key].type = mongoose.Schema.Types.ObjectId; | ||
if (schema[key].validateExistance) { | ||
delete schema[key].validateExistance; | ||
schema[key].validate = { | ||
isAsync: true, | ||
validator: (v, cb) => { | ||
decorator[schema[key].ref] | ||
.findById(v) | ||
.then(() => { | ||
cb(true); | ||
}) | ||
.catch(() => { | ||
cb( | ||
false, | ||
`${schema[key].ref} with ID ${v} does not exist in database!` | ||
); | ||
}); | ||
} | ||
}; | ||
} | ||
} else if (schema[key].length !== undefined) { | ||
schema[key].forEach(member => { | ||
if (member.type === "ObjectId") { | ||
member.type = mongoose.Schema.Types.ObjectId; | ||
if (member.validateExistance) { | ||
delete member.validateExistance; | ||
member.validate = { | ||
isAsync: true, | ||
validator: (v, cb) => { | ||
decorator[member.ref] | ||
.findById(v) | ||
.then(() => { | ||
cb(true); | ||
}) | ||
.catch(() => { | ||
/* istanbul ignore next */ | ||
cb(false, `Post with ID ${v} does not exist in database!`); | ||
}); | ||
} | ||
}; | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
async function mongooseConnector( | ||
fastify, | ||
{ uri, settings, models = [], useNameAndAlias = false } | ||
) { | ||
await mongoose.connect( | ||
@@ -19,7 +75,20 @@ uri, | ||
models.forEach(model => { | ||
decorator[ | ||
model.alias | ||
? model.alias | ||
: `${model.name[0].toUpperCase()}${model.name.slice(1)}` | ||
] = mongoose.model(model.name, new mongoose.Schema(model.schema)); | ||
fixReferences(decorator, model.schema); | ||
if (useNameAndAlias) { | ||
if (model.alias === undefined) | ||
throw new Error(`No alias defined for ${model.name}`); | ||
decorator[model.alias] = mongoose.model( | ||
model.alias, | ||
new mongoose.Schema(model.schema), | ||
model.name | ||
); | ||
} else { | ||
decorator[ | ||
model.alias | ||
? model.alias | ||
: `${model.name[0].toUpperCase()}${model.name.slice(1)}` | ||
] = mongoose.model(model.name, new mongoose.Schema(model.schema)); | ||
} | ||
}); | ||
@@ -26,0 +95,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"A Fastify plugin to connect to a MongoDB instance via the Mongoose ODM", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"main": "index.js", | ||
@@ -8,0 +8,0 @@ "scripts": { |
@@ -1,4 +0,4 @@ | ||
# Fastify CockroachDB Plugin using Sequelize ORM | ||
# Fastify MongoDB Plugin using Mongoose ODM | ||
[![NPM](https://nodei.co/npm/fastify-mongoose.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/fastify-mongoose/) | ||
[![NPM](https://nodei.co/npm/fastify-mongoose-driver.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/fastify-mongoose-driver/) | ||
@@ -10,3 +10,3 @@ [![CircleCI](https://circleci.com/gh/alex-ppg/fastify-mongoose.svg?style=svg)](https://circleci.com/gh/alex-ppg/fastify-mongoose) | ||
```bash | ||
npm i fastify-mongoose -s | ||
npm i fastify-mongoose-driver -s | ||
``` | ||
@@ -19,3 +19,3 @@ | ||
fastify.register( | ||
require("fastify-mongoose"), | ||
require("fastify-mongoose-driver"), | ||
{ | ||
@@ -31,2 +31,16 @@ uri: "mongodb://admin:pass@localhost:27017/database_name", | ||
{ | ||
name: "posts", | ||
alias: "Post", | ||
schema: { | ||
title: { | ||
type: String, | ||
required: true | ||
}, | ||
content: { | ||
type: String, | ||
required: true | ||
} | ||
} | ||
}, | ||
{ | ||
name: "accounts", | ||
@@ -55,2 +69,10 @@ alias: "Account", | ||
}, | ||
// We can add references to other Schemas like-so | ||
posts: [ | ||
{ | ||
type: "ObjectId", | ||
ref: "Post", | ||
validateExistance: true | ||
} | ||
], | ||
createdAtUTC: { | ||
@@ -62,3 +84,4 @@ type: Date, | ||
} | ||
] | ||
], | ||
useNameAndAlias: true | ||
}, | ||
@@ -96,2 +119,4 @@ err => { | ||
Keep in mind that, if an `"ObjectId"` is specified as the `type`, the referenced `Schema` must have been defined first in the `models` input array of the library. | ||
## Author | ||
@@ -98,0 +123,0 @@ |
24
test.js
@@ -20,2 +20,16 @@ "use strict"; | ||
{ | ||
name: "posts", | ||
alias: "Post", | ||
schema: { | ||
title: { | ||
type: String, | ||
required: true | ||
}, | ||
content: { | ||
type: String, | ||
required: true | ||
} | ||
} | ||
}, | ||
{ | ||
name: "accounts", | ||
@@ -44,2 +58,9 @@ alias: "Account", | ||
}, | ||
posts: [ | ||
{ | ||
type: "ObjectId", | ||
ref: "Post", | ||
validateExistance: true | ||
} | ||
], | ||
createdAtUTC: { | ||
@@ -51,3 +72,4 @@ type: Date, | ||
} | ||
] | ||
], | ||
useNameAndAlias: true | ||
}); | ||
@@ -54,0 +76,0 @@ |
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
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
47565
186
123