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