taosobjects.js 2.2 KB
Newer Older
S
StoneT2000 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
const FieldTypes = require('./constants');

/**
 * Various objects such as TaosRow and TaosColumn that help make parsing data easier
 * @module TaosObjects
 *
 */

/**
 * The TaosRow object. Contains the data from a retrieved row from a database and functions that parse the data.
 * @typedef {Object} TaosRow - A row of data retrieved from a table.
 * @global
 * @example
 * var trow = new TaosRow(row);
 * console.log(trow.data);
 */
function TaosRow (row) {
  this.data = row;
  this.length = row.length;
  return this;
}

/**
 * @typedef {Object} TaosField - A field/column's metadata from a table.
 * @global
 * @example
 * var tfield = new TaosField(field);
 * console.log(tfield.name);
 */

function TaosField(field) {
 this._field = field;
 this.name = field.name;
 this.type = FieldTypes.getType(field.type);
 return this;
}

/**
 * A TaosTimestamp object, which is the standard date object with added functionality
 * @global
 * @memberof TaosObjects
 * @param {Date} date - A Javascript date time object or the time in milliseconds past 1970-1-1 00:00:00.000
 */
class TaosTimestamp extends Date {
45
  constructor(date, micro = false) {
S
StoneT2000 已提交
46 47
    super(date);
    this._type = 'TaosTimestamp';
48 49 50
    if (micro) {
      this.microTime = date - Math.floor(date);
    }
S
StoneT2000 已提交
51 52 53 54 55 56
  }
  /**
   * @function Returns the date into a string usable by TDengine
   * @return {string} A Taos Timestamp String
   */
  toTaosString(){
57 58 59 60 61
    var tzo = -this.getTimezoneOffset(),
        dif = tzo >= 0 ? '+' : '-',
        pad = function(num) {
            var norm = Math.floor(Math.abs(num));
            return (norm < 10 ? '0' : '') + norm;
S
StoneT2000 已提交
62 63 64 65 66 67
        },
        pad2 = function(num) {
            var norm = Math.floor(Math.abs(num));
            if (norm < 10) return '00' + norm;
            if (norm < 100) return '0' + norm;
            if (norm < 1000) return norm;
68 69 70 71 72 73 74
        };
    return this.getFullYear() +
        '-' + pad(this.getMonth() + 1) +
        '-' + pad(this.getDate()) +
        ' ' + pad(this.getHours()) +
        ':' + pad(this.getMinutes()) +
        ':' + pad(this.getSeconds()) +
S
StoneT2000 已提交
75 76
        '.' + pad2(this.getMilliseconds()) +
        ''  + (this.microTime ? pad2(Math.round(this.microTime * 1000)) : '');
S
StoneT2000 已提交
77 78 79 80
  }
}

module.exports = {TaosRow, TaosField, TaosTimestamp}