Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

data-mapper

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

data-mapper

mybatis-like data mapper for javascript

  • 0.1.17
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

data-mapper

  • mybatis-like data mapper for javascript

Installation

npm install data-mapper

Usage

Settings

data-mapper-conf.json

    {
        "connectors": {
            "mysql": {
                "type": "mysql",
                "connectionLimit": 5,
                "host": "localhost",
                "user": "test",
                "password": "test",
                "database": "test",
                "default": true
            }
        },
        "mappers": {
            "user": "test/conf/mapper/user-mapper.json5",
            "group": "test/conf/mapper/group-mapper.json",
            ...
        }
    }

user-mapper.json5

    {
        "maps": {
            "default": {
                "table": "t_user",
                "columns": {
                    "id": { "key": true, "model": "userId" },
                    "name": "userName",
                    "phone": "userPhone",
                    "is_admin": { "model": "isAdmin" },
                    "create_time": { "model": "created", "type": "createTime" },
                    "update_time": { "model": "updated", "type": "updateTime" }
                }
            },
            "userDetail": {
                "table": "t_user",
                "columns": {
                    "id": { "key": true, "model": "userId" }
                }
            }
        },
        "queries": {
            "getUserById": "SELECT * FROM t_user WHERE id = {{id}}",
            "getUsers": "SELECT * FROM t_user",
            "modifyUser": "UPDATE t_user \
                {#set} \
                    {#if name} name = {{name}}, {/if} \
                    {#if phone} phone = {{phone}}, {/if} \
                {/set} \
                WHERE id = {{id}}",
            "deleteUserById": "DELETE FROM t_user WHERE id = {{id}}",
            "deleteAllUser": "DELETE FROM t_user",
            "addUser": "INSERT INTO t_user VALUES ({{id}}, {{name}}, {{phone}}, 0, now(), now())",
            "getUserByIds": "SELECT * FROM t_user \
            {#where} \
                {#if list} \
                    {#foreach item=it, collection=list, sep=',', open='id IN (', close=')' } \
                        {{it}} \
                    {/foreach} \
                {/if} \
            {/where}"
        }
    }

Initialize

var dataMapperConf = require('./conf/data-mapper-conf.json');
var dataMapper = require('./../lib/data-mapper').init(dataMapperConf);

// get dao named 'user' from dataMapper
var userDao = dataMapper.dao('user');

Use Default Methods of Dao

These some methods(similar to mongodb) are auto generated by default map configuration.

$findOne
...
userDao.$findOne({userId: 1000}, function(err, context) {
    if(err) {
        ...
    } else {
        var aUser = context.result();
        console.log(aUser.userName);
    }
});
$find
...
userDao.$find({isAdmin: 1}, function(err, context) {
    if(err) {
        ...
    } else {
        var adminList = context.result();
        ...
    }
});
$find
...
userDao.$count({isAdmin: 1}, function(err, context) {
    if(err) {
        ...
    } else {
        var numberOfAdmin = context.result();
        ...
    }
});
$save
...
userDao.$save({userId: 100001, userName: 'new user name', isAdmin: 1, userPhone: 'phone number'}, function(err, context) {
    if(err) {
        ...
    } else {
        var savedResult = context.result();
        ...
    }
});
$update
...
userDao.$update({userId: 100001, $set: { userName: 'updated user name', isAdmin: 0, userPhone: 'updated phone number'} }, function(err, context) {
    if(err) {
        ...
    } else {
        var updatedResult = context.result();
        ...
    }
});
$remove
...
userDao.$remove({userId: 100001}, function(err, context) {
    if(err) {
        ...
    } else {
        var removedResult = context.result();
        ...
    }
});

Use custom methods

userDao.getUserByIds({list: [10000, 100001] }, function(err, context) {
    if(err) {
        ...
    } else {
        var userList = context.result();
        ...
    }
});

Transaction

You can run transactional tasks with a simpler manner.

var dataMapperConf = require('./conf/data-mapper-conf.json');
var dataMapper = require('./../lib/data-mapper').init(dataMapperConf);

var userDao = dataMapper.dao('user');
var Transaction = dataMapper.Transaction;

var transaction = new Transaction([
    userDao.$find({name: 'userName'}),
    function (context, next) {
        var user = context.result();
        if (!user) {
            next('unknown user');
        } else {
            context.data('userId', user.userId);
            next();
        }
    },
    function(context, next) {
        var id = context.data('userId');
        userDao.$remove({userId: id}, next, context);
    },
    userdao.$save({userId: genId(), userName: 'newUser', userPhone: 'phone#'})
]);
transaction.start(function (err, context) { 
    if(err) {
        // handle error
    } else {
        console.log('success');
    }
});

FAQs

Package last updated on 24 Dec 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc