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

data-mapper

v0.1.17

Published

mybatis-like data mapper for javascript

Downloads

12

Readme

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');
    }
});