You can also use NextAuth.js with the built-in Adapter for Prisma. This is included in the core
next-auth package at the moment. The other adapter needs to be installed from its own additional package.
You may have noticed there is a
prisma-legacy adapter. This is due to historical reasons, but the code has mostly converged so that there is no longer much difference between the two. The legacy adapter, however, does have the ability to rename tables which the newer version does not.
To use this Adapter, you need to install Prisma Client and Prisma CLI:
Configure your NextAuth.js to use the Prisma Adapter:
While Prisma includes an experimental feature in the migration command that is able to generate SQL from a schema, creating tables and columns using the provided SQL is currently recommended instead as SQL schemas automatically generated by Prisma may differ from the recommended schemas.
Schema for the Prisma Adapter
Create a schema file in
prisma/schema.prisma similar to this one:
Once you have saved your schema, use the Prisma CLI to generate the Prisma Client:
To configure you database to use the new schema (i.e. create tables and columns) use the
prisma migrate command:
To generate a schema in this way with the above example code, you will need to specify your database connection string in the environment variable
DATABASE_URL. You can do this by setting it in a
.env file at the root of your project.
As this feature is experimental in Prisma, it is behind a feature flag. You should check your database schema manually after using this option. See the Prisma documentation for information on how to use
If you experience issues with Prisma opening too many database connections in local development mode (e.g. due to Hot Module Reloading) you can use an approach like this when initalising the Prisma Client:
You can add properties to the schema and map them to any database column names you wish, but you should not change the base properties or types defined in the example schema.
The model names themselves can be changed with a configuration option, and the datasource can be changed to anything supported by Prisma.
You can use custom model names by using the
modelMapping option (shown here with default values).