Search code examples
next.jsrepositoryadaptertypeormnext-auth

NextAuth and TypeOrm - Retrieve a repository in an api endpoint


I've got an NextJS app with NextAuth and TypeORM. I'm looking for the possibility to retrieve a specific user or custom entity in an api endpoint. However, i don't know how to do this properly. I don't want to re-initialize the connection options on every api for TypeORM and would like to use the connection set by the adapter. (idea of a singleton).

Someone has any idea on how to achieve that ?

Have a nice day !


Solution

  • I don’t know if I got your question, but I think the best way of doing it is by creating your Entities, connecting via TypeORM with synchronize option true. It will create and update all your tables and columns from your defined model.

    Then, you can create another connection to use in your api routes. I don’t think Vercel can deploy it with a single connection without a custom implementation server implementation.

    This guy made a solution that you can try

    https://github.com/vercel/next.js/discussions/12254#discussioncomment-19769

    This is another approach to the same function

    https://github.com/typeorm/typeorm/issues/6241#issuecomment-643690383