Node.js连接influxdb的使用封装

2018-05-01  本文已影响0人  许文同学

分别基于2个Node.js的influxdb模块做了封装,看自己喜好选择

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;
}
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;
}
上一篇下一篇

猜你喜欢

热点阅读