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

express-mongoose-helper

v1.0.11

Published

simply for your express/mongoose app with autoloaded models that are accessed through the express app instance.

Downloads

6

Readme

express-mongoose-helper

adds app.model() function and app.model[ModelName] to express.

install


npm install --save express-mongoose-helper

basic info


module signature

	function( 
		Express.Application app, //the express application
		Object options //options for usage with the library. see options below.
	)

app.model function signature

for legacy reasons, schemaOptions and callback may be given in either order, or not at all.

	app.model( 
		String name, //name of the model
		Object schema, //plain object defining a mongoose schema.
		Object schemaOptions, // optional schemaOptions to pass to the mongoose schema constructor
		Function callback //optional a function callback that receives the generated mongoose schema. for adding indexes, static methods, instance methods, plugins to model.
	)

model file signature (Note: you can edit what is injected to the required module, these are the defaults.)

each model file should export a function of the following signature.

	function(
		Express.Application app, //the Express app
		mongoose.Schema.Types Types,//the mongoose.Schema.Types object for convenience/brevity.
	)

basic usage

app.js


var app = require('express')(); // 1. include express

require('express-mongoose-helper')(app,{
	path: __dirname + '/models/',
	connectionString: 'mongodb://localhost/test',
	debug: true
});

//if you want to load your controllers,start your app, any other action requiring the models to be loaded/defined
// you may listen for the 'mongoose.models.ready' event like so:
app.on('mongoose.models.ready',function(){

    // your models are now available on the express app
    const dog = new app.model.Dog({
        name: 'Rowdy',
        owner: null
    });

    dog.save().then(dog=>{
        console.log(dog.name); // Rowdy
    })
	
	//load your controllers here, so that models are available.
	
	app.listen(3000);//start up our server
	
});

models/User.js

//an example mongoose model in express-mongoose-helper
//your module should export a function like so.
//you can customize which arguments are injected, the defaults are shown below.
module.exports = function( app, Types ){

	//creates a mongoose model named user and adds it to the express app.
	app.model('User',{
		name: {
			first: String,
			middle: String,
			last: String,
		},
		email: String,
		referredBy: { type: Types.ObjectId, ref: 'User', default: null }
	},function(schema){
		//here you can add indexes, static methods, instance methods, plugins etc to the schema.
		schema.index({ email: 1},{unique: true});
	});
	
	// now you can access the model like `app.model.User` from your controllers.

};

new in version 1.0.7:

  • module exports a function called standalone for creating a standalone instance without express, useful for background scripts or with other server environments
  • when constructing models with app.model() the model will now be returned. the model will also contain a function you can use to get the mongoose instance.
  • callback to app.model() will now receive mongoose as a second argument in addition to schema.

new standalone feature


const {standalone} = require('express-mongoose-helper');

const app = standalone({
	path: __dirname + '/models/',
	connectionString: 'mongodb://localhost/your-db',
	debug: true
});

app.on('mongoose.models.ready',function(){
	
	// your backend code here
	
});

changes to app.model

const model = app.model('Test',{foo:String},(schema,mongoose)=>{
    //modify the schema here
    //you can use mongoose here too
});
const mongoose = model.mongoose();// do something with mongoose

new in version 1.0.2, app.model supports a 4th parameter, schemaOptions

please note, to avoid breaking backwards compatability schemaOptions and callback can be given in either order. both are optional.

module.exports = function( app, Types ){
	
	app.model('UploadedFile',{
		path: String,
		owner: { type: Types.ObjectId, ref: 'User' },
		mimetype: String,
		filesize: Number,
	}, { timestamps: true },function(schema){
	
		schema.index({ filesize: 1 });
	
	});

};

options


//default options
options = {
	connectionString: '',// mongodb connection string
	connectionOptions: {},// mongodb connection options
	path: '',// path to folder containing models
	inject: ['app','types'], //arguments to inject into each required model
	debug: false, //show log messages
	log: function(){
		return this.debug && console.log.apply(console,arguments);
	}, //default logging function, can be changed
	extend: function(mongoose){} //extend mongoose with global plugins, custom types, etc.
};

events


several events are emitted. they are listed below.

  • mongoose.models.ready = emitted when all the model files have been loaded and added to the app.
  • mongoose.connected = emitted when database connection is established.
  • mongoose.disconnected = emitted when database is disconnected.
  • mongoose.error = emitted when an error occurs establishing connection to mongodb.

testing


No testing is done yet.