@better-auth/core 1.5.0-beta.2 → 1.5.0-beta.20

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 (331) hide show
  1. package/LICENSE.md +15 -12
  2. package/README.md +17 -0
  3. package/dist/api/index.d.mts +280 -2
  4. package/dist/api/index.mjs +4 -2
  5. package/dist/api/index.mjs.map +1 -0
  6. package/dist/async_hooks/index.d.mts +2 -1
  7. package/dist/async_hooks/index.mjs +2 -1
  8. package/dist/async_hooks/index.mjs.map +1 -0
  9. package/dist/async_hooks/pure.index.d.mts +2 -1
  10. package/dist/async_hooks/pure.index.mjs +2 -1
  11. package/dist/async_hooks/pure.index.mjs.map +1 -0
  12. package/dist/context/endpoint-context.d.mts +20 -0
  13. package/dist/context/endpoint-context.mjs +32 -0
  14. package/dist/context/endpoint-context.mjs.map +1 -0
  15. package/dist/context/global.d.mts +7 -0
  16. package/dist/context/global.mjs +38 -0
  17. package/dist/context/global.mjs.map +1 -0
  18. package/dist/context/index.d.mts +5 -53
  19. package/dist/context/index.mjs +5 -2
  20. package/dist/context/request-state.d.mts +28 -0
  21. package/dist/context/request-state.mjs +50 -0
  22. package/dist/context/request-state.mjs.map +1 -0
  23. package/dist/context/transaction.d.mts +25 -0
  24. package/dist/context/transaction.mjs +96 -0
  25. package/dist/context/transaction.mjs.map +1 -0
  26. package/dist/db/adapter/factory.d.mts +20 -0
  27. package/dist/db/adapter/factory.mjs +725 -0
  28. package/dist/db/adapter/factory.mjs.map +1 -0
  29. package/dist/db/adapter/get-default-field-name.d.mts +19 -0
  30. package/dist/db/adapter/get-default-field-name.mjs +39 -0
  31. package/dist/db/adapter/get-default-field-name.mjs.map +1 -0
  32. package/dist/db/adapter/get-default-model-name.d.mts +13 -0
  33. package/dist/db/adapter/get-default-model-name.mjs +33 -0
  34. package/dist/db/adapter/get-default-model-name.mjs.map +1 -0
  35. package/dist/db/adapter/get-field-attributes.d.mts +30 -0
  36. package/dist/db/adapter/get-field-attributes.mjs +40 -0
  37. package/dist/db/adapter/get-field-attributes.mjs.map +1 -0
  38. package/dist/db/adapter/get-field-name.d.mts +19 -0
  39. package/dist/db/adapter/get-field-name.mjs +34 -0
  40. package/dist/db/adapter/get-field-name.mjs.map +1 -0
  41. package/dist/db/adapter/get-id-field.d.mts +40 -0
  42. package/dist/db/adapter/get-id-field.mjs +68 -0
  43. package/dist/db/adapter/get-id-field.mjs.map +1 -0
  44. package/dist/db/adapter/get-model-name.d.mts +13 -0
  45. package/dist/db/adapter/get-model-name.mjs +24 -0
  46. package/dist/db/adapter/get-model-name.mjs.map +1 -0
  47. package/dist/db/adapter/index.d.mts +520 -2
  48. package/dist/db/adapter/index.mjs +27 -972
  49. package/dist/db/adapter/index.mjs.map +1 -0
  50. package/dist/db/adapter/types.d.mts +108 -0
  51. package/dist/db/adapter/utils.d.mts +8 -0
  52. package/dist/db/adapter/utils.mjs +39 -0
  53. package/dist/db/adapter/utils.mjs.map +1 -0
  54. package/dist/db/get-tables.d.mts +9 -0
  55. package/dist/{get-tables-CMc_Emww.mjs → db/get-tables.mjs} +52 -39
  56. package/dist/db/get-tables.mjs.map +1 -0
  57. package/dist/db/index.d.mts +10 -2
  58. package/dist/db/index.mjs +7 -60
  59. package/dist/db/plugin.d.mts +13 -0
  60. package/dist/db/schema/account.d.mts +30 -0
  61. package/dist/db/schema/account.mjs +20 -0
  62. package/dist/db/schema/account.mjs.map +1 -0
  63. package/dist/db/schema/rate-limit.d.mts +20 -0
  64. package/dist/db/schema/rate-limit.mjs +12 -0
  65. package/dist/db/schema/rate-limit.mjs.map +1 -0
  66. package/dist/db/schema/session.d.mts +25 -0
  67. package/dist/db/schema/session.mjs +15 -0
  68. package/dist/db/schema/session.mjs.map +1 -0
  69. package/dist/db/schema/shared.d.mts +11 -0
  70. package/dist/db/schema/shared.mjs +12 -0
  71. package/dist/db/schema/shared.mjs.map +1 -0
  72. package/dist/db/schema/user.d.mts +24 -0
  73. package/dist/db/schema/user.mjs +14 -0
  74. package/dist/db/schema/user.mjs.map +1 -0
  75. package/dist/db/schema/verification.d.mts +23 -0
  76. package/dist/db/schema/verification.mjs +13 -0
  77. package/dist/db/schema/verification.mjs.map +1 -0
  78. package/dist/db/type.d.mts +169 -0
  79. package/dist/env/color-depth.d.mts +5 -0
  80. package/dist/env/color-depth.mjs +89 -0
  81. package/dist/env/color-depth.mjs.map +1 -0
  82. package/dist/env/env-impl.d.mts +33 -0
  83. package/dist/env/env-impl.mjs +83 -0
  84. package/dist/env/env-impl.mjs.map +1 -0
  85. package/dist/env/index.d.mts +4 -2
  86. package/dist/env/index.mjs +3 -1
  87. package/dist/{index-BRBu0-5h.d.mts → env/logger.d.mts} +2 -35
  88. package/dist/env/logger.mjs +82 -0
  89. package/dist/env/logger.mjs.map +1 -0
  90. package/dist/error/codes.d.mts +69 -0
  91. package/dist/{error-C7mY-p0f.mjs → error/codes.mjs} +7 -30
  92. package/dist/error/codes.mjs.map +1 -0
  93. package/dist/error/index.d.mts +6 -187
  94. package/dist/error/index.mjs +29 -4
  95. package/dist/error/index.mjs.map +1 -0
  96. package/dist/index.d.mts +8 -2
  97. package/dist/oauth2/client-credentials-token.d.mts +58 -0
  98. package/dist/oauth2/client-credentials-token.mjs +67 -0
  99. package/dist/oauth2/client-credentials-token.mjs.map +1 -0
  100. package/dist/oauth2/create-authorization-url.d.mts +48 -0
  101. package/dist/oauth2/create-authorization-url.mjs +44 -0
  102. package/dist/oauth2/create-authorization-url.mjs.map +1 -0
  103. package/dist/oauth2/index.d.mts +8 -2
  104. package/dist/oauth2/index.mjs +7 -3
  105. package/dist/oauth2/oauth-provider.d.mts +195 -0
  106. package/dist/oauth2/refresh-access-token.d.mts +56 -0
  107. package/dist/oauth2/refresh-access-token.mjs +76 -0
  108. package/dist/oauth2/refresh-access-token.mjs.map +1 -0
  109. package/dist/oauth2/utils.d.mts +8 -0
  110. package/dist/oauth2/utils.mjs +28 -0
  111. package/dist/oauth2/utils.mjs.map +1 -0
  112. package/dist/oauth2/validate-authorization-code.d.mts +88 -0
  113. package/dist/oauth2/validate-authorization-code.mjs +83 -0
  114. package/dist/oauth2/validate-authorization-code.mjs.map +1 -0
  115. package/dist/oauth2/verify.d.mts +43 -0
  116. package/dist/oauth2/verify.mjs +96 -0
  117. package/dist/oauth2/verify.mjs.map +1 -0
  118. package/dist/social-providers/apple.d.mts +120 -0
  119. package/dist/social-providers/apple.mjs +105 -0
  120. package/dist/social-providers/apple.mjs.map +1 -0
  121. package/dist/social-providers/atlassian.d.mts +73 -0
  122. package/dist/social-providers/atlassian.mjs +84 -0
  123. package/dist/social-providers/atlassian.mjs.map +1 -0
  124. package/dist/social-providers/cognito.d.mts +88 -0
  125. package/dist/social-providers/cognito.mjs +166 -0
  126. package/dist/social-providers/cognito.mjs.map +1 -0
  127. package/dist/social-providers/discord.d.mts +127 -0
  128. package/dist/social-providers/discord.mjs +65 -0
  129. package/dist/social-providers/discord.mjs.map +1 -0
  130. package/dist/social-providers/dropbox.d.mts +72 -0
  131. package/dist/social-providers/dropbox.mjs +76 -0
  132. package/dist/social-providers/dropbox.mjs.map +1 -0
  133. package/dist/social-providers/facebook.d.mts +82 -0
  134. package/dist/social-providers/facebook.mjs +121 -0
  135. package/dist/social-providers/facebook.mjs.map +1 -0
  136. package/dist/social-providers/figma.d.mts +64 -0
  137. package/dist/social-providers/figma.mjs +87 -0
  138. package/dist/social-providers/figma.mjs.map +1 -0
  139. package/dist/social-providers/github.d.mts +105 -0
  140. package/dist/social-providers/github.mjs +97 -0
  141. package/dist/social-providers/github.mjs.map +1 -0
  142. package/dist/social-providers/gitlab.d.mts +126 -0
  143. package/dist/social-providers/gitlab.mjs +83 -0
  144. package/dist/social-providers/gitlab.mjs.map +1 -0
  145. package/dist/social-providers/google.d.mts +100 -0
  146. package/dist/social-providers/google.mjs +113 -0
  147. package/dist/social-providers/google.mjs.map +1 -0
  148. package/dist/social-providers/huggingface.d.mts +86 -0
  149. package/dist/social-providers/huggingface.mjs +76 -0
  150. package/dist/social-providers/huggingface.mjs.map +1 -0
  151. package/dist/social-providers/index.d.mts +1777 -2
  152. package/dist/social-providers/index.mjs +37 -2570
  153. package/dist/social-providers/index.mjs.map +1 -0
  154. package/dist/social-providers/kakao.d.mts +164 -0
  155. package/dist/social-providers/kakao.mjs +73 -0
  156. package/dist/social-providers/kakao.mjs.map +1 -0
  157. package/dist/social-providers/kick.d.mts +76 -0
  158. package/dist/social-providers/kick.mjs +72 -0
  159. package/dist/social-providers/kick.mjs.map +1 -0
  160. package/dist/social-providers/line.d.mts +108 -0
  161. package/dist/social-providers/line.mjs +114 -0
  162. package/dist/social-providers/line.mjs.map +1 -0
  163. package/dist/social-providers/linear.d.mts +71 -0
  164. package/dist/social-providers/linear.mjs +89 -0
  165. package/dist/social-providers/linear.mjs.map +1 -0
  166. package/dist/social-providers/linkedin.d.mts +70 -0
  167. package/dist/social-providers/linkedin.mjs +77 -0
  168. package/dist/social-providers/linkedin.mjs.map +1 -0
  169. package/dist/social-providers/microsoft-entra-id.d.mts +177 -0
  170. package/dist/social-providers/microsoft-entra-id.mjs +140 -0
  171. package/dist/social-providers/microsoft-entra-id.mjs.map +1 -0
  172. package/dist/social-providers/naver.d.mts +95 -0
  173. package/dist/social-providers/naver.mjs +68 -0
  174. package/dist/social-providers/naver.mjs.map +1 -0
  175. package/dist/social-providers/notion.d.mts +67 -0
  176. package/dist/social-providers/notion.mjs +76 -0
  177. package/dist/social-providers/notion.mjs.map +1 -0
  178. package/dist/social-providers/paybin.d.mts +74 -0
  179. package/dist/social-providers/paybin.mjs +86 -0
  180. package/dist/social-providers/paybin.mjs.map +1 -0
  181. package/dist/social-providers/paypal.d.mts +132 -0
  182. package/dist/social-providers/paypal.mjs +145 -0
  183. package/dist/social-providers/paypal.mjs.map +1 -0
  184. package/dist/social-providers/polar.d.mts +77 -0
  185. package/dist/social-providers/polar.mjs +74 -0
  186. package/dist/social-providers/polar.mjs.map +1 -0
  187. package/dist/social-providers/railway.d.mts +68 -0
  188. package/dist/social-providers/railway.mjs +78 -0
  189. package/dist/social-providers/railway.mjs.map +1 -0
  190. package/dist/social-providers/reddit.d.mts +65 -0
  191. package/dist/social-providers/reddit.mjs +84 -0
  192. package/dist/social-providers/reddit.mjs.map +1 -0
  193. package/dist/social-providers/roblox.d.mts +73 -0
  194. package/dist/social-providers/roblox.mjs +60 -0
  195. package/dist/social-providers/roblox.mjs.map +1 -0
  196. package/dist/social-providers/salesforce.d.mts +82 -0
  197. package/dist/social-providers/salesforce.mjs +92 -0
  198. package/dist/social-providers/salesforce.mjs.map +1 -0
  199. package/dist/social-providers/slack.d.mts +86 -0
  200. package/dist/social-providers/slack.mjs +69 -0
  201. package/dist/social-providers/slack.mjs.map +1 -0
  202. package/dist/social-providers/spotify.d.mts +66 -0
  203. package/dist/social-providers/spotify.mjs +72 -0
  204. package/dist/social-providers/spotify.mjs.map +1 -0
  205. package/dist/social-providers/tiktok.d.mts +171 -0
  206. package/dist/social-providers/tiktok.mjs +63 -0
  207. package/dist/social-providers/tiktok.mjs.map +1 -0
  208. package/dist/social-providers/twitch.d.mts +82 -0
  209. package/dist/social-providers/twitch.mjs +79 -0
  210. package/dist/social-providers/twitch.mjs.map +1 -0
  211. package/dist/social-providers/twitter.d.mts +129 -0
  212. package/dist/social-providers/twitter.mjs +88 -0
  213. package/dist/social-providers/twitter.mjs.map +1 -0
  214. package/dist/social-providers/vercel.d.mts +65 -0
  215. package/dist/social-providers/vercel.mjs +62 -0
  216. package/dist/social-providers/vercel.mjs.map +1 -0
  217. package/dist/social-providers/vk.d.mts +73 -0
  218. package/dist/social-providers/vk.mjs +84 -0
  219. package/dist/social-providers/vk.mjs.map +1 -0
  220. package/dist/social-providers/zoom.d.mts +166 -0
  221. package/dist/social-providers/zoom.mjs +73 -0
  222. package/dist/social-providers/zoom.mjs.map +1 -0
  223. package/dist/types/context.d.mts +274 -0
  224. package/dist/types/cookie.d.mts +16 -0
  225. package/dist/types/helper.d.mts +11 -0
  226. package/dist/types/index.d.mts +8 -0
  227. package/dist/types/init-options.d.mts +1298 -0
  228. package/dist/types/plugin-client.d.mts +113 -0
  229. package/dist/types/plugin.d.mts +125 -0
  230. package/dist/utils/db.d.mts +12 -0
  231. package/dist/utils/db.mjs +17 -0
  232. package/dist/utils/db.mjs.map +1 -0
  233. package/dist/utils/deprecate.d.mts +10 -0
  234. package/dist/utils/deprecate.mjs +18 -0
  235. package/dist/utils/deprecate.mjs.map +1 -0
  236. package/dist/utils/{index.d.mts → error-codes.d.mts} +9 -16
  237. package/dist/utils/error-codes.mjs +12 -0
  238. package/dist/utils/error-codes.mjs.map +1 -0
  239. package/dist/utils/id.d.mts +5 -0
  240. package/dist/utils/id.mjs +10 -0
  241. package/dist/utils/id.mjs.map +1 -0
  242. package/dist/utils/ip.d.mts +55 -0
  243. package/dist/utils/ip.mjs +119 -0
  244. package/dist/utils/ip.mjs.map +1 -0
  245. package/dist/utils/json.d.mts +5 -0
  246. package/dist/utils/json.mjs +26 -0
  247. package/dist/utils/json.mjs.map +1 -0
  248. package/dist/utils/string.d.mts +5 -0
  249. package/dist/utils/string.mjs +8 -0
  250. package/dist/utils/string.mjs.map +1 -0
  251. package/dist/utils/url.d.mts +21 -0
  252. package/dist/utils/url.mjs +33 -0
  253. package/dist/utils/url.mjs.map +1 -0
  254. package/package.json +36 -18
  255. package/src/context/endpoint-context.ts +7 -6
  256. package/src/context/global.ts +57 -0
  257. package/src/context/index.ts +2 -0
  258. package/src/context/request-state.ts +7 -6
  259. package/src/context/transaction.ts +77 -14
  260. package/src/db/adapter/factory.ts +54 -86
  261. package/src/db/adapter/get-default-model-name.ts +1 -1
  262. package/src/db/adapter/get-id-field.ts +4 -6
  263. package/src/db/adapter/index.ts +20 -15
  264. package/src/db/adapter/types.ts +2 -41
  265. package/src/db/get-tables.ts +54 -37
  266. package/src/db/index.ts +30 -5
  267. package/src/db/schema/account.ts +16 -3
  268. package/src/db/schema/rate-limit.ts +16 -1
  269. package/src/db/schema/session.ts +15 -3
  270. package/src/db/schema/user.ts +15 -3
  271. package/src/db/schema/verification.ts +16 -3
  272. package/src/db/test/get-tables.test.ts +33 -0
  273. package/src/db/type.ts +154 -1
  274. package/src/env/env-impl.ts +2 -2
  275. package/src/env/logger.ts +1 -1
  276. package/src/error/codes.ts +18 -1
  277. package/src/error/index.ts +2 -3
  278. package/src/oauth2/client-credentials-token.ts +26 -2
  279. package/src/oauth2/create-authorization-url.ts +4 -2
  280. package/src/oauth2/index.ts +3 -0
  281. package/src/oauth2/oauth-provider.ts +7 -1
  282. package/src/oauth2/refresh-access-token.test.ts +90 -0
  283. package/src/oauth2/refresh-access-token.ts +37 -4
  284. package/src/oauth2/validate-authorization-code.ts +55 -29
  285. package/src/oauth2/validate-token.test.ts +229 -0
  286. package/src/social-providers/apple.ts +38 -29
  287. package/src/social-providers/cognito.ts +6 -5
  288. package/src/social-providers/dropbox.ts +1 -1
  289. package/src/social-providers/facebook.ts +3 -3
  290. package/src/social-providers/figma.ts +5 -3
  291. package/src/social-providers/github.ts +26 -4
  292. package/src/social-providers/gitlab.ts +2 -2
  293. package/src/social-providers/google.ts +18 -14
  294. package/src/social-providers/huggingface.ts +1 -1
  295. package/src/social-providers/index.ts +9 -5
  296. package/src/social-providers/kakao.ts +1 -1
  297. package/src/social-providers/line.ts +1 -1
  298. package/src/social-providers/microsoft-entra-id.ts +84 -1
  299. package/src/social-providers/naver.ts +1 -1
  300. package/src/social-providers/notion.ts +1 -1
  301. package/src/social-providers/paybin.ts +1 -5
  302. package/src/social-providers/polar.ts +1 -1
  303. package/src/social-providers/railway.ts +100 -0
  304. package/src/social-providers/tiktok.ts +2 -1
  305. package/src/social-providers/vercel.ts +1 -1
  306. package/src/social-providers/zoom.ts +0 -8
  307. package/src/types/context.ts +241 -132
  308. package/src/types/cookie.ts +6 -4
  309. package/src/types/helper.ts +9 -0
  310. package/src/types/index.ts +19 -2
  311. package/src/types/init-options.ts +301 -186
  312. package/src/types/plugin-client.ts +1 -0
  313. package/src/types/plugin.ts +11 -6
  314. package/src/utils/db.ts +20 -0
  315. package/src/utils/deprecate.test.ts +71 -0
  316. package/src/utils/deprecate.ts +21 -0
  317. package/src/utils/error-codes.ts +12 -9
  318. package/src/utils/ip.test.ts +255 -0
  319. package/src/utils/ip.ts +211 -0
  320. package/src/utils/url.ts +43 -0
  321. package/.turbo/turbo-build.log +0 -43
  322. package/dist/context-DblZrIwO.mjs +0 -114
  323. package/dist/env-DbssmzoK.mjs +0 -245
  324. package/dist/index-BpRqx5_q.d.mts +0 -7833
  325. package/dist/oauth2-BjWM15hm.mjs +0 -326
  326. package/dist/utils/index.mjs +0 -4
  327. package/dist/utils-s65Fz0OM.mjs +0 -47
  328. package/src/utils/index.ts +0 -4
  329. package/tsconfig.json +0 -7
  330. package/tsdown.config.ts +0 -22
  331. package/vitest.config.ts +0 -3
@@ -38,20 +38,20 @@ let debugLogs: { instance: string; args: any[] }[] = [];
38
38
  let transactionId = -1;
39
39
 
40
40
  const createAsIsTransaction =
41
- (adapter: DBAdapter<BetterAuthOptions>) =>
42
- <R>(fn: (trx: DBTransactionAdapter<BetterAuthOptions>) => Promise<R>) =>
41
+ <Options extends BetterAuthOptions>(adapter: DBAdapter<Options>) =>
42
+ <R>(fn: (trx: DBTransactionAdapter<Options>) => Promise<R>) =>
43
43
  fn(adapter);
44
44
 
45
- export type AdapterFactory = (
46
- options: BetterAuthOptions,
47
- ) => DBAdapter<BetterAuthOptions>;
45
+ export type AdapterFactory<Options extends BetterAuthOptions> = (
46
+ options: Options,
47
+ ) => DBAdapter<Options>;
48
48
 
49
49
  export const createAdapterFactory =
50
- ({
50
+ <Options extends BetterAuthOptions>({
51
51
  adapter: customAdapter,
52
52
  config: cfg,
53
- }: AdapterFactoryOptions): AdapterFactory =>
54
- (options: BetterAuthOptions): DBAdapter<BetterAuthOptions> => {
53
+ }: AdapterFactoryOptions): AdapterFactory<Options> =>
54
+ (options: Options): DBAdapter<Options> => {
55
55
  const uniqueAdapterFactoryInstanceId = Math.random()
56
56
  .toString(36)
57
57
  .substring(2, 15);
@@ -71,9 +71,7 @@ export const createAdapterFactory =
71
71
  disableTransformJoin: cfg.disableTransformJoin ?? false,
72
72
  } satisfies AdapterFactoryConfig;
73
73
 
74
- const useNumberId =
75
- options.advanced?.database?.useNumberId === true ||
76
- options.advanced?.database?.generateId === "serial";
74
+ const useNumberId = options.advanced?.database?.generateId === "serial";
77
75
  if (useNumberId && config.supportsNumericIds === false) {
78
76
  throw new BetterAuthError(
79
77
  `[${config.adapterName}] Your database or database adapter does not support numeric ids. Please disable "useNumberId" in your config.`,
@@ -189,9 +187,7 @@ export const createAdapterFactory =
189
187
  const fields = schema[defaultModelName]!.fields;
190
188
 
191
189
  const newMappedKeys = config.mapKeysTransformInput ?? {};
192
- const useNumberId =
193
- options.advanced?.database?.useNumberId ||
194
- options.advanced?.database?.generateId === "serial";
190
+ const useNumberId = options.advanced?.database?.generateId === "serial";
195
191
  fields.id = idField({
196
192
  customModelName: defaultModelName,
197
193
  forceAllowId: forceAllowId && "id" in data,
@@ -200,7 +196,7 @@ export const createAdapterFactory =
200
196
  let value = data[field];
201
197
  const fieldAttributes = fields[field];
202
198
 
203
- let newFieldName: string =
199
+ const newFieldName: string =
204
200
  newMappedKeys[field] || fields[field]!.fieldName || field;
205
201
  if (
206
202
  value === undefined &&
@@ -309,9 +305,7 @@ export const createAdapterFactory =
309
305
  const idKey = Object.entries(newMappedKeys).find(
310
306
  ([_, v]) => v === "id",
311
307
  )?.[0];
312
- const useNumberId =
313
- options.advanced?.database?.useNumberId ||
314
- options.advanced?.database?.generateId === "serial";
308
+ const useNumberId = options.advanced?.database?.generateId === "serial";
315
309
  tableSchema[idKey ?? "id"] = {
316
310
  type: useNumberId ? "number" : "string",
317
311
  };
@@ -335,7 +329,7 @@ export const createAdapterFactory =
335
329
  newValue = await field.transform.output(newValue);
336
330
  }
337
331
 
338
- let newFieldName: string = newMappedKeys[key] || key;
332
+ const newFieldName: string = newMappedKeys[key] || key;
339
333
 
340
334
  if (originalKey === "id" || field.references?.field === "id") {
341
335
  // Even if `useNumberId` is true, we must always return a string `id` output.
@@ -392,7 +386,7 @@ export const createAdapterFactory =
392
386
  unsafe_model = getDefaultModelName(unsafe_model);
393
387
  // for now we just transform the base model
394
388
  // later we append the joined models to this object.
395
- let transformedData: Record<string, any> = await transformSingleOutput(
389
+ const transformedData: Record<string, any> = await transformSingleOutput(
396
390
  data,
397
391
  unsafe_model,
398
392
  select,
@@ -443,7 +437,7 @@ export const createAdapterFactory =
443
437
  joinedData = [joinedData];
444
438
  }
445
439
 
446
- let transformed = [];
440
+ const transformed = [];
447
441
 
448
442
  if (Array.isArray(joinedData)) {
449
443
  for (const item of joinedData) {
@@ -523,9 +517,7 @@ export const createAdapterFactory =
523
517
  model: defaultModelName,
524
518
  });
525
519
 
526
- const useNumberId =
527
- options.advanced?.database?.useNumberId ||
528
- options.advanced?.database?.generateId === "serial";
520
+ const useNumberId = options.advanced?.database?.generateId === "serial";
529
521
 
530
522
  if (
531
523
  defaultFieldName === "id" ||
@@ -548,12 +540,32 @@ export const createAdapterFactory =
548
540
  newValue = value.toISOString();
549
541
  }
550
542
 
543
+ if (fieldAttr.type === "boolean" && typeof newValue === "string") {
544
+ newValue = newValue === "true";
545
+ }
546
+
547
+ if (fieldAttr.type === "number") {
548
+ if (typeof newValue === "string" && newValue.trim() !== "") {
549
+ const parsed = Number(newValue);
550
+ if (!Number.isNaN(parsed)) {
551
+ newValue = parsed;
552
+ }
553
+ } else if (Array.isArray(newValue)) {
554
+ const parsed = newValue.map((v) =>
555
+ typeof v === "string" && v.trim() !== "" ? Number(v) : NaN,
556
+ );
557
+ if (parsed.every((n) => !Number.isNaN(n))) {
558
+ newValue = parsed;
559
+ }
560
+ }
561
+ }
562
+
551
563
  if (
552
564
  fieldAttr.type === "boolean" &&
553
- typeof value === "boolean" &&
565
+ typeof newValue === "boolean" &&
554
566
  !config.supportsBooleans
555
567
  ) {
556
- newValue = value ? 1 : 0;
568
+ newValue = newValue ? 1 : 0;
557
569
  }
558
570
 
559
571
  if (
@@ -793,10 +805,8 @@ export const createAdapterFactory =
793
805
  transformWhereClause,
794
806
  });
795
807
 
796
- let lazyLoadTransaction:
797
- | DBAdapter<BetterAuthOptions>["transaction"]
798
- | null = null;
799
- const adapter: DBAdapter<BetterAuthOptions> = {
808
+ let lazyLoadTransaction: DBAdapter<Options>["transaction"] | null = null;
809
+ const adapter: DBAdapter<Options> = {
800
810
  transaction: async (cb) => {
801
811
  if (!lazyLoadTransaction) {
802
812
  if (!config.transaction) {
@@ -822,7 +832,7 @@ export const createAdapterFactory =
822
832
  forceAllowId?: boolean;
823
833
  }): Promise<R> => {
824
834
  transactionId++;
825
- let thisTransactionId = transactionId;
835
+ const thisTransactionId = transactionId;
826
836
  const model = getModelName(unsafeModel);
827
837
  unsafeModel = getDefaultModelName(unsafeModel);
828
838
  if (
@@ -903,7 +913,7 @@ export const createAdapterFactory =
903
913
  update: Record<string, any>;
904
914
  }): Promise<T | null> => {
905
915
  transactionId++;
906
- let thisTransactionId = transactionId;
916
+ const thisTransactionId = transactionId;
907
917
  unsafeModel = getDefaultModelName(unsafeModel);
908
918
  const model = getModelName(unsafeModel);
909
919
  const where = transformWhereClause({
@@ -965,7 +975,7 @@ export const createAdapterFactory =
965
975
  update: Record<string, any>;
966
976
  }) => {
967
977
  transactionId++;
968
- let thisTransactionId = transactionId;
978
+ const thisTransactionId = transactionId;
969
979
  const model = getModelName(unsafeModel);
970
980
  const where = transformWhereClause({
971
981
  model: unsafeModel,
@@ -1021,7 +1031,7 @@ export const createAdapterFactory =
1021
1031
  join?: JoinOption;
1022
1032
  }) => {
1023
1033
  transactionId++;
1024
- let thisTransactionId = transactionId;
1034
+ const thisTransactionId = transactionId;
1025
1035
  const model = getModelName(unsafeModel);
1026
1036
  const where = transformWhereClause({
1027
1037
  model: unsafeModel,
@@ -1083,6 +1093,7 @@ export const createAdapterFactory =
1083
1093
  model: unsafeModel,
1084
1094
  where: unsafeWhere,
1085
1095
  limit: unsafeLimit,
1096
+ select,
1086
1097
  sortBy,
1087
1098
  offset,
1088
1099
  join: unsafeJoin,
@@ -1090,12 +1101,13 @@ export const createAdapterFactory =
1090
1101
  model: string;
1091
1102
  where?: Where[];
1092
1103
  limit?: number;
1104
+ select?: string[] | undefined;
1093
1105
  sortBy?: { field: string; direction: "asc" | "desc" };
1094
1106
  offset?: number;
1095
1107
  join?: JoinOption;
1096
1108
  }) => {
1097
1109
  transactionId++;
1098
- let thisTransactionId = transactionId;
1110
+ const thisTransactionId = transactionId;
1099
1111
  const limit =
1100
1112
  unsafeLimit ??
1101
1113
  options.advanced?.database?.defaultFindManyLimit ??
@@ -1110,13 +1122,10 @@ export const createAdapterFactory =
1110
1122
  let join: JoinConfig | undefined;
1111
1123
  let passJoinToAdapter = true;
1112
1124
  if (!config.disableTransformJoin) {
1113
- const result = transformJoinClause(
1114
- unsafeModel,
1115
- unsafeJoin,
1116
- undefined,
1117
- );
1125
+ const result = transformJoinClause(unsafeModel, unsafeJoin, select);
1118
1126
  if (result) {
1119
1127
  join = result.join;
1128
+ select = result.select;
1120
1129
  }
1121
1130
  // If adapter doesn't support joins and we have joins, don't pass them to the adapter
1122
1131
  const experimentalJoins = options.experimental?.joins;
@@ -1137,6 +1146,7 @@ export const createAdapterFactory =
1137
1146
  model,
1138
1147
  where,
1139
1148
  limit: limit,
1149
+ select,
1140
1150
  sortBy,
1141
1151
  offset,
1142
1152
  join: passJoinToAdapter ? join : undefined,
@@ -1173,7 +1183,7 @@ export const createAdapterFactory =
1173
1183
  where: Where[];
1174
1184
  }) => {
1175
1185
  transactionId++;
1176
- let thisTransactionId = transactionId;
1186
+ const thisTransactionId = transactionId;
1177
1187
  const model = getModelName(unsafeModel);
1178
1188
  const where = transformWhereClause({
1179
1189
  model: unsafeModel,
@@ -1206,7 +1216,7 @@ export const createAdapterFactory =
1206
1216
  where: Where[];
1207
1217
  }) => {
1208
1218
  transactionId++;
1209
- let thisTransactionId = transactionId;
1219
+ const thisTransactionId = transactionId;
1210
1220
  const model = getModelName(unsafeModel);
1211
1221
  const where = transformWhereClause({
1212
1222
  model: unsafeModel,
@@ -1240,7 +1250,7 @@ export const createAdapterFactory =
1240
1250
  where?: Where[];
1241
1251
  }) => {
1242
1252
  transactionId++;
1243
- let thisTransactionId = transactionId;
1253
+ const thisTransactionId = transactionId;
1244
1254
  const model = getModelName(unsafeModel);
1245
1255
  const where = transformWhereClause({
1246
1256
  model: unsafeModel,
@@ -1284,42 +1294,6 @@ export const createAdapterFactory =
1284
1294
  delete tables.session;
1285
1295
  }
1286
1296
 
1287
- if (
1288
- options.rateLimit &&
1289
- options.rateLimit.storage === "database" &&
1290
- // rate-limit will default to enabled in production,
1291
- // and given storage is database, it will try to use the rate-limit table,
1292
- // so we should make sure to generate rate-limit table schema
1293
- (typeof options.rateLimit.enabled === "undefined" ||
1294
- // and of course if they forcefully set to true, then they want rate-limit,
1295
- // thus we should also generate rate-limit table schema
1296
- options.rateLimit.enabled === true)
1297
- ) {
1298
- tables.ratelimit = {
1299
- modelName: options.rateLimit.modelName ?? "ratelimit",
1300
- fields: {
1301
- key: {
1302
- type: "string",
1303
- unique: true,
1304
- required: true,
1305
- fieldName: options.rateLimit.fields?.key ?? "key",
1306
- },
1307
- count: {
1308
- type: "number",
1309
- required: true,
1310
- fieldName: options.rateLimit.fields?.count ?? "count",
1311
- },
1312
- lastRequest: {
1313
- type: "number",
1314
- required: true,
1315
- bigint: true,
1316
- defaultValue: () => Date.now(),
1317
- fieldName:
1318
- options.rateLimit.fields?.lastRequest ?? "lastRequest",
1319
- },
1320
- },
1321
- };
1322
- }
1323
1297
  return adapterInstance.createSchema!({ file, tables });
1324
1298
  }
1325
1299
  : undefined,
@@ -1350,7 +1324,7 @@ export const createAdapterFactory =
1350
1324
  }
1351
1325
 
1352
1326
  //`${colors.fg.blue}|${colors.reset} `,
1353
- let log: any[] = logs
1327
+ const log: any[] = logs
1354
1328
  .reverse()
1355
1329
  .map((log) => {
1356
1330
  log.args[0] = `\n${log.args[0]}`;
@@ -1390,9 +1364,3 @@ function formatMethod(method: string) {
1390
1364
  function formatAction(action: string) {
1391
1365
  return `${TTY_COLORS.dim}(${action})${TTY_COLORS.reset}`;
1392
1366
  }
1393
-
1394
- /**
1395
- * @deprecated Use `createAdapterFactory` instead. This export will be removed in a future version.
1396
- * @alias
1397
- */
1398
- export const createAdapter = createAdapterFactory;
@@ -23,7 +23,7 @@ export const initGetDefaultModelName = ({
23
23
  // It's possible this `model` could had applied `usePlural`.
24
24
  // Thus we'll try the search but without the trailing `s`.
25
25
  if (usePlural && model.charAt(model.length - 1) === "s") {
26
- let pluralessModel = model.slice(0, -1);
26
+ const pluralessModel = model.slice(0, -1);
27
27
  let m = schema[pluralessModel] ? pluralessModel : undefined;
28
28
  if (!m) {
29
29
  m = Object.entries(schema).find(
@@ -1,6 +1,6 @@
1
1
  import { logger } from "../../env";
2
2
  import type { BetterAuthOptions } from "../../types";
3
- import { generateId as defaultGenerateId } from "../../utils";
3
+ import { generateId as defaultGenerateId } from "../../utils/id";
4
4
  import type { BetterAuthDBSchema, DBFieldAttribute } from "../type";
5
5
  import { initGetDefaultModelName } from "./get-default-model-name";
6
6
 
@@ -31,12 +31,10 @@ export const initGetIdField = ({
31
31
  customModelName?: string;
32
32
  forceAllowId?: boolean;
33
33
  }) => {
34
- const useNumberId =
35
- options.advanced?.database?.useNumberId ||
36
- options.advanced?.database?.generateId === "serial";
34
+ const useNumberId = options.advanced?.database?.generateId === "serial";
37
35
  const useUUIDs = options.advanced?.database?.generateId === "uuid";
38
36
 
39
- let shouldGenerateId: boolean = (() => {
37
+ const shouldGenerateId: boolean = (() => {
40
38
  if (disableIdGeneration) {
41
39
  return false;
42
40
  } else if (useNumberId && !forceAllowId) {
@@ -58,7 +56,7 @@ export const initGetIdField = ({
58
56
  ? {
59
57
  defaultValue() {
60
58
  if (disableIdGeneration) return undefined;
61
- let generateId = options.advanced?.database?.generateId;
59
+ const generateId = options.advanced?.database?.generateId;
62
60
  if (generateId === false || useNumberId) return undefined;
63
61
  if (typeof generateId === "function") {
64
62
  return generateId({
@@ -301,25 +301,27 @@ export interface DBAdapterFactoryConfig<
301
301
  disableTransformJoin?: boolean | undefined;
302
302
  }
303
303
 
304
+ export const whereOperators = [
305
+ "eq",
306
+ "ne",
307
+ "lt",
308
+ "lte",
309
+ "gt",
310
+ "gte",
311
+ "in",
312
+ "not_in",
313
+ "contains",
314
+ "starts_with",
315
+ "ends_with",
316
+ ] as const;
317
+
318
+ export type WhereOperator = (typeof whereOperators)[number];
319
+
304
320
  export type Where = {
305
321
  /**
306
322
  * @default eq
307
323
  */
308
- operator?:
309
- | (
310
- | "eq"
311
- | "ne"
312
- | "lt"
313
- | "lte"
314
- | "gt"
315
- | "gte"
316
- | "in"
317
- | "not_in"
318
- | "contains"
319
- | "starts_with"
320
- | "ends_with"
321
- )
322
- | undefined;
324
+ operator?: WhereOperator | undefined;
323
325
  value: string | number | boolean | string[] | number[] | Date | null;
324
326
  field: string;
325
327
  /**
@@ -405,6 +407,7 @@ export type DBAdapter<Options extends BetterAuthOptions = BetterAuthOptions> = {
405
407
  model: string;
406
408
  where?: Where[] | undefined;
407
409
  limit?: number | undefined;
410
+ select?: string[] | undefined;
408
411
  sortBy?:
409
412
  | {
410
413
  field: string;
@@ -493,6 +496,7 @@ export interface CustomAdapter {
493
496
  model,
494
497
  where,
495
498
  limit,
499
+ select,
496
500
  sortBy,
497
501
  offset,
498
502
  join,
@@ -500,6 +504,7 @@ export interface CustomAdapter {
500
504
  model: string;
501
505
  where?: CleanedWhere[] | undefined;
502
506
  limit: number;
507
+ select?: string[] | undefined;
503
508
  sortBy?: { field: string; direction: "asc" | "desc" } | undefined;
504
509
  offset?: number | undefined;
505
510
  join?: JoinConfig | undefined;
@@ -1,9 +1,8 @@
1
1
  import type { BetterAuthOptions } from "../../types";
2
- import type { Prettify } from "../../types/helper";
3
2
  import type { BetterAuthDBSchema, DBFieldAttribute } from "../type";
4
3
  import type {
5
- DBAdapterSchemaCreation as AdapterSchemaCreation,
6
- CustomAdapter as CoreCustomAdapter,
4
+ CleanedWhere,
5
+ CustomAdapter,
7
6
  DBAdapterFactoryConfig,
8
7
  JoinConfig,
9
8
  DBTransactionAdapter as TransactionAdapter,
@@ -127,45 +126,7 @@ export type AdapterFactoryCustomizeAdapterCreator = (config: {
127
126
  }) => W extends undefined ? undefined : CleanedWhere[];
128
127
  }) => CustomAdapter;
129
128
 
130
- /**
131
- * @deprecated Use `CustomAdapter` from `@better-auth/core/db/adapter` instead.
132
- */
133
- export interface CustomAdapter extends Omit<CoreCustomAdapter, "createSchema"> {
134
- createSchema?:
135
- | ((props: {
136
- /**
137
- * The file the user may have passed in to the `generate` command as the expected schema file output path.
138
- */
139
- file?: string;
140
- /**
141
- * The tables from the user's Better-Auth instance schema.
142
- */
143
- tables: BetterAuthDBSchema;
144
- }) => Promise<AdapterSchemaCreation>)
145
- | undefined;
146
- }
147
-
148
- /**
149
- * @deprecated Use `CleanedWhere` from `@better-auth/core/db/adapter` instead.
150
- */
151
- export type CleanedWhere = Prettify<Required<Where>>;
152
-
153
129
  export type AdapterTestDebugLogs = {
154
130
  resetDebugLogs: () => void;
155
131
  printDebugLogs: () => void;
156
132
  };
157
-
158
- /**
159
- * @deprecated Use `AdapterFactoryOptions` instead. This export will be removed in a future version.
160
- */
161
- export type CreateAdapterOptions = AdapterFactoryOptions;
162
-
163
- /**
164
- * @deprecated Use `AdapterFactoryConfig` instead. This export will be removed in a future version.
165
- */
166
- export type AdapterConfig = AdapterFactoryConfig;
167
-
168
- /**
169
- * @deprecated Use `AdapterFactoryCustomizeAdapterCreator` instead. This export will be removed in a future version.
170
- */
171
- export type CreateCustomAdapter = AdapterFactoryCustomizeAdapterCreator;
@@ -32,16 +32,21 @@ export const getAuthTables = (
32
32
  fields: {
33
33
  key: {
34
34
  type: "string",
35
+ unique: true,
36
+ required: true,
35
37
  fieldName: options.rateLimit?.fields?.key || "key",
36
38
  },
37
39
  count: {
38
40
  type: "number",
41
+ required: true,
39
42
  fieldName: options.rateLimit?.fields?.count || "count",
40
43
  },
41
44
  lastRequest: {
42
45
  type: "number",
43
46
  bigint: true,
47
+ required: true,
44
48
  fieldName: options.rateLimit?.fields?.lastRequest || "lastRequest",
49
+ defaultValue: () => Date.now(),
45
50
  },
46
51
  },
47
52
  },
@@ -50,6 +55,46 @@ export const getAuthTables = (
50
55
  const { user, session, account, verification, ...pluginTables } =
51
56
  pluginSchema;
52
57
 
58
+ const verificationTable = {
59
+ verification: {
60
+ modelName: options.verification?.modelName || "verification",
61
+ fields: {
62
+ identifier: {
63
+ type: "string",
64
+ required: true,
65
+ fieldName: options.verification?.fields?.identifier || "identifier",
66
+ index: true,
67
+ },
68
+ value: {
69
+ type: "string",
70
+ required: true,
71
+ fieldName: options.verification?.fields?.value || "value",
72
+ },
73
+ expiresAt: {
74
+ type: "date",
75
+ required: true,
76
+ fieldName: options.verification?.fields?.expiresAt || "expiresAt",
77
+ },
78
+ createdAt: {
79
+ type: "date",
80
+ required: true,
81
+ defaultValue: () => new Date(),
82
+ fieldName: options.verification?.fields?.createdAt || "createdAt",
83
+ },
84
+ updatedAt: {
85
+ type: "date",
86
+ required: true,
87
+ defaultValue: () => new Date(),
88
+ onUpdate: () => new Date(),
89
+ fieldName: options.verification?.fields?.updatedAt || "updatedAt",
90
+ },
91
+ ...verification?.fields,
92
+ ...options.verification?.additionalFields,
93
+ },
94
+ order: 4,
95
+ },
96
+ } satisfies BetterAuthDBSchema;
97
+
53
98
  const sessionTable = {
54
99
  session: {
55
100
  modelName: options.session?.modelName || "session",
@@ -183,21 +228,25 @@ export const getAuthTables = (
183
228
  accessToken: {
184
229
  type: "string",
185
230
  required: false,
231
+ returned: false,
186
232
  fieldName: options.account?.fields?.accessToken || "accessToken",
187
233
  },
188
234
  refreshToken: {
189
235
  type: "string",
190
236
  required: false,
237
+ returned: false,
191
238
  fieldName: options.account?.fields?.refreshToken || "refreshToken",
192
239
  },
193
240
  idToken: {
194
241
  type: "string",
195
242
  required: false,
243
+ returned: false,
196
244
  fieldName: options.account?.fields?.idToken || "idToken",
197
245
  },
198
246
  accessTokenExpiresAt: {
199
247
  type: "date",
200
248
  required: false,
249
+ returned: false,
201
250
  fieldName:
202
251
  options.account?.fields?.accessTokenExpiresAt ||
203
252
  "accessTokenExpiresAt",
@@ -205,6 +254,7 @@ export const getAuthTables = (
205
254
  refreshTokenExpiresAt: {
206
255
  type: "date",
207
256
  required: false,
257
+ returned: false,
208
258
  fieldName:
209
259
  options.account?.fields?.refreshTokenExpiresAt ||
210
260
  "refreshTokenExpiresAt",
@@ -217,6 +267,7 @@ export const getAuthTables = (
217
267
  password: {
218
268
  type: "string",
219
269
  required: false,
270
+ returned: false,
220
271
  fieldName: options.account?.fields?.password || "password",
221
272
  },
222
273
  createdAt: {
@@ -236,43 +287,9 @@ export const getAuthTables = (
236
287
  },
237
288
  order: 3,
238
289
  },
239
- verification: {
240
- modelName: options.verification?.modelName || "verification",
241
- fields: {
242
- identifier: {
243
- type: "string",
244
- required: true,
245
- fieldName: options.verification?.fields?.identifier || "identifier",
246
- index: true,
247
- },
248
- value: {
249
- type: "string",
250
- required: true,
251
- fieldName: options.verification?.fields?.value || "value",
252
- },
253
- expiresAt: {
254
- type: "date",
255
- required: true,
256
- fieldName: options.verification?.fields?.expiresAt || "expiresAt",
257
- },
258
- createdAt: {
259
- type: "date",
260
- required: true,
261
- defaultValue: () => new Date(),
262
- fieldName: options.verification?.fields?.createdAt || "createdAt",
263
- },
264
- updatedAt: {
265
- type: "date",
266
- required: true,
267
- defaultValue: () => new Date(),
268
- onUpdate: () => new Date(),
269
- fieldName: options.verification?.fields?.updatedAt || "updatedAt",
270
- },
271
- ...verification?.fields,
272
- ...options.verification?.additionalFields,
273
- },
274
- order: 4,
275
- },
290
+ ...(!options.secondaryStorage || options.verification?.storeInDatabase
291
+ ? verificationTable
292
+ : {}),
276
293
  ...pluginTables,
277
294
  ...(shouldAddRateLimitTable ? rateLimitTable : {}),
278
295
  } satisfies BetterAuthDBSchema;
package/src/db/index.ts CHANGED
@@ -1,11 +1,27 @@
1
1
  export { getAuthTables } from "./get-tables";
2
2
  export type { BetterAuthPluginDBSchema } from "./plugin";
3
- export { type Account, accountSchema } from "./schema/account";
4
- export { type RateLimit, rateLimitSchema } from "./schema/rate-limit";
5
- export { type Session, sessionSchema } from "./schema/session";
3
+ export {
4
+ type Account,
5
+ accountSchema,
6
+ type BaseAccount,
7
+ } from "./schema/account";
8
+ export {
9
+ type BaseRateLimit,
10
+ type RateLimit,
11
+ rateLimitSchema,
12
+ } from "./schema/rate-limit";
13
+ export {
14
+ type BaseSession,
15
+ type Session,
16
+ sessionSchema,
17
+ } from "./schema/session";
6
18
  export { coreSchema } from "./schema/shared";
7
- export { type User, userSchema } from "./schema/user";
8
- export { type Verification, verificationSchema } from "./schema/verification";
19
+ export { type BaseUser, type User, userSchema } from "./schema/user";
20
+ export {
21
+ type BaseVerification,
22
+ type Verification,
23
+ verificationSchema,
24
+ } from "./schema/verification";
9
25
  export type {
10
26
  BaseModelNames,
11
27
  BetterAuthDBSchema,
@@ -13,6 +29,15 @@ export type {
13
29
  DBFieldAttributeConfig,
14
30
  DBFieldType,
15
31
  DBPrimitive,
32
+ InferDBFieldInput,
33
+ InferDBFieldOutput,
34
+ InferDBFieldsFromOptions,
35
+ InferDBFieldsFromOptionsInput,
36
+ InferDBFieldsFromPlugins,
37
+ InferDBFieldsFromPluginsInput,
38
+ InferDBFieldsInput,
39
+ InferDBFieldsOutput,
40
+ InferDBValueType,
16
41
  ModelNames,
17
42
  SecondaryStorage,
18
43
  } from "./type";