UNPKG

@timescaledb/typeorm

Version:

This is the official TimescaleDB plugin for TypeORM.

49 lines 1.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTimeBucket = getTimeBucket; const Hypertable_1 = require("../decorators/Hypertable"); const core_1 = require("@timescaledb/core"); const schemas_1 = require("@timescaledb/schemas"); const debug_1 = require("../debug"); const debug = (0, debug_1.debugTypeOrm)('getTimeBucket'); async function getTimeBucket(options) { const target = this.target; debug(`Getting time bucket for ${target.name}`); const hypertableOptions = Reflect.getMetadata(Hypertable_1.HYPERTABLE_METADATA_KEY, target); if (!hypertableOptions) { const error = 'Entity is not a hypertable'; debug(error); throw new Error(`Entity is not a hypertable`); } const hypertable = core_1.TimescaleDB.createHypertable(this.metadata.tableName, hypertableOptions); const builderConfig = { interval: options.bucket.interval, metrics: options.bucket.metrics.map((metric) => ({ type: metric.type, column: metric.column?.toString(), alias: metric.alias, })), }; schemas_1.TimeBucketConfigSchema.parse(builderConfig); const { sql, params } = hypertable.timeBucket(builderConfig).build({ range: { start: options.timeRange.start, end: options.timeRange.end, }, where: options.where, }); const results = await this.query(sql, params); const result = results.map((row) => { const formattedRow = { interval: row.interval, }; options.bucket.metrics.forEach((metric) => { const alias = metric.alias || 'count'; formattedRow[alias] = Number(row[alias]); }); return formattedRow; }); debug('Time bucket retrieved'); return result; } //# sourceMappingURL=get-time-bucket.js.map