citong-db

1.5.4 • Public • Published

STOP develop; Move to -> febs-db <-

citong db库用于连接数据库,目前仅支持mysql

exception

定义了常用的错误类型. 异常是如下的对象: {msg, code, filename, line}; 其中code有如下意义

// @desc: 数据查询条件错误。参数语句问题.
exception.DB_ERROR_SQL
// @desc: 数据连接问题.
exception.DB_ERROR_CONNECT
// @desc: 数据执行错误.
exception.DB_ERROR

事务处理中发生异常将自动rollback.

define table.


var citong_table   = require('.').table;
 
class TableDemo extends citong_table {
  constructor(dbclient) {
    super(
      dbclient, // database
      'Admin',  // table name.
      'ID',     // primary key.
      {         // cols.
        ID:       {type: 'integer', size: 8, key: true}, // the auto-incrementing
        Name:     {type: 'text',    size:10},
        NumberCol:{type: 'number',  size: 4},
        IntCol:   {type: 'integer', size: 4},
        IntCol:   {type: 'integer', size: 8}, // big int.
        BoolCol:  {type: 'boolean'}
      }
    );
  }
}

example:

var database  = require('citong-db').database;
 
var db = new database({});
var tableDemo = new TableDemo(db);

combined primary key


var citong_table   = require('.').table;
 
class TableDemo extends citong_table {
  constructor(dbclient) {
    super(
      dbclient, // database
      'Admin',  // table name.
      ['ID', 'IntCol'],     // primary keys.
      {         // cols.
        ID:       {type: 'integer', size: 8},
        Name:     {type: 'text',    size:10},
        NumberCol:{type: 'number',  size: 4},
        IntCol:   {type: 'integer', size: 4, key: true},  // the auto-incrementing
        IntCol:   {type: 'integer', size: 8}, // big int.
        BoolCol:  {type: 'boolean'}
      }
    );
  }
}

connect db.


see mysql pool-options.

var database = require('citong-db').database;
 
var opt = {
  connectionLimit   : 10,
  supportBigNumbers : true,
  bigNumberStrings  : false,
  host              : '',
  port              : 3306,
  user              : '',
  password          : '',
  database          : '',
 
  /* ext */
  queryTimeout      : 5000,
};
var db = new database(opt);
var table = new TableDemo(db);

exist.


function* isExist() {
  let r;
  r = yield table.isExist(1);
  r = yield table.isExistWhere("id=43");
}

query.


function* query() {
  let r;
  r = yield table.queryById(1);
  r = yield table.queryById(1, ['ID','Name']);      // only query 'ID','Name' cols.
 
  r = yield table.queryById({ID:1,IntCol:1}, ['ID','Name']);  // if combined primary key.
 
  r = yield table.queryTop("id = 43");
  r = yield table.queryTop("id = 43", ['ID','Name']);  // only query 'ID','Name' cols.
 
  let where = table.make_condition('id', 43); // == " `id`=43 ".
  where = table.make_condition_like('name', '%dfdfd%'); // == " `name` LIKE '%dfdfd%' ".
  r = yield table.queryTop(where);
 
  r = yield table.queryWhere(where, [0, 100], {ID:true});       // where id = 43 limit 0,100 order by id asc.
  r = yield table.queryWhere(where, ['ID', 'Name']);            // only query 'ID','Name' cols.
  r = yield table.queryWhere(where, [0, 100],  ['ID', 'Name']); // only query 'ID','Name' cols. limit 0,100
  r = yield table.queryWhere(where, {ID:true}, ['ID', 'Name']); // only query 'ID','Name' cols. orderby ID
  r = yield table.queryWhere(where, ['COUNT(ID) as x']);
}

query Lock Row.


function* queryLockRow() {
 
  var conn = yield global.db.getConnection();
  if (conn)
  {
    return yield conn.transaction(function*(){
 
      // lock row id = 1, and unlock after update or exit transaction.
      let r;
      r = yield table.queryLockRow(1, conn);
      // or.
      r = yield table.queryLockRow(1, ['col1','col2'], conn);
 
 
      return false; // will rollback.
      return true;  // will commit.
    });
  }
}

count.


function* query() {
  let r;
  let where = table.make_condition('id', 43); // == " `id`=43 ".
  r = yield table.count(where);
}

add.


function* add() {
  var r = yield table.add({ID:8,...});
}

update.


function* update() {
  var mod = {
    ID: 1,
    name:"name",
    intCol:table.make_update_inc(1),
    intCol2:table.make_origin_sql('`intCol2`+1')
  };
  var r = yield table.update(mod);  // ID=1,name="name",intCol=intCol+1,intCol2=intCol2+1
}

remove.


function* remove() {
  var r = yield table.remove(where);
}

transaction.


function* transaction() {
  var conn = yield global.db.getConnection();
  if (conn)
  {
    return yield conn.transaction(function*(){
      // attach conn.
      console.log((yield table.add(mod, conn)));
 
      mod.id = 1;
      mod.name = 'a1';
      console.log((yield table.update(mod, conn)));
 
      return false; // will rollback.
      return true;  // will commit.
    });
  }
}

Class database_table_base

所有的数据库查询方法都存在相应的同步调用方式, 如: queryWhereSync();


constructor.

/**
@param client: 数据库对象.
@param tablename: 本表名.
@param idKeyName: 本表主键列表, 如果为单主键可以直接为字符串, 如果为联合多主键则需要为数组.
@param model: 本表模型.
*/
*constructor(client, tablename, idKeyName, model)

model的定义格式如下:

{
colName: {type: 'integer', size: 8, key: true}, // the auto-incrementing
...
}

  • colName: 表示列名称

  • type: 表示列类型

    类型 说明 size
    'integer' 整型 指明字节长度
    'text' 字符串 指明字符长度
    'number' 浮点型 指明字节长度
    'boolean' 布尔型 无意义
  • size: 字段长度

  • key: 是否是自增键; (同一个表只能有一个自增键, 当指定多个自增键时, 只认为最后一个为自增)


isExist.

/**
@desc: isExist
*         id is Object if table is combined primary. 
*         the last param can be conn.
@return: boolean.
*/
*isExist( id )
/**
@desc: isExist
*         id is Object if table is combined primary. 
*         the last param can be conn.
@param id, cb
*         - cb: function(err, r:boolean)  {} 
*/
isExistSync( id, cb )

isExistWhere.

/**
@desc: isExitWhere
*         the last param can be conn.
@return: boolean.
*/
*isExistWhere( where )
/**
@desc: isExitWhere
*         the last param can be conn.
@param where, cb
*         - cb: function(err, r:boolrean)  {} 
*/
isExistWhereSync()

count.

/**
@desc: count
*         the last param can be conn.
@param: where
@return: int.
*/
*count()
/**
@desc: count
*         the last param can be conn.
@param: where, cb
*           - cb: function(err, ret:int)  {} 
*/
countSync()

add.

/**
@desc: add
*         the last param can be conn.
*         (insertId will set to item.id)
@return: bool
*/
*add( item )
/**
@param cb: cb(err, r:boolean)  {} 
@return: void
*/
addSync( item, cb )

remove.

/**
@desc: remove
*         the last param can be conn.
@return: bool.
*/
*remove( where )
/**
@param cb: cb(err, r:boolean)  {} 
@return:.
*/
removeSync( where, cb )

update.

/**
@desc: update;  where id = item.id
*         if item.id is existed, sql condition is: 'id=value' AND (where)
*         otherwise sql condition is: where 
*         the last param can be conn.
@param item, where.
@return: boolean.
*/
*update( item )
/**
@desc: update;  where id = item.id
*         if item.id is existed, sql condition is: 'id=value' AND (where)
*         otherwise sql condition is: where 
*         the last param can be conn.
@param item, where, cb.
*         - cb: function(err, r:boolrean) {} 
@return:.
*/
updateSync( item )

queryById.

/**
@desc: query by id.
*         id is Object if table is combined primary. 
*         the last param can be conn.
@param: id, [query_cols]
*           query_cols: [col1,col2], the cols will be query.
@return: mod.
*/
*queryById( id )
/**
@desc: query by id.
*         id is Array if table is combined primary.
*         the last param can be conn.
@param: id, [query_cols], cb
*          - query_cols: [col1,col2], the cols will be query.
*          - cb: function(err, ret:mod) {} 
*/
queryByIdSync( id )

queryLockRow.

/**
@desc: query by id and lock row for update (use in transaction).
*         id is Object if table is combined primary.
*         the last param can be conn.
@param: id, [query_cols]
*           query_cols: [col1,col2], the cols will be query.
@return: mod.
*/
*queryLockRow( id )
/**
@desc: query by id and lock row for update (use in transaction).
*         id is Object if table is combined primary.
*         the last param can be conn.
@param: id, [query_cols], cb
*           - query_cols: [col1,col2], the cols will be query.
*           - cb: function(err, ret:mod)  {} 
@return: mod.
*/
queryLockRowSync( id )

queryTop.

/**
@desc: query top.
*         the last param can be conn.
@param: where, {orderby}, [query_cols]
*           orderby: {key:true/false} true-means asc, false-means desc.
*           query_cols: [col1,col2], the cols will be query.
@return: mod.
*/
*queryTop( where )
/**
@desc: query top.
*         the last param can be conn.
@param: where, {orderby}, [query_cols], cb
*           - orderby: {key:true/false} true-means asc, false-means desc.
*           - query_cols: [col1,col2], the cols will be query.
*           - cb: function(err, ret:mod)  {} 
*/
queryTopSync( where )

queryWhere.

/**
@desc: query
*         the last param can be conn.
@param: where, [offset,limit], {orderby}, [query_cols]
*           - orderby: {key:true/false} true means asc, false means desc.
*           - query_cols: [col1,col2], the cols will be query. e.g. ['id', 'name']
@return: [mod,mod,...].
*/
*queryWhere( where )
/**
@desc: query
*         the last param can be conn.
@param: where, [offset,limit], {orderby}, [query_cols]
*           - orderby: {key:true/false} true means asc, false means desc.
*           - query_cols: [col1,col2], the cols will be query. e.g. ['id', 'name']
*           -cb:  function(err, ret:Array)  {} 
*/
queryWhereSync( where )

get_conn.

/**
@desc: 获得最后一个参数,如果为 database_connection则返回.
@return:
*/
get_conn(arguments)

escape.

/**
@desc: escape
@return: str.
*/
escape( v )

make_condition.

/**
@desc: 构造一个 key=value的sql条件语句.
@return: sql;
*/
make_condition( key, value )

make_condition_not_equal.

/**
@desc: 构造一个 key<>value的sql条件语句.
@return: sql;
*/
make_condition_not_equal( key, value )

make_condition_more.

/**
@desc: 构造一个 key>value的sql条件语句.
@return: sql;
*/
make_condition_more( key, value )

make_condition_more_equal.

/**
@desc: 构造一个 key>=value的sql条件语句.
@return: sql;
*/
make_condition_more_equal( key, value )

make_condition_less_equal.

/**
@desc: 构造一个 key<=value的sql条件语句.
@return: sql;
*/
make_condition_less_equal( key, value )

make_condition_less.

/**
@desc: 构造一个 key<value的sql条件语句.
@return: sql;
*/
make_condition_less( key, value )

make_condition_like.

/**
@desc: 构造一个 key LIKE value的sql条件语句.
@return: sql;
*/
make_condition_like( key, value )

make_update_inc.

/**
@desc: 用于表明update时字段自增n.
*/
make_update_inc( n )

make_update_inc.

/**
@desc: 用于表明使用原始v, 不做安全检验.
*/
make_origin_sql( v )

Dependencies (0)

    Dev Dependencies (3)

    Package Sidebar

    Install

    npm i citong-db

    Weekly Downloads

    47

    Version

    1.5.4

    License

    MIT

    Last publish

    Collaborators

    • brainpoint