这样的代码也能上线?
发布于 8 年前 作者 yfsoftcom 4943 次浏览 来自 分享

yf-fast-dbm

就是一个简单的sql语句生成的小框架,省去自己重复编写sql的工作

git地址 >>>>>>> https://github.com/yfsoftcom/yf-fast-dbm

解决的一些问题:
  • 屏蔽了一些sql注入的关键字
  • 支持批量插入
  • 使用逻辑删除

node新手,深受OOP(java code 5y+)毒害。没事就爱搞搞这些,好作~~ 欢迎各路大神来喷~

21 回复

yf-fast-dbm

快速极简的orm框架

  • 源码地址: https://github.com/yfsoftcom/yf-fast-dbm
  • 目前支持mysql,使用 jugglingdb
  • 通过delflag实现逻辑删除
  • 默认带有四个字段:id,createAt,updateAt,delflag
  • 支持批量插入
  • TODO:事务,存储过程,mongodb语法

1.Installation

$ npm install yf-fast-dbm

2.API List

  • adapter

获取原生的数据库适配器,可执行自定义的sql来满足一些复杂的业务操作

  • find

通过一组查询、排序、分页的条件筛选一组数据结果。

  • first

通过一组查询、排序、分页的条件筛选一行数据结果。

  • count

通过筛选条件进行统计计数

  • findAndCount

通过一组查询、排序、分页的条件筛选一组数据结果,并返回符合条件的所有数据行数

  • get

通过数据的ID获取到唯一的数据

  • update

修改一些数据

  • remove

删除一条已知的数据

  • clear

通过筛选条件删除一组数据

  • create

添加一条或者多条数据

3.Configuration

模块自带的一些配置信息:

  • Code List 1:
{
    host:'localhost',           //mysql host
    port:3306,                  //mysql port
    database:'test',            //mysql dbname
    username:'root',            //mysql username
    password:'',                //mysql password
    debug:false,                //true:输出jugglingdb生成的sql语句
    showSql:false,              //true:输出本模块生成的sql语句
    pool:{
        connectionLimit:10,     //链接池的配置
        queueLimit:0,
        waitForConnections:true
    }
}

在初始化的时候,可以通过传入的参数覆盖这些默认值

  • Code List 2:
var C = {
    host:'192.168.1.1',
    database:'test',
    username:'root',
    password:'root',
};
var M = require('yf-fast-dbm')(C);

4.Useage

find
  • Code List 3:
// M 的初始化代码请参看 Code List:2
var arg = {
 table: "test",
 condition: "delflag=0",
 fields: "id,article,ptags,product"
};
M.find(arg).then(function (data) {
 // do success here
}).catch(function (err) {
 // do error here
});
first
  • Code List 4:
// M 的初始化代码请参看 Code List:2
var arg = {
 table: "test",
 condition: "delflag=0",
 fields: "id,article,ptags,product"
};
M.first(arg).then(function (data) {
 // do success here
}).catch(function (err) {
 // do error here
});
count
  • Code List 5:
// M 的初始化代码请参看 Code List:2
var arg = {
 table: "test",
 condition: "delflag=0"
};
M.count(arg).then(function (c) {
 // do success here
}).catch(function (err) {
 // do error here
});
findAndCount
  • Code List 6:
// M 的初始化代码请参看 Code List:2
var arg = {
 table: "test",
 condition: "delflag=0",
 fields: "id,article,ptags,product"
};
M.first(arg).then(function (data) {
 // do success here
}).catch(function (err) {
 // do error here
});
get
  • Code List 7:
// M 的初始化代码请参看 Code List:2
var arg = {
 table: "test",
 id: 1
};
M.get(arg).then(function (data) {
 // do success here
}).catch(function (err) {
 // do error here
});
update
  • Code List 8: 修改所有key为test的val为123
// M 的初始化代码请参看 Code List:2
var arg = {
 table: "test",
 condition: "key = 'test'",
    row:{val:"123"}
};
M.update(arg).then(function (data) {
 // do success here
}).catch(function (err) {
 // do error here
});
remove
  • Code List 9:
// M 的初始化代码请参看 Code List:2
var arg = {
 table: "test",
 id: 1
};
M.remove(arg).then(function (data) {
 // do success here
}).catch(function (err) {
 // do error here
});
clear
  • Code List 10:
// M 的初始化代码请参看 Code List:2
var arg = {
 table: "test",
 condition: "delflag=0"
};
M.clear(arg).then(function (data) {
 // do success here
}).catch(function (err) {
 // do error here
});
create
  • Code List 11:
// M 的初始化代码请参看 Code List:2
var arg = {
 table: "test",
 row: {key:"test",val:"mmm"}
};
M.create(arg).then(function (data) {
 // do success here
}).catch(function (err) {
 // do error here
});

or batch insert

  • Code List 12:
// M 的初始化代码请参看 Code List:2
var arg = {
 table: "test",
 row:[{key:"test",val:"mmm"},{key:"test2",val:"mmm2"}]
};
M.create(arg).then(function (data) {
 // do success here
}).catch(function (err) {
 // do error here
});

目前这个代码已经在生产环境运行6个月,表现稳定~ 用起来比较简单,也不用做大量的OR映射,很自由~ 搞起来很带感

如果where条件是多个或者 是嵌套的and/or 怎么写?

@yakczh condition 实际上就是where条件,可以自己随意组织

@yakczh 系统代码会自动添加2个条件 and 1 = 1 and delflag = 0

@yfsoftcom where条件也可以用json
格式1

var where=[
['city','=','010'],
['logintime','>',144334343],
['nickname','like','%xxx%'],
];

格式2

var where={'city|=':'010',
'logintime|>':14433343,
'nickname|like':'%xxxx%'}

这两种格式哪种看起来简单?

@yakczh 格式1会更好 更多考虑到了开发者的体验 我要加入进来 ^_^

@yakczh 对于这些条件是使用 or 或者 and 你有什么好的建议呢?

对与这点我能想到的就是:

var condition = [
	['city','=','010'],
	['logintime','>',144334343],
	['or',['nickname','like','%xxx%']],
];
//解析成
where city = '010' and logintime > 144334343 or nickname like '%xxx%'

@yakczh 已更新:

//condition 字段接受各种格式
//比如下面的格式同样支持
var arg = {
    table: "api_app",
    condition: [
      ['appid' , '=' , '10001'],
      "appkey = '45883198abcdc109'",
      ['or',['appname' ,'=','DEV_Activity']]
    ],
    fields: "id,appid,appname,apptype"
};
//最终解析出来的语句是这样的:
// where ( 1 = 1  and appid='10001' and appkey = '45883198abcdc109' or appname='DEV_Activity') and delflag = 0

@yakczh 修改的源码部分:

function getValue(val){
  //如果是string类型添加单引号
  if(_.isString(val)){
    return "'" + val + "'";
  }
  return val;
}
/**
[
	['city','=','010'],
	['and',['logintime','>',144334343]],
	" password <> '' ",
	['or',['nickname','like','%xxx%']],
]
*/
function parseCondition(where){
  if(_.isEmpty(where)){
    return ' 1 = 1 ';
  }
  if(_.isString(where)){
    return where;
  }
  if(_.isArray(where)){
    var _condition = ' 1 = 1 ';
    var _keywords = ['and','or'];
    var _key,_operater,_value,_logic;
    _.each(where,function(item){
      if(_.isString(item)){
        _condition = _condition + ' and ' + item;
      }else if(_.isArray(item)){
        var _key = item[0];
        if(_.contains(_keywords,_key)){
          //正常的条件语句,通过and进行拼接
          _logic = item[0];
          _key = item[1][0];
          _operater = item[1][1];
          _value = item[1][2];
        }else{
          //正常的条件语句,通过and进行拼接
          _operater = item[1];
          _value = item[2];
          _logic = 'and'
        }
        _logic = ' ' + _logic + ' ';
        _condition = _condition + _logic + _key + _operater + getValue(_value);
      }
    });
    return _condition;
  }
}

mark

来自酷炫的 CNodeMD

标题党。。。

@TakWolf 谢谢,多提提宝贵意见~

@xinyu198736 哈哈 刚开始封装这个的时候,自己确实没想用在生产环境,纯属自己烦写 OR 和 sql 经过一些精进,就在团队中使用开了,也在生产环境中使用到了~

很多这么写的,我记得我们以前导师还写了一下子执行多条语句的,不知道楼主有没有

@NetLogoclub 你是指:多条不相关的sql缓存,然后批量执行么?

@NetLogoclub 如果延迟批量执行 会不会影响其它业务代码来访问这些数据变量?

@yfsoftcom业务没有那么复杂,所以也没有延迟

回到顶部