GithubHelp home page GithubHelp logo

moma's Introduction

moma

What

Model mapper for node.js

Why

We need an easy way to map a set of fields from one model to another. It could be the data model we fetched from the database that we want exposed to the web layer in a limited fashion. Or it could be the form data submitted from a web page that we want to massage in a model suitable for the database. Or it can be any other model you can think of.

Quick Example

Suppose your registration page posts data in the following format:

{
        "usr": "joe",
        "email": "[email protected]"
    	"creditcard": "1234 5678 9012 3456"
}

And you plan to store the username and the email in your database but your DAO layer expects something like:

{
    Username: ...
    Email: ...
}

Obviously you need to map one model to another. There are multiple ways to accomplish this but this is moma's way:

var dbModel = moma.map(viewModel)

For this to work though you need a mapping file like this:

{
    "global": {
		"mappings": {
			"usr": "Username",
            "email": "Email
		}
	}
}

Or even better, if you use moma as a middleware, just do

var moma = require('moma').middleware();
...
app.use(moma);
...

And your req.body will look like this:

{
	"Username":"joe",
	"Email":"[email protected]"
}

This is especially useful when the HTML is developed by another team, the field names might change and you don't want to change your code.

Similarly when exposing a REST service, you might want to filter out some of the fields extracted from the database. Also you may want to shorten/rename some fields in the JSON output. Moma will help you with that too.

moma's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.