@sentzunhat/zacatl
Version:
A modular, high-performance TypeScript microservice framework for Node.js, featuring layered architecture, dependency injection, and robust validation for building scalable APIs and distributed systems.
135 lines • 4.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongooseAdapter = void 0;
const error_1 = require("../../../../../error");
const third_party_1 = require("../../../../../third-party");
const mongoose_1 = require("../../../../../third-party/mongoose");
class MongooseAdapter {
_model = null;
config;
constructor(config) {
this.config = config;
}
get model() {
const existingModel = this._model;
if (existingModel != null) {
return existingModel;
}
const mongoose = third_party_1.container.resolve(mongoose_1.Mongoose);
const { name, schema } = this.config;
const model = mongoose.model(name ?? (0, third_party_1.uuidv4)(), schema);
this._model = model;
return model;
}
async initialize() {
const m = this.model;
await m.createCollection();
await m.createIndexes();
await m.init();
}
toLean(input) {
if (input == null) {
return null;
}
let base;
if (input != null &&
typeof input === 'object' &&
'toObject' in input &&
typeof input['toObject'] === 'function') {
base = input.toObject({
virtuals: true,
});
}
else {
base = input;
}
const baseRec = base;
const idVal = (() => {
if (typeof baseRec['id'] === 'string')
return baseRec['id'];
if (typeof baseRec['_id'] === 'string')
return baseRec['_id'];
if (baseRec['_id'] !== undefined && baseRec['_id'] !== null)
return String(baseRec['_id']);
return '';
})();
const result = {
...base,
id: idVal,
createdAt: base.createdAt instanceof Date
? base.createdAt
: base.createdAt != null
? new Date(base.createdAt)
: new Date(),
updatedAt: base.updatedAt instanceof Date
? base.updatedAt
: base.updatedAt != null
? new Date(base.updatedAt)
: new Date(),
};
return result;
}
async findById(id) {
try {
const entity = await this.model.findById(id).lean({ virtuals: true }).exec();
return this.toLean(entity);
}
catch {
return null;
}
}
async findMany(filter = {}) {
const entities = await this.model
.find(filter)
.lean({ virtuals: true })
.exec();
return entities
.map((entity) => this.toLean(entity))
.filter((entity) => entity != null);
}
async create(entity) {
const document = await this.model.create(entity);
const leanDocument = this.toLean(document);
if (leanDocument == null) {
throw new error_1.InternalServerError({
message: 'Failed to create document',
reason: 'Document was created but toLean returned null',
component: 'MongooseAdapter',
operation: 'create',
});
}
return leanDocument;
}
async update(id, update) {
try {
const entity = await this.model
.findByIdAndUpdate(id, update, { new: true })
.lean({ virtuals: true })
.exec();
return this.toLean(entity);
}
catch {
return null;
}
}
async delete(id) {
try {
const entity = await this.model.findByIdAndDelete(id).lean({ virtuals: true }).exec();
return this.toLean(entity);
}
catch {
return null;
}
}
async exists(id) {
try {
const result = await this.model.exists({ _id: id });
return result != null;
}
catch {
return false;
}
}
}
exports.MongooseAdapter = MongooseAdapter;
//# sourceMappingURL=adapter.js.map