@inaiat/fastify-papr
Version:
Fastify Papr Plugin Integration
41 lines (40 loc) • 1.37 kB
JavaScript
// src/papr-helper.ts
import Papr from "papr";
var paprHelper = (fastify, db, disableSchemaReconciliation = false) => {
const papr = new Papr();
papr.initialize(db);
const registerModel = async (collectionName, collectionSchema) => {
const model = papr.model(collectionName, collectionSchema);
if (!disableSchemaReconciliation) {
await papr.updateSchema(model);
}
return model;
};
const registerIndexes = async (collectionName, indexes) => db.collection(collectionName).createIndexes(indexes);
return {
/**
* Registers multiple models at once
* @param models Model registration definitions
* @returns Object with registered models
*/
async register(models) {
return Object.fromEntries(
await Promise.all(
Object.entries(models).map(async ([name, paprModel]) => {
const model = await registerModel(paprModel.name, paprModel.schema);
fastify.log.info(`Model ${name} decorated`);
if (paprModel.indexes) {
const index = await registerIndexes(paprModel.name, paprModel.indexes);
fastify.log.info(`Indexes for ${paprModel.name} => ${index.join(", ")} created.`);
}
return [name, model];
})
)
);
}
};
};
export {
paprHelper
};
//# sourceMappingURL=chunk-Q3HDH7VO.js.map