Security News
npm Updates Search Experience with New Objective Sorting Options
npm has a revamped search experience with new, more transparent sorting options—Relevance, Downloads, Dependents, and Publish Date.
data-mapper
Advanced tools
npm install data-mapper
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}"
}
}
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');
These some methods(similar to mongodb) are auto generated by default map configuration.
...
userDao.$findOne({userId: 1000}, function(err, context) {
if(err) {
...
} else {
var aUser = context.result();
console.log(aUser.userName);
}
});
...
userDao.$find({isAdmin: 1}, function(err, context) {
if(err) {
...
} else {
var adminList = context.result();
...
}
});
...
userDao.$count({isAdmin: 1}, function(err, context) {
if(err) {
...
} else {
var numberOfAdmin = context.result();
...
}
});
...
userDao.$save({userId: 100001, userName: 'new user name', isAdmin: 1, userPhone: 'phone number'}, function(err, context) {
if(err) {
...
} else {
var savedResult = context.result();
...
}
});
...
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();
...
}
});
...
userDao.$remove({userId: 100001}, function(err, context) {
if(err) {
...
} else {
var removedResult = context.result();
...
}
});
userDao.getUserByIds({list: [10000, 100001] }, function(err, context) {
if(err) {
...
} else {
var userList = context.result();
...
}
});
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
mybatis-like data mapper for javascript
The npm package data-mapper receives a total of 2 weekly downloads. As such, data-mapper popularity was classified as not popular.
We found that data-mapper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
npm has a revamped search experience with new, more transparent sorting options—Relevance, Downloads, Dependents, and Publish Date.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.