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

ts-node throwing RepositoryNotFoundError #682

Closed
zackarychapple opened this Issue Jul 18, 2017 · 12 comments

Comments

Projects
None yet
6 participants
@zackarychapple

zackarychapple commented Jul 18, 2017

Here is the example repo. https://github.com/zackarychapple/flagger

When I am starting the server with nodemon everything initializes correctly however when seed tries to run it bombs out with the below error. I tried several paths for mapping to my entities however it seems none of them pick up. Any thoughts?

(node:99829) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): RepositoryNotFoundError: No repository for "FlagsEntity" was found. Looks like this entity is not registered in current "default" connection?
(node:99829) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
@priestine

This comment has been minimized.

priestine commented Jul 19, 2017

entities: [
        // any entity file under src/modules
        __dirname + '/*.entity.ts'
],

I can't find any *.entity.ts in the folder where this file (flags.database.config.ts) exists. It cannot grab your entities if the files they live in do not match the pattern you've specified.

@pleerock

This comment has been minimized.

Member

pleerock commented Jul 19, 2017

first, you don't need TypeOrmDatabaseService extra wrapper - typeorm already has everything you do and you just create extra redundant layer. Second, better if you'll use single ormconfig.json file and store your connection settings there. Third, create connection in your app bootstrap file, in a place like server.ts.

And regarding your problem - as @apologetics told you the problem in __dirname + '/*.entity.ts' - it does not point to any file, make sure your path are really point to entities.

@zackarychapple

This comment has been minimized.

zackarychapple commented Jul 19, 2017

@apologetics you're right. The globber was not matching what I expected. __dirname + '/../*/*.entity.ts' is what got it to work for me. Thank you for your help.

@zackarychapple

This comment has been minimized.

zackarychapple commented Jul 19, 2017

@pleerock thank you for the notes, I will go through and simplify per your instructions.

@chriszrc

This comment has been minimized.

chriszrc commented Sep 21, 2017

I also had the same No repository for ... in current "default" connection and it worked fine for development (running with nodemon), but failed after tsc compilation running under node. I had to change:

entities: [
          __dirname + '/../**/**.entity.ts'
 ],

to:

entities: [
          __dirname + '/../**/**.entity{.ts,.js}'
 ],

I see a reference to this in the typeorm docs, is this the prescribed way for dealing with entities in typescript? (https://typeorm.github.io/connection.html#creating-connection)

@pleerock

This comment has been minimized.

Member

pleerock commented Sep 22, 2017

no, it depends how you run your app. If you are running via ts-node then you can use ".ts" files as entities. If you aren't using ts-node then you must specify ".js" files. And you need to make sure to specify proper output directory (if you are using outDir tsc option) to ensure paths to your generated js files are correct.

@chriszrc

This comment has been minimized.

chriszrc commented Sep 22, 2017

Ok, thanks for clarifying, yes I use ts-node, but only during development (hence it worked running locally with nodemon). As a new typeorm user, this wasn't immediately evident to me from the docs, but I just started using ts-node, so maybe it's just a noob issue-

@priestine

This comment has been minimized.

priestine commented Oct 3, 2017

@chriszrc Prove me wrong but I think it worked when I totally omitted the file extension and simply wrote

entities: [
    `${__dirname}/**/*.entity`,
]
@FriOne

This comment has been minimized.

FriOne commented Feb 20, 2018

@pleerock could you explain how to make the same with .env config? I mean the __dirname

@pleerock

This comment has been minimized.

Member

pleerock commented Feb 20, 2018

its not possible. With .env it read files from your app root (where you have package.json), otherwise you must use absolute path (e.g. /etc/user/....)

@priestine

This comment has been minimized.

priestine commented Feb 21, 2018

@FriOne IMHO, it's incorrect to put path to the files containing entity classes in an environment variable. It's not the kind of data that requires secrecy or changing from machine to machine. The other side of the medal is that your configuration object for database connection DOES have sensitive apart from this. What I would do is put the database connection config object right in the code without outsourcing it to JSON or whatever, and only mark sensitive fields to be environmental. To me, this would give you the most in terms of control and freedom. Something like:

createConnection({
  user: process.env.DB_USER || 'root',
  password: process.env.DB_PASSWORD || '12345',
  entities: [
    `${__dirname}/**/*.entity`,
  ],
  // ...
}).then((c) => {
  // ...
});

NOTE: If you use Docker and official images, you can use straightforward env variables adopted by the owner of the DB image, like MYSQL_DATABASE_NAME.

@ewrfedf

This comment has been minimized.

ewrfedf commented Oct 26, 2018

just import and use your Entity in the bootstrap ts file
like
console.log("Koa application is "+Entity.toString());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment