npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nyxtom/database

v1.2.1

Published

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

Downloads

30

Readme

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: '[email protected]',
    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.