{"version":3,"file":"cuid2-extension.cjs","sources":["../../src/cuid2-extension.ts"],"sourcesContent":["import { init } from \"@paralleldrive/cuid2\";\nimport { Prisma } from \"@prisma/client\";\nimport { produce } from \"immer\";\n\nimport getExactFieldsFactory from \"./factories/get-exact-fields-factory\";\nimport { type GetFieldsFunction } from \"./factories/get-fields-function\";\nimport getWildcardFieldsFactory from \"./factories/get-wildcard-fields-factory\";\nimport { type ExcludeField, type Field, type IncludeField } from \"./valid-fields\";\nimport { exactValidator, wildcardValidator } from \"./validators\";\n\nconst OPERATIONS = [\"create\", \"createMany\", \"upsert\"];\n\ntype Cuid2InitOptions = Parameters<typeof init>[0];\n\nexport type Cuid2ExtensionOptionsBase = {\n  /**\n   * This allows you to customize the CUID2 generation.\n   *\n   * A useful option is to set the `fingerprint` to a unique value for your application.\n   *\n   * @example\n   *   const cuid2 = cuid2Extension({\n   *     cuid2Options: {\n   *       fingerprint: process.env.DEVICE_ID\n   *     }\n   *   })\n   *\n   * @see https://github.com/paralleldrive/cuid2?tab=readme-ov-file#configuration\n   */\n  cuid2Options?: Cuid2InitOptions;\n};\n\ntype Cuid2ExtensionOptionsWildcard = {\n  /**\n   * The fields to automatically set the CUID2 value on.\n   *\n   * Provide the fields in the format of `ModelName:FieldName`. You can use `*` to match all models.\n   *\n   * @example [\"User:userId\", \"Post:postId\"]\n   * @example [\"*:id\", \"Post:secondId\"]\n   *\n   * @default [\"*:id\"]\n   */\n  includeFields?: IncludeField[];\n\n  /**\n   * The fields to exclude from being automatically set the CUID2 value on.\n   *\n   * This is useful if you have a small number of fields that you want to exclude from being automatically set.\n   * You can use `*` to exclude all fields on a model.\n   *\n   * Provide the fields in the format of `ModelName:FieldName`.\n   *\n   * @example [\"User:id\"]\n   * @example [\"Post:*\"]\n   */\n  excludeFields?: ExcludeField[];\n};\n\ntype Cuid2ExtensionOptionsExact = {\n  /**\n   * Requires the exact fields to include for the CUID2 extension.\n   *\n   * This is the recommended way to use the extension as it provides a clear understanding of which fields are being\n   * affected and supports type safety.\n   */\n  fields: Field[];\n};\n\nexport type Cuid2ExtensionOptions = Cuid2ExtensionOptionsBase &\n  (Cuid2ExtensionOptionsWildcard | Cuid2ExtensionOptionsExact);\n\nexport default function cuid2Extension(options?: Cuid2ExtensionOptions) {\n  if (options && \"fields\" in options && (\"includeFields\" in options || \"excludeFields\" in options)) {\n    throw new Error(\"You cannot provide both `fields` and `includeFields`/`excludeFields` options.\");\n  }\n\n  let getFields: GetFieldsFunction;\n  if (options === undefined) {\n    getFields = getWildcardFieldsFactory([\"*:id\"]);\n  } else if (\"fields\" in options) {\n    const validatedOptions = exactValidator.parse(options);\n    getFields = getExactFieldsFactory(validatedOptions.fields);\n  } else {\n    const validatedOptions = wildcardValidator.parse(options);\n    getFields = getWildcardFieldsFactory(validatedOptions.includeFields, validatedOptions.excludeFields);\n  }\n\n  const createId = init(options?.cuid2Options);\n\n  return Prisma.defineExtension({\n    name: \"cuid2\",\n\n    query: {\n      $allModels: {\n        $allOperations({ model, operation, query, args }) {\n          if (!OPERATIONS.includes(operation)) {\n            return query(args);\n          }\n\n          // Get the fields to apply the CUID2 extension\n          const fields = getFields(model);\n\n          const argsWithNewId = produce(args, (draft) => {\n            // @ts-expect-error `create` is the correct field for upsert\n            const data = operation === \"upsert\" ? draft.create : draft.data;\n\n            fields.forEach((field) => {\n              /* v8 ignore next 6 - Create Many not supported by SQLite*/\n              if (Array.isArray(data)) {\n                data.forEach((item) => {\n                  if (!item[field]) {\n                    item[field] = createId();\n                  }\n                });\n              } else {\n                if (!data[field]) {\n                  data[field] = createId();\n                }\n              }\n            });\n          });\n\n          return query(argsWithNewId);\n        },\n      },\n    },\n  });\n}\n"],"names":["exactValidator","wildcardValidator","init","Prisma","produce"],"mappings":";;;;;;;AAUA,MAAM,aAAa,CAAC,UAAU,cAAc,QAAQ;AA8DpD,SAAwB,eAAe,SAAiC;AACtE,MAAI,WAAW,YAAY,YAAY,mBAAmB,WAAW,mBAAmB,UAAU;AAC1F,UAAA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