@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.
75 lines • 2.5 kB
JavaScript
export const createMongooseAdapter = (config) => {
class LazyMongooseAdapter {
cfg;
_inner = null;
_loading = null;
constructor(cfg) {
this.cfg = cfg;
void this.ensureLoaded();
}
async ensureLoaded() {
if (this._inner != null)
return;
if (this._loading != null) {
await this._loading;
return;
}
this._loading = (async () => {
try {
const mod = await import('./adapter.js');
this._inner = new mod.MongooseAdapter(this.cfg);
}
catch (err) {
const e = err instanceof Error ? err : new Error(String(err));
throw new Error(`Failed to load Mongoose adapter. Ensure 'mongoose' is installed. Original error: ${e.message}`);
}
})();
await this._loading;
}
get inner() {
if (this._inner == null) {
throw new Error('MongooseAdapter is not yet loaded. Await any async repository method (findById, create, etc.) before accessing the adapter directly.');
}
return this._inner;
}
get model() {
return this.inner.model;
}
async initialize() {
await this.ensureLoaded();
const withInit = this.inner;
await withInit.initialize();
}
toLean(input) {
if (this._inner != null)
return this._inner.toLean(input);
return null;
}
async findById(id) {
await this.ensureLoaded();
return this.inner.findById(id);
}
async findMany(filter = {}) {
await this.ensureLoaded();
return this.inner.findMany(filter);
}
async create(entity) {
await this.ensureLoaded();
return this.inner.create(entity);
}
async update(id, update) {
await this.ensureLoaded();
return this.inner.update(id, update);
}
async delete(id) {
await this.ensureLoaded();
return this.inner.delete(id);
}
async exists(id) {
await this.ensureLoaded();
return this.inner.exists(id);
}
}
return new LazyMongooseAdapter(config);
};
//# sourceMappingURL=adapter-loader.js.map