Please refer to the documentation, the example project and existing issues before creating a new issue.
Your question
I am looking an integrating the latest version (v3) into my nextjs application. I have created a custom model and configured the connection options as follows:
import Providers from 'next-auth/providers'
import Adapters from "next-auth/adapters"
import Entity from 'entity'
const databaseUrl = 'postgres://postgres:password@localhost:5432/bss?synchronize=true'
export const connectionOptions = {
providers: [
Providers.Auth0({
domain: process.env.AUTH0_DOMAIN,
clientId: process.env.AUTH0_CLIENT_ID,
clientSecret: process.env.AUTH0_CLIENT_SECRET,
})
],
adapter: Adapters.TypeORM.Adapter(
databaseUrl,
{
models: {
User: Entity.User,
},
}
),
database: databaseUrl,
secret: process.env.SECRET,
}
This is imported and used in my [...nextauth].ts file. I am also using a ormconfig.json file with the following:
{
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "postgres",
"password": "password",
"database": "bss",
"synchronize": true,
"logging": true,
"entities": [
"src/entity/**/*.ts"
],
"migrations": [
"src/migration/**/*.ts"
],
"subscribers": [
"src/subscriber/**/*.ts"
],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
}
This works for as long as i dont make a change to the ts files. If i make a change when the db connection is utilised again the following error is thrown
RepositoryNotFoundError: No repository for "User" was found. Looks like this entity is not registered in current "default" connection?
After doing some googling it seem like other libraries have the same issue: https://github.com/nestjs/nest/issues/4283, https://github.com/vercel/next.js/discussions/12254#discussioncomment-7846, https://github.com/typeorm/typeorm/issues/3017. From what i understand its because im referencing the ts files. What is the appropriate way to set this up so that it works with HMR?
There is this solution but it looks like this might need to be implemented in this library itself?
https://github.com/typeorm/typeorm/issues/6241#issuecomment-643690383
What are you trying to do
Define a custom User model and utilise the v3 version of nextAuth.js inside my next.js application.
Documentation feedback
Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.
Here is a basic example repo. https://github.com/deep-c/next-auth-typeorm. Basically i get an EntityMetadataNotFound: No metadata for "User" was found error when the app in recompiled on any change, in my case i was editing the client index.tsx page.
Here is another take on the error
https://github.com/typeorm/typeorm/issues/5876
Looks like the appropriate fix is to basically either close and restart the connection or per this comment re-sync the connection https://github.com/typeorm/typeorm/issues/6241#issuecomment-643690383.
Hi @deep-c
Closing/restarting the connection means that some existing requests may be interrupted. For example, some request works with the connection, i.e. tries to retrieve entities from database or writes there, etc. And other parallel request is simply closing the connection. The best way鈥攊n my opinion鈥攊s to upgrade the connection with updated entities.
Hi @deep-c
Closing/restarting the connection means that some existing requests may be interrupted. For example, some request works with the connection, i.e. tries to retrieve entities from database or writes there, etc. And other parallel request is simply closing the connection. The best way鈥攊n my opinion鈥攊s to upgrade the connection with updated entities.
Thanks for that @plashenkov. Hi @iaincollins is this something deemed won't fix? As far as I can tell from doing extensive search, other frameworks such as nestjs have had to fix this on there end. While it would be nice if typeorm had a simple api to fix a problem like this it does not seem so. If @plashenkov example works is there any reason it couldn't be adapted to the typeorm adaptor? Currently this is the only blocker thus far stopping me from using this awesome library since the development flow is broken under typescript which is a must for me and the company I work for. 馃檪
I don't think there is going to be a fix for it with TypeORM any time soon.
I'm not against changes to the TypeORM adapter that would resolve issues for folks having problems like this, but it's probably going to have to come from the community as I think it's specific to certain projects configurations / use cases.
You could always try the Prisma Adapter instead and seeing if you have a better time with that.
Note: Recent v3 beta releases also have some bug fixes for the TypeORM adapter, so I'd give that a go first if you haven't tried a new version recently!
@iaincollins no worries, im intrigued at how this wouldnt be a problem for everyone using webpack / HMR / typescript (which is a very big use case for this lib). Prisma isnt a viable choice for us at the moment. Let me see what i can do and if its generic enough ill raise a PR or if @plashenkov already has something working for this library maybe you could share the changes you made?
As for the version, I am already using v3 :) but ill make sure its the latest.
Thanks
@plashenkov ive used your code from https://github.com/typeorm/typeorm/issues/6241#issuecomment-643690383 and created a fix here https://github.com/deep-c/next-auth/tree/development-hmr. Ive only done some basic testing so far but looks good to me. @iaincollins ill continue using this for now but if i dont see any issues do you see a glaring issue if i were to open a PR?
Thanks!
@deep-c Hey, no I don't have a problem with that and would welcome a PR!
We have v3 out the door now (so things are much more settled) and a bunch of refactoring has gone on with the TypeORM adapter since @plashenkov first raised this so I think we are in a much better place to be able to approach resolving this issue, which it sounds like is impacting multiple folks.
Just for context, I'm curious to know folks experiencing this issue using the built-in Next.js TypeScript support, or if they have their own webpack/babel configs. I'm also curious if anyone using TypeScript is NOT experiencing problems, and what their setup is like.
@iaincollins sounds like a good idea. ill open the pr just in case others would like to give input etc