@prisma/orm-better-auth 8.0.0-rc.25-dev.7 → 8.0.0-rc.25-dev.9
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.
- package/README.md +49 -19
- package/dist/contract-check-Cw4rCQvX.mjs +799 -0
- package/dist/contract-check-Cw4rCQvX.mjs.map +1 -0
- package/dist/edited-contract-source-uFJmCt41.mjs +487 -0
- package/dist/edited-contract-source-uFJmCt41.mjs.map +1 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +5 -774
- package/dist/index.mjs.map +1 -1
- package/package.json +13 -10
package/README.md
CHANGED
|
@@ -18,10 +18,10 @@ export const auth = betterAuth({
|
|
|
18
18
|
|
|
19
19
|
## Install
|
|
20
20
|
|
|
21
|
-
Install the adapter at the same version as your Prisma 8 database package, together with Better Auth 1.7.
|
|
21
|
+
Install the adapter at the same version as your Prisma 8 database package, together with Better Auth 1.7.22 or a later 1.7 release:
|
|
22
22
|
|
|
23
23
|
```sh
|
|
24
|
-
pnpm add @prisma/orm-better-auth@<version of @prisma/orm-postgres or @prisma/orm-sqlite> better-auth@~1.7.
|
|
24
|
+
pnpm add @prisma/orm-better-auth@<version of @prisma/orm-postgres or @prisma/orm-sqlite> better-auth@~1.7.22
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
The adapter uses the Prisma packages your database package already installs. If the versions differ, your package manager warns you, or refuses to install when it checks peer dependencies strictly. Keep them the same, or the adapter loads a second copy of Prisma's code.
|
|
@@ -31,7 +31,7 @@ Better Auth must load one copy of `@better-auth/core`, the one `better-auth` its
|
|
|
31
31
|
## Supported databases and versions
|
|
32
32
|
|
|
33
33
|
- Postgres and SQLite, through `@prisma/orm-postgres` and `@prisma/orm-sqlite`. The adapter refuses a client for another database.
|
|
34
|
-
- Better Auth 1.7.
|
|
34
|
+
- Better Auth 1.7.22 or a later 1.7 release (`>=1.7.22 <1.8.0`). It is tested against 1.7.22 and 1.7.27. Releases from 1.7.6 to 1.7.21 run the contract check when the auth instance is created, and log a contract that is missing something as "Could not validate the database schema. Check your database connection.", which is wrong. From 1.7.22 the check runs before the first request instead, and the request fails with the adapter's message.
|
|
35
35
|
|
|
36
36
|
## Set up
|
|
37
37
|
|
|
@@ -106,7 +106,7 @@ model Verification {
|
|
|
106
106
|
}
|
|
107
107
|
```
|
|
108
108
|
|
|
109
|
-
Plugins and `additionalFields` add models and fields. You do not have to work them out: `auth generate`
|
|
109
|
+
Plugins and `additionalFields` add models and fields. You do not have to work them out: `auth generate` adds them to your contract (see below).
|
|
110
110
|
|
|
111
111
|
Then emit the contract and update your database:
|
|
112
112
|
|
|
@@ -153,16 +153,31 @@ Pass the client itself, not a transaction, to `prismaAdapter`, as in the example
|
|
|
153
153
|
|
|
154
154
|
## When the contract is missing something
|
|
155
155
|
|
|
156
|
-
The adapter compares your contract with what your Better Auth configuration needs. When
|
|
156
|
+
The adapter compares your contract with what your Better Auth configuration needs. When your contract is written in PSL and lacks only models or fields, `auth generate` adds them. For example, after adding Better Auth's two-factor plugin to the configuration above:
|
|
157
157
|
|
|
158
|
-
|
|
158
|
+
```console
|
|
159
|
+
$ npx auth@latest generate
|
|
160
|
+
Changes to prisma/contract.prisma: 2 fields added to User, model TwoFactor added. After the file is written, run `prisma contract emit` and update your database.
|
|
161
|
+
✔ The file /home/me/my-app/prisma/contract.prisma already exists. Do you want to overwrite the schema to the file? … yes
|
|
162
|
+
🚀 Schema was overwritten successfully!
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Better Auth asks before it writes the file, and `--yes` answers for you. Its question names the file by its full path. The new fields go after the last field of the model they belong to, before its `@@` lines, indented like that field. The new models go at the end of the file, indented like your other models, and inside `namespace <name> { ... }` when you set the adapter's `namespace` option. Every line you wrote stays as it was. When Better Auth asks for a foreign-key action the printed PSL cannot use, a `Note:` line after the first line says what is written instead. Then run `prisma contract emit` and `prisma db update`. After that, `auth generate` prints `Your schema is already up to date.` and `auth check` prints `Schema check passed against adapter "prisma-8".`, both with exit code 0.
|
|
166
|
+
|
|
167
|
+
`auth generate` finds the contract files through `prisma.config.ts` in the directory the command runs in, and skips any file there that does not start with `// use prisma-8`, as `prisma contract emit` does. To name the file yourself, pass it with `--output`, for example `npx auth@latest generate --output prisma/contract.prisma`; the file must be one of your contract's files. With Better Auth's `--cwd` option, always pass `--output`: Better Auth does not tell the adapter the directory `--cwd` names, so the adapter would look for `prisma.config.ts` in the directory you run the command in. A contract split over several files works when everything to add belongs in one of them.
|
|
168
|
+
|
|
169
|
+
### When nothing is written
|
|
170
|
+
|
|
171
|
+
In every other case the adapter tells you what to add or change, and writes nothing. You see the same text in three places:
|
|
172
|
+
|
|
173
|
+
- `auth generate` prints it and exits with code 1.
|
|
159
174
|
- `auth check` prints it and exits with code 1.
|
|
160
175
|
- Every request your application serves, and every server-side Better Auth call it makes, fails with it until the contract is fixed.
|
|
161
176
|
|
|
162
|
-
For example,
|
|
177
|
+
For example, `auth check` with the two-factor plugin, before `auth generate` has added its models:
|
|
163
178
|
|
|
164
179
|
```text
|
|
165
|
-
$ npx auth@latest
|
|
180
|
+
$ npx auth@latest check
|
|
166
181
|
Your Prisma contract is missing models or fields that Better Auth needs.
|
|
167
182
|
|
|
168
183
|
Add to your contract:
|
|
@@ -190,14 +205,29 @@ model TwoFactor {
|
|
|
190
205
|
Then run `prisma contract emit` and update your database.
|
|
191
206
|
```
|
|
192
207
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
208
|
+
The message can also list fields whose type is wrong; optional Better Auth fields that your contract makes required, which you make optional; and fields your contract requires that Better Auth never writes, which you make optional or give a default. `auth generate` does not make these changes. Make them, run `prisma contract emit`, and run `auth generate` again for what is left to add.
|
|
209
|
+
|
|
210
|
+
`auth generate` also writes nothing, and adds a line to the message that says why, when:
|
|
211
|
+
|
|
212
|
+
- the contract is written in TypeScript. The message is in PSL: add the equivalent models and fields in TypeScript.
|
|
213
|
+
- the contract is read from a Prisma 7 schema. The message is in Prisma 8 syntax: add the equivalent lines by hand.
|
|
214
|
+
- there is neither `--output` nor a `prisma.config.ts` that names the contract, `prisma.config.ts` does not load, or Prisma's config loader is not installed.
|
|
215
|
+
- the file `--output` names does not exist or cannot be read.
|
|
216
|
+
- `--output` names a directory. Better Auth then passes the adapter a file named `auth-schema.ts` in that directory, so `--output` must name the contract file itself. The adapter treats any `--output` file named `auth-schema.ts` this way, whether or not it exists.
|
|
217
|
+
- a contract file does not parse, the file `--output` names does not start with `// use prisma-8`, or none of the files `prisma.config.ts` names does.
|
|
218
|
+
- the contract inputs `prisma.config.ts` names match no file.
|
|
219
|
+
- a model it would add fields to is declared in none of the contract files, or in more than one.
|
|
220
|
+
- the additions belong in more than one file, because Better Auth writes one file.
|
|
221
|
+
- a model has no field named `id` (see [Limits](#limits)).
|
|
222
|
+
- a model it would add fields to is written on one line, with its `}` on the line of its last field.
|
|
223
|
+
- the contract file already declares a model or field it would add, under the name Better Auth looks it up by. This happens when you edit the contract and do not run `prisma contract emit`: run it, then `auth generate` again.
|
|
224
|
+
- the contract file stores a model in the table, or a field in the column, that Better Auth's model or field needs, under another name. Emitting does not help, because the adapter finds models and fields by name, not by table or column. The message names the model or field Better Auth looks for: rename yours to that name, then run `prisma contract emit`. Better Auth's own naming options are the other way to make the names agree (see the model and field names under [Options](#options)). On SQLite, table and column names that differ only in case count as the same.
|
|
225
|
+
- the contract file declares something else, such as an enum, a composite type or a mixin, with the name of a model it would add. Rename that declaration and run `auth generate` again.
|
|
226
|
+
- Prisma would not accept the edited file although it accepted the original, for example because of a duplicate declaration the other checks did not foresee. The line names Prisma's first new error.
|
|
196
227
|
|
|
197
228
|
Some things to know:
|
|
198
229
|
|
|
199
|
-
- The commands read the contract through your application's client, so they compare against the contract you last emitted with `prisma contract emit`.
|
|
200
|
-
- A contract written in TypeScript gets the same message, in PSL. Add the equivalent models and fields in TypeScript.
|
|
230
|
+
- The commands read the contract through your application's client, so they compare against the contract you last emitted with `prisma contract emit`. `auth generate` then edits the contract's source file.
|
|
201
231
|
- Setting Better Auth's `advanced.database.validateSchema` to `false` turns off the check on requests. It does not make an incomplete contract work: a call that uses a field your contract lacks then fails with Prisma's own error, `ORM.FIELD_UNKNOWN`.
|
|
202
232
|
|
|
203
233
|
## Options
|
|
@@ -206,12 +236,12 @@ Some things to know:
|
|
|
206
236
|
|
|
207
237
|
| Option | Type | Default | What it does |
|
|
208
238
|
| --- | --- | --- | --- |
|
|
209
|
-
| `namespace` | `string` | none | The Postgres schema that holds Better Auth's models, for example `auth`. You need it only when a model name exists in more than one schema. Models `auth generate` prints then go inside `namespace <name> { ... }`. |
|
|
239
|
+
| `namespace` | `string` | none | The Postgres schema that holds Better Auth's models, for example `auth`. You need it only when a model name exists in more than one schema. Models `auth generate` adds or prints then go inside `namespace <name> { ... }`. |
|
|
210
240
|
| `usePlural` | `boolean` | `false` | Better Auth's model names get an `s`: `users`, `sessions`, and so on. |
|
|
211
241
|
| `transaction` | `boolean` | `false` | When `true`, Better Auth's multi-step operations run in a database transaction. |
|
|
212
242
|
| `debugLogs` | Better Auth's debug log option | none | Logs each call the adapter makes. |
|
|
213
243
|
|
|
214
|
-
Model and field names: the adapter matches Better Auth's model names to your contract's model names, either exactly (`user`) or with the first letter upper-cased (`User`), and Better Auth's field names to your field names exactly. Table and column names come from `@@map` and `@map` in your contract. Rename a model or field on Better Auth's side with its `modelName` and `fields` options
|
|
244
|
+
Model and field names: the adapter matches Better Auth's model names to your contract's model names, either exactly (`user`) or with the first letter upper-cased (`User`), and Better Auth's field names to your field names exactly. Table and column names come from `@@map` and `@map` in your contract. Rename a model or field on Better Auth's side with its `modelName` and `fields` options, for example `user: { modelName: 'person', fields: { email: 'emailAddress' } }`. A plugin's models and the fields it adds are renamed through the plugin's `schema` option instead, for example `twoFactor({ schema: { twoFactor: { modelName: 'mfa' }, user: { fields: { twoFactorEnabled: 'mfaOn' } } } })`. A name must be letters, digits and `_`, and must not start with a digit. Two Better Auth model names must not differ only in the case of the first letter (`user` and `User`); the adapter does not support that configuration and refuses it. Set a different `modelName` on all but one of them.
|
|
215
245
|
|
|
216
246
|
## Limits
|
|
217
247
|
|
|
@@ -231,18 +261,18 @@ Model and field names: the adapter matches Better Auth's model names to your con
|
|
|
231
261
|
- **Numbers.** The printed PSL declares Better Auth's number fields as `Int`, which on Postgres holds whole numbers up to about two billion. A number field that holds fractions or larger values, such as an interval in milliseconds, should be declared `Float` or `BigInt(as: number)` instead; both are accepted.
|
|
232
262
|
- **Optional fields must be optional.** Every field Better Auth declares optional must be optional (`?`) in your contract, as the printed PSL writes it, because Better Auth can write `null` to it. A default does not help, whether it is a database default or Better Auth's own, since a default replaces only a missing value and never an explicit `null`. The contract check reports an optional field that is not optional.
|
|
233
263
|
- **Ids.** With `generateId: false`, declare the id with a default, as the printed PSL does (`id String @id @onCreate(uuid())`). A `generateId` function that returns `false` for some models needs the same for those models' ids.
|
|
234
|
-
- **A primary key that is not named `id`.** Better Auth needs a field named `id`. When a model's primary key has another name and the model has no `id` field, the printed PSL adds `id String @id`, which
|
|
264
|
+
- **A primary key that is not named `id`.** Better Auth needs a field named `id`. When a model's primary key has another name and the model has no `id` field, the printed PSL adds `id String @id`, which would give the model two `@id` fields and does not emit, so `auth generate` does not write it. Add the line yourself, with `@unique` in place of `@id`.
|
|
235
265
|
|
|
236
266
|
## Responsibilities
|
|
237
267
|
|
|
238
268
|
- Translate Better Auth's database calls into calls on your Prisma 8 client.
|
|
239
269
|
- Compare your contract with the models and fields Better Auth needs, and say what to add or change, through `auth generate`, `auth check` and the check on requests.
|
|
240
|
-
-
|
|
270
|
+
- Add missing models and fields to a PSL contract through `auth generate`, and print the PSL to add when it cannot.
|
|
241
271
|
|
|
242
272
|
## Dependencies
|
|
243
273
|
|
|
244
|
-
- `@prisma/orm-framework` and `@prisma/orm-
|
|
245
|
-
- `@better-auth/core` 1.7.
|
|
274
|
+
- `@prisma/orm-framework`, `@prisma/orm-family-sql` and `@prisma/orm-toolchain`, as peers at the same version as your database package, which already depends on all three. The adapter loads `@prisma/orm-toolchain` only while `auth generate` runs, to read `prisma.config.ts`. Reading it also needs `@prisma/cli-engine`, which your `prisma.config.ts` already imports.
|
|
275
|
+
- `@better-auth/core` 1.7.22 or a later 1.7 release, below 1.8, as a peer.
|
|
246
276
|
|
|
247
277
|
The package imports only published entrypoints, so it works outside the Prisma repository. [DEVELOPING.md](DEVELOPING.md) has notes for people who change it.
|
|
248
278
|
|