data-mapper-js

A lightweight data access tool supporting Postgres and MySql today.

npm install data-mapper-js
4 downloads in the last month

Okay, so what is it?

data-mapper-js is a little bit more than massive-js, but not so much that it gets in your way. Initially I was going to try and just extend massive-js to meet my needs, but my approach seemed different enough from that of massive-js that I gave birth to data-mapper-js. Currently it supports MySql and Postgres, but you should find it easily extensible, if you are so inclined.

How do I get started?

I wasn't going to, but I went ahead and registered the package so installation is as easy as:

npm install data-mapper-js

Then just this to start using it:

var mapper = require("data-mapper-js");

The important part, if you are trying to wire up any relational data queries, is to define the schema.

var schemaBuilder = mapper.schema();
    schemaBuilder
        .add("customers", "customerid")
            .hasManyWithMany("addresses", "customeraddresses", "addressid")
            .hasOne("profile", "profiles")
            .save()
        .add("addresses", "addressid").save()
        .add("profiles").save();

And lastly tell it what engine you want to use and how to connect to the database:

var db = mapper.init("mysql", connString, schemaBuilder.schema);

Querying

The query syntax is borrowed almost exactly from massive-js, but we don't build the tables for you.

A quick query to pull all the records in the customers table.

db.find().in("customers").execute(function(err, result) {
    console.log(result[0].name);
});

You can also pass in one or more parameters.

db.find({name: "brad", email: "bradley.teller@gmail.com"}).in("customers").execute(function(err, result) {
    console.log(result[0].name);
});

You can also tell it to load up the addresses for records matching your query.

db.find({name: "bobcat"}).in("customers").load("addresses").execute(function(err, result) {
    console.log(name[0].addresses.length);
});

Inserts

Nothing really special here.

db.insert({name: "new", email: "test@test.com"}).in("customers").execute(function(err, result) {
    //do something here
});

Updates

Yeah, you can update stuff as well.

db.update({name: "updated"}).in("customers").where({"customerid >": 1}).execute(function(err, result) {
    //do something here
});

Deletes

And just in case you have to get rid of some things you can do that too.

db.delete({"customerid >": 2}).in("customers").execute(function(err, result) {
    assert.equal(err, null);
    done();        
});
npm loves you