zamza
Version:
Apache Kafka discovery, indexing, searches, storage, hooks and HTTP gateway
73 lines (72 loc) • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Debug = require("debug");
const debug = Debug("zamza:model:topicmetadata");
class TopicMetadataModel {
constructor(zamza) {
this.metrics = zamza.metrics;
this.name = "topicmetadata";
this.model = null;
}
registerModel(mongoose, schemaConstructor) {
const schemaDefinition = {
topic: String,
messageCount: Number,
partitionCount: Number,
earliestOffset: Number,
latestOffset: Number,
earliestMessage: Number,
latestMessage: Number,
partitions: Object,
timestamp: Number,
};
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().then((topicMetadata) => {
if (!topicMetadata) {
return topicMetadata;
}
delete topicMetadata._id;
delete topicMetadata.__v;
return topicMetadata;
});
}
list() {
return this.model.find({}).lean().exec().then((topicMetadatas) => {
return topicMetadatas.map((topicMetadata) => {
delete topicMetadata._id;
delete topicMetadata.__v;
return topicMetadata;
});
});
}
upsert(topicMetadata) {
const query = {
topic: topicMetadata.topic,
};
const queryOptions = {
upsert: true,
};
return this.model.findOneAndUpdate(query, topicMetadata, queryOptions).exec();
}
delete(topic) {
return this.model.deleteMany({ topic }).exec();
}
truncateCollection() {
debug("Truncating collection");
return this.model.deleteMany({}).exec();
}
}
exports.TopicMetadataModel = TopicMetadataModel;