database
Simple mongoose/mongodb boilerplate with support for model based yaml definitions/plugins, validators, formatters and common mongo models
Plugin model
Simple ES6/Promise based plugin model for loading models into the database. For every project I've run into I have all this setup code that I use and I got tired of doing it. I like being able to define the common models without all the extra usual stuff like importing commonly used formatters, validators, schemas..etc. @nyxtom/database
uses a singleton instance for the repository as well so the way we will setup plugins is as so. Also check out the test/
folder for a test plugin example.
import * as repo from '@nyxtom/database';
import * as testPlugin from './test-plugin';
repo.configure({
dbs: ['test'],
foo: {
connectionString: 'mongodb://localhost/test'
}
});
repo.plugin('foo', testPlugin);
let models = await repo.models('test');
let doc = new models.Foo({
firstName: 'Foo',
lastName: 'Bar',
email: 'test@example.com',
password: 'test123'
});
console.log(doc.name);
doc.save();
in test-plugin/Foo.yml
name: Foo
db: test
schema:
firstName: String
lastName: String
email:
type: String
required: true
unique: true
set: toLowerCase
validator: isEmail
password:
type: String
required: true
bcrypt: true
virtual:
name:
type: String
formatter:
name:
- firstName
- lastName
index:
- email: 1
options:
unique: true
plugins:
- fooNewDocument
- fooHello
test-plugin/virtual-formatters.js
export function name(a, b) {
return `${this[a]} ${this[b]}`;
}
test-plugin/index.js
import path from 'path';
import * as virtualFormatters from './virtual-formatters';
import * as schemaPlugins from './schema-plugins';
const definitions = [
'Foo'
];
async function load(repository) {
repository.addVirtualFormatters(virtualFormatters);
repository.addSchemaPlugins(schemaPlugins);
let promises = definitions.map(async definition => {
await repository.addDefinitionUri(path.resolve(__dirname + `/${definition}.yml`));
});
await Promise.all(promises);
}
export default load;
Alternatively, you can simply export the virtual formatters, schema plugins, set formatters, validators and the definitions as constants rather than use a function to load.
import path from 'path';
import * as virtualFormatters from './virtual-formatters';
import * as schemaPlugins from './schema-plugins';
const definitions = ['Foo'].map(d => {
return path.resolve(__dirname + `/${d}.yml`);
});
export { definitions, virtualFormatters, schemaPlugins };
Repository Manager API
Adding definitions (inside the test-plugin prior to calling repository.plugin
async function load(repository) {
await repository.addDefinitionUri(path.resolve(__dirname + '/Foo.yml'));
}
Adding definitions via url:
async function load(repository) {
await repository.addDefinitionUri('https://www.example.com/Foo.yml');
}
Or alternatively via the export:
export const definitions = [
'https://www.example.com/Foo.yml'
];
Add validators, schemas plugins, virtual formatters, set formatters.
async function load(repository) {
repository.addVirtualFormatters(virtualFormatters);
repository.addSchemaPlugins(schemaPlugins);
repository.addSetFormatters(setFormatters);
repository.addValidators(validators);
}
Or export via consts:
import * as virtualFormatters from './virtual-formatters';
import * as schemaPlugins from './schema-plugins';
import * as setFormatters from './set-formatters';
import * as validators from './validators';
const definitions = [
path.resolve(__dirname + '/Foo.yml')
];
export { virtualFormatters, schemaPlugins, setFormatters, validators, definitions };
Default Plugins
By default the mongoose-bcrypt
plugin is used to handle bcrypt fields. Additional support for other types may be used depending on long term needs. Otherwise, the base minimum has been setup. Additional plugins can be added via repository.addSchemaPlugins
, repository.addVirtualFormatters
, repository.addSetFormatters
, repository.addValidators
.
LICENSE
Copyright (c) 2018 Thomas Holloway
Licensed under the MIT license.