@better-auth/core 1.3.26 → 1.3.28

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 (130) hide show
  1. package/.turbo/turbo-build.log +60 -9
  2. package/build.config.ts +7 -0
  3. package/dist/db/adapter/index.cjs +2 -0
  4. package/dist/db/adapter/index.d.cts +14 -0
  5. package/dist/db/adapter/index.d.mts +14 -0
  6. package/dist/db/adapter/index.d.ts +14 -0
  7. package/dist/db/adapter/index.mjs +1 -0
  8. package/dist/db/index.cjs +89 -0
  9. package/dist/db/index.d.cts +16 -107
  10. package/dist/db/index.d.mts +16 -107
  11. package/dist/db/index.d.ts +16 -107
  12. package/dist/db/index.mjs +69 -0
  13. package/dist/env/index.cjs +312 -0
  14. package/dist/env/index.d.cts +36 -0
  15. package/dist/env/index.d.mts +36 -0
  16. package/dist/env/index.d.ts +36 -0
  17. package/dist/env/index.mjs +297 -0
  18. package/dist/error/index.cjs +44 -0
  19. package/dist/error/index.d.cts +33 -0
  20. package/dist/error/index.d.mts +33 -0
  21. package/dist/error/index.d.ts +33 -0
  22. package/dist/error/index.mjs +41 -0
  23. package/dist/index.d.cts +179 -1
  24. package/dist/index.d.mts +179 -1
  25. package/dist/index.d.ts +179 -1
  26. package/dist/middleware/index.cjs +25 -0
  27. package/dist/middleware/index.d.cts +14 -0
  28. package/dist/middleware/index.d.mts +14 -0
  29. package/dist/middleware/index.d.ts +14 -0
  30. package/dist/middleware/index.mjs +21 -0
  31. package/dist/oauth2/index.cjs +368 -0
  32. package/dist/oauth2/index.d.cts +100 -0
  33. package/dist/oauth2/index.d.mts +100 -0
  34. package/dist/oauth2/index.d.ts +100 -0
  35. package/dist/oauth2/index.mjs +357 -0
  36. package/dist/shared/core.BJPBStdk.d.ts +1693 -0
  37. package/dist/shared/core.Bl6TpxyD.d.mts +181 -0
  38. package/dist/shared/core.Bqe5IGAi.d.ts +13 -0
  39. package/dist/shared/core.BwoNUcJQ.d.cts +53 -0
  40. package/dist/shared/core.BwoNUcJQ.d.mts +53 -0
  41. package/dist/shared/core.BwoNUcJQ.d.ts +53 -0
  42. package/dist/shared/core.CajxAutx.d.cts +143 -0
  43. package/dist/shared/core.CajxAutx.d.mts +143 -0
  44. package/dist/shared/core.CajxAutx.d.ts +143 -0
  45. package/dist/shared/core.CkkLHQWc.d.mts +1693 -0
  46. package/dist/shared/core.DkdZ1o38.d.ts +181 -0
  47. package/dist/shared/core.Dl-70uns.d.cts +84 -0
  48. package/dist/shared/core.Dl-70uns.d.mts +84 -0
  49. package/dist/shared/core.Dl-70uns.d.ts +84 -0
  50. package/dist/shared/core.DyEdx0m7.d.cts +181 -0
  51. package/dist/shared/core.E9DfzGLz.d.mts +13 -0
  52. package/dist/shared/core.HqYn20Fi.d.cts +13 -0
  53. package/dist/shared/core.gYIBmdi1.d.cts +1693 -0
  54. package/dist/social-providers/index.cjs +2793 -0
  55. package/dist/social-providers/index.d.cts +3903 -0
  56. package/dist/social-providers/index.d.mts +3903 -0
  57. package/dist/social-providers/index.d.ts +3903 -0
  58. package/dist/social-providers/index.mjs +2743 -0
  59. package/dist/utils/index.cjs +7 -0
  60. package/dist/utils/index.d.cts +10 -0
  61. package/dist/utils/index.d.mts +10 -0
  62. package/dist/utils/index.d.ts +10 -0
  63. package/dist/utils/index.mjs +5 -0
  64. package/package.json +109 -2
  65. package/src/db/adapter/index.ts +448 -0
  66. package/src/db/index.ts +13 -0
  67. package/src/db/plugin.ts +11 -0
  68. package/src/db/schema/account.ts +34 -0
  69. package/src/db/schema/rate-limit.ts +21 -0
  70. package/src/db/schema/session.ts +17 -0
  71. package/src/db/schema/shared.ts +7 -0
  72. package/src/db/schema/user.ts +16 -0
  73. package/src/db/schema/verification.ts +15 -0
  74. package/src/db/type.ts +50 -0
  75. package/src/env/color-depth.ts +172 -0
  76. package/src/env/env-impl.ts +123 -0
  77. package/src/env/index.ts +23 -0
  78. package/src/env/logger.test.ts +33 -0
  79. package/src/env/logger.ts +145 -0
  80. package/src/error/codes.ts +31 -0
  81. package/src/error/index.ts +11 -0
  82. package/src/index.ts +1 -1
  83. package/src/middleware/index.ts +33 -0
  84. package/src/oauth2/client-credentials-token.ts +102 -0
  85. package/src/oauth2/create-authorization-url.ts +85 -0
  86. package/src/oauth2/index.ts +22 -0
  87. package/src/oauth2/oauth-provider.ts +194 -0
  88. package/src/oauth2/refresh-access-token.ts +124 -0
  89. package/src/oauth2/utils.ts +36 -0
  90. package/src/oauth2/validate-authorization-code.ts +156 -0
  91. package/src/social-providers/apple.ts +213 -0
  92. package/src/social-providers/atlassian.ts +130 -0
  93. package/src/social-providers/cognito.ts +269 -0
  94. package/src/social-providers/discord.ts +172 -0
  95. package/src/social-providers/dropbox.ts +112 -0
  96. package/src/social-providers/facebook.ts +204 -0
  97. package/src/social-providers/figma.ts +115 -0
  98. package/src/social-providers/github.ts +154 -0
  99. package/src/social-providers/gitlab.ts +152 -0
  100. package/src/social-providers/google.ts +171 -0
  101. package/src/social-providers/huggingface.ts +116 -0
  102. package/src/social-providers/index.ts +118 -0
  103. package/src/social-providers/kakao.ts +178 -0
  104. package/src/social-providers/kick.ts +95 -0
  105. package/src/social-providers/line.ts +169 -0
  106. package/src/social-providers/linear.ts +120 -0
  107. package/src/social-providers/linkedin.ts +110 -0
  108. package/src/social-providers/microsoft-entra-id.ts +243 -0
  109. package/src/social-providers/naver.ts +112 -0
  110. package/src/social-providers/notion.ts +106 -0
  111. package/src/social-providers/paypal.ts +261 -0
  112. package/src/social-providers/reddit.ts +122 -0
  113. package/src/social-providers/roblox.ts +110 -0
  114. package/src/social-providers/salesforce.ts +157 -0
  115. package/src/social-providers/slack.ts +114 -0
  116. package/src/social-providers/spotify.ts +93 -0
  117. package/src/social-providers/tiktok.ts +211 -0
  118. package/src/social-providers/twitch.ts +111 -0
  119. package/src/social-providers/twitter.ts +194 -0
  120. package/src/social-providers/vk.ts +128 -0
  121. package/src/social-providers/zoom.ts +218 -0
  122. package/src/types/context.ts +313 -0
  123. package/src/types/cookie.ts +7 -0
  124. package/src/types/helper.ts +5 -0
  125. package/src/types/index.ts +20 -1
  126. package/src/types/init-options.ts +1161 -0
  127. package/src/types/plugin-client.ts +69 -0
  128. package/src/types/plugin.ts +134 -0
  129. package/src/utils/error-codes.ts +51 -0
  130. package/src/utils/index.ts +1 -0
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ function defineErrorCodes(codes) {
4
+ return codes;
5
+ }
6
+
7
+ exports.defineErrorCodes = defineErrorCodes;
@@ -0,0 +1,10 @@
1
+ type UpperLetter = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z";
2
+ type SpecialCharacter = "_";
3
+ type IsValidUpperSnakeCase<S extends string> = S extends `${infer F}${infer R}` ? F extends UpperLetter | SpecialCharacter ? IsValidUpperSnakeCase<R> : false : true;
4
+ type InvalidKeyError<K extends string> = `Invalid error code key: "${K}" - must only contain uppercase letters (A-Z) and underscores (_)`;
5
+ type ValidateErrorCodes<T> = {
6
+ [K in keyof T]: K extends string ? IsValidUpperSnakeCase<K> extends false ? InvalidKeyError<K> : T[K] : T[K];
7
+ };
8
+ declare function defineErrorCodes<const T extends Record<string, string>>(codes: ValidateErrorCodes<T>): T;
9
+
10
+ export { defineErrorCodes };
@@ -0,0 +1,10 @@
1
+ type UpperLetter = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z";
2
+ type SpecialCharacter = "_";
3
+ type IsValidUpperSnakeCase<S extends string> = S extends `${infer F}${infer R}` ? F extends UpperLetter | SpecialCharacter ? IsValidUpperSnakeCase<R> : false : true;
4
+ type InvalidKeyError<K extends string> = `Invalid error code key: "${K}" - must only contain uppercase letters (A-Z) and underscores (_)`;
5
+ type ValidateErrorCodes<T> = {
6
+ [K in keyof T]: K extends string ? IsValidUpperSnakeCase<K> extends false ? InvalidKeyError<K> : T[K] : T[K];
7
+ };
8
+ declare function defineErrorCodes<const T extends Record<string, string>>(codes: ValidateErrorCodes<T>): T;
9
+
10
+ export { defineErrorCodes };
@@ -0,0 +1,10 @@
1
+ type UpperLetter = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z";
2
+ type SpecialCharacter = "_";
3
+ type IsValidUpperSnakeCase<S extends string> = S extends `${infer F}${infer R}` ? F extends UpperLetter | SpecialCharacter ? IsValidUpperSnakeCase<R> : false : true;
4
+ type InvalidKeyError<K extends string> = `Invalid error code key: "${K}" - must only contain uppercase letters (A-Z) and underscores (_)`;
5
+ type ValidateErrorCodes<T> = {
6
+ [K in keyof T]: K extends string ? IsValidUpperSnakeCase<K> extends false ? InvalidKeyError<K> : T[K] : T[K];
7
+ };
8
+ declare function defineErrorCodes<const T extends Record<string, string>>(codes: ValidateErrorCodes<T>): T;
9
+
10
+ export { defineErrorCodes };
@@ -0,0 +1,5 @@
1
+ function defineErrorCodes(codes) {
2
+ return codes;
3
+ }
4
+
5
+ export { defineErrorCodes };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/core",
3
- "version": "1.3.26",
3
+ "version": "1.3.28",
4
4
  "description": "The most comprehensive authentication library for TypeScript.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -26,6 +26,46 @@
26
26
  "default": "./dist/async_hooks/index.cjs"
27
27
  }
28
28
  },
29
+ "./env": {
30
+ "import": {
31
+ "types": "./dist/env/index.d.ts",
32
+ "default": "./dist/env/index.mjs"
33
+ },
34
+ "require": {
35
+ "types": "./dist/env/index.d.cts",
36
+ "default": "./dist/env/index.cjs"
37
+ }
38
+ },
39
+ "./error": {
40
+ "import": {
41
+ "types": "./dist/error/index.d.ts",
42
+ "default": "./dist/error/index.mjs"
43
+ },
44
+ "require": {
45
+ "types": "./dist/error/index.d.cts",
46
+ "default": "./dist/error/index.cjs"
47
+ }
48
+ },
49
+ "./utils": {
50
+ "import": {
51
+ "types": "./dist/utils/index.d.ts",
52
+ "default": "./dist/utils/index.mjs"
53
+ },
54
+ "require": {
55
+ "types": "./dist/utils/index.d.cts",
56
+ "default": "./dist/utils/index.cjs"
57
+ }
58
+ },
59
+ "./social-providers": {
60
+ "import": {
61
+ "types": "./dist/social-providers/index.d.ts",
62
+ "default": "./dist/social-providers/index.mjs"
63
+ },
64
+ "require": {
65
+ "types": "./dist/social-providers/index.d.cts",
66
+ "default": "./dist/social-providers/index.cjs"
67
+ }
68
+ },
29
69
  "./db": {
30
70
  "import": {
31
71
  "types": "./dist/db/index.d.ts",
@@ -35,6 +75,36 @@
35
75
  "types": "./dist/db/index.d.cts",
36
76
  "default": "./dist/db/index.cjs"
37
77
  }
78
+ },
79
+ "./db/adapter": {
80
+ "import": {
81
+ "types": "./dist/db/adapter/index.d.ts",
82
+ "default": "./dist/db/adapter/index.mjs"
83
+ },
84
+ "require": {
85
+ "types": "./dist/db/adapter/index.d.cts",
86
+ "default": "./dist/db/adapter/index.cjs"
87
+ }
88
+ },
89
+ "./oauth2": {
90
+ "import": {
91
+ "types": "./dist/oauth2/index.d.ts",
92
+ "default": "./dist/oauth2/index.mjs"
93
+ },
94
+ "require": {
95
+ "types": "./dist/oauth2/index.d.cts",
96
+ "default": "./dist/oauth2/index.cjs"
97
+ }
98
+ },
99
+ "./middleware": {
100
+ "import": {
101
+ "types": "./dist/middleware/index.d.ts",
102
+ "default": "./dist/middleware/index.mjs"
103
+ },
104
+ "require": {
105
+ "types": "./dist/middleware/index.d.cts",
106
+ "default": "./dist/middleware/index.cjs"
107
+ }
38
108
  }
39
109
  },
40
110
  "typesVersions": {
@@ -47,16 +117,53 @@
47
117
  ],
48
118
  "db": [
49
119
  "dist/db.d.ts"
120
+ ],
121
+ "db/adapter": [
122
+ "dist/db/adapter/index.d.ts"
123
+ ],
124
+ "env": [
125
+ "dist/env.d.ts"
126
+ ],
127
+ "error": [
128
+ "dist/error.d.ts"
129
+ ],
130
+ "middleware": [
131
+ "dist/middleware.d.ts"
132
+ ],
133
+ "oauth2": [
134
+ "dist/oauth2.d.ts"
135
+ ],
136
+ "social-providers": [
137
+ "dist/social-providers.d.ts"
138
+ ],
139
+ "utils": [
140
+ "dist/utils.d.ts"
50
141
  ]
51
142
  }
52
143
  },
53
144
  "devDependencies": {
145
+ "@better-auth/utils": "0.3.0",
146
+ "@better-fetch/fetch": "1.1.18",
147
+ "@types/better-sqlite3": "^7.6.13",
148
+ "better-call": "1.0.19",
149
+ "better-sqlite3": "^12.4.1",
150
+ "jose": "^6.1.0",
151
+ "kysely": "^0.28.5",
152
+ "nanostores": "^1.0.1",
54
153
  "unbuild": "3.6.1"
55
154
  },
56
155
  "dependencies": {
57
- "better-call": "1.0.19",
58
156
  "zod": "^4.1.5"
59
157
  },
158
+ "peerDependencies": {
159
+ "@better-auth/utils": "0.3.0",
160
+ "@better-fetch/fetch": "1.1.18",
161
+ "better-call": "1.0.19",
162
+ "better-sqlite3": "^12.4.1",
163
+ "jose": "^6.1.0",
164
+ "kysely": "^0.28.5",
165
+ "nanostores": "^1.0.1"
166
+ },
60
167
  "scripts": {
61
168
  "build": "unbuild --clean",
62
169
  "stub": "unbuild --stub",
@@ -0,0 +1,448 @@
1
+ import type { BetterAuthDBSchema, DBFieldAttribute } from "../type";
2
+ import type { BetterAuthOptions } from "../../types";
3
+
4
+ export type DBAdapterDebugLogOption =
5
+ | boolean
6
+ | {
7
+ /**
8
+ * Useful when you want to log only certain conditions.
9
+ */
10
+ logCondition?: (() => boolean) | undefined;
11
+ create?: boolean;
12
+ update?: boolean;
13
+ updateMany?: boolean;
14
+ findOne?: boolean;
15
+ findMany?: boolean;
16
+ delete?: boolean;
17
+ deleteMany?: boolean;
18
+ count?: boolean;
19
+ }
20
+ | {
21
+ /**
22
+ * Only used for adapter tests to show debug logs if a test fails.
23
+ *
24
+ * @deprecated Not actually deprecated. Doing this for IDEs to show this option at the very bottom and stop end-users from using this.
25
+ */
26
+ isRunningAdapterTests: boolean;
27
+ };
28
+
29
+ export type DBAdapterSchemaCreation = {
30
+ /**
31
+ * Code to be inserted into the file
32
+ */
33
+ code: string;
34
+ /**
35
+ * Path to the file, including the file name and extension.
36
+ * Relative paths are supported, with the current working directory of the developer's project as the base.
37
+ */
38
+ path: string;
39
+ /**
40
+ * Append the file if it already exists.
41
+ * Note: This will not apply if `overwrite` is set to true.
42
+ */
43
+ append?: boolean;
44
+ /**
45
+ * Overwrite the file if it already exists
46
+ */
47
+ overwrite?: boolean;
48
+ };
49
+
50
+ export interface DBAdapterFactoryConfig<
51
+ Options extends BetterAuthOptions = BetterAuthOptions,
52
+ > {
53
+ /**
54
+ * Use plural table names.
55
+ *
56
+ * All tables will be named with an `s` at the end.
57
+ *
58
+ * @default false
59
+ */
60
+ usePlural?: boolean;
61
+ /**
62
+ * Enable debug logs.
63
+ *
64
+ * @default false
65
+ */
66
+ debugLogs?: DBAdapterDebugLogOption;
67
+ /**
68
+ * Name of the adapter.
69
+ *
70
+ * This is used to identify the adapter in the debug logs.
71
+ *
72
+ * @default `adapterId`
73
+ */
74
+ adapterName?: string;
75
+ /**
76
+ * Adapter id
77
+ */
78
+ adapterId: string;
79
+ /**
80
+ * If the database supports numeric ids, set this to `true`.
81
+ *
82
+ * @default true
83
+ */
84
+ supportsNumericIds?: boolean;
85
+ /**
86
+ * If the database doesn't support JSON columns, set this to `false`.
87
+ *
88
+ * We will handle the translation between using `JSON` columns, and saving `string`s to the database.
89
+ *
90
+ * @default false
91
+ */
92
+ supportsJSON?: boolean;
93
+ /**
94
+ * If the database doesn't support dates, set this to `false`.
95
+ *
96
+ * We will handle the translation between using `Date` objects, and saving `string`s to the database.
97
+ *
98
+ * @default true
99
+ */
100
+ supportsDates?: boolean;
101
+ /**
102
+ * If the database doesn't support booleans, set this to `false`.
103
+ *
104
+ * We will handle the translation between using `boolean`s, and saving `0`s and `1`s to the database.
105
+ *
106
+ * @default true
107
+ */
108
+ supportsBooleans?: boolean;
109
+ /**
110
+ * Execute multiple operations in a transaction.
111
+ *
112
+ * If the database doesn't support transactions, set this to `false` and operations will be executed sequentially.
113
+ *
114
+ * @default false
115
+ */
116
+ transaction?:
117
+ | false
118
+ | (<R>(
119
+ callback: (trx: DBTransactionAdapter<Options>) => Promise<R>,
120
+ ) => Promise<R>);
121
+ /**
122
+ * Disable id generation for the `create` method.
123
+ *
124
+ * This is useful for databases that don't support custom id values and would auto-generate them for you.
125
+ *
126
+ * @default false
127
+ */
128
+ disableIdGeneration?: boolean;
129
+ /**
130
+ * Map the keys of the input data.
131
+ *
132
+ * This is useful for databases that expect a different key name for a given situation.
133
+ *
134
+ * For example, MongoDB uses `_id` while in Better-Auth we use `id`.
135
+ *
136
+ *
137
+ * @example
138
+ * Each key represents the old key to replace.
139
+ * The value represents the new key
140
+ *
141
+ * This can be a partial object that only transforms some keys.
142
+ *
143
+ * ```ts
144
+ * mapKeysTransformInput: {
145
+ * id: "_id" // We want to replace `id` to `_id` to save into MongoDB
146
+ * }
147
+ * ```
148
+ */
149
+ mapKeysTransformInput?: Record<string, string>;
150
+ /**
151
+ * Map the keys of the output data.
152
+ *
153
+ * This is useful for databases that expect a different key name for a given situation.
154
+ *
155
+ * For example, MongoDB uses `_id` while in Better-Auth we use `id`.
156
+ *
157
+ * @example
158
+ * Each key represents the old key to replace.
159
+ * The value represents the new key
160
+ *
161
+ * This can be a partial object that only transforms some keys.
162
+ *
163
+ * ```ts
164
+ * mapKeysTransformOutput: {
165
+ * _id: "id" // In MongoDB, we save `id` as `_id`. So we want to replace `_id` with `id` when we get the data back.
166
+ * }
167
+ * ```
168
+ */
169
+ mapKeysTransformOutput?: Record<string, string>;
170
+ /**
171
+ * Custom transform input function.
172
+ *
173
+ * This function is used to transform the input data before it is saved to the database.
174
+ */
175
+ customTransformInput?: (props: {
176
+ data: any;
177
+ /**
178
+ * The fields of the model.
179
+ */
180
+ fieldAttributes: DBFieldAttribute;
181
+ /**
182
+ * The field to transform.
183
+ */
184
+ field: string;
185
+ /**
186
+ * The action which was called from the adapter.
187
+ */
188
+ action: "create" | "update";
189
+ /**
190
+ * The model name.
191
+ */
192
+ model: string;
193
+ /**
194
+ * The schema of the user's Better-Auth instance.
195
+ */
196
+ schema: BetterAuthDBSchema;
197
+ /**
198
+ * The options of the user's Better-Auth instance.
199
+ */
200
+ options: Options;
201
+ }) => any;
202
+ /**
203
+ * Custom transform output function.
204
+ *
205
+ * This function is used to transform the output data before it is returned to the user.
206
+ */
207
+ customTransformOutput?: (props: {
208
+ data: any;
209
+ /**
210
+ * The fields of the model.
211
+ */
212
+ fieldAttributes: DBFieldAttribute;
213
+ /**
214
+ * The field to transform.
215
+ */
216
+ field: string;
217
+ /**
218
+ * The fields to select.
219
+ */
220
+ select: string[];
221
+ /**
222
+ * The model name.
223
+ */
224
+ model: string;
225
+ /**
226
+ * The schema of the user's Better-Auth instance.
227
+ */
228
+ schema: BetterAuthDBSchema;
229
+ /**
230
+ * The options of the user's Better-Auth instance.
231
+ */
232
+ options: Options;
233
+ }) => any;
234
+ /**
235
+ * Custom ID generator function.
236
+ *
237
+ * By default, we can handle ID generation for you, however if the database your adapter is for only supports a specific custom id generation,
238
+ * then you can use this function to generate your own IDs.
239
+ *
240
+ *
241
+ * Notes:
242
+ * - If the user enabled `useNumberId`, then this option will be ignored. Unless this adapter config has `supportsNumericIds` set to `false`.
243
+ * - If `generateId` is `false` in the user's Better-Auth config, then this option will be ignored.
244
+ * - If `generateId` is a function, then it will override this option.
245
+ *
246
+ * @example
247
+ *
248
+ * ```ts
249
+ * customIdGenerator: ({ model }) => {
250
+ * return "my-super-unique-id";
251
+ * }
252
+ * ```
253
+ */
254
+ customIdGenerator?: (props: { model: string }) => string;
255
+ /**
256
+ * Whether to disable the transform output.
257
+ * Do not use this option unless you know what you are doing.
258
+ * @default false
259
+ */
260
+ disableTransformOutput?: boolean;
261
+ /**
262
+ * Whether to disable the transform input.
263
+ * Do not use this option unless you know what you are doing.
264
+ * @default false
265
+ */
266
+ disableTransformInput?: boolean;
267
+ }
268
+
269
+ export type Where = {
270
+ /**
271
+ * @default eq
272
+ */
273
+ operator?:
274
+ | "eq"
275
+ | "ne"
276
+ | "lt"
277
+ | "lte"
278
+ | "gt"
279
+ | "gte"
280
+ | "in"
281
+ | "not_in"
282
+ | "contains"
283
+ | "starts_with"
284
+ | "ends_with";
285
+ value: string | number | boolean | string[] | number[] | Date | null;
286
+ field: string;
287
+ /**
288
+ * @default AND
289
+ */
290
+ connector?: "AND" | "OR";
291
+ };
292
+
293
+ export type DBTransactionAdapter<
294
+ Options extends BetterAuthOptions = BetterAuthOptions,
295
+ > = Omit<DBAdapter<Options>, "transaction">;
296
+
297
+ export type DBAdapter<Options extends BetterAuthOptions = BetterAuthOptions> = {
298
+ id: string;
299
+ create: <T extends Record<string, any>, R = T>(data: {
300
+ model: string;
301
+ data: Omit<T, "id">;
302
+ select?: string[];
303
+ /**
304
+ * By default, any `id` provided in `data` will be ignored.
305
+ *
306
+ * If you want to force the `id` to be the same as the `data.id`, set this to `true`.
307
+ */
308
+ forceAllowId?: boolean;
309
+ }) => Promise<R>;
310
+ findOne: <T>(data: {
311
+ model: string;
312
+ where: Where[];
313
+ select?: string[];
314
+ }) => Promise<T | null>;
315
+ findMany: <T>(data: {
316
+ model: string;
317
+ where?: Where[];
318
+ limit?: number;
319
+ sortBy?: {
320
+ field: string;
321
+ direction: "asc" | "desc";
322
+ };
323
+ offset?: number;
324
+ }) => Promise<T[]>;
325
+ count: (data: { model: string; where?: Where[] }) => Promise<number>;
326
+ /**
327
+ * ⚠︎ Update may not return the updated data
328
+ * if multiple where clauses are provided
329
+ */
330
+ update: <T>(data: {
331
+ model: string;
332
+ where: Where[];
333
+ update: Record<string, any>;
334
+ }) => Promise<T | null>;
335
+ updateMany: (data: {
336
+ model: string;
337
+ where: Where[];
338
+ update: Record<string, any>;
339
+ }) => Promise<number>;
340
+ delete: <T>(data: { model: string; where: Where[] }) => Promise<void>;
341
+ deleteMany: (data: { model: string; where: Where[] }) => Promise<number>;
342
+ /**
343
+ * Execute multiple operations in a transaction.
344
+ * If the adapter doesn't support transactions, operations will be executed sequentially.
345
+ */
346
+ transaction: <R>(
347
+ callback: (trx: DBTransactionAdapter<Options>) => Promise<R>,
348
+ ) => Promise<R>;
349
+ /**
350
+ *
351
+ * @param options
352
+ * @param file - file path if provided by the user
353
+ */
354
+ createSchema?: (
355
+ options: Options,
356
+ file?: string,
357
+ ) => Promise<DBAdapterSchemaCreation>;
358
+ options?: {
359
+ adapterConfig: DBAdapterFactoryConfig<Options>;
360
+ } & CustomAdapter["options"];
361
+ };
362
+
363
+ export type CleanedWhere = Required<Where>;
364
+
365
+ export interface CustomAdapter {
366
+ create: <T extends Record<string, any>>({
367
+ data,
368
+ model,
369
+ select,
370
+ }: {
371
+ model: string;
372
+ data: T;
373
+ select?: string[];
374
+ }) => Promise<T>;
375
+ update: <T>(data: {
376
+ model: string;
377
+ where: CleanedWhere[];
378
+ update: T;
379
+ }) => Promise<T | null>;
380
+ updateMany: (data: {
381
+ model: string;
382
+ where: CleanedWhere[];
383
+ update: Record<string, any>;
384
+ }) => Promise<number>;
385
+ findOne: <T>({
386
+ model,
387
+ where,
388
+ select,
389
+ }: {
390
+ model: string;
391
+ where: CleanedWhere[];
392
+ select?: string[];
393
+ }) => Promise<T | null>;
394
+ findMany: <T>({
395
+ model,
396
+ where,
397
+ limit,
398
+ sortBy,
399
+ offset,
400
+ }: {
401
+ model: string;
402
+ where?: CleanedWhere[];
403
+ limit: number;
404
+ sortBy?: { field: string; direction: "asc" | "desc" };
405
+ offset?: number;
406
+ }) => Promise<T[]>;
407
+ delete: ({
408
+ model,
409
+ where,
410
+ }: {
411
+ model: string;
412
+ where: CleanedWhere[];
413
+ }) => Promise<void>;
414
+ deleteMany: ({
415
+ model,
416
+ where,
417
+ }: {
418
+ model: string;
419
+ where: CleanedWhere[];
420
+ }) => Promise<number>;
421
+ count: ({
422
+ model,
423
+ where,
424
+ }: {
425
+ model: string;
426
+ where?: CleanedWhere[];
427
+ }) => Promise<number>;
428
+ createSchema?: (props: {
429
+ /**
430
+ * The file the user may have passed in to the `generate` command as the expected schema file output path.
431
+ */
432
+ file?: string;
433
+ /**
434
+ * The tables from the user's Better-Auth instance schema.
435
+ */
436
+ tables: BetterAuthDBSchema;
437
+ }) => Promise<DBAdapterSchemaCreation>;
438
+ /**
439
+ * Your adapter's options.
440
+ */
441
+ options?: Record<string, any> | undefined;
442
+ }
443
+
444
+ export interface DBAdapterInstance<
445
+ Options extends BetterAuthOptions = BetterAuthOptions,
446
+ > {
447
+ (options: BetterAuthOptions): DBAdapter<Options>;
448
+ }
package/src/db/index.ts CHANGED
@@ -5,6 +5,15 @@ import type {
5
5
  DBPrimitive,
6
6
  BetterAuthDBSchema,
7
7
  } from "./type";
8
+ import type { BetterAuthPluginDBSchema } from "./plugin";
9
+ export type { BetterAuthPluginDBSchema } from "./plugin";
10
+ export type { SecondaryStorage } from "./type";
11
+ export { coreSchema } from "./schema/shared";
12
+ export { userSchema, type User } from "./schema/user";
13
+ export { accountSchema, type Account } from "./schema/account";
14
+ export { sessionSchema, type Session } from "./schema/session";
15
+ export { verificationSchema, type Verification } from "./schema/verification";
16
+ export { rateLimitSchema, type RateLimit } from "./schema/rate-limit";
8
17
 
9
18
  export type {
10
19
  DBFieldAttribute,
@@ -14,6 +23,10 @@ export type {
14
23
  BetterAuthDBSchema,
15
24
  };
16
25
 
26
+ /**
27
+ * @deprecated Backport for 1.3.x, we will remove this in 1.4.x
28
+ */
29
+ export type AuthPluginSchema = BetterAuthPluginDBSchema;
17
30
  /**
18
31
  * @deprecated Backport for 1.3.x, we will remove this in 1.4.x
19
32
  */
@@ -0,0 +1,11 @@
1
+ import type { DBFieldAttribute } from "./type";
2
+
3
+ export type BetterAuthPluginDBSchema = {
4
+ [table in string]: {
5
+ fields: {
6
+ [field in string]: DBFieldAttribute;
7
+ };
8
+ disableMigration?: boolean;
9
+ modelName?: string;
10
+ };
11
+ };