better-auth 1.2.6-beta.7 → 1.2.7-beta.1

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 (273) hide show
  1. package/dist/adapters/drizzle-adapter/index.cjs +186 -249
  2. package/dist/adapters/drizzle-adapter/index.d.cts +11 -49
  3. package/dist/adapters/drizzle-adapter/index.d.mts +11 -49
  4. package/dist/adapters/drizzle-adapter/index.d.ts +11 -49
  5. package/dist/adapters/drizzle-adapter/index.mjs +186 -249
  6. package/dist/adapters/index.cjs +26 -0
  7. package/dist/adapters/index.d.cts +17 -0
  8. package/dist/adapters/index.d.mts +17 -0
  9. package/dist/adapters/index.d.ts +17 -0
  10. package/dist/adapters/index.mjs +20 -0
  11. package/dist/adapters/kysely-adapter/index.cjs +7 -7
  12. package/dist/adapters/kysely-adapter/index.d.cts +17 -49
  13. package/dist/adapters/kysely-adapter/index.d.mts +17 -49
  14. package/dist/adapters/kysely-adapter/index.d.ts +17 -49
  15. package/dist/adapters/kysely-adapter/index.mjs +8 -8
  16. package/dist/adapters/memory-adapter/index.cjs +7 -8
  17. package/dist/adapters/memory-adapter/index.d.cts +9 -49
  18. package/dist/adapters/memory-adapter/index.d.mts +9 -49
  19. package/dist/adapters/memory-adapter/index.d.ts +9 -49
  20. package/dist/adapters/memory-adapter/index.mjs +8 -9
  21. package/dist/adapters/mongodb-adapter/index.cjs +2 -2
  22. package/dist/adapters/mongodb-adapter/index.d.cts +4 -4
  23. package/dist/adapters/mongodb-adapter/index.d.mts +4 -4
  24. package/dist/adapters/mongodb-adapter/index.d.ts +4 -4
  25. package/dist/adapters/mongodb-adapter/index.mjs +3 -3
  26. package/dist/adapters/prisma-adapter/index.cjs +130 -203
  27. package/dist/adapters/prisma-adapter/index.d.cts +17 -49
  28. package/dist/adapters/prisma-adapter/index.d.mts +17 -49
  29. package/dist/adapters/prisma-adapter/index.d.ts +17 -49
  30. package/dist/adapters/prisma-adapter/index.mjs +131 -204
  31. package/dist/adapters/test.cjs +710 -377
  32. package/dist/adapters/test.d.cts +64 -5
  33. package/dist/adapters/test.d.mts +64 -5
  34. package/dist/adapters/test.d.ts +64 -5
  35. package/dist/adapters/test.mjs +712 -380
  36. package/dist/api/index.cjs +61 -25
  37. package/dist/api/index.d.cts +3 -3
  38. package/dist/api/index.d.mts +3 -3
  39. package/dist/api/index.d.ts +3 -3
  40. package/dist/api/index.mjs +63 -27
  41. package/dist/client/index.d.cts +3 -3
  42. package/dist/client/index.d.mts +3 -3
  43. package/dist/client/index.d.ts +3 -3
  44. package/dist/client/plugins/index.cjs +13 -15
  45. package/dist/client/plugins/index.d.cts +80 -19
  46. package/dist/client/plugins/index.d.mts +80 -19
  47. package/dist/client/plugins/index.d.ts +80 -19
  48. package/dist/client/plugins/index.mjs +13 -16
  49. package/dist/client/react/index.cjs +4 -4
  50. package/dist/client/react/index.d.cts +3 -3
  51. package/dist/client/react/index.d.mts +3 -3
  52. package/dist/client/react/index.d.ts +3 -3
  53. package/dist/client/solid/index.d.cts +3 -3
  54. package/dist/client/solid/index.d.mts +3 -3
  55. package/dist/client/solid/index.d.ts +3 -3
  56. package/dist/client/svelte/index.d.cts +3 -3
  57. package/dist/client/svelte/index.d.mts +3 -3
  58. package/dist/client/svelte/index.d.ts +3 -3
  59. package/dist/client/vue/index.d.cts +3 -3
  60. package/dist/client/vue/index.d.mts +3 -3
  61. package/dist/client/vue/index.d.ts +3 -3
  62. package/dist/cookies/index.cjs +13 -2
  63. package/dist/cookies/index.d.cts +3 -3
  64. package/dist/cookies/index.d.mts +3 -3
  65. package/dist/cookies/index.d.ts +3 -3
  66. package/dist/cookies/index.mjs +13 -2
  67. package/dist/db/index.cjs +6 -5
  68. package/dist/db/index.d.cts +4 -4
  69. package/dist/db/index.d.mts +4 -4
  70. package/dist/db/index.d.ts +4 -4
  71. package/dist/db/index.mjs +7 -6
  72. package/dist/index.cjs +11 -7
  73. package/dist/index.d.cts +4 -4
  74. package/dist/index.d.mts +4 -4
  75. package/dist/index.d.ts +4 -4
  76. package/dist/index.mjs +14 -10
  77. package/dist/integrations/next-js.cjs +4 -5
  78. package/dist/integrations/next-js.d.cts +3 -3
  79. package/dist/integrations/next-js.d.mts +3 -3
  80. package/dist/integrations/next-js.d.ts +3 -3
  81. package/dist/integrations/next-js.mjs +5 -6
  82. package/dist/integrations/node.d.cts +3 -3
  83. package/dist/integrations/node.d.mts +3 -3
  84. package/dist/integrations/node.d.ts +3 -3
  85. package/dist/integrations/react-start.cjs +5 -6
  86. package/dist/integrations/react-start.d.cts +3 -3
  87. package/dist/integrations/react-start.d.mts +3 -3
  88. package/dist/integrations/react-start.d.ts +3 -3
  89. package/dist/integrations/react-start.mjs +6 -7
  90. package/dist/integrations/svelte-kit.d.cts +3 -3
  91. package/dist/integrations/svelte-kit.d.mts +3 -3
  92. package/dist/integrations/svelte-kit.d.ts +3 -3
  93. package/dist/oauth2/index.d.cts +5 -5
  94. package/dist/oauth2/index.d.mts +5 -5
  95. package/dist/oauth2/index.d.ts +5 -5
  96. package/dist/plugins/access/index.d.cts +1 -1
  97. package/dist/plugins/access/index.d.mts +1 -1
  98. package/dist/plugins/access/index.d.ts +1 -1
  99. package/dist/plugins/admin/access/index.d.cts +1 -1
  100. package/dist/plugins/admin/access/index.d.mts +1 -1
  101. package/dist/plugins/admin/access/index.d.ts +1 -1
  102. package/dist/plugins/admin/index.cjs +4 -4
  103. package/dist/plugins/admin/index.d.cts +74 -14
  104. package/dist/plugins/admin/index.d.mts +74 -14
  105. package/dist/plugins/admin/index.d.ts +74 -14
  106. package/dist/plugins/admin/index.mjs +5 -5
  107. package/dist/plugins/anonymous/index.cjs +4 -5
  108. package/dist/plugins/anonymous/index.d.cts +3 -3
  109. package/dist/plugins/anonymous/index.d.mts +3 -3
  110. package/dist/plugins/anonymous/index.d.ts +3 -3
  111. package/dist/plugins/anonymous/index.mjs +5 -6
  112. package/dist/plugins/bearer/index.cjs +2 -2
  113. package/dist/plugins/bearer/index.d.cts +3 -3
  114. package/dist/plugins/bearer/index.d.mts +3 -3
  115. package/dist/plugins/bearer/index.d.ts +3 -3
  116. package/dist/plugins/bearer/index.mjs +3 -3
  117. package/dist/plugins/captcha/index.cjs +110 -45
  118. package/dist/plugins/captcha/index.d.cts +26 -6
  119. package/dist/plugins/captcha/index.d.mts +26 -6
  120. package/dist/plugins/captcha/index.d.ts +26 -6
  121. package/dist/plugins/captcha/index.mjs +110 -45
  122. package/dist/plugins/custom-session/index.cjs +24 -5
  123. package/dist/plugins/custom-session/index.d.cts +25 -6
  124. package/dist/plugins/custom-session/index.d.mts +25 -6
  125. package/dist/plugins/custom-session/index.d.ts +25 -6
  126. package/dist/plugins/custom-session/index.mjs +25 -6
  127. package/dist/plugins/email-otp/index.cjs +96 -30
  128. package/dist/plugins/email-otp/index.d.cts +33 -10
  129. package/dist/plugins/email-otp/index.d.mts +33 -10
  130. package/dist/plugins/email-otp/index.d.ts +33 -10
  131. package/dist/plugins/email-otp/index.mjs +97 -31
  132. package/dist/plugins/generic-oauth/index.cjs +81 -20
  133. package/dist/plugins/generic-oauth/index.d.cts +46 -3
  134. package/dist/plugins/generic-oauth/index.d.mts +46 -3
  135. package/dist/plugins/generic-oauth/index.d.ts +46 -3
  136. package/dist/plugins/generic-oauth/index.mjs +82 -21
  137. package/dist/plugins/haveibeenpwned/index.cjs +98 -0
  138. package/dist/plugins/haveibeenpwned/index.d.cts +36 -0
  139. package/dist/plugins/haveibeenpwned/index.d.mts +36 -0
  140. package/dist/plugins/haveibeenpwned/index.d.ts +36 -0
  141. package/dist/plugins/haveibeenpwned/index.mjs +96 -0
  142. package/dist/plugins/index.cjs +583 -19
  143. package/dist/plugins/index.d.cts +7 -5
  144. package/dist/plugins/index.d.mts +7 -5
  145. package/dist/plugins/index.d.ts +7 -5
  146. package/dist/plugins/index.mjs +583 -21
  147. package/dist/plugins/jwt/index.cjs +45 -21
  148. package/dist/plugins/jwt/index.d.cts +52 -6
  149. package/dist/plugins/jwt/index.d.mts +52 -6
  150. package/dist/plugins/jwt/index.d.ts +52 -6
  151. package/dist/plugins/jwt/index.mjs +46 -22
  152. package/dist/plugins/magic-link/index.cjs +3 -3
  153. package/dist/plugins/magic-link/index.mjs +4 -4
  154. package/dist/plugins/multi-session/index.cjs +3 -3
  155. package/dist/plugins/multi-session/index.d.cts +3 -3
  156. package/dist/plugins/multi-session/index.d.mts +3 -3
  157. package/dist/plugins/multi-session/index.d.ts +3 -3
  158. package/dist/plugins/multi-session/index.mjs +4 -4
  159. package/dist/plugins/oauth-proxy/index.cjs +4 -4
  160. package/dist/plugins/oauth-proxy/index.d.cts +3 -3
  161. package/dist/plugins/oauth-proxy/index.d.mts +3 -3
  162. package/dist/plugins/oauth-proxy/index.d.ts +3 -3
  163. package/dist/plugins/oauth-proxy/index.mjs +5 -5
  164. package/dist/plugins/oidc-provider/index.cjs +227 -8
  165. package/dist/plugins/oidc-provider/index.d.cts +215 -3
  166. package/dist/plugins/oidc-provider/index.d.mts +215 -3
  167. package/dist/plugins/oidc-provider/index.d.ts +215 -3
  168. package/dist/plugins/oidc-provider/index.mjs +228 -9
  169. package/dist/plugins/one-tap/index.cjs +5 -5
  170. package/dist/plugins/one-tap/index.mjs +6 -6
  171. package/dist/plugins/one-time-token/index.cjs +119 -0
  172. package/dist/plugins/one-time-token/index.d.cts +134 -0
  173. package/dist/plugins/one-time-token/index.d.mts +134 -0
  174. package/dist/plugins/one-time-token/index.d.ts +134 -0
  175. package/dist/plugins/one-time-token/index.mjs +117 -0
  176. package/dist/plugins/open-api/index.cjs +3 -3
  177. package/dist/plugins/open-api/index.d.cts +3 -3
  178. package/dist/plugins/open-api/index.d.mts +3 -3
  179. package/dist/plugins/open-api/index.d.ts +3 -3
  180. package/dist/plugins/open-api/index.mjs +4 -4
  181. package/dist/plugins/organization/access/index.d.cts +1 -1
  182. package/dist/plugins/organization/access/index.d.mts +1 -1
  183. package/dist/plugins/organization/access/index.d.ts +1 -1
  184. package/dist/plugins/organization/index.cjs +4 -4
  185. package/dist/plugins/organization/index.d.cts +708 -55
  186. package/dist/plugins/organization/index.d.mts +708 -55
  187. package/dist/plugins/organization/index.d.ts +708 -55
  188. package/dist/plugins/organization/index.mjs +5 -5
  189. package/dist/plugins/passkey/index.cjs +82 -8
  190. package/dist/plugins/passkey/index.d.cts +72 -3
  191. package/dist/plugins/passkey/index.d.mts +72 -3
  192. package/dist/plugins/passkey/index.d.ts +72 -3
  193. package/dist/plugins/passkey/index.mjs +83 -9
  194. package/dist/plugins/phone-number/index.cjs +194 -26
  195. package/dist/plugins/phone-number/index.d.cts +132 -8
  196. package/dist/plugins/phone-number/index.d.mts +132 -8
  197. package/dist/plugins/phone-number/index.d.ts +132 -8
  198. package/dist/plugins/phone-number/index.mjs +195 -27
  199. package/dist/plugins/sso/index.cjs +190 -7
  200. package/dist/plugins/sso/index.d.cts +181 -15
  201. package/dist/plugins/sso/index.d.mts +181 -15
  202. package/dist/plugins/sso/index.d.ts +181 -15
  203. package/dist/plugins/sso/index.mjs +191 -8
  204. package/dist/plugins/two-factor/index.cjs +443 -92
  205. package/dist/plugins/two-factor/index.d.cts +230 -396
  206. package/dist/plugins/two-factor/index.d.mts +230 -396
  207. package/dist/plugins/two-factor/index.d.ts +230 -396
  208. package/dist/plugins/two-factor/index.mjs +431 -80
  209. package/dist/plugins/username/index.cjs +34 -31
  210. package/dist/plugins/username/index.d.cts +15 -12
  211. package/dist/plugins/username/index.d.mts +15 -12
  212. package/dist/plugins/username/index.d.ts +15 -12
  213. package/dist/plugins/username/index.mjs +35 -32
  214. package/dist/shared/better-auth.1DR6suCQ.mjs +307 -0
  215. package/dist/shared/{better-auth.BSsp73pg.cjs → better-auth.B7cZ2juS.cjs} +15 -14
  216. package/dist/shared/{better-auth.bKwabe3I.d.mts → better-auth.B88xucNq.d.mts} +529 -39
  217. package/dist/shared/{better-auth.CApEjVDP.cjs → better-auth.BW8BpneG.cjs} +4 -1
  218. package/dist/shared/{better-auth.BiQsvaIP.d.cts → better-auth.BcU1Kjyq.d.cts} +2051 -518
  219. package/dist/shared/better-auth.BfG24BjZ.cjs +118 -0
  220. package/dist/shared/{better-auth.A3TjrU8G.mjs → better-auth.Bk5IMdhM.mjs} +32 -12
  221. package/dist/shared/{better-auth.D9VnBkRI.mjs → better-auth.Bm9HxIzE.mjs} +47 -24
  222. package/dist/shared/{better-auth.BRf6Iynu.d.ts → better-auth.Bwc-6kOr.d.ts} +1 -1
  223. package/dist/shared/{better-auth.D-oLmHIj.d.mts → better-auth.CA2hFK4N.d.ts} +2051 -518
  224. package/dist/shared/{better-auth.Dmhe30iW.d.mts → better-auth.CGukGrxT.d.cts} +1 -1
  225. package/dist/shared/{better-auth.CsSpq0zL.cjs → better-auth.CHUzBidy.cjs} +46 -23
  226. package/dist/shared/{better-auth.DWRligF8.d.cts → better-auth.CT9J6rD-.d.cts} +539 -7
  227. package/dist/shared/better-auth.CVCo5Z2T.cjs +310 -0
  228. package/dist/shared/{better-auth.D4jH-sJA.mjs → better-auth.CWwVo_61.mjs} +458 -118
  229. package/dist/shared/{better-auth.Bi8FQwDD.d.cts → better-auth.CYegVoq1.d.cts} +1 -1
  230. package/dist/shared/{better-auth.Bi8FQwDD.d.mts → better-auth.CYegVoq1.d.mts} +1 -1
  231. package/dist/shared/{better-auth.Bi8FQwDD.d.ts → better-auth.CYegVoq1.d.ts} +1 -1
  232. package/dist/shared/{better-auth.CepcSj5H.mjs → better-auth.Cc72UxUH.mjs} +1 -2
  233. package/dist/shared/{better-auth.BWp5dztg.d.ts → better-auth.CmN4mlPh.d.ts} +539 -7
  234. package/dist/shared/{better-auth.DH3YjMQH.mjs → better-auth.Cqykj82J.mjs} +1 -1
  235. package/dist/shared/{better-auth.wcdMj2cT.d.mts → better-auth.DIt2e3lu.d.mts} +539 -7
  236. package/dist/shared/{better-auth.BANAxdkL.d.ts → better-auth.DNTAFSt1.d.ts} +529 -39
  237. package/dist/shared/{better-auth.DU2QNVc_.d.ts → better-auth.DQ7OSJbI.d.mts} +2051 -518
  238. package/dist/shared/{better-auth.DLTzKoOS.cjs → better-auth.DSVbLSt7.cjs} +4 -1
  239. package/dist/shared/{better-auth.B2Fw1vhH.d.cts → better-auth.DTiSPWEk.d.cts} +529 -39
  240. package/dist/shared/better-auth.DURsStt9.mjs +116 -0
  241. package/dist/shared/{better-auth.BIjcZ_vt.cjs → better-auth.DYoLD99C.cjs} +31 -11
  242. package/dist/shared/{better-auth.CV1L7TPV.cjs → better-auth.D_ZIX1O8.cjs} +317 -47
  243. package/dist/shared/{better-auth.C5H9XEzZ.cjs → better-auth.DcWKCjjf.cjs} +1 -2
  244. package/dist/shared/{better-auth.BDYXUcLv.cjs → better-auth.Dg0siV5C.cjs} +457 -117
  245. package/dist/shared/better-auth.DjryM8pE.cjs +760 -0
  246. package/dist/shared/{better-auth.DPBQN9Fs.mjs → better-auth.Dn_Ms1Uf.mjs} +318 -48
  247. package/dist/shared/{better-auth.DiG4KL2x.mjs → better-auth.OuYYTHC7.mjs} +4 -1
  248. package/dist/shared/{better-auth.DtC8i3pf.d.cts → better-auth.S1jimRbX.d.mts} +1 -1
  249. package/dist/shared/better-auth.SPmq4a4z.d.mts +344 -0
  250. package/dist/shared/{better-auth.cOCrlspr.mjs → better-auth.bkwPl2G4.mjs} +4 -1
  251. package/dist/shared/better-auth.cp2rC2iM.d.ts +344 -0
  252. package/dist/shared/better-auth.eVy4DZvP.d.cts +344 -0
  253. package/dist/shared/{better-auth.BrOpzmqo.mjs → better-auth.iKoUsdFE.mjs} +15 -14
  254. package/dist/shared/better-auth.rSYJCd3o.mjs +758 -0
  255. package/dist/social-providers/index.cjs +75 -3
  256. package/dist/social-providers/index.d.cts +2 -2
  257. package/dist/social-providers/index.d.mts +2 -2
  258. package/dist/social-providers/index.d.ts +2 -2
  259. package/dist/social-providers/index.mjs +77 -6
  260. package/dist/types/index.d.cts +4 -4
  261. package/dist/types/index.d.mts +4 -4
  262. package/dist/types/index.d.ts +4 -4
  263. package/package.json +42 -5
  264. package/dist/chunks/server.cjs +0 -905
  265. package/dist/chunks/server.mjs +0 -895
  266. package/dist/shared/better-auth.BcoSd9tC.mjs +0 -10
  267. package/dist/shared/better-auth.BnRFp-t0.mjs +0 -405
  268. package/dist/shared/better-auth.C1-vpKly.cjs +0 -12
  269. package/dist/shared/better-auth.ClTSOgiD.mjs +0 -140
  270. package/dist/shared/better-auth.DC8JQbiE.mjs +0 -173
  271. package/dist/shared/better-auth.DWHWPllD.cjs +0 -175
  272. package/dist/shared/better-auth.DqLjzBlO.cjs +0 -408
  273. package/dist/shared/better-auth.m575EIBC.cjs +0 -144
@@ -0,0 +1,758 @@
1
+ import { s as safeJSONParse } from './better-auth.tB5eU6EY.mjs';
2
+ import { w as withApplyDefault } from './better-auth.0TC26uRi.mjs';
3
+ import { g as getAuthTables } from './better-auth.DORkW_Ge.mjs';
4
+ import { g as generateId } from './better-auth.BUPPRXfK.mjs';
5
+ import 'zod';
6
+ import 'better-call';
7
+ import '@better-auth/utils/hash';
8
+ import '@noble/ciphers/chacha';
9
+ import '@noble/ciphers/utils';
10
+ import '@noble/ciphers/webcrypto';
11
+ import '@better-auth/utils/base64';
12
+ import 'jose';
13
+ import '@noble/hashes/scrypt';
14
+ import '@better-auth/utils';
15
+ import '@better-auth/utils/hex';
16
+ import '@noble/hashes/utils';
17
+ import './better-auth.B4Qoxdgc.mjs';
18
+ import { l as logger } from './better-auth.Cqykj82J.mjs';
19
+
20
+ let debugLogs = [];
21
+ let transactionId = -1;
22
+ const colors = {
23
+ reset: "\x1B[0m",
24
+ bright: "\x1B[1m",
25
+ dim: "\x1B[2m",
26
+ fg: {
27
+ yellow: "\x1B[33m",
28
+ magenta: "\x1B[35m"},
29
+ bg: {
30
+ black: "\x1B[40m"}
31
+ };
32
+ const createAdapter = ({
33
+ adapter,
34
+ config: cfg
35
+ }) => (options) => {
36
+ const config = {
37
+ ...cfg,
38
+ supportsBooleans: cfg.supportsBooleans ?? true,
39
+ supportsDates: cfg.supportsDates ?? true,
40
+ supportsJSON: cfg.supportsJSON ?? false,
41
+ adapterName: cfg.adapterName ?? cfg.adapterId,
42
+ supportsNumericIds: cfg.supportsNumericIds ?? true
43
+ };
44
+ if (options.advanced?.database?.useNumberId === true && config.supportsNumericIds === false) {
45
+ throw new Error(
46
+ `[${config.adapterName}] Your database or database adapter does not support numeric ids. Please disable "useNumberId" in your config.`
47
+ );
48
+ }
49
+ const schema = getAuthTables(options);
50
+ const getDefaultFieldName = ({
51
+ field,
52
+ model: unsafe_model
53
+ }) => {
54
+ if (field === "id") {
55
+ return field;
56
+ }
57
+ const model = getDefaultModelName(unsafe_model);
58
+ let f = schema[model]?.fields[field];
59
+ if (!f) {
60
+ f = Object.values(schema[model]?.fields).find(
61
+ (f2) => f2.fieldName === field
62
+ );
63
+ }
64
+ if (!f) {
65
+ debugLog(`Field ${field} not found in model ${model}`);
66
+ debugLog(`Schema:`, schema);
67
+ throw new Error(`Field ${field} not found in model ${model}`);
68
+ }
69
+ return field;
70
+ };
71
+ const getDefaultModelName = (model) => {
72
+ if (config.usePlural && model.charAt(model.length - 1) === "s") {
73
+ let pluralessModel = model.slice(0, -1);
74
+ let m2 = schema[pluralessModel] ? pluralessModel : void 0;
75
+ if (!m2) {
76
+ m2 = Object.entries(schema).find(
77
+ ([_, f]) => f.modelName === pluralessModel
78
+ )?.[0];
79
+ }
80
+ if (m2) {
81
+ return m2;
82
+ }
83
+ }
84
+ let m = schema[model] ? model : void 0;
85
+ if (!m) {
86
+ m = Object.entries(schema).find(([_, f]) => f.modelName === model)?.[0];
87
+ }
88
+ if (!m) {
89
+ debugLog(`Model "${model}" not found in schema`);
90
+ debugLog(`Schema:`, schema);
91
+ throw new Error(`Model "${model}" not found in schema`);
92
+ }
93
+ return m;
94
+ };
95
+ const getModelName = (model) => {
96
+ return schema[model].modelName !== model ? schema[model].modelName : config.usePlural ? `${model}s` : model;
97
+ };
98
+ function getFieldName({
99
+ model: model_name,
100
+ field: field_name
101
+ }) {
102
+ const model = getDefaultModelName(model_name);
103
+ const field = getDefaultFieldName({ model, field: field_name });
104
+ return schema[model]?.fields[field]?.fieldName || field;
105
+ }
106
+ const debugLog = (...args) => {
107
+ if (config.debugLogs === true || typeof config.debugLogs === "object") {
108
+ if (typeof config.debugLogs === "object" && "isRunningAdapterTests" in config.debugLogs) {
109
+ if (config.debugLogs.isRunningAdapterTests) {
110
+ args.shift();
111
+ debugLogs.push(args);
112
+ }
113
+ return;
114
+ }
115
+ if (typeof config.debugLogs === "object" && config.debugLogs.logCondition && !config.debugLogs.logCondition?.()) {
116
+ return;
117
+ }
118
+ if (typeof args[0] === "object" && "method" in args[0]) {
119
+ const method = args.shift().method;
120
+ if (typeof config.debugLogs === "object") {
121
+ if (method === "create" && !config.debugLogs.create) {
122
+ return;
123
+ } else if (method === "update" && !config.debugLogs.update) {
124
+ return;
125
+ } else if (method === "updateMany" && !config.debugLogs.updateMany) {
126
+ return;
127
+ } else if (method === "findOne" && !config.debugLogs.findOne) {
128
+ return;
129
+ } else if (method === "findMany" && !config.debugLogs.findMany) {
130
+ return;
131
+ } else if (method === "delete" && !config.debugLogs.delete) {
132
+ return;
133
+ } else if (method === "deleteMany" && !config.debugLogs.deleteMany) {
134
+ return;
135
+ } else if (method === "count" && !config.debugLogs.count) {
136
+ return;
137
+ }
138
+ }
139
+ logger.info(`[${config.adapterName}]`, ...args);
140
+ } else {
141
+ logger.info(`[${config.adapterName}]`, ...args);
142
+ }
143
+ }
144
+ };
145
+ const idField = ({ customModelName }) => {
146
+ const shouldGenerateId = !config.disableIdGeneration && !options.advanced?.database?.useNumberId;
147
+ const model = getDefaultModelName(customModelName ?? "id");
148
+ return {
149
+ type: options.advanced?.database?.useNumberId ? "number" : "string",
150
+ required: shouldGenerateId ? true : false,
151
+ ...shouldGenerateId ? {
152
+ defaultValue() {
153
+ if (config.disableIdGeneration) return void 0;
154
+ const useNumberId = options.advanced?.database?.useNumberId;
155
+ let generateId$1 = options.advanced?.database?.generateId;
156
+ if (options.advanced?.generateId) {
157
+ logger.warn(
158
+ "Your Better Auth config includes advanced.generateId which is deprecated. Please use advanced.database.generateId instead. This will be removed in future releases."
159
+ );
160
+ generateId$1 = options.advanced?.generateId;
161
+ }
162
+ if (generateId$1 === false || useNumberId) return void 0;
163
+ if (generateId$1) {
164
+ return generateId$1({
165
+ model
166
+ });
167
+ }
168
+ if (config.customIdGenerator) {
169
+ return config.customIdGenerator({ model });
170
+ }
171
+ return generateId();
172
+ }
173
+ } : {}
174
+ };
175
+ };
176
+ const getFieldAttributes = ({
177
+ model,
178
+ field
179
+ }) => {
180
+ const defaultModelName = getDefaultModelName(model);
181
+ const defaultFieldName = getDefaultFieldName({
182
+ field,
183
+ model
184
+ });
185
+ const fields = schema[defaultModelName].fields;
186
+ fields.id = idField({ customModelName: defaultModelName });
187
+ return fields[defaultFieldName];
188
+ };
189
+ const adapterInstance = adapter({
190
+ options,
191
+ schema,
192
+ debugLog,
193
+ getFieldName,
194
+ getModelName,
195
+ getDefaultModelName,
196
+ getDefaultFieldName,
197
+ getFieldAttributes
198
+ });
199
+ const transformInput = async (data, unsafe_model, action) => {
200
+ const transformedData = {};
201
+ const fields = schema[unsafe_model].fields;
202
+ const newMappedKeys = config.mapKeysTransformInput ?? {};
203
+ if (!config.disableIdGeneration && !options.advanced?.database?.useNumberId) {
204
+ fields.id = idField({ customModelName: unsafe_model });
205
+ }
206
+ for (const field in fields) {
207
+ const value = data[field];
208
+ const fieldAttributes = fields[field];
209
+ let newFieldName = newMappedKeys[field] || fields[field].fieldName || field;
210
+ if (value === void 0 && (!fieldAttributes.defaultValue && !fieldAttributes.transform?.input || action === "update")) {
211
+ continue;
212
+ }
213
+ let newValue = withApplyDefault(value, fieldAttributes, action);
214
+ if (fieldAttributes.transform?.input) {
215
+ newValue = await fieldAttributes.transform.input(newValue);
216
+ }
217
+ if (fieldAttributes.references?.field === "id" && options.advanced?.database?.useNumberId) {
218
+ if (Array.isArray(newValue)) {
219
+ newValue = newValue.map(Number);
220
+ } else {
221
+ newValue = Number(newValue);
222
+ }
223
+ } else if (config.supportsJSON === false && typeof newValue === "object" && //@ts-expect-error -Future proofing
224
+ fieldAttributes.type === "json") {
225
+ newValue = JSON.stringify(newValue);
226
+ } else if (config.supportsDates === false && newValue instanceof Date && fieldAttributes.type === "date") {
227
+ newValue = newValue.toISOString();
228
+ } else if (config.supportsBooleans === false && typeof newValue === "boolean") {
229
+ newValue = newValue ? 1 : 0;
230
+ }
231
+ if (config.customTransformInput) {
232
+ newValue = config.customTransformInput({
233
+ data: newValue,
234
+ action,
235
+ field: newFieldName,
236
+ fieldAttributes,
237
+ model: unsafe_model,
238
+ schema,
239
+ options
240
+ });
241
+ }
242
+ transformedData[newFieldName] = newValue;
243
+ }
244
+ return transformedData;
245
+ };
246
+ const transformOutput = async (data, unsafe_model, select = []) => {
247
+ if (!data) return null;
248
+ const newMappedKeys = config.mapKeysTransformOutput ?? {};
249
+ const transformedData = {};
250
+ const tableSchema = schema[unsafe_model].fields;
251
+ const idKey = Object.entries(newMappedKeys).find(
252
+ ([_, v]) => v === "id"
253
+ )?.[0];
254
+ tableSchema[idKey ?? "id"] = {
255
+ type: options.advanced?.database?.useNumberId ? "number" : "string"
256
+ };
257
+ for (const key in tableSchema) {
258
+ if (select.length && !select.includes(key)) {
259
+ continue;
260
+ }
261
+ const field = tableSchema[key];
262
+ if (field) {
263
+ const originalKey = field.fieldName || key;
264
+ let newValue = data[Object.entries(newMappedKeys).find(
265
+ ([_, v]) => v === originalKey
266
+ )?.[0] || originalKey];
267
+ if (field.transform?.output) {
268
+ newValue = await field.transform.output(newValue);
269
+ }
270
+ let newFieldName = newMappedKeys[key] || key;
271
+ if (originalKey === "id" || field.references?.field === "id") {
272
+ if (typeof newValue !== "undefined") newValue = String(newValue);
273
+ } else if (config.supportsJSON === false && typeof newValue === "string" && //@ts-expect-error - Future proofing
274
+ field.type === "json") {
275
+ newValue = safeJSONParse(newValue);
276
+ } else if (config.supportsDates === false && typeof newValue === "string" && field.type === "date") {
277
+ newValue = new Date(newValue);
278
+ } else if (config.supportsBooleans === false && typeof newValue === "number" && field.type === "boolean") {
279
+ newValue = newValue === 1;
280
+ }
281
+ if (config.customTransformOutput) {
282
+ newValue = config.customTransformOutput({
283
+ data: newValue,
284
+ field: newFieldName,
285
+ fieldAttributes: field,
286
+ select,
287
+ model: unsafe_model,
288
+ schema,
289
+ options
290
+ });
291
+ }
292
+ transformedData[newFieldName] = newValue;
293
+ }
294
+ }
295
+ return transformedData;
296
+ };
297
+ const transformWhereClause = ({
298
+ model,
299
+ where
300
+ }) => {
301
+ if (!where) return void 0;
302
+ return where.map((w) => {
303
+ const {
304
+ field: unsafe_field,
305
+ value,
306
+ operator = "eq",
307
+ connector = "AND"
308
+ } = w;
309
+ if (operator === "in") {
310
+ if (!Array.isArray(value)) {
311
+ throw new Error("Value must be an array");
312
+ }
313
+ }
314
+ const defaultModelName = getDefaultModelName(model);
315
+ const defaultFieldName = getDefaultFieldName({
316
+ field: unsafe_field,
317
+ model
318
+ });
319
+ const fieldName = getFieldName({
320
+ field: defaultFieldName,
321
+ model: defaultModelName
322
+ });
323
+ const fieldAttr = getFieldAttributes({
324
+ field: defaultFieldName,
325
+ model: defaultModelName
326
+ });
327
+ if (defaultFieldName === "id" || fieldAttr.references?.field === "id") {
328
+ if (options.advanced?.database?.useNumberId) {
329
+ if (Array.isArray(value)) {
330
+ return {
331
+ operator,
332
+ connector,
333
+ field: fieldName,
334
+ value: value.map(Number)
335
+ };
336
+ }
337
+ return {
338
+ operator,
339
+ connector,
340
+ field: fieldName,
341
+ value: Number(value)
342
+ };
343
+ }
344
+ }
345
+ return {
346
+ operator,
347
+ connector,
348
+ field: fieldName,
349
+ value
350
+ };
351
+ });
352
+ };
353
+ return {
354
+ create: async ({
355
+ data: unsafeData,
356
+ model: unsafeModel,
357
+ select
358
+ }) => {
359
+ transactionId++;
360
+ let thisTransactionId = transactionId;
361
+ const model = getModelName(unsafeModel);
362
+ if ("id" in unsafeData) {
363
+ logger.warn(
364
+ `[${config.adapterName}] - You are trying to create a record with an id. This is not allowed as we handle id generation for you. The id will be ignored.`
365
+ );
366
+ const err = new Error();
367
+ const stack = err.stack?.split("\n").filter((_, i) => i !== 1).join("\n").replace("Error:", "Create method with `id` being called at:");
368
+ console.log(stack);
369
+ unsafeData.id = void 0;
370
+ }
371
+ debugLog(
372
+ { method: "create" },
373
+ `${formatTransactionId(thisTransactionId)} ${formatStep(1, 4)}`,
374
+ `${formatMethod("create")} ${formatAction("Unsafe Input")}:`,
375
+ { model, data: unsafeData }
376
+ );
377
+ const data = await transformInput(
378
+ unsafeData,
379
+ unsafeModel,
380
+ "create"
381
+ );
382
+ debugLog(
383
+ { method: "create" },
384
+ `${formatTransactionId(thisTransactionId)} ${formatStep(2, 4)}`,
385
+ `${formatMethod("create")} ${formatAction("Parsed Input")}:`,
386
+ { model, data }
387
+ );
388
+ const res = await adapterInstance.create({ data, model });
389
+ debugLog(
390
+ { method: "create" },
391
+ `${formatTransactionId(thisTransactionId)} ${formatStep(3, 4)}`,
392
+ `${formatMethod("create")} ${formatAction("DB Result")}:`,
393
+ { model, res }
394
+ );
395
+ const transformed = await transformOutput(res, unsafeModel, select);
396
+ debugLog(
397
+ { method: "create" },
398
+ `${formatTransactionId(thisTransactionId)} ${formatStep(4, 4)}`,
399
+ `${formatMethod("create")} ${formatAction("Parsed Result")}:`,
400
+ { model, data: transformed }
401
+ );
402
+ return transformed;
403
+ },
404
+ update: async ({
405
+ model: unsafeModel,
406
+ where: unsafeWhere,
407
+ update: unsafeData
408
+ }) => {
409
+ transactionId++;
410
+ let thisTransactionId = transactionId;
411
+ const model = getModelName(unsafeModel);
412
+ const where = transformWhereClause({
413
+ model: unsafeModel,
414
+ where: unsafeWhere
415
+ });
416
+ debugLog(
417
+ { method: "update" },
418
+ `${formatTransactionId(thisTransactionId)} ${formatStep(1, 4)}`,
419
+ `${formatMethod("update")} ${formatAction("Unsafe Input")}:`,
420
+ { model, data: unsafeData }
421
+ );
422
+ const data = await transformInput(
423
+ unsafeData,
424
+ unsafeModel,
425
+ "update"
426
+ );
427
+ debugLog(
428
+ { method: "update" },
429
+ `${formatTransactionId(thisTransactionId)} ${formatStep(2, 4)}`,
430
+ `${formatMethod("update")} ${formatAction("Parsed Input")}:`,
431
+ { model, data }
432
+ );
433
+ const res = await adapterInstance.update({
434
+ model,
435
+ where,
436
+ update: data
437
+ });
438
+ debugLog(
439
+ { method: "update" },
440
+ `${formatTransactionId(thisTransactionId)} ${formatStep(3, 4)}`,
441
+ `${formatMethod("update")} ${formatAction("DB Result")}:`,
442
+ { model, data: res }
443
+ );
444
+ const transformed = await transformOutput(res, unsafeModel);
445
+ debugLog(
446
+ { method: "update" },
447
+ `${formatTransactionId(thisTransactionId)} ${formatStep(4, 4)}`,
448
+ `${formatMethod("update")} ${formatAction("Parsed Result")}:`,
449
+ { model, data: transformed }
450
+ );
451
+ return transformed;
452
+ },
453
+ updateMany: async ({
454
+ model: unsafeModel,
455
+ where: unsafeWhere,
456
+ update: unsafeData
457
+ }) => {
458
+ transactionId++;
459
+ let thisTransactionId = transactionId;
460
+ const model = getModelName(unsafeModel);
461
+ const where = transformWhereClause({
462
+ model: unsafeModel,
463
+ where: unsafeWhere
464
+ });
465
+ debugLog(
466
+ { method: "updateMany" },
467
+ `${formatTransactionId(thisTransactionId)} ${formatStep(1, 4)}`,
468
+ `${formatMethod("updateMany")} ${formatAction("Unsafe Input")}:`,
469
+ { model, data: unsafeData }
470
+ );
471
+ const data = await transformInput(unsafeData, unsafeModel, "update");
472
+ debugLog(
473
+ { method: "updateMany" },
474
+ `${formatTransactionId(thisTransactionId)} ${formatStep(2, 4)}`,
475
+ `${formatMethod("updateMany")} ${formatAction("Parsed Input")}:`,
476
+ { model, data }
477
+ );
478
+ const updatedCount = await adapterInstance.updateMany({
479
+ model,
480
+ where,
481
+ update: data
482
+ });
483
+ debugLog(
484
+ { method: "updateMany" },
485
+ `${formatTransactionId(thisTransactionId)} ${formatStep(3, 4)}`,
486
+ `${formatMethod("updateMany")} ${formatAction("DB Result")}:`,
487
+ { model, data: updatedCount }
488
+ );
489
+ debugLog(
490
+ { method: "updateMany" },
491
+ `${formatTransactionId(thisTransactionId)} ${formatStep(4, 4)}`,
492
+ `${formatMethod("updateMany")} ${formatAction("Parsed Result")}:`,
493
+ { model, data: updatedCount }
494
+ );
495
+ return updatedCount;
496
+ },
497
+ findOne: async ({
498
+ model: unsafeModel,
499
+ where: unsafeWhere,
500
+ select
501
+ }) => {
502
+ transactionId++;
503
+ let thisTransactionId = transactionId;
504
+ const model = getModelName(unsafeModel);
505
+ const where = transformWhereClause({
506
+ model: unsafeModel,
507
+ where: unsafeWhere
508
+ });
509
+ debugLog(
510
+ { method: "findOne" },
511
+ `${formatTransactionId(thisTransactionId)} ${formatStep(1, 3)}`,
512
+ `${formatMethod("findOne")}:`,
513
+ { model, where, select }
514
+ );
515
+ const res = await adapterInstance.findOne({
516
+ model,
517
+ where,
518
+ select
519
+ });
520
+ debugLog(
521
+ { method: "findOne" },
522
+ `${formatTransactionId(thisTransactionId)} ${formatStep(2, 3)}`,
523
+ `${formatMethod("findOne")} ${formatAction("DB Result")}:`,
524
+ { model, data: res }
525
+ );
526
+ const transformed = await transformOutput(
527
+ res,
528
+ unsafeModel,
529
+ select
530
+ );
531
+ debugLog(
532
+ { method: "findOne" },
533
+ `${formatTransactionId(thisTransactionId)} ${formatStep(3, 3)}`,
534
+ `${formatMethod("findOne")} ${formatAction("Parsed Result")}:`,
535
+ { model, data: transformed }
536
+ );
537
+ return transformed;
538
+ },
539
+ findMany: async ({
540
+ model: unsafeModel,
541
+ where: unsafeWhere,
542
+ limit: unsafeLimit,
543
+ sortBy,
544
+ offset
545
+ }) => {
546
+ transactionId++;
547
+ let thisTransactionId = transactionId;
548
+ const limit = unsafeLimit ?? options.advanced?.database?.defaultFindManyLimit ?? 100;
549
+ const model = getModelName(unsafeModel);
550
+ const where = transformWhereClause({
551
+ model: unsafeModel,
552
+ where: unsafeWhere
553
+ });
554
+ debugLog(
555
+ { method: "findMany" },
556
+ `${formatTransactionId(thisTransactionId)} ${formatStep(1, 3)}`,
557
+ `${formatMethod("findMany")}:`,
558
+ { model, where, limit, sortBy, offset }
559
+ );
560
+ const res = await adapterInstance.findMany({
561
+ model,
562
+ where,
563
+ limit,
564
+ sortBy,
565
+ offset
566
+ });
567
+ debugLog(
568
+ { method: "findMany" },
569
+ `${formatTransactionId(thisTransactionId)} ${formatStep(2, 3)}`,
570
+ `${formatMethod("findMany")} ${formatAction("DB Result")}:`,
571
+ { model, data: res }
572
+ );
573
+ const transformed = await Promise.all(
574
+ res.map(async (r) => await transformOutput(r, unsafeModel))
575
+ );
576
+ debugLog(
577
+ { method: "findMany" },
578
+ `${formatTransactionId(thisTransactionId)} ${formatStep(3, 3)}`,
579
+ `${formatMethod("findMany")} ${formatAction("Parsed Result")}:`,
580
+ { model, data: transformed }
581
+ );
582
+ return transformed;
583
+ },
584
+ delete: async ({
585
+ model: unsafeModel,
586
+ where: unsafeWhere
587
+ }) => {
588
+ transactionId++;
589
+ let thisTransactionId = transactionId;
590
+ const model = getModelName(unsafeModel);
591
+ const where = transformWhereClause({
592
+ model: unsafeModel,
593
+ where: unsafeWhere
594
+ });
595
+ debugLog(
596
+ { method: "delete" },
597
+ `${formatTransactionId(thisTransactionId)} ${formatStep(1, 2)}`,
598
+ `${formatMethod("delete")}:`,
599
+ { model, where }
600
+ );
601
+ await adapterInstance.delete({
602
+ model,
603
+ where
604
+ });
605
+ debugLog(
606
+ { method: "delete" },
607
+ `${formatTransactionId(thisTransactionId)} ${formatStep(2, 2)}`,
608
+ `${formatMethod("delete")} ${formatAction("DB Result")}:`,
609
+ { model }
610
+ );
611
+ },
612
+ deleteMany: async ({
613
+ model: unsafeModel,
614
+ where: unsafeWhere
615
+ }) => {
616
+ transactionId++;
617
+ let thisTransactionId = transactionId;
618
+ const model = getModelName(unsafeModel);
619
+ const where = transformWhereClause({
620
+ model: unsafeModel,
621
+ where: unsafeWhere
622
+ });
623
+ debugLog(
624
+ { method: "deleteMany" },
625
+ `${formatTransactionId(thisTransactionId)} ${formatStep(1, 2)}`,
626
+ `${formatMethod("deleteMany")} ${formatAction("DeleteMany")}:`,
627
+ { model, where }
628
+ );
629
+ const res = await adapterInstance.deleteMany({
630
+ model,
631
+ where
632
+ });
633
+ debugLog(
634
+ { method: "deleteMany" },
635
+ `${formatTransactionId(thisTransactionId)} ${formatStep(2, 2)}`,
636
+ `${formatMethod("deleteMany")} ${formatAction("DB Result")}:`,
637
+ { model, data: res }
638
+ );
639
+ return res;
640
+ },
641
+ count: async ({
642
+ model: unsafeModel,
643
+ where: unsafeWhere
644
+ }) => {
645
+ transactionId++;
646
+ let thisTransactionId = transactionId;
647
+ const model = getModelName(unsafeModel);
648
+ const where = transformWhereClause({
649
+ model: unsafeModel,
650
+ where: unsafeWhere
651
+ });
652
+ debugLog(
653
+ { method: "count" },
654
+ `${formatTransactionId(thisTransactionId)} ${formatStep(1, 2)}`,
655
+ `${formatMethod("count")}:`,
656
+ {
657
+ model,
658
+ where
659
+ }
660
+ );
661
+ const res = await adapterInstance.count({
662
+ model,
663
+ where
664
+ });
665
+ debugLog(
666
+ { method: "count" },
667
+ `${formatTransactionId(thisTransactionId)} ${formatStep(2, 2)}`,
668
+ `${formatMethod("count")}:`,
669
+ {
670
+ model,
671
+ data: res
672
+ }
673
+ );
674
+ return res;
675
+ },
676
+ createSchema: adapterInstance.createSchema ? async (_, file) => {
677
+ const tables = getAuthTables(options);
678
+ if (options.secondaryStorage && !options.session?.storeSessionInDatabase) {
679
+ delete tables.session;
680
+ }
681
+ if (options.rateLimit && options.rateLimit.storage === "database" && // rate-limit will default to enabled in production,
682
+ // and given storage is database, it will try to use the rate-limit table,
683
+ // so we should make sure to generate rate-limit table schema
684
+ (typeof options.rateLimit.enabled === "undefined" || // and of course if they forcefully set to true, then they want rate-limit,
685
+ // thus we should also generate rate-limit table schema
686
+ options.rateLimit.enabled === true)) {
687
+ tables.ratelimit = {
688
+ modelName: options.rateLimit.modelName ?? "ratelimit",
689
+ fields: {
690
+ key: {
691
+ type: "string",
692
+ unique: true,
693
+ required: true,
694
+ fieldName: options.rateLimit.fields?.key ?? "key"
695
+ },
696
+ count: {
697
+ type: "number",
698
+ required: true,
699
+ fieldName: options.rateLimit.fields?.count ?? "count"
700
+ },
701
+ lastRequest: {
702
+ type: "number",
703
+ required: true,
704
+ bigint: true,
705
+ defaultValue: () => Date.now(),
706
+ fieldName: options.rateLimit.fields?.lastRequest ?? "lastRequest"
707
+ }
708
+ }
709
+ };
710
+ }
711
+ return adapterInstance.createSchema({ file, tables });
712
+ } : void 0,
713
+ options: {
714
+ adapterConfig: config,
715
+ ...adapterInstance.options ?? {}
716
+ },
717
+ id: config.adapterId,
718
+ // Secretly export values ONLY if this adapter has enabled adapter-test-debug-logs.
719
+ // This would then be used during our adapter-tests to help print debug logs if a test fails.
720
+ //@ts-expect-error - ^^
721
+ ...config.debugLogs?.isRunningAdapterTests ? {
722
+ adapterTestDebugLogs: {
723
+ resetDebugLogs() {
724
+ debugLogs = [];
725
+ },
726
+ printDebugLogs() {
727
+ const separator = `\u2500`.repeat(80);
728
+ let log = debugLogs.reverse().map((log2) => {
729
+ log2[0] = `
730
+ ${log2[0]}`;
731
+ return [...log2, "\n"];
732
+ }).reduce(
733
+ (prev, curr) => {
734
+ return [...curr, ...prev];
735
+ },
736
+ [`
737
+ ${separator}`]
738
+ );
739
+ console.log(...log);
740
+ }
741
+ }
742
+ } : {}
743
+ };
744
+ };
745
+ function formatTransactionId(transactionId2) {
746
+ return `${colors.fg.magenta}#${transactionId2}${colors.reset}`;
747
+ }
748
+ function formatStep(step, total) {
749
+ return `${colors.bg.black}${colors.fg.yellow}[${step}/${total}]${colors.reset}`;
750
+ }
751
+ function formatMethod(method) {
752
+ return `${colors.bright}${method}${colors.reset}`;
753
+ }
754
+ function formatAction(action) {
755
+ return `${colors.dim}(${action})${colors.reset}`;
756
+ }
757
+
758
+ export { createAdapter as c };