@energyweb/node-red-contrib-green-proof-worker
Version:
## Peer dependencies
46 lines (45 loc) • 1.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessedUpdateSqlite = void 0;
const tslib_1 = require("tslib");
const z = tslib_1.__importStar(require("zod"));
const errors_1 = require("../errors");
const input_message_1 = require("../input-message");
const node_1 = require("../node");
const InputMessage = input_message_1.InputMessages.UnitsChanged();
const Config = z.object({
sqliteConfig: z.string(),
});
const CORRECT_OUTPUT = 0;
const ProcessedUpdateSqlite = (api) => class ProcessedUpdateSqlite extends node_1.Node {
constructor(config) {
super(api, config, InputMessage);
const parsedConfig = Config.parse(config);
const configNode = this.api.getNode(parsedConfig.sqliteConfig);
if (!configNode) {
throw new errors_1.GGPError(errors_1.ErrorCode.SqliteConfigNotFound, {});
}
this.database = configNode.database;
}
async onInput(message) {
const db = await this.database;
const changes = message.payload.txLog.changes;
if (changes.length === 0) {
this.sendBuilder(message).sendToOutput(CORRECT_OUTPUT);
return;
}
await db
.insertInto('processed')
.values(changes.map(c => ({
owner: c.owner,
prev_owner: c.prevOwner,
root_unit_id: message.payload.txLog.rootUnitId,
unit_id: c.unitId,
volume: c.volume,
created_at: Date.now(),
})))
.execute();
this.sendBuilder(message).sendToOutput(CORRECT_OUTPUT);
}
};
exports.ProcessedUpdateSqlite = ProcessedUpdateSqlite;