Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nyxtom/database

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nyxtom/database

Simple mongoose/mongodb/graphql boilerplate with support for model based yaml definitions/plugins, graphql, validators, formatters and common mongo models

  • 1.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
increased by50%
Maintainers
1
Weekly downloads
 
Created
Source

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); // Foo Bar
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: # middleware formatter + arguments to pass
        - 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';

/**
 * List of definition files to load.
 */
const definitions = [
    'Foo'
];

/**
 * For each model name in definitions, calls addDefinitionUri for the
 * resolved path in './${definition}.yml'
 * @typedef {import('../../src/repository-manager').RepositoryManager} RepositoryManager
 *
 * @param {RepositoryManager} repository - Database repository with methods for adding definitions/schemas
 */
async function load(repository) {
    repository.addVirtualFormatters(virtualFormatters);
    repository.addSchemaPlugins(schemaPlugins);

    let promises = definitions.map(async definition => {
        // TODO: use special asset loading to make sure we work in the browser
        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.

FAQs

Package last updated on 12 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc