better-auth 1.5.0-beta.1 → 1.5.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/dist/adapters/memory-adapter/memory-adapter.mjs +1 -0
  2. package/dist/adapters/memory-adapter/memory-adapter.mjs.map +1 -1
  3. package/dist/adapters/mongodb-adapter/mongodb-adapter.mjs +1 -0
  4. package/dist/adapters/mongodb-adapter/mongodb-adapter.mjs.map +1 -1
  5. package/dist/api/index.d.mts +101 -93
  6. package/dist/api/index.mjs +8 -9
  7. package/dist/api/index.mjs.map +1 -1
  8. package/dist/api/middlewares/index.d.mts +1 -1
  9. package/dist/api/middlewares/index.mjs +1 -1
  10. package/dist/api/middlewares/origin-check.d.mts +11 -6
  11. package/dist/api/middlewares/origin-check.mjs +56 -10
  12. package/dist/api/middlewares/origin-check.mjs.map +1 -1
  13. package/dist/api/routes/account.d.mts +11 -11
  14. package/dist/api/routes/callback.d.mts +2 -2
  15. package/dist/api/routes/email-verification.d.mts +4 -4
  16. package/dist/api/routes/error.d.mts +2 -2
  17. package/dist/api/routes/ok.d.mts +2 -2
  18. package/dist/api/routes/reset-password.d.mts +5 -5
  19. package/dist/api/routes/session.d.mts +14 -14
  20. package/dist/api/routes/sign-in.d.mts +5 -3
  21. package/dist/api/routes/sign-in.mjs +3 -0
  22. package/dist/api/routes/sign-in.mjs.map +1 -1
  23. package/dist/api/routes/sign-out.d.mts +2 -2
  24. package/dist/api/routes/sign-up.d.mts +4 -2
  25. package/dist/api/routes/sign-up.mjs +4 -0
  26. package/dist/api/routes/sign-up.mjs.map +1 -1
  27. package/dist/api/routes/update-user.d.mts +13 -13
  28. package/dist/api/to-auth-endpoints.mjs +48 -32
  29. package/dist/api/to-auth-endpoints.mjs.map +1 -1
  30. package/dist/auth/base.mjs +1 -1
  31. package/dist/auth/trusted-origins.mjs +1 -1
  32. package/dist/client/index.d.mts +1 -2
  33. package/dist/client/lynx/index.d.mts +15 -15
  34. package/dist/client/plugins/index.d.mts +1 -1
  35. package/dist/client/proxy.mjs +3 -0
  36. package/dist/client/proxy.mjs.map +1 -1
  37. package/dist/client/react/index.d.mts +15 -15
  38. package/dist/client/session-refresh.mjs +2 -2
  39. package/dist/client/session-refresh.mjs.map +1 -1
  40. package/dist/client/solid/index.d.mts +13 -13
  41. package/dist/client/svelte/index.d.mts +15 -15
  42. package/dist/client/vanilla.d.mts +15 -15
  43. package/dist/client/vue/index.d.mts +13 -13
  44. package/dist/context/create-context.mjs +6 -7
  45. package/dist/context/create-context.mjs.map +1 -1
  46. package/dist/cookies/cookie-utils.d.mts +7 -1
  47. package/dist/cookies/cookie-utils.mjs +11 -1
  48. package/dist/cookies/cookie-utils.mjs.map +1 -1
  49. package/dist/cookies/index.d.mts +2 -2
  50. package/dist/cookies/index.mjs +5 -5
  51. package/dist/cookies/index.mjs.map +1 -1
  52. package/dist/db/field.d.mts +6 -6
  53. package/dist/db/internal-adapter.mjs +30 -12
  54. package/dist/db/internal-adapter.mjs.map +1 -1
  55. package/dist/integrations/next-js.d.mts +4 -4
  56. package/dist/integrations/svelte-kit.d.mts +2 -2
  57. package/dist/integrations/tanstack-start.d.mts +4 -4
  58. package/dist/plugins/admin/admin.d.mts +118 -118
  59. package/dist/plugins/admin/admin.mjs +2 -2
  60. package/dist/plugins/admin/admin.mjs.map +1 -1
  61. package/dist/plugins/admin/routes.mjs +1 -1
  62. package/dist/plugins/admin/routes.mjs.map +1 -1
  63. package/dist/plugins/anonymous/index.d.mts +5 -5
  64. package/dist/plugins/anonymous/index.mjs +1 -1
  65. package/dist/plugins/anonymous/index.mjs.map +1 -1
  66. package/dist/plugins/api-key/index.d.mts +59 -59
  67. package/dist/plugins/bearer/index.d.mts +6 -6
  68. package/dist/plugins/captcha/index.d.mts +2 -2
  69. package/dist/plugins/custom-session/index.d.mts +5 -5
  70. package/dist/plugins/device-authorization/index.d.mts +6 -6
  71. package/dist/plugins/email-otp/index.d.mts +50 -50
  72. package/dist/plugins/generic-oauth/index.d.mts +29 -29
  73. package/dist/plugins/haveibeenpwned/index.d.mts +3 -3
  74. package/dist/plugins/index.d.mts +4 -5
  75. package/dist/plugins/jwt/index.d.mts +9 -9
  76. package/dist/plugins/last-login-method/index.d.mts +4 -4
  77. package/dist/plugins/magic-link/index.d.mts +4 -4
  78. package/dist/plugins/mcp/index.d.mts +10 -10
  79. package/dist/plugins/mcp/index.mjs +1 -1
  80. package/dist/plugins/multi-session/index.d.mts +14 -14
  81. package/dist/plugins/oauth-proxy/index.d.mts +8 -8
  82. package/dist/plugins/oauth-proxy/index.mjs +23 -11
  83. package/dist/plugins/oauth-proxy/index.mjs.map +1 -1
  84. package/dist/plugins/oauth-proxy/utils.mjs +8 -1
  85. package/dist/plugins/oauth-proxy/utils.mjs.map +1 -1
  86. package/dist/plugins/oidc-provider/index.d.mts +13 -13
  87. package/dist/plugins/one-tap/index.d.mts +2 -2
  88. package/dist/plugins/one-tap/index.mjs +1 -1
  89. package/dist/plugins/one-tap/index.mjs.map +1 -1
  90. package/dist/plugins/one-time-token/index.d.mts +5 -5
  91. package/dist/plugins/open-api/index.d.mts +3 -3
  92. package/dist/plugins/organization/access/statement.d.mts +25 -25
  93. package/dist/plugins/organization/adapter.d.mts +1 -1
  94. package/dist/plugins/organization/client.d.mts +1 -1
  95. package/dist/plugins/organization/index.d.mts +1 -3
  96. package/dist/plugins/organization/organization.d.mts +5 -5
  97. package/dist/plugins/organization/routes/crud-access-control.d.mts +23 -23
  98. package/dist/plugins/organization/routes/crud-invites.d.mts +60 -60
  99. package/dist/plugins/organization/routes/crud-members.d.mts +29 -29
  100. package/dist/plugins/organization/routes/crud-org.d.mts +25 -25
  101. package/dist/plugins/organization/routes/crud-team.d.mts +79 -79
  102. package/dist/plugins/phone-number/index.d.mts +33 -33
  103. package/dist/plugins/siwe/index.d.mts +3 -3
  104. package/dist/plugins/siwe/index.mjs +1 -1
  105. package/dist/plugins/two-factor/backup-codes/index.d.mts +5 -5
  106. package/dist/plugins/two-factor/index.d.mts +18 -18
  107. package/dist/plugins/two-factor/otp/index.d.mts +3 -3
  108. package/dist/plugins/two-factor/totp/index.d.mts +5 -5
  109. package/dist/plugins/username/index.d.mts +12 -12
  110. package/dist/plugins/username/schema.d.mts +3 -3
  111. package/dist/test-utils/test-instance.d.mts +1219 -1191
  112. package/dist/test-utils/test-instance.mjs +1 -1
  113. package/package.json +4 -4
@@ -10,6 +10,7 @@ const memoryAdapter = (db, config) => {
10
10
  adapterName: "Memory Adapter",
11
11
  usePlural: false,
12
12
  debugLogs: config?.debugLogs || false,
13
+ supportsArrays: true,
13
14
  customTransformInput(props) {
14
15
  if ((props.options.advanced?.database?.useNumberId || props.options.advanced?.database?.generateId === "serial") && props.field === "id" && props.action === "create") return db[props.model].length + 1;
15
16
  return props.data;
@@ -1 +1 @@
1
- {"version":3,"file":"memory-adapter.mjs","names":["lazyOptions: BetterAuthOptions | null","model","where","nested: Record<string, any>"],"sources":["../../../src/adapters/memory-adapter/memory-adapter.ts"],"sourcesContent":["import type { BetterAuthOptions } from \"@better-auth/core\";\nimport type {\n\tCleanedWhere,\n\tDBAdapterDebugLogOption,\n\tJoinConfig,\n} from \"@better-auth/core/db/adapter\";\nimport { createAdapterFactory } from \"@better-auth/core/db/adapter\";\nimport { logger } from \"@better-auth/core/env\";\n\nexport interface MemoryDB {\n\t[key: string]: any[];\n}\n\nexport interface MemoryAdapterConfig {\n\tdebugLogs?: DBAdapterDebugLogOption | undefined;\n}\n\nexport const memoryAdapter = (\n\tdb: MemoryDB,\n\tconfig?: MemoryAdapterConfig | undefined,\n) => {\n\tlet lazyOptions: BetterAuthOptions | null = null;\n\tlet adapterCreator = createAdapterFactory({\n\t\tconfig: {\n\t\t\tadapterId: \"memory\",\n\t\t\tadapterName: \"Memory Adapter\",\n\t\t\tusePlural: false,\n\t\t\tdebugLogs: config?.debugLogs || false,\n\t\t\tcustomTransformInput(props) {\n\t\t\t\tconst useNumberId =\n\t\t\t\t\tprops.options.advanced?.database?.useNumberId ||\n\t\t\t\t\tprops.options.advanced?.database?.generateId === \"serial\";\n\t\t\t\tif (useNumberId && props.field === \"id\" && props.action === \"create\") {\n\t\t\t\t\treturn db[props.model]!.length + 1;\n\t\t\t\t}\n\t\t\t\treturn props.data;\n\t\t\t},\n\t\t\ttransaction: async (cb) => {\n\t\t\t\tlet clone = structuredClone(db);\n\t\t\t\ttry {\n\t\t\t\t\tconst r = await cb(adapterCreator(lazyOptions!));\n\t\t\t\t\treturn r;\n\t\t\t\t} catch (error) {\n\t\t\t\t\t// Rollback changes\n\t\t\t\t\tObject.keys(db).forEach((key) => {\n\t\t\t\t\t\tdb[key] = clone[key]!;\n\t\t\t\t\t});\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t\tadapter: ({ getFieldName, options, getModelName }) => {\n\t\t\tconst applySortToRecords = (\n\t\t\t\trecords: any[],\n\t\t\t\tsortBy: { field: string; direction: \"asc\" | \"desc\" } | undefined,\n\t\t\t\tmodel: string,\n\t\t\t) => {\n\t\t\t\tif (!sortBy) return records;\n\t\t\t\treturn records.sort((a: any, b: any) => {\n\t\t\t\t\tconst field = getFieldName({ model, field: sortBy.field });\n\t\t\t\t\tconst aValue = a[field];\n\t\t\t\t\tconst bValue = b[field];\n\n\t\t\t\t\tlet comparison = 0;\n\n\t\t\t\t\t// Handle null/undefined values\n\t\t\t\t\tif (aValue == null && bValue == null) {\n\t\t\t\t\t\tcomparison = 0;\n\t\t\t\t\t} else if (aValue == null) {\n\t\t\t\t\t\tcomparison = -1;\n\t\t\t\t\t} else if (bValue == null) {\n\t\t\t\t\t\tcomparison = 1;\n\t\t\t\t\t}\n\t\t\t\t\t// Handle string comparison\n\t\t\t\t\telse if (typeof aValue === \"string\" && typeof bValue === \"string\") {\n\t\t\t\t\t\tcomparison = aValue.localeCompare(bValue);\n\t\t\t\t\t}\n\t\t\t\t\t// Handle date comparison\n\t\t\t\t\telse if (aValue instanceof Date && bValue instanceof Date) {\n\t\t\t\t\t\tcomparison = aValue.getTime() - bValue.getTime();\n\t\t\t\t\t}\n\t\t\t\t\t// Handle numeric comparison\n\t\t\t\t\telse if (typeof aValue === \"number\" && typeof bValue === \"number\") {\n\t\t\t\t\t\tcomparison = aValue - bValue;\n\t\t\t\t\t}\n\t\t\t\t\t// Handle boolean comparison\n\t\t\t\t\telse if (typeof aValue === \"boolean\" && typeof bValue === \"boolean\") {\n\t\t\t\t\t\tcomparison = aValue === bValue ? 0 : aValue ? 1 : -1;\n\t\t\t\t\t}\n\t\t\t\t\t// Fallback to string comparison\n\t\t\t\t\telse {\n\t\t\t\t\t\tcomparison = String(aValue).localeCompare(String(bValue));\n\t\t\t\t\t}\n\n\t\t\t\t\treturn sortBy.direction === \"asc\" ? comparison : -comparison;\n\t\t\t\t});\n\t\t\t};\n\n\t\t\tfunction convertWhereClause(\n\t\t\t\twhere: CleanedWhere[],\n\t\t\t\tmodel: string,\n\t\t\t\tjoin?: JoinConfig,\n\t\t\t): any[] {\n\t\t\t\tconst execute = (where: CleanedWhere[], model: string) => {\n\t\t\t\t\tconst table = db[model];\n\t\t\t\t\tif (!table) {\n\t\t\t\t\t\tlogger.error(\n\t\t\t\t\t\t\t`[MemoryAdapter] Model ${model} not found in the DB`,\n\t\t\t\t\t\t\tObject.keys(db),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tthrow new Error(`Model ${model} not found`);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst evalClause = (record: any, clause: CleanedWhere): boolean => {\n\t\t\t\t\t\tconst { field, value, operator } = clause;\n\t\t\t\t\t\tswitch (operator) {\n\t\t\t\t\t\t\tcase \"in\":\n\t\t\t\t\t\t\t\tif (!Array.isArray(value)) {\n\t\t\t\t\t\t\t\t\tthrow new Error(\"Value must be an array\");\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\t\treturn value.includes(record[field]);\n\t\t\t\t\t\t\tcase \"not_in\":\n\t\t\t\t\t\t\t\tif (!Array.isArray(value)) {\n\t\t\t\t\t\t\t\t\tthrow new Error(\"Value must be an array\");\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\t\treturn !value.includes(record[field]);\n\t\t\t\t\t\t\tcase \"contains\":\n\t\t\t\t\t\t\t\treturn record[field].includes(value);\n\t\t\t\t\t\t\tcase \"starts_with\":\n\t\t\t\t\t\t\t\treturn record[field].startsWith(value);\n\t\t\t\t\t\t\tcase \"ends_with\":\n\t\t\t\t\t\t\t\treturn record[field].endsWith(value);\n\t\t\t\t\t\t\tcase \"ne\":\n\t\t\t\t\t\t\t\treturn record[field] !== value;\n\t\t\t\t\t\t\tcase \"gt\":\n\t\t\t\t\t\t\t\treturn value != null && Boolean(record[field] > value);\n\t\t\t\t\t\t\tcase \"gte\":\n\t\t\t\t\t\t\t\treturn value != null && Boolean(record[field] >= value);\n\t\t\t\t\t\t\tcase \"lt\":\n\t\t\t\t\t\t\t\treturn value != null && Boolean(record[field] < value);\n\t\t\t\t\t\t\tcase \"lte\":\n\t\t\t\t\t\t\t\treturn value != null && Boolean(record[field] <= value);\n\t\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\t\treturn record[field] === value;\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\treturn table.filter((record: any) => {\n\t\t\t\t\t\tif (!where.length || where.length === 0) {\n\t\t\t\t\t\t\treturn true;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tlet result = evalClause(record, where[0]!);\n\t\t\t\t\t\tfor (const clause of where) {\n\t\t\t\t\t\t\tconst clauseResult = evalClause(record, clause);\n\n\t\t\t\t\t\t\tif (clause.connector === \"OR\") {\n\t\t\t\t\t\t\t\tresult = result || clauseResult;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tresult = result && clauseResult;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t});\n\t\t\t\t};\n\n\t\t\t\tif (!join) return execute(where, model);\n\n\t\t\t\tconst baseRecords = execute(where, model);\n\n\t\t\t\t// Group results by base model and nest joined data as arrays\n\t\t\t\tconst grouped = new Map<string, any>();\n\t\t\t\t// Track seen IDs per joined model for O(1) deduplication\n\t\t\t\tconst seenIds = new Map<string, Set<string>>();\n\n\t\t\t\tfor (const baseRecord of baseRecords) {\n\t\t\t\t\tconst baseId = String(baseRecord.id);\n\n\t\t\t\t\tif (!grouped.has(baseId)) {\n\t\t\t\t\t\tconst nested: Record<string, any> = { ...baseRecord };\n\n\t\t\t\t\t\t// Initialize joined data structures based on isUnique\n\t\t\t\t\t\tfor (const [joinModel, joinAttr] of Object.entries(join)) {\n\t\t\t\t\t\t\tconst joinModelName = getModelName(joinModel);\n\t\t\t\t\t\t\tif (joinAttr.relation === \"one-to-one\") {\n\t\t\t\t\t\t\t\tnested[joinModelName] = null;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tnested[joinModelName] = [];\n\t\t\t\t\t\t\t\tseenIds.set(`${baseId}-${joinModel}`, new Set());\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tgrouped.set(baseId, nested);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst nestedEntry = grouped.get(baseId)!;\n\n\t\t\t\t\t// Add joined data\n\t\t\t\t\tfor (const [joinModel, joinAttr] of Object.entries(join)) {\n\t\t\t\t\t\tconst joinModelName = getModelName(joinModel);\n\t\t\t\t\t\tconst joinTable = db[joinModelName];\n\t\t\t\t\t\tif (!joinTable) {\n\t\t\t\t\t\t\tlogger.error(\n\t\t\t\t\t\t\t\t`[MemoryAdapter] JoinOption model ${joinModelName} not found in the DB`,\n\t\t\t\t\t\t\t\tObject.keys(db),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tthrow new Error(`JoinOption model ${joinModelName} not found`);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst matchingRecords = joinTable.filter(\n\t\t\t\t\t\t\t(joinRecord: any) =>\n\t\t\t\t\t\t\t\tjoinRecord[joinAttr.on.to] === baseRecord[joinAttr.on.from],\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tif (joinAttr.relation === \"one-to-one\") {\n\t\t\t\t\t\t\t// For unique relationships, store a single object (or null)\n\t\t\t\t\t\t\tnestedEntry[joinModelName] = matchingRecords[0] || null;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// For non-unique relationships, store array with limit\n\t\t\t\t\t\t\tconst seenSet = seenIds.get(`${baseId}-${joinModel}`)!;\n\t\t\t\t\t\t\tconst limit = joinAttr.limit ?? 100;\n\t\t\t\t\t\t\tlet count = 0;\n\n\t\t\t\t\t\t\tfor (const matchingRecord of matchingRecords) {\n\t\t\t\t\t\t\t\tif (count >= limit) break;\n\t\t\t\t\t\t\t\tif (!seenSet.has(matchingRecord.id)) {\n\t\t\t\t\t\t\t\t\tnestedEntry[joinModelName].push(matchingRecord);\n\t\t\t\t\t\t\t\t\tseenSet.add(matchingRecord.id);\n\t\t\t\t\t\t\t\t\tcount++;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn Array.from(grouped.values());\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tcreate: async ({ model, data }) => {\n\t\t\t\t\tconst useNumberId =\n\t\t\t\t\t\toptions.advanced?.database?.useNumberId ||\n\t\t\t\t\t\toptions.advanced?.database?.generateId === \"serial\";\n\t\t\t\t\tif (useNumberId) {\n\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\tdata.id = db[getModelName(model)]!.length + 1;\n\t\t\t\t\t}\n\t\t\t\t\tif (!db[model]) {\n\t\t\t\t\t\tdb[model] = [];\n\t\t\t\t\t}\n\t\t\t\t\tdb[model]!.push(data);\n\t\t\t\t\treturn data;\n\t\t\t\t},\n\t\t\t\tfindOne: async ({ model, where, join }) => {\n\t\t\t\t\tconst res = convertWhereClause(where, model, join);\n\t\t\t\t\tif (join) {\n\t\t\t\t\t\t// When join is present, res is an array of nested objects\n\t\t\t\t\t\tconst resArray = res as any[];\n\t\t\t\t\t\tif (!resArray.length) {\n\t\t\t\t\t\t\treturn null;\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// Return the first nested object\n\t\t\t\t\t\treturn resArray[0];\n\t\t\t\t\t}\n\t\t\t\t\t// Without join, res is an array\n\t\t\t\t\tconst resArray = res as any[];\n\t\t\t\t\tconst record = resArray[0] || null;\n\t\t\t\t\treturn record;\n\t\t\t\t},\n\t\t\t\tfindMany: async ({ model, where, sortBy, limit, offset, join }) => {\n\t\t\t\t\tlet res = convertWhereClause(where || [], model, join);\n\n\t\t\t\t\tif (join) {\n\t\t\t\t\t\t// When join is present, res is an array of nested objects\n\t\t\t\t\t\tconst resArray = res as any[];\n\t\t\t\t\t\tif (!resArray.length) {\n\t\t\t\t\t\t\treturn [];\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Apply sorting to nested objects\n\t\t\t\t\t\tapplySortToRecords(resArray, sortBy, model);\n\n\t\t\t\t\t\t// Apply offset and limit\n\t\t\t\t\t\tlet paginatedRecords = resArray;\n\t\t\t\t\t\tif (offset !== undefined) {\n\t\t\t\t\t\t\tpaginatedRecords = paginatedRecords.slice(offset);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (limit !== undefined) {\n\t\t\t\t\t\t\tpaginatedRecords = paginatedRecords.slice(0, limit);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn paginatedRecords;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Without join - original logic\n\t\t\t\t\tconst resArray = res as any[];\n\t\t\t\t\tlet table = applySortToRecords(resArray, sortBy, model);\n\t\t\t\t\tif (offset !== undefined) {\n\t\t\t\t\t\ttable = table!.slice(offset);\n\t\t\t\t\t}\n\t\t\t\t\tif (limit !== undefined) {\n\t\t\t\t\t\ttable = table!.slice(0, limit);\n\t\t\t\t\t}\n\t\t\t\t\treturn table || [];\n\t\t\t\t},\n\t\t\t\tcount: async ({ model, where }) => {\n\t\t\t\t\tif (where) {\n\t\t\t\t\t\tconst filteredRecords = convertWhereClause(where, model);\n\t\t\t\t\t\treturn filteredRecords.length;\n\t\t\t\t\t}\n\t\t\t\t\treturn db[model]!.length;\n\t\t\t\t},\n\t\t\t\tupdate: async ({ model, where, update }) => {\n\t\t\t\t\tconst res = convertWhereClause(where, model);\n\t\t\t\t\tres.forEach((record) => {\n\t\t\t\t\t\tObject.assign(record, update);\n\t\t\t\t\t});\n\t\t\t\t\treturn res[0] || null;\n\t\t\t\t},\n\t\t\t\tdelete: async ({ model, where }) => {\n\t\t\t\t\tconst table = db[model]!;\n\t\t\t\t\tconst res = convertWhereClause(where, model);\n\t\t\t\t\tdb[model] = table.filter((record) => !res.includes(record));\n\t\t\t\t},\n\t\t\t\tdeleteMany: async ({ model, where }) => {\n\t\t\t\t\tconst table = db[model]!;\n\t\t\t\t\tconst res = convertWhereClause(where, model);\n\t\t\t\t\tlet count = 0;\n\t\t\t\t\tdb[model] = table.filter((record) => {\n\t\t\t\t\t\tif (res.includes(record)) {\n\t\t\t\t\t\t\tcount++;\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn !res.includes(record);\n\t\t\t\t\t});\n\t\t\t\t\treturn count;\n\t\t\t\t},\n\t\t\t\tupdateMany({ model, where, update }) {\n\t\t\t\t\tconst res = convertWhereClause(where, model);\n\t\t\t\t\tres.forEach((record) => {\n\t\t\t\t\t\tObject.assign(record, update);\n\t\t\t\t\t});\n\t\t\t\t\treturn res[0] || null;\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t});\n\treturn (options: BetterAuthOptions) => {\n\t\tlazyOptions = options;\n\t\treturn adapterCreator(options);\n\t};\n};\n"],"mappings":";;;;AAiBA,MAAa,iBACZ,IACA,WACI;CACJ,IAAIA,cAAwC;CA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AAO,OAAO,EAAE,OAAO,YAAY;AAClC,SAAI,MAEH,QADwB,mBAAmB,OAAO,MAAM,CACjC;AAExB,YAAO,GAAG,OAAQ;;IAEnB,QAAQ,OAAO,EAAE,OAAO,OAAO,aAAa;KAC3C,MAAM,MAAM,mBAAmB,OAAO,MAAM;AAC5C,SAAI,SAAS,WAAW;AACvB,aAAO,OAAO,QAAQ,OAAO;OAC5B;AACF,YAAO,IAAI,MAAM;;IAElB,QAAQ,OAAO,EAAE,OAAO,YAAY;KACnC,MAAM,QAAQ,GAAG;KACjB,MAAM,MAAM,mBAAmB,OAAO,MAAM;AAC5C,QAAG,SAAS,MAAM,QAAQ,WAAW,CAAC,IAAI,SAAS,OAAO,CAAC;;IAE5D,YAAY,OAAO,EAAE,OAAO,YAAY;KACvC,MAAM,QAAQ,GAAG;KACjB,MAAM,MAAM,mBAAmB,OAAO,MAAM;KAC5C,IAAI,QAAQ;AACZ,QAAG,SAAS,MAAM,QAAQ,WAAW;AACpC,UAAI,IAAI,SAAS,OAAO,EAAE;AACzB;AACA,cAAO;;AAER,aAAO,CAAC,IAAI,SAAS,OAAO;OAC3B;AACF,YAAO;;IAER,WAAW,EAAE,OAAO,OAAO,UAAU;KACpC,MAAM,MAAM,mBAAmB,OAAO,MAAM;AAC5C,SAAI,SAAS,WAAW;AACvB,aAAO,OAAO,QAAQ,OAAO;OAC5B;AACF,YAAO,IAAI,MAAM;;IAElB;;EAEF,CAAC;AACF,SAAQ,YAA+B;AACtC,gBAAc;AACd,SAAO,eAAe,QAAQ"}
1
+ {"version":3,"file":"memory-adapter.mjs","names":["lazyOptions: BetterAuthOptions | null","model","where","nested: Record<string, any>"],"sources":["../../../src/adapters/memory-adapter/memory-adapter.ts"],"sourcesContent":["import type { BetterAuthOptions } from \"@better-auth/core\";\nimport type {\n\tCleanedWhere,\n\tDBAdapterDebugLogOption,\n\tJoinConfig,\n} from \"@better-auth/core/db/adapter\";\nimport { createAdapterFactory } from \"@better-auth/core/db/adapter\";\nimport { logger } from \"@better-auth/core/env\";\n\nexport interface MemoryDB {\n\t[key: string]: any[];\n}\n\nexport interface MemoryAdapterConfig {\n\tdebugLogs?: DBAdapterDebugLogOption | undefined;\n}\n\nexport const memoryAdapter = (\n\tdb: MemoryDB,\n\tconfig?: MemoryAdapterConfig | undefined,\n) => {\n\tlet lazyOptions: BetterAuthOptions | null = null;\n\tlet adapterCreator = createAdapterFactory({\n\t\tconfig: {\n\t\t\tadapterId: \"memory\",\n\t\t\tadapterName: \"Memory Adapter\",\n\t\t\tusePlural: false,\n\t\t\tdebugLogs: config?.debugLogs || false,\n\t\t\tsupportsArrays: true,\n\t\t\tcustomTransformInput(props) {\n\t\t\t\tconst useNumberId =\n\t\t\t\t\tprops.options.advanced?.database?.useNumberId ||\n\t\t\t\t\tprops.options.advanced?.database?.generateId === \"serial\";\n\t\t\t\tif (useNumberId && props.field === \"id\" && props.action === \"create\") {\n\t\t\t\t\treturn db[props.model]!.length + 1;\n\t\t\t\t}\n\t\t\t\treturn props.data;\n\t\t\t},\n\t\t\ttransaction: async (cb) => {\n\t\t\t\tlet clone = structuredClone(db);\n\t\t\t\ttry {\n\t\t\t\t\tconst r = await cb(adapterCreator(lazyOptions!));\n\t\t\t\t\treturn r;\n\t\t\t\t} catch (error) {\n\t\t\t\t\t// Rollback changes\n\t\t\t\t\tObject.keys(db).forEach((key) => {\n\t\t\t\t\t\tdb[key] = clone[key]!;\n\t\t\t\t\t});\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t\tadapter: ({ getFieldName, options, getModelName }) => {\n\t\t\tconst applySortToRecords = (\n\t\t\t\trecords: any[],\n\t\t\t\tsortBy: { field: string; direction: \"asc\" | \"desc\" } | undefined,\n\t\t\t\tmodel: string,\n\t\t\t) => {\n\t\t\t\tif (!sortBy) return records;\n\t\t\t\treturn records.sort((a: any, b: any) => {\n\t\t\t\t\tconst field = getFieldName({ model, field: sortBy.field });\n\t\t\t\t\tconst aValue = a[field];\n\t\t\t\t\tconst bValue = b[field];\n\n\t\t\t\t\tlet comparison = 0;\n\n\t\t\t\t\t// Handle null/undefined values\n\t\t\t\t\tif (aValue == null && bValue == null) {\n\t\t\t\t\t\tcomparison = 0;\n\t\t\t\t\t} else if (aValue == null) {\n\t\t\t\t\t\tcomparison = -1;\n\t\t\t\t\t} else if (bValue == null) {\n\t\t\t\t\t\tcomparison = 1;\n\t\t\t\t\t}\n\t\t\t\t\t// Handle string comparison\n\t\t\t\t\telse if (typeof aValue === \"string\" && typeof bValue === \"string\") {\n\t\t\t\t\t\tcomparison = aValue.localeCompare(bValue);\n\t\t\t\t\t}\n\t\t\t\t\t// Handle date comparison\n\t\t\t\t\telse if (aValue instanceof Date && bValue instanceof Date) {\n\t\t\t\t\t\tcomparison = aValue.getTime() - bValue.getTime();\n\t\t\t\t\t}\n\t\t\t\t\t// Handle numeric comparison\n\t\t\t\t\telse if (typeof aValue === \"number\" && typeof bValue === \"number\") {\n\t\t\t\t\t\tcomparison = aValue - bValue;\n\t\t\t\t\t}\n\t\t\t\t\t// Handle boolean comparison\n\t\t\t\t\telse if (typeof aValue === \"boolean\" && typeof bValue === \"boolean\") {\n\t\t\t\t\t\tcomparison = aValue === bValue ? 0 : aValue ? 1 : -1;\n\t\t\t\t\t}\n\t\t\t\t\t// Fallback to string comparison\n\t\t\t\t\telse {\n\t\t\t\t\t\tcomparison = String(aValue).localeCompare(String(bValue));\n\t\t\t\t\t}\n\n\t\t\t\t\treturn sortBy.direction === \"asc\" ? comparison : -comparison;\n\t\t\t\t});\n\t\t\t};\n\n\t\t\tfunction convertWhereClause(\n\t\t\t\twhere: CleanedWhere[],\n\t\t\t\tmodel: string,\n\t\t\t\tjoin?: JoinConfig,\n\t\t\t): any[] {\n\t\t\t\tconst execute = (where: CleanedWhere[], model: string) => {\n\t\t\t\t\tconst table = db[model];\n\t\t\t\t\tif (!table) {\n\t\t\t\t\t\tlogger.error(\n\t\t\t\t\t\t\t`[MemoryAdapter] Model ${model} not found in the DB`,\n\t\t\t\t\t\t\tObject.keys(db),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tthrow new Error(`Model ${model} not found`);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst evalClause = (record: any, clause: CleanedWhere): boolean => {\n\t\t\t\t\t\tconst { field, value, operator } = clause;\n\t\t\t\t\t\tswitch (operator) {\n\t\t\t\t\t\t\tcase \"in\":\n\t\t\t\t\t\t\t\tif (!Array.isArray(value)) {\n\t\t\t\t\t\t\t\t\tthrow new Error(\"Value must be an array\");\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\t\treturn value.includes(record[field]);\n\t\t\t\t\t\t\tcase \"not_in\":\n\t\t\t\t\t\t\t\tif (!Array.isArray(value)) {\n\t\t\t\t\t\t\t\t\tthrow new Error(\"Value must be an array\");\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\t\treturn !value.includes(record[field]);\n\t\t\t\t\t\t\tcase \"contains\":\n\t\t\t\t\t\t\t\treturn record[field].includes(value);\n\t\t\t\t\t\t\tcase \"starts_with\":\n\t\t\t\t\t\t\t\treturn record[field].startsWith(value);\n\t\t\t\t\t\t\tcase \"ends_with\":\n\t\t\t\t\t\t\t\treturn record[field].endsWith(value);\n\t\t\t\t\t\t\tcase \"ne\":\n\t\t\t\t\t\t\t\treturn record[field] !== value;\n\t\t\t\t\t\t\tcase \"gt\":\n\t\t\t\t\t\t\t\treturn value != null && Boolean(record[field] > value);\n\t\t\t\t\t\t\tcase \"gte\":\n\t\t\t\t\t\t\t\treturn value != null && Boolean(record[field] >= value);\n\t\t\t\t\t\t\tcase \"lt\":\n\t\t\t\t\t\t\t\treturn value != null && Boolean(record[field] < value);\n\t\t\t\t\t\t\tcase \"lte\":\n\t\t\t\t\t\t\t\treturn value != null && Boolean(record[field] <= value);\n\t\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\t\treturn record[field] === value;\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\treturn table.filter((record: any) => {\n\t\t\t\t\t\tif (!where.length || where.length === 0) {\n\t\t\t\t\t\t\treturn true;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tlet result = evalClause(record, where[0]!);\n\t\t\t\t\t\tfor (const clause of where) {\n\t\t\t\t\t\t\tconst clauseResult = evalClause(record, clause);\n\n\t\t\t\t\t\t\tif (clause.connector === \"OR\") {\n\t\t\t\t\t\t\t\tresult = result || clauseResult;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tresult = result && clauseResult;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t});\n\t\t\t\t};\n\n\t\t\t\tif (!join) return execute(where, model);\n\n\t\t\t\tconst baseRecords = execute(where, model);\n\n\t\t\t\t// Group results by base model and nest joined data as arrays\n\t\t\t\tconst grouped = new Map<string, any>();\n\t\t\t\t// Track seen IDs per joined model for O(1) deduplication\n\t\t\t\tconst seenIds = new Map<string, Set<string>>();\n\n\t\t\t\tfor (const baseRecord of baseRecords) {\n\t\t\t\t\tconst baseId = String(baseRecord.id);\n\n\t\t\t\t\tif (!grouped.has(baseId)) {\n\t\t\t\t\t\tconst nested: Record<string, any> = { ...baseRecord };\n\n\t\t\t\t\t\t// Initialize joined data structures based on isUnique\n\t\t\t\t\t\tfor (const [joinModel, joinAttr] of Object.entries(join)) {\n\t\t\t\t\t\t\tconst joinModelName = getModelName(joinModel);\n\t\t\t\t\t\t\tif (joinAttr.relation === \"one-to-one\") {\n\t\t\t\t\t\t\t\tnested[joinModelName] = null;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tnested[joinModelName] = [];\n\t\t\t\t\t\t\t\tseenIds.set(`${baseId}-${joinModel}`, new Set());\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tgrouped.set(baseId, nested);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst nestedEntry = grouped.get(baseId)!;\n\n\t\t\t\t\t// Add joined data\n\t\t\t\t\tfor (const [joinModel, joinAttr] of Object.entries(join)) {\n\t\t\t\t\t\tconst joinModelName = getModelName(joinModel);\n\t\t\t\t\t\tconst joinTable = db[joinModelName];\n\t\t\t\t\t\tif (!joinTable) {\n\t\t\t\t\t\t\tlogger.error(\n\t\t\t\t\t\t\t\t`[MemoryAdapter] JoinOption model ${joinModelName} not found in the DB`,\n\t\t\t\t\t\t\t\tObject.keys(db),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tthrow new Error(`JoinOption model ${joinModelName} not found`);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst matchingRecords = joinTable.filter(\n\t\t\t\t\t\t\t(joinRecord: any) =>\n\t\t\t\t\t\t\t\tjoinRecord[joinAttr.on.to] === baseRecord[joinAttr.on.from],\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tif (joinAttr.relation === \"one-to-one\") {\n\t\t\t\t\t\t\t// For unique relationships, store a single object (or null)\n\t\t\t\t\t\t\tnestedEntry[joinModelName] = matchingRecords[0] || null;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// For non-unique relationships, store array with limit\n\t\t\t\t\t\t\tconst seenSet = seenIds.get(`${baseId}-${joinModel}`)!;\n\t\t\t\t\t\t\tconst limit = joinAttr.limit ?? 100;\n\t\t\t\t\t\t\tlet count = 0;\n\n\t\t\t\t\t\t\tfor (const matchingRecord of matchingRecords) {\n\t\t\t\t\t\t\t\tif (count >= limit) break;\n\t\t\t\t\t\t\t\tif (!seenSet.has(matchingRecord.id)) {\n\t\t\t\t\t\t\t\t\tnestedEntry[joinModelName].push(matchingRecord);\n\t\t\t\t\t\t\t\t\tseenSet.add(matchingRecord.id);\n\t\t\t\t\t\t\t\t\tcount++;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn Array.from(grouped.values());\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tcreate: async ({ model, data }) => {\n\t\t\t\t\tconst useNumberId =\n\t\t\t\t\t\toptions.advanced?.database?.useNumberId ||\n\t\t\t\t\t\toptions.advanced?.database?.generateId === \"serial\";\n\t\t\t\t\tif (useNumberId) {\n\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\tdata.id = db[getModelName(model)]!.length + 1;\n\t\t\t\t\t}\n\t\t\t\t\tif (!db[model]) {\n\t\t\t\t\t\tdb[model] = [];\n\t\t\t\t\t}\n\t\t\t\t\tdb[model]!.push(data);\n\t\t\t\t\treturn data;\n\t\t\t\t},\n\t\t\t\tfindOne: async ({ model, where, join }) => {\n\t\t\t\t\tconst res = convertWhereClause(where, model, join);\n\t\t\t\t\tif (join) {\n\t\t\t\t\t\t// When join is present, res is an array of nested objects\n\t\t\t\t\t\tconst resArray = res as any[];\n\t\t\t\t\t\tif (!resArray.length) {\n\t\t\t\t\t\t\treturn null;\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// Return the first nested object\n\t\t\t\t\t\treturn resArray[0];\n\t\t\t\t\t}\n\t\t\t\t\t// Without join, res is an array\n\t\t\t\t\tconst resArray = res as any[];\n\t\t\t\t\tconst record = resArray[0] || null;\n\t\t\t\t\treturn record;\n\t\t\t\t},\n\t\t\t\tfindMany: async ({ model, where, sortBy, limit, offset, join }) => {\n\t\t\t\t\tlet res = convertWhereClause(where || [], model, join);\n\n\t\t\t\t\tif (join) {\n\t\t\t\t\t\t// When join is present, res is an array of nested objects\n\t\t\t\t\t\tconst resArray = res as any[];\n\t\t\t\t\t\tif (!resArray.length) {\n\t\t\t\t\t\t\treturn [];\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Apply sorting to nested objects\n\t\t\t\t\t\tapplySortToRecords(resArray, sortBy, model);\n\n\t\t\t\t\t\t// Apply offset and limit\n\t\t\t\t\t\tlet paginatedRecords = resArray;\n\t\t\t\t\t\tif (offset !== undefined) {\n\t\t\t\t\t\t\tpaginatedRecords = paginatedRecords.slice(offset);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (limit !== undefined) {\n\t\t\t\t\t\t\tpaginatedRecords = paginatedRecords.slice(0, limit);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn paginatedRecords;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Without join - original logic\n\t\t\t\t\tconst resArray = res as any[];\n\t\t\t\t\tlet table = applySortToRecords(resArray, sortBy, model);\n\t\t\t\t\tif (offset !== undefined) {\n\t\t\t\t\t\ttable = table!.slice(offset);\n\t\t\t\t\t}\n\t\t\t\t\tif (limit !== undefined) {\n\t\t\t\t\t\ttable = table!.slice(0, limit);\n\t\t\t\t\t}\n\t\t\t\t\treturn table || [];\n\t\t\t\t},\n\t\t\t\tcount: async ({ model, where }) => {\n\t\t\t\t\tif (where) {\n\t\t\t\t\t\tconst filteredRecords = convertWhereClause(where, model);\n\t\t\t\t\t\treturn filteredRecords.length;\n\t\t\t\t\t}\n\t\t\t\t\treturn db[model]!.length;\n\t\t\t\t},\n\t\t\t\tupdate: async ({ model, where, update }) => {\n\t\t\t\t\tconst res = convertWhereClause(where, model);\n\t\t\t\t\tres.forEach((record) => {\n\t\t\t\t\t\tObject.assign(record, update);\n\t\t\t\t\t});\n\t\t\t\t\treturn res[0] || null;\n\t\t\t\t},\n\t\t\t\tdelete: async ({ model, where }) => {\n\t\t\t\t\tconst table = db[model]!;\n\t\t\t\t\tconst res = convertWhereClause(where, model);\n\t\t\t\t\tdb[model] = table.filter((record) => !res.includes(record));\n\t\t\t\t},\n\t\t\t\tdeleteMany: async ({ model, where }) => {\n\t\t\t\t\tconst table = db[model]!;\n\t\t\t\t\tconst res = convertWhereClause(where, model);\n\t\t\t\t\tlet count = 0;\n\t\t\t\t\tdb[model] = table.filter((record) => {\n\t\t\t\t\t\tif (res.includes(record)) {\n\t\t\t\t\t\t\tcount++;\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn !res.includes(record);\n\t\t\t\t\t});\n\t\t\t\t\treturn count;\n\t\t\t\t},\n\t\t\t\tupdateMany({ model, where, update }) {\n\t\t\t\t\tconst res = convertWhereClause(where, model);\n\t\t\t\t\tres.forEach((record) => {\n\t\t\t\t\t\tObject.assign(record, update);\n\t\t\t\t\t});\n\t\t\t\t\treturn res[0] || null;\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t});\n\treturn (options: BetterAuthOptions) => {\n\t\tlazyOptions = options;\n\t\treturn adapterCreator(options);\n\t};\n};\n"],"mappings":";;;;AAiBA,MAAa,iBACZ,IACA,WACI;CACJ,IAAIA,cAAwC;CAC5C,IAAI,iBAAiB,qBAAqB;EACzC,QAAQ;GACP,WAAW;GACX,aAAa;GACb,WAAW;GACX,WAAW,QAAQ,aAAa;GAChC,gBAAgB;GAChB,qBAAqB,OAAO;AAI3B,SAFC,MAAM,QAAQ,UAAU,UAAU,eAClC,MAAM,QAAQ,UAAU,UAAU,eAAe,aAC/B,MAAM,UAAU,QAAQ,MAAM,WAAW,SAC3D,QAAO,GAAG,MAAM,OAAQ,SAAS;AAElC,WAAO,MAAM;;GAEd,aAAa,OAAO,OAAO;IAC1B,IAAI,QAAQ,gBAAgB,GAAG;AAC/B,QAAI;AAEH,YADU,MAAM,GAAG,eAAe,YAAa,CAAC;aAExC,OAAO;AAEf,YAAO,KAAK,GAAG,CAAC,SAAS,QAAQ;AAChC,SAAG,OAAO,MAAM;OACf;AACF,WAAM;;;GAGR;EACD,UAAU,EAAE,cAAc,SAAS,mBAAmB;GACrD,MAAM,sBACL,SACA,QACA,UACI;AACJ,QAAI,CAAC,OAAQ,QAAO;AACpB,WAAO,QAAQ,MAAM,GAAQ,MAAW;KACvC,MAAM,QAAQ,aAAa;MAAE;MAAO,OAAO,OAAO;MAAO,CAAC;KAC1D,MAAM,SAAS,EAAE;KACjB,MAAM,SAAS,EAAE;KAEjB,I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AM,mBAAmB,OAAO,MAAM;AAC5C,SAAI,SAAS,WAAW;AACvB,aAAO,OAAO,QAAQ,OAAO;OAC5B;AACF,YAAO,IAAI,MAAM;;IAElB;;EAEF,CAAC;AACF,SAAQ,YAA+B;AACtC,gBAAc;AACd,SAAO,eAAe,QAAQ"}
@@ -307,6 +307,7 @@ const mongodbAdapter = (db, config) => {
307
307
  debugLogs: config?.debugLogs ?? false,
308
308
  mapKeysTransformInput: { id: "_id" },
309
309
  mapKeysTransformOutput: { _id: "id" },
310
+ supportsArrays: true,
310
311
  supportsNumericIds: false,
311
312
  transaction: config?.client && (config?.transaction ?? true) ? async (cb) => {
312
313
  if (!config.client) return cb(lazyAdapter(lazyOptions));
@@ -1 +1 @@
1
- {"version":3,"file":"mongodb-adapter.mjs","names":["lazyOptions: BetterAuthOptions | null","condition: any","db","pipeline: any[]","projection: any","limit","lazyAdapter:\n\t\t| ((options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>)\n\t\t| null","adapterOptions: AdapterFactoryOptions | null"],"sources":["../../../src/adapters/mongodb-adapter/mongodb-adapter.ts"],"sourcesContent":["import type { BetterAuthOptions } from \"@better-auth/core\";\nimport type {\n\tAdapterFactoryCustomizeAdapterCreator,\n\tAdapterFactoryOptions,\n\tDBAdapter,\n\tDBAdapterDebugLogOption,\n\tWhere,\n} from \"@better-auth/core/db/adapter\";\nimport { createAdapterFactory } from \"@better-auth/core/db/adapter\";\nimport type { ClientSession, Db, MongoClient } from \"mongodb\";\nimport { ObjectId } from \"mongodb\";\n\nexport interface MongoDBAdapterConfig {\n\t/**\n\t * MongoDB client instance\n\t * If not provided, Database transactions won't be enabled.\n\t */\n\tclient?: MongoClient | undefined;\n\t/**\n\t * Enable debug logs for the adapter\n\t *\n\t * @default false\n\t */\n\tdebugLogs?: DBAdapterDebugLogOption | undefined;\n\t/**\n\t * Use plural table names\n\t *\n\t * @default false\n\t */\n\tusePlural?: boolean | undefined;\n\t/**\n\t * Whether to execute multiple operations in a transaction.\n\t *\n\t * If the database doesn't support transactions,\n\t * set this to `false` and operations will be executed sequentially.\n\t * @default false\n\t */\n\ttransaction?: boolean | undefined;\n}\n\nexport const mongodbAdapter = (\n\tdb: Db,\n\tconfig?: MongoDBAdapterConfig | undefined,\n) => {\n\tlet lazyOptions: BetterAuthOptions | null;\n\n\tconst getCustomIdGenerator = (options: BetterAuthOptions) => {\n\t\tconst generator = options.advanced?.database?.generateId;\n\t\tif (typeof generator === \"function\") {\n\t\t\treturn generator;\n\t\t}\n\t\treturn undefined;\n\t};\n\n\tconst createCustomAdapter =\n\t\t(\n\t\t\tdb: Db,\n\t\t\tsession?: ClientSession | undefined,\n\t\t): AdapterFactoryCustomizeAdapterCreator =>\n\t\t({\n\t\t\tgetFieldAttributes,\n\t\t\tgetFieldName,\n\t\t\tschema,\n\t\t\tgetDefaultModelName,\n\t\t\toptions,\n\t\t}) => {\n\t\t\tconst customIdGen = getCustomIdGenerator(options);\n\n\t\t\tfunction serializeID({\n\t\t\t\tfield,\n\t\t\t\tvalue,\n\t\t\t\tmodel,\n\t\t\t}: {\n\t\t\t\tfield: string;\n\t\t\t\tvalue: any;\n\t\t\t\tmodel: string;\n\t\t\t}) {\n\t\t\t\tif (customIdGen) {\n\t\t\t\t\treturn value;\n\t\t\t\t}\n\t\t\t\tmodel = getDefaultModelName(model);\n\t\t\t\tif (\n\t\t\t\t\tfield === \"id\" ||\n\t\t\t\t\tfield === \"_id\" ||\n\t\t\t\t\tschema[model]!.fields[field]?.references?.field === \"id\"\n\t\t\t\t) {\n\t\t\t\t\tif (value === null || value === undefined) {\n\t\t\t\t\t\treturn value;\n\t\t\t\t\t}\n\t\t\t\t\tif (typeof value !== \"string\") {\n\t\t\t\t\t\tif (value instanceof ObjectId) {\n\t\t\t\t\t\t\treturn value;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (Array.isArray(value)) {\n\t\t\t\t\t\t\treturn value.map((v) => {\n\t\t\t\t\t\t\t\tif (v === null || v === undefined) {\n\t\t\t\t\t\t\t\t\treturn v;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (typeof v === \"string\") {\n\t\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\t\treturn new ObjectId(v);\n\t\t\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t\t\treturn v;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (v instanceof ObjectId) {\n\t\t\t\t\t\t\t\t\treturn v;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\t\t\"Invalid id value, received: \" + JSON.stringify(v),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\"Invalid id value, received: \" + JSON.stringify(value),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\ttry {\n\t\t\t\t\t\treturn new ObjectId(value);\n\t\t\t\t\t} catch {\n\t\t\t\t\t\treturn value;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn value;\n\t\t\t}\n\n\t\t\tfunction convertWhereClause({\n\t\t\t\twhere,\n\t\t\t\tmodel,\n\t\t\t}: {\n\t\t\t\twhere: Where[];\n\t\t\t\tmodel: string;\n\t\t\t}) {\n\t\t\t\tif (!where.length) return {};\n\t\t\t\tconst conditions = where.map((w) => {\n\t\t\t\t\tconst {\n\t\t\t\t\t\tfield: field_,\n\t\t\t\t\t\tvalue,\n\t\t\t\t\t\toperator = \"eq\",\n\t\t\t\t\t\tconnector = \"AND\",\n\t\t\t\t\t} = w;\n\t\t\t\t\tlet condition: any;\n\t\t\t\t\tlet field = getFieldName({ model, field: field_ });\n\t\t\t\t\tif (field === \"id\") field = \"_id\";\n\t\t\t\t\tswitch (operator.toLowerCase()) {\n\t\t\t\t\t\tcase \"eq\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: serializeID({\n\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"in\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$in: Array.isArray(value)\n\t\t\t\t\t\t\t\t\t\t? value.map((v) => serializeID({ field, value: v, model }))\n\t\t\t\t\t\t\t\t\t\t: [serializeID({ field, value, model })],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"not_in\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$nin: Array.isArray(value)\n\t\t\t\t\t\t\t\t\t\t? value.map((v) => serializeID({ field, value: v, model }))\n\t\t\t\t\t\t\t\t\t\t: [serializeID({ field, value, model })],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"gt\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$gt: serializeID({\n\t\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"gte\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$gte: serializeID({\n\t\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"lt\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$lt: serializeID({\n\t\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"lte\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$lte: serializeID({\n\t\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"ne\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$ne: serializeID({\n\t\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"contains\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$regex: `.*${escapeForMongoRegex(value as string)}.*`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"starts_with\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: { $regex: `^${escapeForMongoRegex(value as string)}` },\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"ends_with\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: { $regex: `${escapeForMongoRegex(value as string)}$` },\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\tthrow new Error(`Unsupported operator: ${operator}`);\n\t\t\t\t\t}\n\t\t\t\t\treturn { condition, connector };\n\t\t\t\t});\n\t\t\t\tif (conditions.length === 1) {\n\t\t\t\t\treturn conditions[0]!.condition;\n\t\t\t\t}\n\t\t\t\tconst andConditions = conditions\n\t\t\t\t\t.filter((c) => c.connector === \"AND\")\n\t\t\t\t\t.map((c) => c.condition);\n\t\t\t\tconst orConditions = conditions\n\t\t\t\t\t.filter((c) => c.connector === \"OR\")\n\t\t\t\t\t.map((c) => c.condition);\n\n\t\t\t\tlet clause = {};\n\t\t\t\tif (andConditions.length) {\n\t\t\t\t\tclause = { ...clause, $and: andConditions };\n\t\t\t\t}\n\t\t\t\tif (orConditions.length) {\n\t\t\t\t\tclause = { ...clause, $or: orConditions };\n\t\t\t\t}\n\t\t\t\treturn clause;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tasync create({ model, data: values }) {\n\t\t\t\t\tconst res = await db.collection(model).insertOne(values, { session });\n\t\t\t\t\tconst insertedData = { _id: res.insertedId.toString(), ...values };\n\t\t\t\t\treturn insertedData as any;\n\t\t\t\t},\n\t\t\t\tasync findOne({ model, where, select, join }) {\n\t\t\t\t\tconst matchStage = where\n\t\t\t\t\t\t? { $match: convertWhereClause({ where, model }) }\n\t\t\t\t\t\t: { $match: {} };\n\t\t\t\t\tconst pipeline: any[] = [matchStage];\n\n\t\t\t\t\tif (join) {\n\t\t\t\t\t\tfor (const [joinedModel, joinConfig] of Object.entries(join)) {\n\t\t\t\t\t\t\tconst localField = getFieldName({\n\t\t\t\t\t\t\t\tfield: joinConfig.on.from,\n\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tconst foreignField = getFieldName({\n\t\t\t\t\t\t\t\tfield: joinConfig.on.to,\n\t\t\t\t\t\t\t\tmodel: joinedModel,\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tconst localFieldName = localField === \"id\" ? \"_id\" : localField;\n\t\t\t\t\t\t\tconst foreignFieldName =\n\t\t\t\t\t\t\t\tforeignField === \"id\" ? \"_id\" : foreignField;\n\n\t\t\t\t\t\t\t// Only unwind if the foreign field has a unique constraint (one-to-one relationship)\n\t\t\t\t\t\t\tconst joinedModelSchema =\n\t\t\t\t\t\t\t\tschema[getDefaultModelName(joinedModel)];\n\t\t\t\t\t\t\tconst foreignFieldAttribute =\n\t\t\t\t\t\t\t\tjoinedModelSchema?.fields[joinConfig.on.to];\n\t\t\t\t\t\t\tconst isUnique = foreignFieldAttribute?.unique === true;\n\n\t\t\t\t\t\t\t// For unique relationships, limit is ignored (as per JoinConfig type)\n\t\t\t\t\t\t\t// For non-unique relationships, apply limit if specified\n\t\t\t\t\t\t\tconst shouldLimit = !isUnique && joinConfig.limit !== undefined;\n\t\t\t\t\t\t\tlet limit =\n\t\t\t\t\t\t\t\tjoinConfig.limit ??\n\t\t\t\t\t\t\t\toptions.advanced?.database?.defaultFindManyLimit ??\n\t\t\t\t\t\t\t\t100;\n\t\t\t\t\t\t\tif (shouldLimit && limit > 0) {\n\t\t\t\t\t\t\t\t// Use pipeline syntax to support limit\n\t\t\t\t\t\t\t\t// Construct the field reference string for the foreign field\n\t\t\t\t\t\t\t\tconst foreignFieldRef = `$${foreignFieldName}`;\n\t\t\t\t\t\t\t\tpipeline.push({\n\t\t\t\t\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t\t\t\t\tfrom: joinedModel,\n\t\t\t\t\t\t\t\t\t\tlet: { localFieldValue: `$${localFieldName}` },\n\t\t\t\t\t\t\t\t\t\tpipeline: [\n\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t$expr: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t$eq: [foreignFieldRef, \"$$localFieldValue\"],\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t{ $limit: limit },\n\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\tas: joinedModel,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// Use simple syntax when no limit is needed\n\t\t\t\t\t\t\t\tpipeline.push({\n\t\t\t\t\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t\t\t\t\tfrom: joinedModel,\n\t\t\t\t\t\t\t\t\t\tlocalField: localFieldName,\n\t\t\t\t\t\t\t\t\t\tforeignField: foreignFieldName,\n\t\t\t\t\t\t\t\t\t\tas: joinedModel,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (isUnique) {\n\t\t\t\t\t\t\t\t// For one-to-one relationships, unwind to flatten to a single object\n\t\t\t\t\t\t\t\tpipeline.push({\n\t\t\t\t\t\t\t\t\t$unwind: {\n\t\t\t\t\t\t\t\t\t\tpath: `$${joinedModel}`,\n\t\t\t\t\t\t\t\t\t\tpreserveNullAndEmptyArrays: true,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// For one-to-many, keep as array - no unwind\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (select) {\n\t\t\t\t\t\tconst projection: any = {};\n\t\t\t\t\t\tselect.forEach((field) => {\n\t\t\t\t\t\t\tprojection[getFieldName({ field, model })] = 1;\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\t// Include joined collections in projection\n\t\t\t\t\t\tif (join) {\n\t\t\t\t\t\t\tfor (const joinedModel of Object.keys(join)) {\n\t\t\t\t\t\t\t\tprojection[joinedModel] = 1;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tpipeline.push({ $project: projection });\n\t\t\t\t\t}\n\n\t\t\t\t\tpipeline.push({ $limit: 1 });\n\n\t\t\t\t\tconst res = await db\n\t\t\t\t\t\t.collection(model)\n\t\t\t\t\t\t.aggregate(pipeline, { session })\n\t\t\t\t\t\t.toArray();\n\n\t\t\t\t\tif (!res || res.length === 0) return null;\n\t\t\t\t\treturn res[0] as any;\n\t\t\t\t},\n\t\t\t\tasync findMany({ model, where, limit, offset, sortBy, join }) {\n\t\t\t\t\tconst matchStage = where\n\t\t\t\t\t\t? { $match: convertWhereClause({ where, model }) }\n\t\t\t\t\t\t: { $match: {} };\n\t\t\t\t\tconst pipeline: any[] = [matchStage];\n\n\t\t\t\t\tif (join) {\n\t\t\t\t\t\tfor (const [joinedModel, joinConfig] of Object.entries(join)) {\n\t\t\t\t\t\t\tconst localField = getFieldName({\n\t\t\t\t\t\t\t\tfield: joinConfig.on.from,\n\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tconst foreignField = getFieldName({\n\t\t\t\t\t\t\t\tfield: joinConfig.on.to,\n\t\t\t\t\t\t\t\tmodel: joinedModel,\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tconst localFieldName = localField === \"id\" ? \"_id\" : localField;\n\t\t\t\t\t\t\tconst foreignFieldName =\n\t\t\t\t\t\t\t\tforeignField === \"id\" ? \"_id\" : foreignField;\n\n\t\t\t\t\t\t\t// Only unwind if the foreign field has a unique constraint (one-to-one relationship)\n\t\t\t\t\t\t\tconst foreignFieldAttribute = getFieldAttributes({\n\t\t\t\t\t\t\t\tmodel: joinedModel,\n\t\t\t\t\t\t\t\tfield: joinConfig.on.to,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tconst isUnique = foreignFieldAttribute?.unique === true;\n\n\t\t\t\t\t\t\t// For unique relationships, limit is ignored (as per JoinConfig type)\n\t\t\t\t\t\t\t// For non-unique relationships, apply limit if specified\n\t\t\t\t\t\t\tconst shouldLimit =\n\t\t\t\t\t\t\t\tjoinConfig.relation !== \"one-to-one\" &&\n\t\t\t\t\t\t\t\tjoinConfig.limit !== undefined;\n\n\t\t\t\t\t\t\tlet limit =\n\t\t\t\t\t\t\t\tjoinConfig.limit ??\n\t\t\t\t\t\t\t\toptions.advanced?.database?.defaultFindManyLimit ??\n\t\t\t\t\t\t\t\t100;\n\t\t\t\t\t\t\tif (shouldLimit && limit > 0) {\n\t\t\t\t\t\t\t\t// Use pipeline syntax to support limit\n\t\t\t\t\t\t\t\t// Construct the field reference string for the foreign field\n\t\t\t\t\t\t\t\tconst foreignFieldRef = `$${foreignFieldName}`;\n\t\t\t\t\t\t\t\tpipeline.push({\n\t\t\t\t\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t\t\t\t\tfrom: joinedModel,\n\t\t\t\t\t\t\t\t\t\tlet: { localFieldValue: `$${localFieldName}` },\n\t\t\t\t\t\t\t\t\t\tpipeline: [\n\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t$expr: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t$eq: [foreignFieldRef, \"$$localFieldValue\"],\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t{ $limit: limit },\n\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\tas: joinedModel,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// Use simple syntax when no limit is needed\n\t\t\t\t\t\t\t\tpipeline.push({\n\t\t\t\t\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t\t\t\t\tfrom: joinedModel,\n\t\t\t\t\t\t\t\t\t\tlocalField: localFieldName,\n\t\t\t\t\t\t\t\t\t\tforeignField: foreignFieldName,\n\t\t\t\t\t\t\t\t\t\tas: joinedModel,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (isUnique) {\n\t\t\t\t\t\t\t\t// For one-to-one relationships, unwind to flatten to a single object\n\t\t\t\t\t\t\t\tpipeline.push({\n\t\t\t\t\t\t\t\t\t$unwind: {\n\t\t\t\t\t\t\t\t\t\tpath: `$${joinedModel}`,\n\t\t\t\t\t\t\t\t\t\tpreserveNullAndEmptyArrays: true,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// For one-to-many, keep as array - no unwind\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (sortBy) {\n\t\t\t\t\t\tpipeline.push({\n\t\t\t\t\t\t\t$sort: {\n\t\t\t\t\t\t\t\t[getFieldName({ field: sortBy.field, model })]:\n\t\t\t\t\t\t\t\t\tsortBy.direction === \"desc\" ? -1 : 1,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tif (offset) {\n\t\t\t\t\t\tpipeline.push({ $skip: offset });\n\t\t\t\t\t}\n\n\t\t\t\t\tif (limit) {\n\t\t\t\t\t\tpipeline.push({ $limit: limit });\n\t\t\t\t\t}\n\n\t\t\t\t\tconst res = await db\n\t\t\t\t\t\t.collection(model)\n\t\t\t\t\t\t.aggregate(pipeline, { session })\n\t\t\t\t\t\t.toArray();\n\n\t\t\t\t\treturn res as any;\n\t\t\t\t},\n\t\t\t\tasync count({ model, where }) {\n\t\t\t\t\tconst matchStage = where\n\t\t\t\t\t\t? { $match: convertWhereClause({ where, model }) }\n\t\t\t\t\t\t: { $match: {} };\n\t\t\t\t\tconst pipeline: any[] = [matchStage, { $count: \"total\" }];\n\n\t\t\t\t\tconst res = await db\n\t\t\t\t\t\t.collection(model)\n\t\t\t\t\t\t.aggregate(pipeline, { session })\n\t\t\t\t\t\t.toArray();\n\n\t\t\t\t\tif (!res || res.length === 0) return 0;\n\t\t\t\t\treturn res[0]?.total ?? 0;\n\t\t\t\t},\n\t\t\t\tasync update({ model, where, update: values }) {\n\t\t\t\t\tconst clause = convertWhereClause({ where, model });\n\n\t\t\t\t\tconst res = await db.collection(model).findOneAndUpdate(\n\t\t\t\t\t\tclause,\n\t\t\t\t\t\t{ $set: values as any },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tsession,\n\t\t\t\t\t\t\treturnDocument: \"after\",\n\t\t\t\t\t\t\tincludeResultMetadata: true,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\t\t\t\t\tconst doc = (res as any)?.value ?? null;\n\t\t\t\t\tif (!doc) return null;\n\t\t\t\t\treturn doc as any;\n\t\t\t\t},\n\t\t\t\tasync updateMany({ model, where, update: values }) {\n\t\t\t\t\tconst clause = convertWhereClause({ where, model });\n\n\t\t\t\t\tconst res = await db.collection(model).updateMany(\n\t\t\t\t\t\tclause,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$set: values as any,\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{ session },\n\t\t\t\t\t);\n\t\t\t\t\treturn res.modifiedCount;\n\t\t\t\t},\n\t\t\t\tasync delete({ model, where }) {\n\t\t\t\t\tconst clause = convertWhereClause({ where, model });\n\t\t\t\t\tawait db.collection(model).deleteOne(clause, { session });\n\t\t\t\t},\n\t\t\t\tasync deleteMany({ model, where }) {\n\t\t\t\t\tconst clause = convertWhereClause({ where, model });\n\t\t\t\t\tconst res = await db\n\t\t\t\t\t\t.collection(model)\n\t\t\t\t\t\t.deleteMany(clause, { session });\n\t\t\t\t\treturn res.deletedCount;\n\t\t\t\t},\n\t\t\t};\n\t\t};\n\n\tlet lazyAdapter:\n\t\t| ((options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>)\n\t\t| null = null;\n\tlet adapterOptions: AdapterFactoryOptions | null = null;\n\tadapterOptions = {\n\t\tconfig: {\n\t\t\tadapterId: \"mongodb-adapter\",\n\t\t\tadapterName: \"MongoDB Adapter\",\n\t\t\tusePlural: config?.usePlural ?? false,\n\t\t\tdebugLogs: config?.debugLogs ?? false,\n\t\t\tmapKeysTransformInput: {\n\t\t\t\tid: \"_id\",\n\t\t\t},\n\t\t\tmapKeysTransformOutput: {\n\t\t\t\t_id: \"id\",\n\t\t\t},\n\t\t\tsupportsNumericIds: false,\n\t\t\ttransaction:\n\t\t\t\tconfig?.client && (config?.transaction ?? true)\n\t\t\t\t\t? async (cb) => {\n\t\t\t\t\t\t\tif (!config.client) {\n\t\t\t\t\t\t\t\treturn cb(lazyAdapter!(lazyOptions!));\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst session = config.client.startSession();\n\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tsession.startTransaction();\n\n\t\t\t\t\t\t\t\tconst adapter = createAdapterFactory({\n\t\t\t\t\t\t\t\t\tconfig: adapterOptions!.config,\n\t\t\t\t\t\t\t\t\tadapter: createCustomAdapter(db, session),\n\t\t\t\t\t\t\t\t})(lazyOptions!);\n\n\t\t\t\t\t\t\t\tconst result = await cb(adapter);\n\n\t\t\t\t\t\t\t\tawait session.commitTransaction();\n\t\t\t\t\t\t\t\treturn result;\n\t\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\t\tawait session.abortTransaction();\n\t\t\t\t\t\t\t\tthrow err;\n\t\t\t\t\t\t\t} finally {\n\t\t\t\t\t\t\t\tawait session.endSession();\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t: false,\n\t\t\tcustomTransformInput({\n\t\t\t\taction,\n\t\t\t\tdata,\n\t\t\t\tfield,\n\t\t\t\tfieldAttributes,\n\t\t\t\tschema,\n\t\t\t\tmodel,\n\t\t\t\toptions,\n\t\t\t}) {\n\t\t\t\tconst customIdGen = getCustomIdGenerator(options);\n\t\t\t\tif (field === \"_id\" || fieldAttributes.references?.field === \"id\") {\n\t\t\t\t\tif (customIdGen) {\n\t\t\t\t\t\treturn data;\n\t\t\t\t\t}\n\t\t\t\t\tif (action !== \"create\") {\n\t\t\t\t\t\treturn data;\n\t\t\t\t\t}\n\t\t\t\t\tif (Array.isArray(data)) {\n\t\t\t\t\t\treturn data.map((v) => {\n\t\t\t\t\t\t\tif (typeof v === \"string\") {\n\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\tconst oid = new ObjectId(v);\n\t\t\t\t\t\t\t\t\treturn oid;\n\t\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t\treturn v;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn v;\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tif (typeof data === \"string\") {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst oid = new ObjectId(data);\n\t\t\t\t\t\t\treturn oid;\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\treturn data;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (\n\t\t\t\t\t\tfieldAttributes?.references?.field === \"id\" &&\n\t\t\t\t\t\t!fieldAttributes?.required &&\n\t\t\t\t\t\tdata === null\n\t\t\t\t\t) {\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\t\t\t\t\tconst oid = new ObjectId();\n\t\t\t\t\treturn oid;\n\t\t\t\t}\n\t\t\t\treturn data;\n\t\t\t},\n\t\t\tcustomTransformOutput({ data, field, fieldAttributes }) {\n\t\t\t\tif (field === \"id\" || fieldAttributes.references?.field === \"id\") {\n\t\t\t\t\tif (data instanceof ObjectId) {\n\t\t\t\t\t\treturn data.toHexString();\n\t\t\t\t\t}\n\t\t\t\t\tif (Array.isArray(data)) {\n\t\t\t\t\t\treturn data.map((v) => {\n\t\t\t\t\t\t\tif (v instanceof ObjectId) {\n\t\t\t\t\t\t\t\treturn v.toHexString();\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn v;\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\treturn data;\n\t\t\t\t}\n\t\t\t\treturn data;\n\t\t\t},\n\t\t\tcustomIdGenerator() {\n\t\t\t\treturn new ObjectId().toString();\n\t\t\t},\n\t\t},\n\t\tadapter: createCustomAdapter(db),\n\t};\n\tlazyAdapter = createAdapterFactory(adapterOptions);\n\n\treturn (options: BetterAuthOptions): DBAdapter<BetterAuthOptions> => {\n\t\tlazyOptions = options;\n\t\treturn lazyAdapter(options);\n\t};\n};\n\n/**\n * Safely escape user input for use in a MongoDB regex.\n * This ensures the resulting pattern is treated as literal text,\n * and not as a regex with special syntax.\n *\n * @param input - The input string to escape. Any type that isn't a string will be converted to an empty string.\n * @param maxLength - The maximum length of the input string to escape. Defaults to 256. This is to prevent DOS attacks.\n * @returns The escaped string.\n */\nfunction escapeForMongoRegex(input: string, maxLength = 256): string {\n\tif (typeof input !== \"string\") return \"\";\n\n\t// Escape all PCRE special characters\n\t// Source: PCRE docs — https://www.pcre.org/original/doc/html/pcrepattern.html\n\treturn input.slice(0, maxLength).replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n}\n"],"mappings":";;;;AAwCA,MAAa,kBACZ,IACA,WACI;CACJ,IAAIA;CAEJ,MAAM,wBAAwB,YAA+B;EAC5D,MAAM,YAAY,Q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AAa,SAChB,QAAO,EAAE,aAAa;AAEvB,aAAO;OACN;AAEH,YAAO;;AAER,WAAO;;GAER,oBAAoB;AACnB,WAAO,IAAI,UAAU,CAAC,UAAU;;GAEjC;EACD,SAAS,oBAAoB,GAAG;EAChC;AACD,eAAc,qBAAqB,eAAe;AAElD,SAAQ,YAA6D;AACpE,gBAAc;AACd,SAAO,YAAY,QAAQ;;;;;;;;;;;;AAa7B,SAAS,oBAAoB,OAAe,YAAY,KAAa;AACpE,KAAI,OAAO,UAAU,SAAU,QAAO;AAItC,QAAO,MAAM,MAAM,GAAG,UAAU,CAAC,QAAQ,uBAAuB,OAAO"}
1
+ {"version":3,"file":"mongodb-adapter.mjs","names":["lazyOptions: BetterAuthOptions | null","condition: any","db","pipeline: any[]","projection: any","limit","lazyAdapter:\n\t\t| ((options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>)\n\t\t| null","adapterOptions: AdapterFactoryOptions | null"],"sources":["../../../src/adapters/mongodb-adapter/mongodb-adapter.ts"],"sourcesContent":["import type { BetterAuthOptions } from \"@better-auth/core\";\nimport type {\n\tAdapterFactoryCustomizeAdapterCreator,\n\tAdapterFactoryOptions,\n\tDBAdapter,\n\tDBAdapterDebugLogOption,\n\tWhere,\n} from \"@better-auth/core/db/adapter\";\nimport { createAdapterFactory } from \"@better-auth/core/db/adapter\";\nimport type { ClientSession, Db, MongoClient } from \"mongodb\";\nimport { ObjectId } from \"mongodb\";\n\nexport interface MongoDBAdapterConfig {\n\t/**\n\t * MongoDB client instance\n\t * If not provided, Database transactions won't be enabled.\n\t */\n\tclient?: MongoClient | undefined;\n\t/**\n\t * Enable debug logs for the adapter\n\t *\n\t * @default false\n\t */\n\tdebugLogs?: DBAdapterDebugLogOption | undefined;\n\t/**\n\t * Use plural table names\n\t *\n\t * @default false\n\t */\n\tusePlural?: boolean | undefined;\n\t/**\n\t * Whether to execute multiple operations in a transaction.\n\t *\n\t * If the database doesn't support transactions,\n\t * set this to `false` and operations will be executed sequentially.\n\t * @default false\n\t */\n\ttransaction?: boolean | undefined;\n}\n\nexport const mongodbAdapter = (\n\tdb: Db,\n\tconfig?: MongoDBAdapterConfig | undefined,\n) => {\n\tlet lazyOptions: BetterAuthOptions | null;\n\n\tconst getCustomIdGenerator = (options: BetterAuthOptions) => {\n\t\tconst generator = options.advanced?.database?.generateId;\n\t\tif (typeof generator === \"function\") {\n\t\t\treturn generator;\n\t\t}\n\t\treturn undefined;\n\t};\n\n\tconst createCustomAdapter =\n\t\t(\n\t\t\tdb: Db,\n\t\t\tsession?: ClientSession | undefined,\n\t\t): AdapterFactoryCustomizeAdapterCreator =>\n\t\t({\n\t\t\tgetFieldAttributes,\n\t\t\tgetFieldName,\n\t\t\tschema,\n\t\t\tgetDefaultModelName,\n\t\t\toptions,\n\t\t}) => {\n\t\t\tconst customIdGen = getCustomIdGenerator(options);\n\n\t\t\tfunction serializeID({\n\t\t\t\tfield,\n\t\t\t\tvalue,\n\t\t\t\tmodel,\n\t\t\t}: {\n\t\t\t\tfield: string;\n\t\t\t\tvalue: any;\n\t\t\t\tmodel: string;\n\t\t\t}) {\n\t\t\t\tif (customIdGen) {\n\t\t\t\t\treturn value;\n\t\t\t\t}\n\t\t\t\tmodel = getDefaultModelName(model);\n\t\t\t\tif (\n\t\t\t\t\tfield === \"id\" ||\n\t\t\t\t\tfield === \"_id\" ||\n\t\t\t\t\tschema[model]!.fields[field]?.references?.field === \"id\"\n\t\t\t\t) {\n\t\t\t\t\tif (value === null || value === undefined) {\n\t\t\t\t\t\treturn value;\n\t\t\t\t\t}\n\t\t\t\t\tif (typeof value !== \"string\") {\n\t\t\t\t\t\tif (value instanceof ObjectId) {\n\t\t\t\t\t\t\treturn value;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (Array.isArray(value)) {\n\t\t\t\t\t\t\treturn value.map((v) => {\n\t\t\t\t\t\t\t\tif (v === null || v === undefined) {\n\t\t\t\t\t\t\t\t\treturn v;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (typeof v === \"string\") {\n\t\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\t\treturn new ObjectId(v);\n\t\t\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t\t\treturn v;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (v instanceof ObjectId) {\n\t\t\t\t\t\t\t\t\treturn v;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\t\t\"Invalid id value, received: \" + JSON.stringify(v),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\"Invalid id value, received: \" + JSON.stringify(value),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\ttry {\n\t\t\t\t\t\treturn new ObjectId(value);\n\t\t\t\t\t} catch {\n\t\t\t\t\t\treturn value;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn value;\n\t\t\t}\n\n\t\t\tfunction convertWhereClause({\n\t\t\t\twhere,\n\t\t\t\tmodel,\n\t\t\t}: {\n\t\t\t\twhere: Where[];\n\t\t\t\tmodel: string;\n\t\t\t}) {\n\t\t\t\tif (!where.length) return {};\n\t\t\t\tconst conditions = where.map((w) => {\n\t\t\t\t\tconst {\n\t\t\t\t\t\tfield: field_,\n\t\t\t\t\t\tvalue,\n\t\t\t\t\t\toperator = \"eq\",\n\t\t\t\t\t\tconnector = \"AND\",\n\t\t\t\t\t} = w;\n\t\t\t\t\tlet condition: any;\n\t\t\t\t\tlet field = getFieldName({ model, field: field_ });\n\t\t\t\t\tif (field === \"id\") field = \"_id\";\n\t\t\t\t\tswitch (operator.toLowerCase()) {\n\t\t\t\t\t\tcase \"eq\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: serializeID({\n\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"in\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$in: Array.isArray(value)\n\t\t\t\t\t\t\t\t\t\t? value.map((v) => serializeID({ field, value: v, model }))\n\t\t\t\t\t\t\t\t\t\t: [serializeID({ field, value, model })],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"not_in\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$nin: Array.isArray(value)\n\t\t\t\t\t\t\t\t\t\t? value.map((v) => serializeID({ field, value: v, model }))\n\t\t\t\t\t\t\t\t\t\t: [serializeID({ field, value, model })],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"gt\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$gt: serializeID({\n\t\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"gte\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$gte: serializeID({\n\t\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"lt\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$lt: serializeID({\n\t\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"lte\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$lte: serializeID({\n\t\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"ne\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$ne: serializeID({\n\t\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"contains\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: {\n\t\t\t\t\t\t\t\t\t$regex: `.*${escapeForMongoRegex(value as string)}.*`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"starts_with\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: { $regex: `^${escapeForMongoRegex(value as string)}` },\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"ends_with\":\n\t\t\t\t\t\t\tcondition = {\n\t\t\t\t\t\t\t\t[field]: { $regex: `${escapeForMongoRegex(value as string)}$` },\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\tthrow new Error(`Unsupported operator: ${operator}`);\n\t\t\t\t\t}\n\t\t\t\t\treturn { condition, connector };\n\t\t\t\t});\n\t\t\t\tif (conditions.length === 1) {\n\t\t\t\t\treturn conditions[0]!.condition;\n\t\t\t\t}\n\t\t\t\tconst andConditions = conditions\n\t\t\t\t\t.filter((c) => c.connector === \"AND\")\n\t\t\t\t\t.map((c) => c.condition);\n\t\t\t\tconst orConditions = conditions\n\t\t\t\t\t.filter((c) => c.connector === \"OR\")\n\t\t\t\t\t.map((c) => c.condition);\n\n\t\t\t\tlet clause = {};\n\t\t\t\tif (andConditions.length) {\n\t\t\t\t\tclause = { ...clause, $and: andConditions };\n\t\t\t\t}\n\t\t\t\tif (orConditions.length) {\n\t\t\t\t\tclause = { ...clause, $or: orConditions };\n\t\t\t\t}\n\t\t\t\treturn clause;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tasync create({ model, data: values }) {\n\t\t\t\t\tconst res = await db.collection(model).insertOne(values, { session });\n\t\t\t\t\tconst insertedData = { _id: res.insertedId.toString(), ...values };\n\t\t\t\t\treturn insertedData as any;\n\t\t\t\t},\n\t\t\t\tasync findOne({ model, where, select, join }) {\n\t\t\t\t\tconst matchStage = where\n\t\t\t\t\t\t? { $match: convertWhereClause({ where, model }) }\n\t\t\t\t\t\t: { $match: {} };\n\t\t\t\t\tconst pipeline: any[] = [matchStage];\n\n\t\t\t\t\tif (join) {\n\t\t\t\t\t\tfor (const [joinedModel, joinConfig] of Object.entries(join)) {\n\t\t\t\t\t\t\tconst localField = getFieldName({\n\t\t\t\t\t\t\t\tfield: joinConfig.on.from,\n\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tconst foreignField = getFieldName({\n\t\t\t\t\t\t\t\tfield: joinConfig.on.to,\n\t\t\t\t\t\t\t\tmodel: joinedModel,\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tconst localFieldName = localField === \"id\" ? \"_id\" : localField;\n\t\t\t\t\t\t\tconst foreignFieldName =\n\t\t\t\t\t\t\t\tforeignField === \"id\" ? \"_id\" : foreignField;\n\n\t\t\t\t\t\t\t// Only unwind if the foreign field has a unique constraint (one-to-one relationship)\n\t\t\t\t\t\t\tconst joinedModelSchema =\n\t\t\t\t\t\t\t\tschema[getDefaultModelName(joinedModel)];\n\t\t\t\t\t\t\tconst foreignFieldAttribute =\n\t\t\t\t\t\t\t\tjoinedModelSchema?.fields[joinConfig.on.to];\n\t\t\t\t\t\t\tconst isUnique = foreignFieldAttribute?.unique === true;\n\n\t\t\t\t\t\t\t// For unique relationships, limit is ignored (as per JoinConfig type)\n\t\t\t\t\t\t\t// For non-unique relationships, apply limit if specified\n\t\t\t\t\t\t\tconst shouldLimit = !isUnique && joinConfig.limit !== undefined;\n\t\t\t\t\t\t\tlet limit =\n\t\t\t\t\t\t\t\tjoinConfig.limit ??\n\t\t\t\t\t\t\t\toptions.advanced?.database?.defaultFindManyLimit ??\n\t\t\t\t\t\t\t\t100;\n\t\t\t\t\t\t\tif (shouldLimit && limit > 0) {\n\t\t\t\t\t\t\t\t// Use pipeline syntax to support limit\n\t\t\t\t\t\t\t\t// Construct the field reference string for the foreign field\n\t\t\t\t\t\t\t\tconst foreignFieldRef = `$${foreignFieldName}`;\n\t\t\t\t\t\t\t\tpipeline.push({\n\t\t\t\t\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t\t\t\t\tfrom: joinedModel,\n\t\t\t\t\t\t\t\t\t\tlet: { localFieldValue: `$${localFieldName}` },\n\t\t\t\t\t\t\t\t\t\tpipeline: [\n\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t$expr: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t$eq: [foreignFieldRef, \"$$localFieldValue\"],\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t{ $limit: limit },\n\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\tas: joinedModel,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// Use simple syntax when no limit is needed\n\t\t\t\t\t\t\t\tpipeline.push({\n\t\t\t\t\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t\t\t\t\tfrom: joinedModel,\n\t\t\t\t\t\t\t\t\t\tlocalField: localFieldName,\n\t\t\t\t\t\t\t\t\t\tforeignField: foreignFieldName,\n\t\t\t\t\t\t\t\t\t\tas: joinedModel,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (isUnique) {\n\t\t\t\t\t\t\t\t// For one-to-one relationships, unwind to flatten to a single object\n\t\t\t\t\t\t\t\tpipeline.push({\n\t\t\t\t\t\t\t\t\t$unwind: {\n\t\t\t\t\t\t\t\t\t\tpath: `$${joinedModel}`,\n\t\t\t\t\t\t\t\t\t\tpreserveNullAndEmptyArrays: true,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// For one-to-many, keep as array - no unwind\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (select) {\n\t\t\t\t\t\tconst projection: any = {};\n\t\t\t\t\t\tselect.forEach((field) => {\n\t\t\t\t\t\t\tprojection[getFieldName({ field, model })] = 1;\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\t// Include joined collections in projection\n\t\t\t\t\t\tif (join) {\n\t\t\t\t\t\t\tfor (const joinedModel of Object.keys(join)) {\n\t\t\t\t\t\t\t\tprojection[joinedModel] = 1;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tpipeline.push({ $project: projection });\n\t\t\t\t\t}\n\n\t\t\t\t\tpipeline.push({ $limit: 1 });\n\n\t\t\t\t\tconst res = await db\n\t\t\t\t\t\t.collection(model)\n\t\t\t\t\t\t.aggregate(pipeline, { session })\n\t\t\t\t\t\t.toArray();\n\n\t\t\t\t\tif (!res || res.length === 0) return null;\n\t\t\t\t\treturn res[0] as any;\n\t\t\t\t},\n\t\t\t\tasync findMany({ model, where, limit, offset, sortBy, join }) {\n\t\t\t\t\tconst matchStage = where\n\t\t\t\t\t\t? { $match: convertWhereClause({ where, model }) }\n\t\t\t\t\t\t: { $match: {} };\n\t\t\t\t\tconst pipeline: any[] = [matchStage];\n\n\t\t\t\t\tif (join) {\n\t\t\t\t\t\tfor (const [joinedModel, joinConfig] of Object.entries(join)) {\n\t\t\t\t\t\t\tconst localField = getFieldName({\n\t\t\t\t\t\t\t\tfield: joinConfig.on.from,\n\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tconst foreignField = getFieldName({\n\t\t\t\t\t\t\t\tfield: joinConfig.on.to,\n\t\t\t\t\t\t\t\tmodel: joinedModel,\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tconst localFieldName = localField === \"id\" ? \"_id\" : localField;\n\t\t\t\t\t\t\tconst foreignFieldName =\n\t\t\t\t\t\t\t\tforeignField === \"id\" ? \"_id\" : foreignField;\n\n\t\t\t\t\t\t\t// Only unwind if the foreign field has a unique constraint (one-to-one relationship)\n\t\t\t\t\t\t\tconst foreignFieldAttribute = getFieldAttributes({\n\t\t\t\t\t\t\t\tmodel: joinedModel,\n\t\t\t\t\t\t\t\tfield: joinConfig.on.to,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tconst isUnique = foreignFieldAttribute?.unique === true;\n\n\t\t\t\t\t\t\t// For unique relationships, limit is ignored (as per JoinConfig type)\n\t\t\t\t\t\t\t// For non-unique relationships, apply limit if specified\n\t\t\t\t\t\t\tconst shouldLimit =\n\t\t\t\t\t\t\t\tjoinConfig.relation !== \"one-to-one\" &&\n\t\t\t\t\t\t\t\tjoinConfig.limit !== undefined;\n\n\t\t\t\t\t\t\tlet limit =\n\t\t\t\t\t\t\t\tjoinConfig.limit ??\n\t\t\t\t\t\t\t\toptions.advanced?.database?.defaultFindManyLimit ??\n\t\t\t\t\t\t\t\t100;\n\t\t\t\t\t\t\tif (shouldLimit && limit > 0) {\n\t\t\t\t\t\t\t\t// Use pipeline syntax to support limit\n\t\t\t\t\t\t\t\t// Construct the field reference string for the foreign field\n\t\t\t\t\t\t\t\tconst foreignFieldRef = `$${foreignFieldName}`;\n\t\t\t\t\t\t\t\tpipeline.push({\n\t\t\t\t\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t\t\t\t\tfrom: joinedModel,\n\t\t\t\t\t\t\t\t\t\tlet: { localFieldValue: `$${localFieldName}` },\n\t\t\t\t\t\t\t\t\t\tpipeline: [\n\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t$expr: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t$eq: [foreignFieldRef, \"$$localFieldValue\"],\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t{ $limit: limit },\n\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\tas: joinedModel,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// Use simple syntax when no limit is needed\n\t\t\t\t\t\t\t\tpipeline.push({\n\t\t\t\t\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t\t\t\t\tfrom: joinedModel,\n\t\t\t\t\t\t\t\t\t\tlocalField: localFieldName,\n\t\t\t\t\t\t\t\t\t\tforeignField: foreignFieldName,\n\t\t\t\t\t\t\t\t\t\tas: joinedModel,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (isUnique) {\n\t\t\t\t\t\t\t\t// For one-to-one relationships, unwind to flatten to a single object\n\t\t\t\t\t\t\t\tpipeline.push({\n\t\t\t\t\t\t\t\t\t$unwind: {\n\t\t\t\t\t\t\t\t\t\tpath: `$${joinedModel}`,\n\t\t\t\t\t\t\t\t\t\tpreserveNullAndEmptyArrays: true,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// For one-to-many, keep as array - no unwind\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (sortBy) {\n\t\t\t\t\t\tpipeline.push({\n\t\t\t\t\t\t\t$sort: {\n\t\t\t\t\t\t\t\t[getFieldName({ field: sortBy.field, model })]:\n\t\t\t\t\t\t\t\t\tsortBy.direction === \"desc\" ? -1 : 1,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tif (offset) {\n\t\t\t\t\t\tpipeline.push({ $skip: offset });\n\t\t\t\t\t}\n\n\t\t\t\t\tif (limit) {\n\t\t\t\t\t\tpipeline.push({ $limit: limit });\n\t\t\t\t\t}\n\n\t\t\t\t\tconst res = await db\n\t\t\t\t\t\t.collection(model)\n\t\t\t\t\t\t.aggregate(pipeline, { session })\n\t\t\t\t\t\t.toArray();\n\n\t\t\t\t\treturn res as any;\n\t\t\t\t},\n\t\t\t\tasync count({ model, where }) {\n\t\t\t\t\tconst matchStage = where\n\t\t\t\t\t\t? { $match: convertWhereClause({ where, model }) }\n\t\t\t\t\t\t: { $match: {} };\n\t\t\t\t\tconst pipeline: any[] = [matchStage, { $count: \"total\" }];\n\n\t\t\t\t\tconst res = await db\n\t\t\t\t\t\t.collection(model)\n\t\t\t\t\t\t.aggregate(pipeline, { session })\n\t\t\t\t\t\t.toArray();\n\n\t\t\t\t\tif (!res || res.length === 0) return 0;\n\t\t\t\t\treturn res[0]?.total ?? 0;\n\t\t\t\t},\n\t\t\t\tasync update({ model, where, update: values }) {\n\t\t\t\t\tconst clause = convertWhereClause({ where, model });\n\n\t\t\t\t\tconst res = await db.collection(model).findOneAndUpdate(\n\t\t\t\t\t\tclause,\n\t\t\t\t\t\t{ $set: values as any },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tsession,\n\t\t\t\t\t\t\treturnDocument: \"after\",\n\t\t\t\t\t\t\tincludeResultMetadata: true,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\t\t\t\t\tconst doc = (res as any)?.value ?? null;\n\t\t\t\t\tif (!doc) return null;\n\t\t\t\t\treturn doc as any;\n\t\t\t\t},\n\t\t\t\tasync updateMany({ model, where, update: values }) {\n\t\t\t\t\tconst clause = convertWhereClause({ where, model });\n\n\t\t\t\t\tconst res = await db.collection(model).updateMany(\n\t\t\t\t\t\tclause,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$set: values as any,\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{ session },\n\t\t\t\t\t);\n\t\t\t\t\treturn res.modifiedCount;\n\t\t\t\t},\n\t\t\t\tasync delete({ model, where }) {\n\t\t\t\t\tconst clause = convertWhereClause({ where, model });\n\t\t\t\t\tawait db.collection(model).deleteOne(clause, { session });\n\t\t\t\t},\n\t\t\t\tasync deleteMany({ model, where }) {\n\t\t\t\t\tconst clause = convertWhereClause({ where, model });\n\t\t\t\t\tconst res = await db\n\t\t\t\t\t\t.collection(model)\n\t\t\t\t\t\t.deleteMany(clause, { session });\n\t\t\t\t\treturn res.deletedCount;\n\t\t\t\t},\n\t\t\t};\n\t\t};\n\n\tlet lazyAdapter:\n\t\t| ((options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>)\n\t\t| null = null;\n\tlet adapterOptions: AdapterFactoryOptions | null = null;\n\tadapterOptions = {\n\t\tconfig: {\n\t\t\tadapterId: \"mongodb-adapter\",\n\t\t\tadapterName: \"MongoDB Adapter\",\n\t\t\tusePlural: config?.usePlural ?? false,\n\t\t\tdebugLogs: config?.debugLogs ?? false,\n\t\t\tmapKeysTransformInput: {\n\t\t\t\tid: \"_id\",\n\t\t\t},\n\t\t\tmapKeysTransformOutput: {\n\t\t\t\t_id: \"id\",\n\t\t\t},\n\t\t\tsupportsArrays: true,\n\t\t\tsupportsNumericIds: false,\n\t\t\ttransaction:\n\t\t\t\tconfig?.client && (config?.transaction ?? true)\n\t\t\t\t\t? async (cb) => {\n\t\t\t\t\t\t\tif (!config.client) {\n\t\t\t\t\t\t\t\treturn cb(lazyAdapter!(lazyOptions!));\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst session = config.client.startSession();\n\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tsession.startTransaction();\n\n\t\t\t\t\t\t\t\tconst adapter = createAdapterFactory({\n\t\t\t\t\t\t\t\t\tconfig: adapterOptions!.config,\n\t\t\t\t\t\t\t\t\tadapter: createCustomAdapter(db, session),\n\t\t\t\t\t\t\t\t})(lazyOptions!);\n\n\t\t\t\t\t\t\t\tconst result = await cb(adapter);\n\n\t\t\t\t\t\t\t\tawait session.commitTransaction();\n\t\t\t\t\t\t\t\treturn result;\n\t\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\t\tawait session.abortTransaction();\n\t\t\t\t\t\t\t\tthrow err;\n\t\t\t\t\t\t\t} finally {\n\t\t\t\t\t\t\t\tawait session.endSession();\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t: false,\n\t\t\tcustomTransformInput({\n\t\t\t\taction,\n\t\t\t\tdata,\n\t\t\t\tfield,\n\t\t\t\tfieldAttributes,\n\t\t\t\tschema,\n\t\t\t\tmodel,\n\t\t\t\toptions,\n\t\t\t}) {\n\t\t\t\tconst customIdGen = getCustomIdGenerator(options);\n\t\t\t\tif (field === \"_id\" || fieldAttributes.references?.field === \"id\") {\n\t\t\t\t\tif (customIdGen) {\n\t\t\t\t\t\treturn data;\n\t\t\t\t\t}\n\t\t\t\t\tif (action !== \"create\") {\n\t\t\t\t\t\treturn data;\n\t\t\t\t\t}\n\t\t\t\t\tif (Array.isArray(data)) {\n\t\t\t\t\t\treturn data.map((v) => {\n\t\t\t\t\t\t\tif (typeof v === \"string\") {\n\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\tconst oid = new ObjectId(v);\n\t\t\t\t\t\t\t\t\treturn oid;\n\t\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t\treturn v;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn v;\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tif (typeof data === \"string\") {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst oid = new ObjectId(data);\n\t\t\t\t\t\t\treturn oid;\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\treturn data;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (\n\t\t\t\t\t\tfieldAttributes?.references?.field === \"id\" &&\n\t\t\t\t\t\t!fieldAttributes?.required &&\n\t\t\t\t\t\tdata === null\n\t\t\t\t\t) {\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\t\t\t\t\tconst oid = new ObjectId();\n\t\t\t\t\treturn oid;\n\t\t\t\t}\n\t\t\t\treturn data;\n\t\t\t},\n\t\t\tcustomTransformOutput({ data, field, fieldAttributes }) {\n\t\t\t\tif (field === \"id\" || fieldAttributes.references?.field === \"id\") {\n\t\t\t\t\tif (data instanceof ObjectId) {\n\t\t\t\t\t\treturn data.toHexString();\n\t\t\t\t\t}\n\t\t\t\t\tif (Array.isArray(data)) {\n\t\t\t\t\t\treturn data.map((v) => {\n\t\t\t\t\t\t\tif (v instanceof ObjectId) {\n\t\t\t\t\t\t\t\treturn v.toHexString();\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn v;\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\treturn data;\n\t\t\t\t}\n\t\t\t\treturn data;\n\t\t\t},\n\t\t\tcustomIdGenerator() {\n\t\t\t\treturn new ObjectId().toString();\n\t\t\t},\n\t\t},\n\t\tadapter: createCustomAdapter(db),\n\t};\n\tlazyAdapter = createAdapterFactory(adapterOptions);\n\n\treturn (options: BetterAuthOptions): DBAdapter<BetterAuthOptions> => {\n\t\tlazyOptions = options;\n\t\treturn lazyAdapter(options);\n\t};\n};\n\n/**\n * Safely escape user input for use in a MongoDB regex.\n * This ensures the resulting pattern is treated as literal text,\n * and not as a regex with special syntax.\n *\n * @param input - The input string to escape. Any type that isn't a string will be converted to an empty string.\n * @param maxLength - The maximum length of the input string to escape. Defaults to 256. This is to prevent DOS attacks.\n * @returns The escaped string.\n */\nfunction escapeForMongoRegex(input: string, maxLength = 256): string {\n\tif (typeof input !== \"string\") return \"\";\n\n\t// Escape all PCRE special characters\n\t// Source: PCRE docs — https://www.pcre.org/original/doc/html/pcrepattern.html\n\treturn input.slice(0, maxLength).replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n}\n"],"mappings":";;;;AAwCA,MAAa,kBACZ,IACA,WACI;CACJ,IAAIA;CAEJ,MAAM,wBAAwB,YAA+B;EAC5D,MAAM,YAAY,QAAQ,UAAU,UAAU;AAC9C,MAAI,OA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