{"version":3,"file":"get-default-field-name.mjs","names":["f"],"sources":["../../../src/db/adapter/get-default-field-name.ts"],"sourcesContent":["import { BetterAuthError } from \"../../error\";\nimport type { BetterAuthDBSchema } from \"../type\";\nimport { initGetDefaultModelName } from \"./get-default-model-name\";\n\nexport const initGetDefaultFieldName = ({\n\tschema,\n\tusePlural,\n}: {\n\tschema: BetterAuthDBSchema;\n\tusePlural: boolean | undefined;\n}) => {\n\tconst getDefaultModelName = initGetDefaultModelName({\n\t\tschema,\n\t\tusePlural,\n\t});\n\n\t/**\n\t * This function helps us get the default field name from the schema defined by devs.\n\t * Often times, the user will be using the `fieldName` which could had been customized by the users.\n\t * This function helps us get the actual field name useful to match against the schema. (eg: schema[model].fields[field])\n\t *\n\t * If it's still unclear what this does:\n\t *\n\t * 1. User can define a custom fieldName.\n\t * 2. When using a custom fieldName, doing something like `schema[model].fields[field]` will not work.\n\t */\n\tconst getDefaultFieldName = ({\n\t\tfield,\n\t\tmodel: unsafeModel,\n\t}: {\n\t\tmodel: string;\n\t\tfield: string;\n\t}) => {\n\t\t// Plugin `schema`s can't define their own `id`. Better-auth auto provides `id` to every schema model.\n\t\t// Given this, we can't just check if the `field` (that being `id`) is within the schema's fields, since it is never defined.\n\t\t// So we check if the `field` is `id` and if so, we return `id` itself. Otherwise, we return the `field` from the schema.\n\t\tif (field === \"id\" || field === \"_id\") {\n\t\t\treturn \"id\";\n\t\t}\n\t\tconst model = getDefaultModelName(unsafeModel); // Just to make sure the model name is correct.\n\n\t\tlet f = schema[model]?.fields[field];\n\t\tif (!f) {\n\t\t\tconst result = Object.entries(schema[model]!.fields!).find(\n\t\t\t\t([_, f]) => f.fieldName === field,\n\t\t\t);\n\t\t\tif (result) {\n\t\t\t\tf = result[1];\n\t\t\t\tfield = result[0];\n\t\t\t}\n\t\t}\n\t\tif (!f) {\n\t\t\tthrow new BetterAuthError(`Field ${field} not found in model ${model}`);\n\t\t}\n\t\treturn field;\n\t};\n\n\treturn getDefaultFieldName;\n};\n"],"mappings":";;;;AAIA,MAAa,2BAA2B,EACvC,QACA,gBAIK;CACL,MAAM,sBAAsB,wBAAwB;EACnD;EACA;EACA,CAAC;;;;;;;;;;;CAYF,MAAM,uBAAuB,EAC5B,OACA,OAAO,kBAIF;AAIL,MAAI,UAAU,QAAQ,UAAU,MAC/B,QAAO;EAER,MAAM,QAAQ,oBAAoB,YAAY;EAE9C,IAAI,IAAI,OAAO,QAAQ,OAAO;AAC9B,MAAI,CAAC,GAAG;GACP,MAAM,SAAS,OAAO,QAAQ,OAAO,OAAQ,OAAQ,CAAC,MACpD,CAAC,GAAGA,SAAOA,IAAE,cAAc,MAC5B;AACD,OAAI,QAAQ;AACX,QAAI,OAAO;AACX,YAAQ,OAAO;;;AAGjB,MAAI,CAAC,EACJ,OAAM,IAAI,gBAAgB,SAAS,MAAM,sBAAsB,QAAQ;AAExE,SAAO;;AAGR,QAAO"}