@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.
68 lines • 2.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SequelizeAdapter = void 0;
class SequelizeAdapter {
model;
constructor(config) {
this.model = config.model;
}
toLean(input) {
if (input == null)
return null;
const maybeWithGet = input;
const plain = typeof maybeWithGet.get === 'function'
? maybeWithGet.get({ plain: true })
: input;
const idValue = plain['id'] ?? plain['_id'];
const createdAtValue = plain['createdAt'];
const updatedAtValue = plain['updatedAt'];
return {
...plain,
id: idValue !== undefined && idValue !== null ? String(idValue) : '',
createdAt: createdAtValue != null ? new Date(createdAtValue) : new Date(),
updatedAt: updatedAtValue != null ? new Date(updatedAtValue) : new Date(),
};
}
async findById(id) {
const entity = await this.model.findByPk(id);
return this.toLean(entity);
}
async findMany(filter = {}) {
const entities = await this.model.findAll({
where: filter,
});
return entities
.map((entity) => this.toLean(entity))
.filter((entity) => entity != null);
}
async create(entity) {
const document = await this.model.create(entity);
return this.toLean(document);
}
async update(id, update) {
const [affectedCount] = await this.model.update(update, {
where: { id },
});
if (affectedCount === 0) {
return null;
}
return this.findById(id);
}
async delete(id) {
const entity = await this.findById(id);
if (entity == null)
return null;
await this.model.destroy({
where: { id },
});
return entity;
}
async exists(id) {
const count = await this.model.count({
where: { id },
});
return (Array.isArray(count) ? count.length : count) > 0;
}
}
exports.SequelizeAdapter = SequelizeAdapter;
//# sourceMappingURL=adapter.js.map