2014年11月22日 星期六

mysql module support pool query

應該是新辦本提供的功能,之前沒看到...

var mysql = require('mysql');  var pool  = mysql.createPool({    connectionLimit : 10,    host            : 'example.org',    user            : 'bob',    password        : 'secret'  });    pool.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {    if (err) throw err;      console.log('The solution is: ', rows[0].solution);  });

2014年7月16日 星期三

同步你的前端資料與後端資料 - 自建object filter

在Node.js/JavaScript中,因為弱型別加上沒有物件導向的概念
常常在開發NoSQL儲存的時候會有許多資料不一致的問題
下面是我常用的做法,給大家參考:

var _ = require('underscore'); 
//建立user model
var user = {
  username: {
    type: "string", require: true, max: 200
  },
  password: {
    type: "string", require: true, max: 200
  },
  nickname: {
    type: "string", require: false, max: 200
  },
  phone:{
    type: "string", require: true, max: 200
  },
  lang: {
    type: "string", require: true, max: 200, pick: ['cht', 'en']
  },
  mobile:{
    type: "string", require: true, max: 200,
    map: function(v) {
      return "886" + Number(v); //format number
    }
  }
}

//建立通用的filter function
function filter(vo, model) {
  var modelKeys = Object.keys(model);
  var out = {};
  for(var i = 0; i< modelKeys.length; i++) {
    var k = modelKeys[i];
    var v = model[k];
    //check required
    if(v.require)
      if(!vo[k]) throw "value of key [" + k + "] not found";
 
    //check vo has value
    if(Object.keys(vo).indexOf(k)>=0){
      out[k] = vo[k];
      if(v.type) {
        if(typeof(vo[k]) != v.type) throw "type of [" + k + "] is not correct"
      }
      //check max
      if(v.max)
        if(vo[k] && vo[k].length > v.max) throw "value of [" + k + "] over the column max size";
       
      //check value in the pick range
      if(v.pick && vo[k]) {
        if(_.indexOf(v.pick, vo[k]) < 0) throw "the value of [" + k + "] is not in the pick range"
      }
      //check execute map function
      if(v.map && vo[k])
        out[k] = v.map(vo[k]);
    }
  }
  return out;
}

透過上面設定的model的各項參數,讓filter function可以統一過濾所有的物件
把該留的欄位留下來,也可以做到過濾內容等等的功能...

使用範例: 
var vo = {
  "username": "xxx@gmail.com",
  "password": "test111",
  "nickname": "xxx",
  "phone":"02234567",
  "mobile":"0912345678",
  "address":"台北市內湖區堤頂大道二段",
  "lang": "cht",
  "created": 1403063028138,
  "user_type": "0",
  "uuid": "0e7d6d4d6-e3a0b9ea9ca7"
}
console.log(filter(vo, user));

2014年5月3日 星期六

Node.js / JavaScript處理含逗號的CSV格式字串

這幾天遇到一個棘手的問題
我需要處理一個CSV-like的字串
字串是逗號隔開的CSV格式
數字部分不加單引號、其他格式使用單引號隔開...
而字串中欄位有可能出現逗號(,)或是跳脫的單引號(\')
範例:
1,'aaa,bbb,ccc','asdf','a12\'3\'4'
2,'aab,bbb,ccc','asdf','b12\'3\'4'
如果你跟我一樣,一開始就以Parser的角度,split後去處理文字....
或是想到Regular Expression去...
那應該頭痛一天也不會有答案...
我最後的答案是...... (沒有看不懂的Regular Expression, 也沒有逐字去拆解分析...)
file: data.csv
> 1,'aaa,bbb,ccc','asdf','a12\'3\'4'

file: test.js
> var fs = require('fs');
> var data = fs.readFileSync('/tmp/data.csv', 'utf-8');
> data.split('\n').forEach(function(v) {
>   if(v.length >0) {
>     console.log(parseArr(v));
>   }
> });
> function parseArr(v) {
>   eval('var arr = [' + v + ']');
>   return arr;
> }
關鍵是紅色的那行
透過javascript的eval方式將字串組織回來... 

2014年1月10日 星期五

Node.js call by reference實驗

操作node.js時候,針對變數操作call by value或reference的一些迷思...來證明一下...

新增加a與b物件,兩者賦予同樣的值,但是實際物件並不相等
$ node
> a={aaa:111,bbb:333}
{ aaa: 111, bbb: 333 }
> b={bbb:333,aaa:111}
{ bbb: 333, aaa: 111 }
> a===b
false
> a==b
false
>

物件內容給予string也是一樣...
> a={aaa:'111',bbb:'222'}
{ aaa: '111', bbb: '222' }
> b={aaa:'111',bbb:'222'}
{ aaa: '111', bbb: '222' }
> a
{ aaa: '111', bbb: '222' }
> b
{ aaa: '111', bbb: '222' }
> a==b
false
> a===b
false
>

實際上物件是以call by reference存在...
因此如果使用b=a賦予b值,則兩個物件實際上是同一個...
> a={aaa:111,bbb:222}
{ aaa: 111, bbb: 222 }
> b=a
{ aaa: 111, bbb: 222 }
> a==b
true
> a===b
true
>

證明call by reference...
如果用b賦予a值,則當a改變時候
b也會跟著改變
> a=b
{ aaa: 333, bbb: 222 }
> a
{ aaa: 333, bbb: 222 }
> b
{ aaa: 333, bbb: 222 }
> a.ccc=123
123
> a
{ aaa: 333, bbb: 222, ccc: 123 }
> b
{ aaa: 333, bbb: 222, ccc: 123 }
> a==b
true
> a===b
true
>