UNPKG

@timescaledb/core

Version:

The `@timescaledb/core` package provides fundamental building blocks for working with TimescaleDB in TypeScript/JavaScript applications. It includes SQL query builders and utilities for managing hypertables, continuous aggregates, compression, and other T

127 lines 5.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Hypertable = exports.HypertableInspectBuilder = void 0; const schemas_1 = require("@timescaledb/schemas"); const errors_1 = require("./errors"); const utils_1 = require("@timescaledb/utils"); const compression_1 = require("./compression"); const time_bucket_1 = require("./time-bucket"); const debug_1 = require("./debug"); const debug = (0, debug_1.debugCore)('Hypertable'); class HypertableUpBuilder { constructor(name, options) { this.statements = []; this.name = name; this.options = options; } build() { debug(`Building up query for hypertable '${this.name}'`); const tableName = (0, utils_1.escapeIdentifier)(this.name); this.statements.push(`SELECT create_hypertable(${(0, utils_1.escapeLiteral)(this.name)}, by_range(${(0, utils_1.escapeLiteral)(this.options.by_range.column_name)}));`); if (this.options.compression?.compress) { const orderBy = (0, utils_1.escapeIdentifier)(this.options.compression.compress_orderby); const segmentBy = (0, utils_1.escapeIdentifier)(this.options.compression.compress_segmentby); const alter = `ALTER TABLE ${tableName} SET ( timescaledb.compress, timescaledb.compress_orderby = ${orderBy}, timescaledb.compress_segmentby = ${segmentBy} );`; this.statements.push(alter); if (this.options.compression.policy) { const interval = (0, utils_1.escapeLiteral)(this.options.compression.policy.schedule_interval); const policy = `SELECT add_compression_policy(${(0, utils_1.escapeLiteral)(this.name)}, INTERVAL ${interval});`; this.statements.push(policy); } } const result = this.statements.join('\n'); debug(`Up query built for '${this.name}':\n${result}`); return result; } } class HypertableDownBuilder { constructor(name, options) { this.statements = []; this.name = name; this.options = options; } build() { debug(`Building down query for hypertable '${this.name}'`); const tableName = (0, utils_1.escapeIdentifier)(this.name); const literalName = (0, utils_1.escapeLiteral)(this.name); if (this.options.compression?.compress) { this.statements.push(`ALTER TABLE ${tableName} SET (timescaledb.compress = false);`); } if (this.options.compression?.policy) { this.statements.push(`SELECT remove_compression_policy(${literalName}, if_exists => true);`); } this.statements.push(`SELECT drop_chunks(${literalName}, NOW()::timestamp without time zone);`); const result = this.statements.join('\n'); debug(`Down query built for '${this.name}':\n${result}`); return result; } } class HypertableInspectBuilder { constructor(name) { this.statements = []; this.name = name; } build() { debug(`Building inspect query for hypertable '${this.name}'`); const literalName = (0, utils_1.escapeLiteral)(this.name); this.statements.push('SELECT'); this.statements.push(` EXISTS ( SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = ${literalName} ) AS table_exists,`); this.statements.push(` EXISTS ( SELECT FROM timescaledb_information.hypertables WHERE hypertable_name = ${literalName} ) AS is_hypertable`); const result = this.statements.join('\n'); debug(`Inspect query built for '${this.name}':\n${result}`); return result; } } exports.HypertableInspectBuilder = HypertableInspectBuilder; class Hypertable { constructor(name, options) { if (!name) { throw new Error(errors_1.HypertableErrors.NAME_REQUIRED); } try { (0, utils_1.validateIdentifier)(name, true); this.name = name; } catch (error) { throw new Error(errors_1.HypertableErrors.INVALID_NAME + ' ' + error.message); } if (!options) { throw new Error(errors_1.HypertableErrors.OPTIONS_REQUIRED); } try { this.options = schemas_1.CreateHypertableOptionsSchema.parse(options); } catch (error) { const e = error; throw new Error(errors_1.HypertableErrors.INVALID_OPTIONS + ' ' + e.message); } } up() { return new HypertableUpBuilder(this.name, this.options); } down() { return new HypertableDownBuilder(this.name, this.options); } inspect() { return new HypertableInspectBuilder(this.name); } compression() { return new compression_1.CompressionBuilder(this.name, this.options); } timeBucket(config) { return new time_bucket_1.TimeBucketBuilder(this.name, this.options.by_range.column_name, config); } } exports.Hypertable = Hypertable; //# sourceMappingURL=hypertable.js.map