taosobjects.js 4.2 KB
Newer Older
S
StoneT2000 已提交
1
const FieldTypes = require('./constants');
2
const util = require('util');
S
StoneT2000 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * 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);
 */
17
function TaosRow(row) {
S
StoneT2000 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31
  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) {
32 33 34 35
  this._field = field;
  this.name = field.name;
  this.type = FieldTypes.getType(field.type);
  return this;
S
StoneT2000 已提交
36 37 38 39 40 41 42 43 44
}

/**
 * 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 46 47 48 49
  constructor(date, precision = 0) {
    if (precision === 1) {
      super(Math.floor(date / 1000));
      this.precisionExtras = date % 1000;
    } else if (precision === 2) {
50 51
      // use BigInt to fix: 1623254400999999999 / 1000000 = 1623254401000 which not expected
      super(parseInt(BigInt(date) / 1000000n));
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
      // use BigInt to fix: 1625801548423914405 % 1000000 = 914496 which not expected (914405)
      this.precisionExtras = parseInt(BigInt(date) % 1000000n);
    } else {
      super(parseInt(date));
    }
    this.precision = precision;
  }

  /**
   * TDengine raw timestamp.
   * @returns raw taos timestamp (int64)
   */
  taosTimestamp() {
    if (this.precision == 1) {
      return (this * 1000 + this.precisionExtras);
    } else if (this.precision == 2) {
      return (this * 1000000 + this.precisionExtras);
    } else {
      return Math.floor(this);
    }
  }

  /**
   * Gets the microseconds of a Date.
   * @return {Int} A microseconds integer
   */
  getMicroseconds() {
    if (this.precision == 1) {
      return this.getMilliseconds() * 1000 + this.precisionExtras;
    } else if (this.precision == 2) {
      return this.getMilliseconds() * 1000 + this.precisionExtras / 1000;
    } else {
      return 0;
    }
  }
  /**
   * Gets the nanoseconds of a TaosTimestamp.
   * @return {Int} A nanoseconds integer
   */
  getNanoseconds() {
    if (this.precision == 1) {
      return this.getMilliseconds() * 1000000 + this.precisionExtras * 1000;
    } else if (this.precision == 2) {
      return this.getMilliseconds() * 1000000 + this.precisionExtras;
    } else {
      return 0;
    }
  }

  /**
   * @returns {String} a string for timestamp string format
   */
  _precisionExtra() {
    if (this.precision == 1) {
      return String(this.precisionExtras).padStart(3, '0');
    } else if (this.precision == 2) {
      return String(this.precisionExtras).padStart(6, '0');
    } else {
      return '';
111
    }
S
StoneT2000 已提交
112 113 114 115 116
  }
  /**
   * @function Returns the date into a string usable by TDengine
   * @return {string} A Taos Timestamp String
   */
117
  toTaosString() {
118
    var tzo = -this.getTimezoneOffset(),
119 120 121 122 123 124 125 126 127 128 129
      dif = tzo >= 0 ? '+' : '-',
      pad = function (num) {
        var norm = Math.floor(Math.abs(num));
        return (norm < 10 ? '0' : '') + norm;
      },
      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;
      };
130
    return this.getFullYear() +
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
      '-' + pad(this.getMonth() + 1) +
      '-' + pad(this.getDate()) +
      ' ' + pad(this.getHours()) +
      ':' + pad(this.getMinutes()) +
      ':' + pad(this.getSeconds()) +
      '.' + pad2(this.getMilliseconds()) +
      '' + this._precisionExtra();
  }

  /**
   * Custom console.log
   * @returns {String} string format for debug
   */
  [util.inspect.custom](depth, opts) {
    return this.toTaosString() + JSON.stringify({ precision: this.precision, precisionExtras: this.precisionExtras }, opts);
  }
  toString() {
    return this.toTaosString();
S
StoneT2000 已提交
149 150 151
  }
}

152
module.exports = { TaosRow, TaosField, TaosTimestamp }