zamza
Version:
Apache Kafka discovery, indexing, searches, storage, hooks and HTTP gateway
80 lines (79 loc) • 2.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Debug = require("debug");
const debug = Debug("zamza:model:replay");
class ReplayModel {
constructor(zamza) {
this.metrics = zamza.metrics;
this.name = "replay";
this.model = null;
}
registerModel(mongoose, schemaConstructor) {
const schemaDefinition = {
instanceId: String,
topic: String,
timestamp: Number,
consumerGroup: String,
};
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.");
}
list() {
return this.model.find({}).lean().exec().then((replays) => {
return replays.map((replay) => {
delete replay._id;
delete replay.__v;
return replay;
});
});
}
get(topic) {
return this.model.findOne({ topic }).lean().exec().then((replay) => {
if (!replay) {
return replay;
}
delete replay._id;
delete replay.__v;
return replay;
});
}
getForInstanceId(instanceId) {
return this.model.findOne({ instanceId }).lean().exec().then((replay) => {
if (!replay) {
return replay;
}
delete replay._id;
delete replay.__v;
return replay;
});
}
upsert(replay) {
const query = {
topic: replay.topic,
};
const queryOptions = {
upsert: true,
};
return this.model.findOneAndUpdate(query, replay, queryOptions).exec();
}
delete(topic) {
return this.model.deleteMany({ topic }).exec();
}
deleteForInstanceId(instanceId) {
return this.model.deleteMany({ instanceId }).exec();
}
truncate() {
return this.model.deleteMany({}).exec();
}
}
exports.ReplayModel = ReplayModel;