Node.js連線influxdb的使用封裝

weixin_34208283發表於2018-05-01

分別基於2個Node.js的influxdb模組做了封裝,看自己喜好選擇

  • influxdb-nodejs模組
const Influx = require('influxdb-nodejs');
const config = {
    influxdb : {
        host : "192.168.128.129",
        port : "8086",
        database : "gps",
        table : "test", // 預設表
        tables : {
            test : {
                // 定義tag欄位允許值 
                // tag寫入後均轉為字串 故不支援型別限定
                // 未定義 || 不匹配  寫入空 
                // 限制方式:陣列列表 *通配
                tagSchema : {
                      udid: '*',
                },
                // 定義欄位型別 
                // 型別不匹配寫入空 與已有表欄位型別不匹配報錯
                // i->integer s->string f->float b->boolean
                fieldSchema : {
                      lat: 's',
                      lng: 's',
                      value: 's',
                }
            },
        }
    }
}
exports.init = function (user_config) {
    let cfg = config.influxdb;
    user_config = user_config || {} ;
    this.host = user_config.host ? user_config.host : cfg.host ;
    this.port = user_config.port ? user_config.port : cfg.port ;
    this.database = user_config.database ? user_config.database : cfg.database ;
    this.table = user_config.table ? user_config.table : cfg.table ;
    this.client = new Influx('http://' + this.host + ':' + this.port + '/' + this.database);
    for (table in this.tables) {
        this.client.schema(table, this.tables[table].fieldSchema, this.tables[table].tagSchema, {
            stripUnknown: true, // default is false
        });
    }
    return this;
}

exports.save = function (data) {
    let table = data.table || this.table ;
    let tags = data.tags ;
    let fields = data.fields ;
    let success = data.success;
    let error = data.error;
    this.client.write(table)
        .tag(tags)
        .field(fields)
        .then(success)
        .catch(error);
    return this;
}
  • influx模組
const Influx = require('influx');
const config = {
    influxdb : {
        host : "192.168.128.129",
        port : "8086",
        database : "gps",
        table : "test", // 預設表
         schema: [
             {
                 measurement: 'test',
                 fields: {
                    lat: 's',
                    lng: 's',
                    value: 's',
                 },
                 tags: [
                   'udid'
                 ]
             }
         ]
    }
}

exports.init = function (user_config) {
    let cfg = config.influxdb;
    // 處理欄位型別定義
    for(let table of cfg.schema){
        for(let field in table.fields){
            switch(table.fields[field].toLowerCase())
            {
                case 's':
                case 'str':
                case 'string':
                  table.fields[field] = Influx.FieldType.STRING;
                  break;
                case 'i':
                case 'int':
                case 'integer':
                  table.fields[field] = Influx.FieldType.INTEGER;
                case 'f':
                case 'float':
                  table.fields[field] = Influx.FieldType.FLOAT;
                  break;
                case 'b':
                case 'bool':
                case 'boolean':
                  table.fields[field] = Influx.FieldType.BOOLEAN;
                  break;
            }
        }
    }
    user_config = user_config || {} ;
    cfg.host = user_config.host ? user_config.host : cfg.host ;
    cfg.port = user_config.port ? user_config.port : cfg.port ;
    cfg.database = user_config.database ? user_config.database : cfg.database ;
    this.table = user_config.table ? user_config.table : cfg.table ;
    this.influx = new Influx.InfluxDB(cfg)
    return this;
}

exports.save = function (data) {
    for(let point of data){
        point.measurement = point.table || point.measurement || this.table ;
    }
    this.influx = this.influx.writePoints(data);
    return this;
}

exports.then = function (data) {
    this.influx = this.influx.then(data);
    return this;
}

相關文章