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-orm-mvc

v1.1.0

Published

Express + ORM MVC

Downloads

7

Readme

express-orm-mvc

NPM version Dependency Status

Coverage Status Code Climate

codeship

As everyone the 1st time I start to use express, there was many problem i have to solve as

  • Database
  • App structure
  • Router
  • Separate modules
  • ...

So hope this library help someone like me.

Any ideas are appreciated.

##Features

  • MVC
  • This library just help you to structure your code and scale up later
  • No Express or ORM hack
  • Config Express and ORM by yourself (Fully control)

##Dependencies

By default:

You can specify those dependencies version by option, please refer to this

##Installation

npm install express-orm-mvc --save

Database package

npm install <your database package>

//example
npm install mysql

Refer to ORM document Connecting to Database

##App structure

/
	models/						-- all of your models here
	controllers/				-- all of your controllers here
	views/
	config/
		express.js				-- your express config
		orm.js					-- your orm config
		routes.js				-- router
		settings.js				-- app settings (ip, port, database, ...)
	app.js						-- root

Please check example folder

##How to use

Please check example folder or follow these document

###Init

require(express-orm-mvc)(function(err){
	if(err) {
		console.log(err);
		return;
	}
	console.log('done');
});

###Models

A model file should be like this

module.exports = function (orm, db) {
    //define your orm model here
};

Example:

models/post.js
module.exports = function (orm, db) {
	var Post = db.define('post', {
		title:      { type: 'text' },
		content:    { type: 'text' }
    });
};

Check ORM document Defining Models

####Note:

express-orm-mvc loads models by alphabet order.

For example comment model has one post (many-to-one relationship) as post_id field, so post model must be loaded before comment model.

Solution: name the file models name as 0_post.js and comment.js.

You can check example

###Controllers

A controller file should be like this

module.exports = {
    //define your controller here
};

Example:

controllers/post.js
module.exports = {
	home: function(req, res, next){
		res.send('home page');
	},
    get: function(req, res, next) {
        req.models.post.find(function(err, data) {
            res.send(data);
        });
    },
    create: function(req, res, next) {
        req.models.post.create({
            title: 'title',
            content: 'content'
        }, function(err, result) {
            res.send(result);
        });
    }
};

Note: you can list all of your models in req.models, check more here

###Settings

config/settings.js

A settings file should be like this

module.exports = {
    mode1: { //development
        ip: <ip>,
        port: <port>,
        db: // orm database setting object
    },
    mode2: { //production
        ip: <ip>,
        port: <port>,
        db: // orm database setting object
    }
};

Example:

module.exports = {
    development: {
        ip: '127.0.0.1',
        port: 8080,
        db: {
            host: '127.0.0.1',
            port: 3306,
            protocol: 'mysql',
            user: 'root',
            password: '123456789',
            database: 'express-orm-mvc-test',
            connectionLimit: 100
        }
    },
    production: {
        ip: '127.0.0.1',
        port: 8080,
        db: {
            host: '127.0.0.1',
            port: 3306,
            protocol: 'mysql',
            user: 'root',
            password: '123456789',
            database: 'express-orm-mvc-test',
            connectionLimit: 100
        }
    }
};

Note: You should set your NODE_ENV variable (development or production), or you can by pass by send directly the mode option when init, check here

Check ORM document Connecting to Database

###Express config

config/express.js

A express config file should be like this

module.exports = function(app, express) {
    //any express config here
};

Example:

module.exports = function(app, express) {
    app.set('title', 'testing');
    app.set('views', '../views');
	app.set('view engine', 'ejs');
    app.use(express.favicon());
};

Check Express document api

Note:

  • As you see there is no views folder in app structure, so create and manage by yourself
  • Library will start a server automatically, so no need this kind of this stuff
http.createServer(app).listen(function(){});

###ORM config

config/orm.js

A orm config file should be like this

module.exports = function(orm, db) {
    //any orm config here
};

Example:

module.exports = function(orm, db) {
    db.settings.set('test', 'testing data');
};

Check ORM document Settings

Note: Library will sync database automatically.

###Routes config

config/routes.js

A routes config file should be like this

module.exports = function(app, controllers) {
	//routes here
};

Example:

module.exports = function(app, controllers) {
    app.get(    '/'       , controllers.post.home);
    app.get(    '/post'   , controllers.post.get);
    app.post(   '/post'   , controllers.post.create);
};

##Options

require(express-orm-mvc)({
	mode: 'development',           //default: production
	path: __dirname,               //default: auto detect
    express: require('express'),   //specify your express version
    orm: require('orm')            //specify your orm version
}, callback);

Example:

var express = require('express')    // Express 4
var orm = require('orm')            // ORM 2.1.0

require(express-orm-mvc)({
    mode: 'development',
    path: '/Code/Project',
    express: express,
    orm: orm
}, callback);

##Return object

express

orm

server web server instance

database orm database instance

app express app instance

settings the current settings

mode the current mode

require(express-orm-mvc)(functiom(err, mvc) {
    mvc.express;
    mvc.orm;
    mvc.server;
    mvc.database;
    mvc.app;
    mvc.settings;
    mvc.mode;
});

##Notes

For your convenience, you can get

  • models: all the orm models
  • settings: the running setings
  • mode: the running mode

###from express req

function (req, res, next) {
    req.models;
    req.settings;
    req.mode;
}

###from express config file

//config/express.js
module.exports = function(app, express, mvc) {
    mvc.mode
    mvc.settings
};

###from orm config file

//config/orm.js
module.exports = function(orm, db, mvc) {
    mvc.mode
    mvc.settings
};