@inaiat/fastify-papr
Version:
Fastify Papr Plugin Integration
123 lines (120 loc) • 4.47 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/fastify-papr-plugin.ts
var fastify_papr_plugin_exports = {};
__export(fastify_papr_plugin_exports, {
asCollection: () => asCollection,
default: () => fastify_papr_plugin_default,
fastifyPaprPlugin: () => fastifyPaprPlugin
});
module.exports = __toCommonJS(fastify_papr_plugin_exports);
var import_fastify_plugin = __toESM(require("fastify-plugin"), 1);
// src/papr-helper.ts
var import_papr = __toESM(require("papr"), 1);
var paprHelper = (fastify, db, disableSchemaReconciliation = false) => {
const papr = new import_papr.default();
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];
})
)
);
}
};
};
// src/fastify-papr-plugin.ts
var asCollection = (name, schema, indexes) => ({
name,
schema,
indexes
});
var fastifyPaprPlugin = async (mutable_fastify, options) => {
const helper = paprHelper(
mutable_fastify,
options.db,
options.disableSchemaReconciliation
);
const models = await helper.register(options.models);
const { name: dbName } = options;
if (dbName) {
if (mutable_fastify.papr) {
if (mutable_fastify.papr[dbName]) {
throw new Error(`Connection name already registered: ${dbName}`);
}
mutable_fastify.log.info(`Registering connection name: ${dbName}`);
mutable_fastify.papr = {
...mutable_fastify.papr,
[dbName]: models
};
} else {
mutable_fastify.decorate("papr", {
[dbName]: models
});
}
} else {
if (mutable_fastify.papr) {
const items = Object.keys(mutable_fastify.papr).join(", ");
throw new Error(`Models already registered: ${items}`);
} else {
mutable_fastify.decorate("papr", models);
}
}
};
var fastify_papr_plugin_default = (0, import_fastify_plugin.default)(fastifyPaprPlugin, {
name: "fastify-papr-plugin",
fastify: "4.x || 5.x"
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
asCollection,
fastifyPaprPlugin
});
//# sourceMappingURL=fastify-papr-plugin.cjs.map