@better-auth/core 1.5.0-beta.8 → 1.5.0

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 (313) hide show
  1. package/README.md +17 -0
  2. package/dist/api/index.d.mts +144 -41
  3. package/dist/api/index.mjs +2 -1
  4. package/dist/api/index.mjs.map +1 -0
  5. package/dist/async_hooks/index.d.mts +2 -1
  6. package/dist/async_hooks/index.mjs +2 -1
  7. package/dist/async_hooks/index.mjs.map +1 -0
  8. package/dist/async_hooks/pure.index.d.mts +2 -1
  9. package/dist/async_hooks/pure.index.mjs +2 -1
  10. package/dist/async_hooks/pure.index.mjs.map +1 -0
  11. package/dist/context/endpoint-context.d.mts +2 -1
  12. package/dist/context/endpoint-context.mjs +4 -3
  13. package/dist/context/endpoint-context.mjs.map +1 -0
  14. package/dist/context/global.d.mts +2 -2
  15. package/dist/context/global.mjs +3 -2
  16. package/dist/context/global.mjs.map +1 -0
  17. package/dist/context/index.d.mts +2 -2
  18. package/dist/context/index.mjs +2 -2
  19. package/dist/context/request-state.d.mts +2 -1
  20. package/dist/context/request-state.mjs +4 -3
  21. package/dist/context/request-state.mjs.map +1 -0
  22. package/dist/context/transaction.d.mts +12 -3
  23. package/dist/context/transaction.mjs +55 -11
  24. package/dist/context/transaction.mjs.map +1 -0
  25. package/dist/db/adapter/factory.d.mts +6 -13
  26. package/dist/db/adapter/factory.mjs +44 -57
  27. package/dist/db/adapter/factory.mjs.map +1 -0
  28. package/dist/db/adapter/get-default-field-name.d.mts +2 -1
  29. package/dist/db/adapter/get-default-field-name.mjs +3 -2
  30. package/dist/db/adapter/get-default-field-name.mjs.map +1 -0
  31. package/dist/db/adapter/get-default-model-name.d.mts +2 -1
  32. package/dist/db/adapter/get-default-model-name.mjs +5 -4
  33. package/dist/db/adapter/get-default-model-name.mjs.map +1 -0
  34. package/dist/db/adapter/get-field-attributes.d.mts +3 -2
  35. package/dist/db/adapter/get-field-attributes.mjs +2 -1
  36. package/dist/db/adapter/get-field-attributes.mjs.map +1 -0
  37. package/dist/db/adapter/get-field-name.d.mts +2 -1
  38. package/dist/db/adapter/get-field-name.mjs +2 -1
  39. package/dist/db/adapter/get-field-name.mjs.map +1 -0
  40. package/dist/db/adapter/get-id-field.d.mts +3 -2
  41. package/dist/db/adapter/get-id-field.mjs +3 -2
  42. package/dist/db/adapter/get-id-field.mjs.map +1 -0
  43. package/dist/db/adapter/get-model-name.d.mts +2 -1
  44. package/dist/db/adapter/get-model-name.mjs +2 -1
  45. package/dist/db/adapter/get-model-name.mjs.map +1 -0
  46. package/dist/db/adapter/index.d.mts +10 -4
  47. package/dist/db/adapter/index.mjs +19 -2
  48. package/dist/db/adapter/index.mjs.map +1 -0
  49. package/dist/db/adapter/types.d.mts +3 -34
  50. package/dist/db/adapter/utils.d.mts +2 -1
  51. package/dist/db/adapter/utils.mjs +2 -1
  52. package/dist/db/adapter/utils.mjs.map +1 -0
  53. package/dist/db/get-tables.d.mts +2 -1
  54. package/dist/db/get-tables.mjs +46 -39
  55. package/dist/db/get-tables.mjs.map +1 -0
  56. package/dist/db/index.d.mts +7 -7
  57. package/dist/db/plugin.d.mts +2 -1
  58. package/dist/db/schema/account.d.mts +8 -4
  59. package/dist/db/schema/account.mjs +2 -1
  60. package/dist/db/schema/account.mjs.map +1 -0
  61. package/dist/db/schema/rate-limit.d.mts +8 -2
  62. package/dist/db/schema/rate-limit.mjs +2 -1
  63. package/dist/db/schema/rate-limit.mjs.map +1 -0
  64. package/dist/db/schema/session.d.mts +8 -4
  65. package/dist/db/schema/session.mjs +2 -1
  66. package/dist/db/schema/session.mjs.map +1 -0
  67. package/dist/db/schema/shared.d.mts +2 -1
  68. package/dist/db/schema/shared.mjs +2 -1
  69. package/dist/db/schema/shared.mjs.map +1 -0
  70. package/dist/db/schema/user.d.mts +8 -4
  71. package/dist/db/schema/user.mjs +2 -1
  72. package/dist/db/schema/user.mjs.map +1 -0
  73. package/dist/db/schema/verification.d.mts +8 -4
  74. package/dist/db/schema/verification.mjs +2 -1
  75. package/dist/db/schema/verification.mjs.map +1 -0
  76. package/dist/db/type.d.mts +28 -2
  77. package/dist/env/color-depth.d.mts +2 -1
  78. package/dist/env/color-depth.mjs +2 -1
  79. package/dist/env/color-depth.mjs.map +1 -0
  80. package/dist/env/env-impl.d.mts +3 -2
  81. package/dist/env/env-impl.mjs +9 -8
  82. package/dist/env/env-impl.mjs.map +1 -0
  83. package/dist/env/logger.d.mts +2 -1
  84. package/dist/env/logger.mjs +3 -2
  85. package/dist/env/logger.mjs.map +1 -0
  86. package/dist/error/codes.d.mts +64 -181
  87. package/dist/error/codes.mjs +6 -2
  88. package/dist/error/codes.mjs.map +1 -0
  89. package/dist/error/index.d.mts +2 -1
  90. package/dist/error/index.mjs +2 -1
  91. package/dist/error/index.mjs.map +1 -0
  92. package/dist/index.d.mts +5 -4
  93. package/dist/oauth2/client-credentials-token.d.mts +25 -3
  94. package/dist/oauth2/client-credentials-token.mjs +15 -2
  95. package/dist/oauth2/client-credentials-token.mjs.map +1 -0
  96. package/dist/oauth2/create-authorization-url.d.mts +5 -2
  97. package/dist/oauth2/create-authorization-url.mjs +3 -1
  98. package/dist/oauth2/create-authorization-url.mjs.map +1 -0
  99. package/dist/oauth2/index.d.mts +4 -4
  100. package/dist/oauth2/index.mjs +4 -4
  101. package/dist/oauth2/oauth-provider.d.mts +3 -2
  102. package/dist/oauth2/refresh-access-token.d.mts +24 -4
  103. package/dist/oauth2/refresh-access-token.mjs +20 -2
  104. package/dist/oauth2/refresh-access-token.mjs.map +1 -0
  105. package/dist/oauth2/utils.d.mts +2 -1
  106. package/dist/oauth2/utils.mjs +2 -1
  107. package/dist/oauth2/utils.mjs.map +1 -0
  108. package/dist/oauth2/validate-authorization-code.d.mts +37 -4
  109. package/dist/oauth2/validate-authorization-code.mjs +25 -13
  110. package/dist/oauth2/validate-authorization-code.mjs.map +1 -0
  111. package/dist/oauth2/verify.d.mts +7 -13
  112. package/dist/oauth2/verify.mjs +2 -1
  113. package/dist/oauth2/verify.mjs.map +1 -0
  114. package/dist/social-providers/apple.d.mts +2 -1
  115. package/dist/social-providers/apple.mjs +22 -21
  116. package/dist/social-providers/apple.mjs.map +1 -0
  117. package/dist/social-providers/atlassian.d.mts +2 -1
  118. package/dist/social-providers/atlassian.mjs +2 -1
  119. package/dist/social-providers/atlassian.mjs.map +1 -0
  120. package/dist/social-providers/cognito.d.mts +2 -1
  121. package/dist/social-providers/cognito.mjs +4 -3
  122. package/dist/social-providers/cognito.mjs.map +1 -0
  123. package/dist/social-providers/discord.d.mts +2 -1
  124. package/dist/social-providers/discord.mjs +2 -1
  125. package/dist/social-providers/discord.mjs.map +1 -0
  126. package/dist/social-providers/dropbox.d.mts +2 -1
  127. package/dist/social-providers/dropbox.mjs +3 -2
  128. package/dist/social-providers/dropbox.mjs.map +1 -0
  129. package/dist/social-providers/facebook.d.mts +2 -1
  130. package/dist/social-providers/facebook.mjs +13 -12
  131. package/dist/social-providers/facebook.mjs.map +1 -0
  132. package/dist/social-providers/figma.d.mts +2 -1
  133. package/dist/social-providers/figma.mjs +2 -1
  134. package/dist/social-providers/figma.mjs.map +1 -0
  135. package/dist/social-providers/github.d.mts +3 -2
  136. package/dist/social-providers/github.mjs +23 -6
  137. package/dist/social-providers/github.mjs.map +1 -0
  138. package/dist/social-providers/gitlab.d.mts +2 -1
  139. package/dist/social-providers/gitlab.mjs +3 -2
  140. package/dist/social-providers/gitlab.mjs.map +1 -0
  141. package/dist/social-providers/google.d.mts +2 -1
  142. package/dist/social-providers/google.mjs +18 -13
  143. package/dist/social-providers/google.mjs.map +1 -0
  144. package/dist/social-providers/huggingface.d.mts +2 -1
  145. package/dist/social-providers/huggingface.mjs +3 -2
  146. package/dist/social-providers/huggingface.mjs.map +1 -0
  147. package/dist/social-providers/index.d.mts +61 -8
  148. package/dist/social-providers/index.mjs +5 -2
  149. package/dist/social-providers/index.mjs.map +1 -0
  150. package/dist/social-providers/kakao.d.mts +3 -2
  151. package/dist/social-providers/kakao.mjs +3 -2
  152. package/dist/social-providers/kakao.mjs.map +1 -0
  153. package/dist/social-providers/kick.d.mts +2 -1
  154. package/dist/social-providers/kick.mjs +2 -1
  155. package/dist/social-providers/kick.mjs.map +1 -0
  156. package/dist/social-providers/line.d.mts +2 -1
  157. package/dist/social-providers/line.mjs +3 -2
  158. package/dist/social-providers/line.mjs.map +1 -0
  159. package/dist/social-providers/linear.d.mts +2 -1
  160. package/dist/social-providers/linear.mjs +2 -1
  161. package/dist/social-providers/linear.mjs.map +1 -0
  162. package/dist/social-providers/linkedin.d.mts +2 -1
  163. package/dist/social-providers/linkedin.mjs +2 -1
  164. package/dist/social-providers/linkedin.mjs.map +1 -0
  165. package/dist/social-providers/microsoft-entra-id.d.mts +4 -1
  166. package/dist/social-providers/microsoft-entra-id.mjs +36 -2
  167. package/dist/social-providers/microsoft-entra-id.mjs.map +1 -0
  168. package/dist/social-providers/naver.d.mts +11 -20
  169. package/dist/social-providers/naver.mjs +3 -2
  170. package/dist/social-providers/naver.mjs.map +1 -0
  171. package/dist/social-providers/notion.d.mts +2 -1
  172. package/dist/social-providers/notion.mjs +3 -2
  173. package/dist/social-providers/notion.mjs.map +1 -0
  174. package/dist/social-providers/paybin.d.mts +2 -1
  175. package/dist/social-providers/paybin.mjs +3 -2
  176. package/dist/social-providers/paybin.mjs.map +1 -0
  177. package/dist/social-providers/paypal.d.mts +2 -1
  178. package/dist/social-providers/paypal.mjs +2 -1
  179. package/dist/social-providers/paypal.mjs.map +1 -0
  180. package/dist/social-providers/polar.d.mts +2 -1
  181. package/dist/social-providers/polar.mjs +3 -2
  182. package/dist/social-providers/polar.mjs.map +1 -0
  183. package/dist/social-providers/railway.d.mts +68 -0
  184. package/dist/social-providers/railway.mjs +78 -0
  185. package/dist/social-providers/railway.mjs.map +1 -0
  186. package/dist/social-providers/reddit.d.mts +2 -1
  187. package/dist/social-providers/reddit.mjs +2 -1
  188. package/dist/social-providers/reddit.mjs.map +1 -0
  189. package/dist/social-providers/roblox.d.mts +2 -1
  190. package/dist/social-providers/roblox.mjs +2 -1
  191. package/dist/social-providers/roblox.mjs.map +1 -0
  192. package/dist/social-providers/salesforce.d.mts +2 -1
  193. package/dist/social-providers/salesforce.mjs +2 -1
  194. package/dist/social-providers/salesforce.mjs.map +1 -0
  195. package/dist/social-providers/slack.d.mts +2 -1
  196. package/dist/social-providers/slack.mjs +2 -1
  197. package/dist/social-providers/slack.mjs.map +1 -0
  198. package/dist/social-providers/spotify.d.mts +2 -1
  199. package/dist/social-providers/spotify.mjs +2 -1
  200. package/dist/social-providers/spotify.mjs.map +1 -0
  201. package/dist/social-providers/tiktok.d.mts +3 -3
  202. package/dist/social-providers/tiktok.mjs +3 -2
  203. package/dist/social-providers/tiktok.mjs.map +1 -0
  204. package/dist/social-providers/twitch.d.mts +2 -1
  205. package/dist/social-providers/twitch.mjs +2 -1
  206. package/dist/social-providers/twitch.mjs.map +1 -0
  207. package/dist/social-providers/twitter.d.mts +14 -25
  208. package/dist/social-providers/twitter.mjs +2 -1
  209. package/dist/social-providers/twitter.mjs.map +1 -0
  210. package/dist/social-providers/vercel.d.mts +2 -1
  211. package/dist/social-providers/vercel.mjs +3 -2
  212. package/dist/social-providers/vercel.mjs.map +1 -0
  213. package/dist/social-providers/vk.d.mts +2 -1
  214. package/dist/social-providers/vk.mjs +2 -1
  215. package/dist/social-providers/vk.mjs.map +1 -0
  216. package/dist/social-providers/zoom.d.mts +3 -10
  217. package/dist/social-providers/zoom.mjs +2 -1
  218. package/dist/social-providers/zoom.mjs.map +1 -0
  219. package/dist/types/context.d.mts +54 -21
  220. package/dist/types/cookie.d.mts +2 -1
  221. package/dist/types/helper.d.mts +4 -1
  222. package/dist/types/index.d.mts +4 -3
  223. package/dist/types/init-options.d.mts +235 -144
  224. package/dist/types/plugin-client.d.mts +4 -1
  225. package/dist/types/plugin.d.mts +12 -11
  226. package/dist/types/secret.d.mts +12 -0
  227. package/dist/utils/db.d.mts +12 -0
  228. package/dist/utils/db.mjs +17 -0
  229. package/dist/utils/db.mjs.map +1 -0
  230. package/dist/utils/deprecate.d.mts +2 -2
  231. package/dist/utils/deprecate.mjs +2 -1
  232. package/dist/utils/deprecate.mjs.map +1 -0
  233. package/dist/utils/error-codes.d.mts +8 -6
  234. package/dist/utils/error-codes.mjs +3 -2
  235. package/dist/utils/error-codes.mjs.map +1 -0
  236. package/dist/utils/id.d.mts +2 -1
  237. package/dist/utils/id.mjs +2 -1
  238. package/dist/utils/id.mjs.map +1 -0
  239. package/dist/utils/ip.d.mts +55 -0
  240. package/dist/utils/ip.mjs +119 -0
  241. package/dist/utils/ip.mjs.map +1 -0
  242. package/dist/utils/json.d.mts +2 -1
  243. package/dist/utils/json.mjs +2 -1
  244. package/dist/utils/json.mjs.map +1 -0
  245. package/dist/utils/string.d.mts +2 -1
  246. package/dist/utils/string.mjs +2 -1
  247. package/dist/utils/string.mjs.map +1 -0
  248. package/dist/utils/url.d.mts +2 -1
  249. package/dist/utils/url.mjs +2 -1
  250. package/dist/utils/url.mjs.map +1 -0
  251. package/package.json +35 -13
  252. package/src/context/index.ts +1 -0
  253. package/src/context/transaction.ts +72 -9
  254. package/src/db/adapter/factory.ts +41 -73
  255. package/src/db/adapter/get-id-field.ts +1 -3
  256. package/src/db/adapter/index.ts +20 -15
  257. package/src/db/adapter/types.ts +2 -41
  258. package/src/db/get-tables.ts +48 -37
  259. package/src/db/index.ts +30 -5
  260. package/src/db/schema/account.ts +16 -3
  261. package/src/db/schema/rate-limit.ts +16 -1
  262. package/src/db/schema/session.ts +15 -3
  263. package/src/db/schema/user.ts +15 -3
  264. package/src/db/schema/verification.ts +16 -3
  265. package/src/db/test/get-tables.test.ts +33 -0
  266. package/src/db/type.ts +154 -1
  267. package/src/env/env-impl.ts +2 -2
  268. package/src/env/logger.ts +1 -1
  269. package/src/error/codes.ts +17 -0
  270. package/src/oauth2/client-credentials-token.ts +26 -2
  271. package/src/oauth2/create-authorization-url.ts +3 -1
  272. package/src/oauth2/index.ts +3 -0
  273. package/src/oauth2/oauth-provider.ts +1 -1
  274. package/src/oauth2/refresh-access-token.test.ts +90 -0
  275. package/src/oauth2/refresh-access-token.ts +37 -4
  276. package/src/oauth2/validate-authorization-code.ts +55 -29
  277. package/src/oauth2/validate-token.test.ts +229 -0
  278. package/src/social-providers/apple.ts +29 -29
  279. package/src/social-providers/cognito.ts +6 -5
  280. package/src/social-providers/dropbox.ts +1 -1
  281. package/src/social-providers/facebook.ts +3 -3
  282. package/src/social-providers/github.ts +26 -4
  283. package/src/social-providers/gitlab.ts +1 -1
  284. package/src/social-providers/google.ts +18 -14
  285. package/src/social-providers/huggingface.ts +1 -1
  286. package/src/social-providers/index.ts +9 -5
  287. package/src/social-providers/kakao.ts +1 -1
  288. package/src/social-providers/line.ts +1 -1
  289. package/src/social-providers/microsoft-entra-id.ts +84 -1
  290. package/src/social-providers/naver.ts +1 -1
  291. package/src/social-providers/notion.ts +1 -1
  292. package/src/social-providers/paybin.ts +1 -5
  293. package/src/social-providers/polar.ts +1 -1
  294. package/src/social-providers/railway.ts +100 -0
  295. package/src/social-providers/tiktok.ts +2 -1
  296. package/src/social-providers/vercel.ts +1 -1
  297. package/src/social-providers/zoom.ts +0 -8
  298. package/src/types/context.ts +79 -15
  299. package/src/types/helper.ts +9 -0
  300. package/src/types/index.ts +14 -2
  301. package/src/types/init-options.ts +298 -171
  302. package/src/types/plugin-client.ts +1 -0
  303. package/src/types/plugin.ts +11 -6
  304. package/src/types/secret.ts +8 -0
  305. package/src/utils/db.ts +20 -0
  306. package/src/utils/deprecate.test.ts +0 -1
  307. package/src/utils/error-codes.ts +12 -9
  308. package/src/utils/ip.test.ts +255 -0
  309. package/src/utils/ip.ts +211 -0
  310. package/.turbo/turbo-build.log +0 -180
  311. package/tsconfig.json +0 -7
  312. package/tsdown.config.ts +0 -32
  313. 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,
@@ -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
  };
@@ -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) {
@@ -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,6 +1101,7 @@ 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;
@@ -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,
@@ -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,
@@ -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;
@@ -31,9 +31,7 @@ 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
37
  const shouldGenerateId: boolean = (() => {
@@ -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",
@@ -242,43 +287,9 @@ export const getAuthTables = (
242
287
  },
243
288
  order: 3,
244
289
  },
245
- verification: {
246
- modelName: options.verification?.modelName || "verification",
247
- fields: {
248
- identifier: {
249
- type: "string",
250
- required: true,
251
- fieldName: options.verification?.fields?.identifier || "identifier",
252
- index: true,
253
- },
254
- value: {
255
- type: "string",
256
- required: true,
257
- fieldName: options.verification?.fields?.value || "value",
258
- },
259
- expiresAt: {
260
- type: "date",
261
- required: true,
262
- fieldName: options.verification?.fields?.expiresAt || "expiresAt",
263
- },
264
- createdAt: {
265
- type: "date",
266
- required: true,
267
- defaultValue: () => new Date(),
268
- fieldName: options.verification?.fields?.createdAt || "createdAt",
269
- },
270
- updatedAt: {
271
- type: "date",
272
- required: true,
273
- defaultValue: () => new Date(),
274
- onUpdate: () => new Date(),
275
- fieldName: options.verification?.fields?.updatedAt || "updatedAt",
276
- },
277
- ...verification?.fields,
278
- ...options.verification?.additionalFields,
279
- },
280
- order: 4,
281
- },
290
+ ...(!options.secondaryStorage || options.verification?.storeInDatabase
291
+ ? verificationTable
292
+ : {}),
282
293
  ...pluginTables,
283
294
  ...(shouldAddRateLimitTable ? rateLimitTable : {}),
284
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";
@@ -1,4 +1,10 @@
1
1
  import * as z from "zod";
2
+ import type { Prettify } from "../../types";
3
+ import type { BetterAuthOptions } from "../../types/init-options";
4
+ import type {
5
+ InferDBFieldsFromOptions,
6
+ InferDBFieldsFromPlugins,
7
+ } from "../type";
2
8
  import { coreSchema } from "./shared";
3
9
 
4
10
  export const accountSchema = coreSchema.extend({
@@ -26,9 +32,16 @@ export const accountSchema = coreSchema.extend({
26
32
  password: z.string().nullish(),
27
33
  });
28
34
 
35
+ export type BaseAccount = z.infer<typeof accountSchema>;
36
+
29
37
  /**
30
38
  * Account schema type used by better-auth, note that it's possible that account could have additional fields
31
- *
32
- * todo: we should use generics to extend this type with additional fields from plugins and options in the future
33
39
  */
34
- export type Account = z.infer<typeof accountSchema>;
40
+ export type Account<
41
+ DBOptions extends BetterAuthOptions["account"] = BetterAuthOptions["account"],
42
+ Plugins extends BetterAuthOptions["plugins"] = BetterAuthOptions["plugins"],
43
+ > = Prettify<
44
+ BaseAccount &
45
+ InferDBFieldsFromOptions<DBOptions> &
46
+ InferDBFieldsFromPlugins<"account", Plugins>
47
+ >;
@@ -1,4 +1,9 @@
1
1
  import * as z from "zod";
2
+ import type { BetterAuthOptions, Prettify } from "../../types";
3
+ import type {
4
+ InferDBFieldsFromOptions,
5
+ InferDBFieldsFromPlugins,
6
+ } from "../type";
2
7
 
3
8
  export const rateLimitSchema = z.object({
4
9
  /**
@@ -15,7 +20,17 @@ export const rateLimitSchema = z.object({
15
20
  lastRequest: z.number(),
16
21
  });
17
22
 
23
+ export type BaseRateLimit = z.infer<typeof rateLimitSchema>;
24
+
18
25
  /**
19
26
  * Rate limit schema type used by better-auth for rate limiting
20
27
  */
21
- export type RateLimit = z.infer<typeof rateLimitSchema>;
28
+ export type RateLimit<
29
+ DBOptions extends
30
+ BetterAuthOptions["rateLimit"] = BetterAuthOptions["rateLimit"],
31
+ Plugins extends BetterAuthOptions["plugins"] = BetterAuthOptions["plugins"],
32
+ > = Prettify<
33
+ BaseRateLimit &
34
+ InferDBFieldsFromOptions<DBOptions> &
35
+ InferDBFieldsFromPlugins<"rateLimit", Plugins>
36
+ >;
@@ -1,4 +1,9 @@
1
1
  import * as z from "zod";
2
+ import type { BetterAuthOptions, Prettify } from "../../types";
3
+ import type {
4
+ InferDBFieldsFromOptions,
5
+ InferDBFieldsFromPlugins,
6
+ } from "../type";
2
7
  import { coreSchema } from "./shared";
3
8
 
4
9
  export const sessionSchema = coreSchema.extend({
@@ -9,9 +14,16 @@ export const sessionSchema = coreSchema.extend({
9
14
  userAgent: z.string().nullish(),
10
15
  });
11
16
 
17
+ export type BaseSession = z.infer<typeof sessionSchema>;
18
+
12
19
  /**
13
20
  * Session schema type used by better-auth, note that it's possible that session could have additional fields
14
- *
15
- * todo: we should use generics to extend this type with additional fields from plugins and options in the future
16
21
  */
17
- export type Session = z.infer<typeof sessionSchema>;
22
+ export type Session<
23
+ DBOptions extends BetterAuthOptions["session"] = BetterAuthOptions["session"],
24
+ Plugins extends BetterAuthOptions["plugins"] = BetterAuthOptions["plugins"],
25
+ > = Prettify<
26
+ z.infer<typeof sessionSchema> &
27
+ InferDBFieldsFromOptions<DBOptions> &
28
+ InferDBFieldsFromPlugins<"session", Plugins>
29
+ >;
@@ -1,4 +1,9 @@
1
1
  import * as z from "zod";
2
+ import type { BetterAuthOptions, Prettify } from "../../types";
3
+ import type {
4
+ InferDBFieldsFromOptions,
5
+ InferDBFieldsFromPlugins,
6
+ } from "../type";
2
7
  import { coreSchema } from "./shared";
3
8
 
4
9
  export const userSchema = coreSchema.extend({
@@ -8,9 +13,16 @@ export const userSchema = coreSchema.extend({
8
13
  image: z.string().nullish(),
9
14
  });
10
15
 
16
+ export type BaseUser = z.infer<typeof userSchema>;
17
+
11
18
  /**
12
19
  * User schema type used by better-auth, note that it's possible that user could have additional fields
13
- *
14
- * todo: we should use generics to extend this type with additional fields from plugins and options in the future
15
20
  */
16
- export type User = z.infer<typeof userSchema>;
21
+ export type User<
22
+ DBOptions extends BetterAuthOptions["user"] = BetterAuthOptions["user"],
23
+ Plugins extends BetterAuthOptions["plugins"] = BetterAuthOptions["plugins"],
24
+ > = Prettify<
25
+ BaseUser &
26
+ InferDBFieldsFromOptions<DBOptions> &
27
+ InferDBFieldsFromPlugins<"user", Plugins>
28
+ >;
@@ -1,4 +1,9 @@
1
1
  import * as z from "zod";
2
+ import type { BetterAuthOptions, Prettify } from "../../types";
3
+ import type {
4
+ InferDBFieldsFromOptions,
5
+ InferDBFieldsFromPlugins,
6
+ } from "../type";
2
7
  import { coreSchema } from "./shared";
3
8
 
4
9
  export const verificationSchema = coreSchema.extend({
@@ -7,9 +12,17 @@ export const verificationSchema = coreSchema.extend({
7
12
  identifier: z.string(),
8
13
  });
9
14
 
15
+ export type BaseVerification = z.infer<typeof verificationSchema>;
16
+
10
17
  /**
11
18
  * Verification schema type used by better-auth, note that it's possible that verification could have additional fields
12
- *
13
- * todo: we should use generics to extend this type with additional fields from plugins and options in the future
14
19
  */
15
- export type Verification = z.infer<typeof verificationSchema>;
20
+ export type Verification<
21
+ DBOptions extends
22
+ BetterAuthOptions["verification"] = BetterAuthOptions["verification"],
23
+ Plugins extends BetterAuthOptions["plugins"] = BetterAuthOptions["plugins"],
24
+ > = Prettify<
25
+ BaseVerification &
26
+ InferDBFieldsFromOptions<DBOptions> &
27
+ InferDBFieldsFromPlugins<"verification", Plugins>
28
+ >;