{"version":3,"file":"get-default-model-name.mjs","names":["m"],"sources":["../../../src/db/adapter/get-default-model-name.ts"],"sourcesContent":["import { BetterAuthError } from \"../../error\";\nimport type { BetterAuthDBSchema } from \"../type\";\n\nexport const initGetDefaultModelName = ({\n\tusePlural,\n\tschema,\n}: {\n\tusePlural: boolean | undefined;\n\tschema: BetterAuthDBSchema;\n}) => {\n\t/**\n\t * This function helps us get the default model name from the schema defined by devs.\n\t * Often times, the user will be using the `modelName` which could had been customized by the users.\n\t * This function helps us get the actual model name useful to match against the schema. (eg: schema[model])\n\t *\n\t * If it's still unclear what this does:\n\t *\n\t * 1. User can define a custom modelName.\n\t * 2. When using a custom modelName, doing something like `schema[model]` will not work.\n\t * 3. Using this function helps us get the actual model name based on the user's defined custom modelName.\n\t */\n\tconst getDefaultModelName = (model: string) => {\n\t\t// It's possible this `model` could had applied `usePlural`.\n\t\t// Thus we'll try the search but without the trailing `s`.\n\t\tif (usePlural && model.charAt(model.length - 1) === \"s\") {\n\t\t\tconst pluralessModel = model.slice(0, -1);\n\t\t\tlet m = schema[pluralessModel] ? pluralessModel : undefined;\n\t\t\tif (!m) {\n\t\t\t\tm = Object.entries(schema).find(\n\t\t\t\t\t([_, f]) => f.modelName === pluralessModel,\n\t\t\t\t)?.[0];\n\t\t\t}\n\n\t\t\tif (m) {\n\t\t\t\treturn m;\n\t\t\t}\n\t\t}\n\n\t\tlet m = schema[model] ? model : undefined;\n\t\tif (!m) {\n\t\t\tm = Object.entries(schema).find(([_, f]) => f.modelName === model)?.[0];\n\t\t}\n\n\t\tif (!m) {\n\t\t\tthrow new BetterAuthError(`Model \"${model}\" not found in schema`);\n\t\t}\n\t\treturn m;\n\t};\n\n\treturn getDefaultModelName;\n};\n"],"mappings":";;;AAGA,MAAa,2BAA2B,EACvC,WACA,aAIK;;;;;;;;;;;;CAYL,MAAM,uBAAuB,UAAkB;AAG9C,MAAI,aAAa,MAAM,OAAO,MAAM,SAAS,EAAE,KAAK,KAAK;GACxD,MAAM,iBAAiB,MAAM,MAAM,GAAG,GAAG;GACzC,IAAIA,MAAI,OAAO,kBAAkB,iBAAiB;AAClD,OAAI,CAACA,IACJ,OAAI,OAAO,QAAQ,OAAO,CAAC,MACzB,CAAC,GAAG,OAAO,EAAE,cAAc,eAC5B,GAAG;AAGL,OAAIA,IACH,QAAOA;;EAIT,IAAI,IAAI,OAAO,SAAS,QAAQ;AAChC,MAAI,CAAC,EACJ,KAAI,OAAO,QAAQ,OAAO,CAAC,MAAM,CAAC,GAAG,OAAO,EAAE,cAAc,MAAM,GAAG;AAGtE,MAAI,CAAC,EACJ,OAAM,IAAI,gBAAgB,UAAU,MAAM,uBAAuB;AAElE,SAAO;;AAGR,QAAO"}