@adinet/indigodb
Version:
ORM for PostgreSQL and MongoDB with real-time support
112 lines (111 loc) • 4.89 kB
JavaScript
"use strict";
// src/models/mongoModel.ts
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const mongodb_1 = require("mongodb");
class MongoModel {
constructor(name, schema, orm) {
this.name = name;
this.schema = schema;
this.orm = orm;
this.collection = this.orm.mongoDb.collection(this.name);
this.setupChangeStream();
}
setupChangeStream() {
const changeStream = this.collection.watch([], {
fullDocument: "updateLookup",
});
changeStream.on("change", (change) => {
const operationType = change.operationType.toUpperCase();
let documentData;
if (change.operationType === "insert" && change.fullDocument) {
documentData = change.fullDocument;
}
else if (change.operationType === "delete" && change.documentKey) {
// En caso de eliminación, podemos usar `documentKey` para obtener el ID del documento eliminado
documentData = { _id: change.documentKey._id };
}
else {
// Si no tenemos `fullDocument`, manejamos el caso adecuadamente
console.warn("fullDocument no está disponible para esta operación:", change.operationType);
return;
}
const payload = {
model: this.name,
operation: operationType,
data: documentData,
};
this.orm.broadcast("databaseUpdate", payload);
});
}
mapDataTypes(data) {
var _a, _b, _c;
const mappedData = {};
for (const [key, value] of Object.entries(data)) {
if (((_a = this.schema[key]) === null || _a === void 0 ? void 0 : _a.type) === "INTEGER" ||
((_b = this.schema[key]) === null || _b === void 0 ? void 0 : _b.type) === "FLOAT") {
mappedData[key] = Number(value);
}
else if (((_c = this.schema[key]) === null || _c === void 0 ? void 0 : _c.type) === "BOOLEAN") {
mappedData[key] = Boolean(value);
}
else {
mappedData[key] = value;
}
}
return mappedData;
}
create(data) {
return __awaiter(this, void 0, void 0, function* () {
const mappedData = this.mapDataTypes(data);
const result = yield this.collection.insertOne(mappedData);
const insertedDocument = yield this.findById(result.insertedId);
return insertedDocument;
});
}
findAll(criteria = {}) {
return __awaiter(this, void 0, void 0, function* () {
const cursor = this.collection.find(criteria);
const results = yield cursor.toArray();
return results;
});
}
findById(id) {
return __awaiter(this, void 0, void 0, function* () {
const objectId = typeof id === "string" ? new mongodb_1.ObjectId(id) : id;
const result = yield this.collection.findOne({ _id: objectId });
return result ? result : null;
});
}
update(id, data) {
return __awaiter(this, void 0, void 0, function* () {
const objectId = typeof id === "string" ? new mongodb_1.ObjectId(id) : id;
const mappedData = this.mapDataTypes(data);
yield this.collection.updateOne({ _id: objectId }, { $set: mappedData });
const updatedDocument = yield this.findById(objectId);
return updatedDocument;
});
}
delete(id) {
return __awaiter(this, void 0, void 0, function* () {
const objectId = typeof id === "string" ? new mongodb_1.ObjectId(id) : id;
const documentToDelete = yield this.findById(objectId);
if (documentToDelete) {
yield this.collection.deleteOne({ _id: objectId });
return documentToDelete;
}
else {
return null;
}
});
}
}
exports.default = MongoModel;