UNPKG

zamza

Version:

Apache Kafka discovery, indexing, searches, storage, hooks and HTTP gateway

100 lines (99 loc) 3.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Debug = require("debug"); const debug = Debug("zamza:model:topicconfig"); const ALLOWED_POLICIES = ["compact", "delete", "none", "compact_and_delete"]; class TopicConfigModel { constructor(zamza, mongoWrapper = null) { this.metrics = zamza.metrics; this.mongoWrapper = mongoWrapper; this.name = "topicconfig"; this.model = null; } registerModel(mongoose, schemaConstructor) { const schemaDefinition = { topic: String, cleanupPolicy: String, retentionMs: Number, timestamp: Number, queryable: Boolean, }; const schema = new schemaConstructor(schemaDefinition); schema.index({ topic: 1, type: -1 }); this.model = mongoose.model(this.name, schema); this.model.on("index", (error) => { if (error) { debug("Index creation failed", error.message); } else { debug("Index creation successfull."); } }); debug("Registered model with schema."); } get(topic) { return this.model.findOne({ topic }).lean().exec(); } async listAsTopics() { const topicConfigs = await this.list(); return topicConfigs.map((topicConfig) => topicConfig.topic); } list() { return this.model.find({}).lean().exec().then((topicConfigs) => { return topicConfigs.map((topicConfig) => { const responseTopicConfig = { topic: topicConfig.topic, cleanupPolicy: topicConfig.cleanupPolicy, retentionMs: topicConfig.retentionMs, timestamp: topicConfig.timestamp, queryable: topicConfig.queryable, }; return responseTopicConfig; }); }); } upsert(topic, cleanupPolicy, retentionMs, timestamp = Date.now(), queryable = false) { if (ALLOWED_POLICIES.indexOf(cleanupPolicy) === -1) { throw new Error("cleanupPolicy not allowed, choose the one of the following: " + ALLOWED_POLICIES.join(", ") + "."); } if (cleanupPolicy === "delete" && (!retentionMs || retentionMs <= 0)) { throw new Error("cleanupPolicy 'delete' requires retentionMs to be set."); } if (cleanupPolicy === "compact" && (retentionMs || retentionMs > 0)) { throw new Error("cleanupPolicy 'compact' requires retentionMs to be 0 or null."); } if (cleanupPolicy === "none" && (retentionMs || retentionMs > 0)) { throw new Error("cleanupPolicy 'none' requires retentionMs to be 0 or null."); } if (cleanupPolicy === "compact_and_delete" && (!retentionMs || retentionMs <= 0)) { throw new Error("cleanupPolicy 'compact_and_delete' requires retentionMs to be set."); } const document = { topic, cleanupPolicy, retentionMs, timestamp, queryable, }; // important to create indices before (key index model) topic gets first writes if (this.mongoWrapper) { this.mongoWrapper.getKeyIndex().ensureModelAndIndicesExist(topic, document); } const query = { topic, }; const queryOptions = { upsert: true, }; return this.model.findOneAndUpdate(query, document, queryOptions).exec(); } delete(topic) { return this.model.deleteMany({ topic }).exec(); } truncateCollection() { debug("Truncating collection"); return this.model.deleteMany({}).exec(); } } exports.TopicConfigModel = TopicConfigModel;