Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adaptor with custom TypeORM model doing custom CRUD will run into EntityMetadataNotFoundError "No metadata for "User" was found", in a serverless setting #780

Closed
2 of 5 tasks
archywillhe opened this issue Oct 16, 2020 · 8 comments
Labels
bug Something isn't working help-needed The maintainer needs help due to time constraint/missing knowledge question Ask how to do something or how something works stale Did not receive any activity for 60 days

Comments

@archywillhe
Copy link

archywillhe commented Oct 16, 2020

Background

This is working very well locally as well as on Heroku too. It just fails to work in a serverless setting such as Vercel. So quite a curious thing.

What I'm doing

I'm doing some customsiation on top of Next Auth where I take Next Auth's typeORM adaptor out and uses it in another API to do custom CRUD for my user table.

Basically I have this:

export const adapterSingleton = ():any=>{
  return Adapters.TypeORM.Adapter(
     process.env.NEXT_ACCOUNT_DATABASE_URL,
     {
       models: Models,
     }
    )
}

and I will take it out like this const adapter = await adapterSingleton().getAdapter() and uses it.

But after deloying into a serverless setting (Vercel) I keep getting EntityMetadataNotFoundError when I'm trying to do custom CRUD. Somehow it can't derive the entity for User?

Questions

  1. Does anyone know what's going on? Can it be related to this: { EntityMetadataNotFound: No metadata for "User" was found. typeorm/typeorm#1327 ? Will using an ormconfig.yml and doing something like
entities: [__dirname + '/**/*.entity.{js,ts}'

fixes it? How do I use ormconfig.yml in this case?

  1. What can I do in order to be able to do custom CRUD using NextAuth's Adaptor without running into this error in a serverless setting? (Was working so well locally!)

  2. Or should I give up trying to get this work in TypeORM cuz TypeORM is not really designed to work in this scenaior if it is serverless?

Any tips on this will be appreicated! Thanks!

What are you trying to do
custom CRUD

Feedback
Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.

  • Found the documentation helpful
  • Found documentation but was incomplete
  • Could not find relevant documentation
  • Found the example project helpful
  • Did not find the example project helpful
@archywillhe archywillhe added the question Ask how to do something or how something works label Oct 16, 2020
@archywillhe
Copy link
Author

archywillhe commented Oct 16, 2020

One solution I tried is to copy adapters/typeorm/index.js into my project with the corresponding dependecines, and plug it into Next Auth instead of pluggin in Adapters.TypeORM.Adapter, while modifying its models directly i.e. using a cloned version of Adapters.TypeORM.Adapter to boostrap a custom Adapter, instead of using Adapters.TypeORM.Adapter & passing a custom Model in options .

And I will run into this error instead: QueryFailedError: relation "user__users" does not exist" i.e. the exact same error as #710

Also note: these errors only show up in a serverless setting. If I'm doing it locally everything works perfecto!

@archywillhe archywillhe changed the title Adaptor with custom TypeORM model doing custom CURD will run into EntityMetadataNotFoundError "No metadata for "User" was found", in a serverless setting Adaptor with custom TypeORM model doing custom CRUD will run into EntityMetadataNotFoundError "No metadata for "User" was found", in a serverless setting Oct 16, 2020
@archywillhe
Copy link
Author

archywillhe commented Oct 17, 2020

I've also opened a discussion of this here: vercel/vercel#5299

I think I know what's the main problem now. Probably something to do with the way Entity metadata is derived in TypeORM (which appers to be related to the file mames? Or something along that line. Haven't got the time to do a deep dive into TypeORM yet to confirm that.)

Found a highly relevant issue opened last year in TypeORM: code minify causes "Entity metadata for e#activities was not found" typeorm/typeorm#4714

So my bet is that the vercel build process for serverless functions somehow messes up the name of custom models used in this manner and the models are safe iff the code & filenames have been prebuilt into a node_module like in NextAuth.

My final solution is to clone & use NextAuth as a local dependency and have my custom models inside the next auth package. And that acutally gets rid of the errors in on Vercel = )

@iaincollins iaincollins added the bug Something isn't working label Oct 27, 2020
@iaincollins
Copy link
Member

@archywillhe Thanks for the insights and work around you have posted!

I have not been able to replicate this issue but I've tagged it with a bug so we are tracking it.

Feedback, even anecdotal, from anyone else seeing or not seeing this bug is appreciated.

@iaincollins iaincollins added the help-needed The maintainer needs help due to time constraint/missing knowledge label Oct 27, 2020
@stale
Copy link

stale bot commented Dec 26, 2020

Hi there! It looks like this issue hasn't had any activity for a while. It will be closed if no further activity occurs. If you think your issue is still relevant, feel free to comment on it to keep it open. (Read more at #912) Thanks!

@stale stale bot added the stale Did not receive any activity for 60 days label Dec 26, 2020
@stale
Copy link

stale bot commented Jan 2, 2021

Hi there! It looks like this issue hasn't had any activity for a while. To keep things tidy, I am going to close this issue for now. If you think your issue is still relevant, just leave a comment and I will reopen it. (Read more at #912) Thanks!

@stale stale bot closed this as completed Jan 2, 2021
@alex-munayyer
Copy link

@iaincollins Quick question.

I've gone through all the documentation and the threads in here. I have only seen CRUD operations mentioned in an old version of next auth here: https://www.npmjs.com/package/next-auth/v/1.1.3 with a file called next-auth.functions.js.

What is the best way to do CRUD operations with say a MongoDB connection with Next Auth? Are there still built-in functions to do this? I have been unable to figure this last part out to deploy my application.

@followbl
Copy link

@iaincollins just bumping this here - am getting ready to release our conversion to next-auth from passport-js and am thrilled with how it's working and how easy it has been to integrate with what we have. Am running in to this exact bug mentioned above here...

QueryFailedError: relation "user__users" does not exist

gonna look in to it and will report back. we are on vercel serverless as well.

@dsmabulage
Copy link

Can Import entities directly to the data source options.

import { FamilyEntity } from '../family/entities/family.entity';
import { PatientEntity } from '../patient/entities/patient.entity';

entities: [PatientEntity, FamilyEntity]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help-needed The maintainer needs help due to time constraint/missing knowledge question Ask how to do something or how something works stale Did not receive any activity for 60 days
Projects
None yet
Development

No branches or pull requests

5 participants