{"version":3,"sources":["../src/fastify-papr-plugin.ts","../src/papr-helper.ts"],"sourcesContent":["import type { FastifyPluginAsync } from 'fastify'\n\nimport fp from 'fastify-plugin'\nimport type { IndexDescription } from 'mongodb'\nimport type { BaseSchema, SchemaOptions } from 'papr'\nimport { paprHelper } from './papr-helper.js'\nimport type { FastifyPapr, FastifyPaprOptions, ModelRegistration } from './types.js'\n\ndeclare module 'fastify' {\n  interface FastifyInstance {\n    /**\n     * Papr models accessible through the fastify instance\n     * Models can be accessed directly or through a named database connection\n     */\n    papr: FastifyPapr\n  }\n}\n\n/**\n * Helper function to create a model registration\n * @param name Collection name\n * @param schema Papr schema definition with options\n * @param indexes Optional MongoDB indexes to create\n * @returns A model registration object\n */\nexport const asCollection = <TSchema extends BaseSchema>(\n  name: string,\n  schema: [TSchema, SchemaOptions<Partial<TSchema>>],\n  indexes?: IndexDescription[],\n): ModelRegistration => ({\n  name,\n  schema,\n  indexes,\n})\n\n/**\n * Main Fastify plugin for Papr integration\n * Registers models to MongoDB and decorates fastify with them\n */\nexport const fastifyPaprPlugin: FastifyPluginAsync<FastifyPaprOptions> = async (mutable_fastify, options) => {\n  const helper = paprHelper(\n    mutable_fastify,\n    options.db,\n    options.disableSchemaReconciliation,\n  )\n\n  const models = await helper.register(options.models)\n  const { name: dbName } = options\n\n  if (dbName) {\n    if (mutable_fastify.papr) {\n      if (mutable_fastify.papr[dbName]) {\n        throw new Error(`Connection name already registered: ${dbName}`)\n      }\n      mutable_fastify.log.info(`Registering connection name: ${dbName}`)\n      mutable_fastify.papr = {\n        ...mutable_fastify.papr,\n\n        [dbName]: models,\n      }\n    } else {\n      mutable_fastify.decorate('papr', {\n        [dbName]: models,\n      })\n    }\n  } else {\n    if (mutable_fastify.papr) {\n      const items = Object.keys(mutable_fastify.papr).join(', ')\n      throw new Error(`Models already registered: ${items}`)\n    } else {\n      mutable_fastify.decorate('papr', models)\n    }\n  }\n}\n\n/**\n * Default export as a Fastify plugin\n * Compatible with Fastify v4 and v5\n */\nexport default fp(fastifyPaprPlugin, {\n  name: 'fastify-papr-plugin',\n  fastify: '4.x || 5.x',\n})\n","import type { FastifyInstance } from 'fastify'\nimport type { Db, IndexDescription } from 'mongodb'\nimport type { BaseSchema, SchemaOptions } from 'papr'\nimport Papr from 'papr'\nimport type { ModelRegistrationPair } from './types.js'\nimport { type FastifyPapr } from './types.js'\n\n/**\n * Creates a helper for managing Papr models\n * Handles model registration and index creation\n *\n * @param fastify Fastify instance for logging\n * @param db MongoDB database connection\n * @param disableSchemaReconciliation Whether to skip schema validation reconciliation\n * @returns Object with registration methods\n */\nexport const paprHelper = (fastify: Readonly<FastifyInstance>, db: Db, disableSchemaReconciliation = false) => {\n  const papr = new Papr()\n  papr.initialize(db)\n\n  /**\n   * Registers a model with Papr\n   * @param collectionName MongoDB collection name\n   * @param collectionSchema Schema definition and options\n   * @returns Registered Papr model\n   */\n  const registerModel = async <TSchema extends BaseSchema, TOptions extends SchemaOptions<TSchema>>(\n    collectionName: string,\n    collectionSchema: [TSchema, TOptions],\n  ) => {\n    const model = papr.model(collectionName, collectionSchema)\n\n    if (!disableSchemaReconciliation) {\n      await papr.updateSchema(model)\n    }\n\n    return model\n  }\n\n  /**\n   * Creates MongoDB indexes for a collection\n   * @param collectionName MongoDB collection name\n   * @param indexes Array of index descriptions\n   * @returns Array of created index names\n   */\n  const registerIndexes = async (collectionName: string, indexes: readonly IndexDescription[]) =>\n    db.collection(collectionName).createIndexes(indexes as IndexDescription[])\n\n  return {\n    /**\n     * Registers multiple models at once\n     * @param models Model registration definitions\n     * @returns Object with registered models\n     */\n    async register(models: ModelRegistrationPair<FastifyPapr>) {\n      return Object.fromEntries(\n        await Promise.all(\n          Object.entries(models).map(async ([name, paprModel]) => {\n            const model = await registerModel(paprModel.name, paprModel.schema)\n            fastify.log.info(`Model ${name} decorated`)\n            if (paprModel.indexes) {\n              const index = await registerIndexes(paprModel.name, paprModel.indexes)\n              fastify.log.info(`Indexes for ${paprModel.name} => ${index.join(', ')} created.`)\n            }\n\n            return [name, model] as const\n          }),\n        ),\n      )\n    },\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,4BAAe;;;ACCf,kBAAiB;AAaV,IAAM,aAAa,CAAC,SAAoC,IAAQ,8BAA8B,UAAU;AAC7G,QAAM,OAAO,IAAI,YAAAA,QAAK;AACtB,OAAK,WAAW,EAAE;AAQlB,QAAM,gBAAgB,OACpB,gBACA,qBACG;AACH,UAAM,QAAQ,KAAK,MAAM,gBAAgB,gBAAgB;AAEzD,QAAI,CAAC,6BAA6B;AAChC,YAAM,KAAK,aAAa,KAAK;AAAA,IAC/B;AAEA,WAAO;AAAA,EACT;AAQA,QAAM,kBAAkB,OAAO,gBAAwB,YACrD,GAAG,WAAW,cAAc,EAAE,cAAc,OAA6B;AAE3E,SAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAML,MAAM,SAAS,QAA4C;AACzD,aAAO,OAAO;AAAA,QACZ,MAAM,QAAQ;AAAA,UACZ,OAAO,QAAQ,MAAM,EAAE,IAAI,OAAO,CAAC,MAAM,SAAS,MAAM;AACtD,kBAAM,QAAQ,MAAM,cAAc,UAAU,MAAM,UAAU,MAAM;AAClE,oBAAQ,IAAI,KAAK,SAAS,IAAI,YAAY;AAC1C,gBAAI,UAAU,SAAS;AACrB,oBAAM,QAAQ,MAAM,gBAAgB,UAAU,MAAM,UAAU,OAAO;AACrE,sBAAQ,IAAI,KAAK,eAAe,UAAU,IAAI,OAAO,MAAM,KAAK,IAAI,CAAC,WAAW;AAAA,YAClF;AAEA,mBAAO,CAAC,MAAM,KAAK;AAAA,UACrB,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;;;AD9CO,IAAM,eAAe,CAC1B,MACA,QACA,aACuB;AAAA,EACvB;AAAA,EACA;AAAA,EACA;AACF;AAMO,IAAM,oBAA4D,OAAO,iBA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