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

rubik-mongoose

v2.4.0

Published

Mongoose's kubik for rubik

Downloads

18

Readme

rubik-mongoose

Mongose's kubik for rubik

Install

npm

npm i mongoose
npm i rubik-mongoose

yarn

yarn add mongoose
yarn add rubik-mongoose

Use

const { App, Kubiks } = require('rubik-main');
const Mongoose = require('rubik-mongoose');
const path = require('path');

// create rubik app
const app = new App();
// config need for most modules
const config = new Kubiks.Config(path.join(__dirname, './config/'));
// you can use any logger you want, just create kubik with it
// default Kubiks.Log use console for logging
const log = new Kubiks.Log();
// argument is a path to the models or schemas directory
const storage = new Mongoose(path.join(__dirname, './storage/models/'));

// Add some extensions if you need
storage.use({
  name: 'User',
  schema: require('./storage/schemas/User.js'),
  collection: 'Users'
});
storage.use(path.join(__dirname, './additional-models/'))

app.add([ config, log, storage ]);

app.up().
then(() => console.info('App started')).
catch(err => console.error(err));

Config

storage.js config in configs volume should contain connection — is a connection options and options — native driver settings.

For example: config/storage.js

module.exports = {
  connection: {
    host: 'localhost',
    port: '27017',
    database: 'test',
    username: null,
    password: null,
    options: null
  },
  options: {
    poolSize: 15,
    keepAlive: 500,
    autoReconnect: true,
    reconnectTries: 120,
    reconnectInterval: 1000,
    useNewUrlParser: true
  }
};
/**
 * Some additional available options for ”connection“ section:
  members: [
    '127.0.0.1',
    '127.0.0.2',
    '127.0.0.3',
  ],
  options: {
    replicaSet: 'rs0'
  }
 * for ”options“ section: http://mongodb.github.io/node-mongodb-native/3.1/reference/connecting/connection-settings/
 */

Extensions

  1. One object with model's desctiption:
storage.use({
  name: 'User',
  schema: require('./storage/schemas/User.js'),
  collection: 'Users'
});
  • name — model name for mongoose.models
  • schema — mongoose Schema instance
  • collection — optional, MongoDB collection name
  1. Array of model's objects
storage.use([{
  name: 'User',
  schema: require('./storage/schemas/User.js'),
  collection: 'Users'
}, {
  name: 'UserTag',
  schema: require('./storage/schemas/UserTag.js')
}]);
  1. Single path to directory with models
storage.use(path.join(__dirname, './storage/models/'));
  1. Array of paths to directory with models
storage.use([
  path.join(__dirname, './storage/models/'),
  path.join(__dirname, './additional-models/')
]);
  1. Mixed array of paths and models
storage.use([
  {
    name: 'UserTag',
    schema: require('./storage/schemas/UserTag.js')
  },
  path.join(__dirname, './additional-models/')
]);

Use kubik with different name, or kubiks with different databases

By default Mongoose kubik has storage name, and the same config filename.

If you need different name, or use two Mongoose kubiks with different databases, you can change name of kubik.

const pathToModels = path.join(__dirname, './storage/models/');

const storage = new Mongoose(pathToModels);

const first = new Mongoose(pathToModels);
first.name = 'first';

const second = new Mongoose(pathToModels);
second.name = 'second';

app.add([storage, first, second]);
// now you should create first.js and second.js files in config directories, and set configuration for different connections

Add plugins for Models

You can add one or more mongooses plugins for all models, or selected ones.

For all

// plugin is a mongoose plugin function
storage.plugin(plugin);

For selected models

// plugin is a mongoose plugin function
// models is a set of names
storage.plugin({ plugin, models })

For one model

// plugin is a mongoose plugin function
// model is a name of model
storage.plugin({ plugin, model })

You should add all your plugins before models.