fix: add missing migrateConfig method, config versioning, and formatters module
ConfigManager.migrateConfig() was called but never defined — would crash at runtime. Added config version checking, migration support, and fixed createEndpoint indentation. New formatters module (csv, influxdb, json) for pluggable output formatting. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
58
src/helper/formatters/index.js
Normal file
58
src/helper/formatters/index.js
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Formatter Registry
|
||||
* ------------------
|
||||
* Maps format names to formatter modules.
|
||||
* Each formatter exports: format(measurement, metadata) => string|object
|
||||
*
|
||||
* Usage:
|
||||
* const { getFormatter, registerFormatter } = require('./formatters');
|
||||
* const fmt = getFormatter('json');
|
||||
* const output = fmt.format('pump1', { fields: {...}, tags: {...} });
|
||||
*/
|
||||
|
||||
const influxdbFormatter = require('./influxdbFormatter');
|
||||
const jsonFormatter = require('./jsonFormatter');
|
||||
const csvFormatter = require('./csvFormatter');
|
||||
|
||||
// Built-in registry
|
||||
const registry = {
|
||||
influxdb: influxdbFormatter,
|
||||
json: jsonFormatter,
|
||||
csv: csvFormatter,
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve a formatter by name.
|
||||
* @param {string} name - Format name (e.g. 'influxdb', 'json', 'csv')
|
||||
* @returns {object} Formatter with a .format() method
|
||||
* @throws {Error} If the format name is not registered
|
||||
*/
|
||||
function getFormatter(name) {
|
||||
const formatter = registry[name];
|
||||
if (!formatter) {
|
||||
throw new Error(`Unknown output format: "${name}". Registered formats: ${Object.keys(registry).join(', ')}`);
|
||||
}
|
||||
return formatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a custom formatter at runtime.
|
||||
* @param {string} name - Format name
|
||||
* @param {object} formatter - Object with a .format(measurement, metadata) method
|
||||
*/
|
||||
function registerFormatter(name, formatter) {
|
||||
if (typeof formatter.format !== 'function') {
|
||||
throw new Error('Formatter must have a .format(measurement, metadata) method');
|
||||
}
|
||||
registry[name] = formatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* List all registered format names.
|
||||
* @returns {string[]}
|
||||
*/
|
||||
function getRegisteredFormats() {
|
||||
return Object.keys(registry);
|
||||
}
|
||||
|
||||
module.exports = { getFormatter, registerFormatter, getRegisteredFormats };
|
||||
Reference in New Issue
Block a user