tonivj5 / typeorm-naming-strategies

Custom naming strategies for typeorm
MIT License
207 stars 28 forks source link

TypeError: namingStrategy.tableName is not a function #8

Open PaulMest opened 4 years ago

PaulMest commented 4 years ago

If I upgrade TypeORM from 0.2.22 to 0.2.26 or 0.2.27 I get the following error:

[Nest] 8223   - 09/29/2020, 10:55:02 AM   [ExceptionHandler] Unable to connect to the database. Retrying (1)... +2531ms
TypeError: namingStrategy.tableName is not a function
    at EntityMetadata.build (/Users/paul/code/my-project/node_modules/typeorm/metadata/EntityMetadata.js:492:58)
    at /Users/paul/code/my-project/node_modules/typeorm/metadata-builder/EntityMetadataBuilder.js:67:72
    at Array.forEach (<anonymous>)
    at EntityMetadataBuilder.build (/Users/paul/code/my-project/node_modules/typeorm/metadata-builder/EntityMetadataBuilder.js:67:14)
    at ConnectionMetadataBuilder.buildEntityMetadatas (/Users/paul/code/my-project/node_modules/typeorm/connection/ConnectionMetadataBuilder.js:58:141)
    at Connection.buildMetadatas (/Users/paul/code/my-project/node_modules/typeorm/connection/Connection.js:515:57)
    at Connection.<anonymous> (/Users/paul/code/my-project/node_modules/typeorm/connection/Connection.js:128:30)
    at step (/Users/paul/code/my-project/node_modules/tslib/tslib.js:141:27)
    at Object.next (/Users/paul/code/my-project/node_modules/tslib/tslib.js:122:57)
    at fulfilled (/Users/paul/code/my-project/node_modules/tslib/tslib.js:112:62)

... any idea what could be the problem?

tonivj5 commented 4 years ago

Hey, could you provide a reproduction repo?

I don't see where the error can come. tableName is defined in the strategy https://github.com/tonivj5/typeorm-naming-strategies/blob/master/src/snake-naming.strategy.ts#L10 and the strategy is setted into the connection https://github.com/typeorm/typeorm/blob/master/src/connection/Connection.ts#L130

alii commented 3 years ago

@PaulMest If you haven't resolved this, it also might be worth checking that you are emitting to es6+ with tsc. es5 and below has quirks with TypeScript and classes annoyingly. Has solved issues not too dissimilar before.

ghdoergeloh commented 3 years ago

I have the same issue. At first I extended this class (we wanted to have the names in plural) and added the class to my project. everything worked fine.

But then I extracted the class into another package (symlinked via npm install ../path) and ran into this issue.

I thought it myght be a problem with my implementation or typescript configuration, so I downloaded the code and tried it with the repo-content. Still the same problem.

Then I tried to use the package tar from npmjs.com. First I put it next to my project and installed it with npm install ../typeorm-naming-strategies

This created a symlink in test/node_modules.

When I tried to run my code the first time, it came up with this error:

Type 'SnakeNamingStrategy' is missing the following properties from type 'NamingStrategyInterface': closureJunctionTableName, primaryKeyName, uniqueConstraintName, relationConstraintName, and 10 more.

Then I installed the dependencies in typeorm-naming-strategies

The next Error was this:

TypeError: tableOrName.split is not a function
    at SnakeNamingStrategy.DefaultNamingStrategy.getTableName (./typeorm-naming-strategies/node_modules/typeorm/naming-strategy/DefaultNamingStrategy.js:20:28)
    at SnakeNamingStrategy.DefaultNamingStrategy.primaryKeyName (./typeorm-naming-strategies/node_modules/typeorm/naming-strategy/DefaultNamingStrategy.js:52:30)
    at PostgresQueryRunner.createTableSql (./test/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:2754:65)
    at PostgresQueryRunner.<anonymous> (./test/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:602:45)

When i deleted the symlink from node_modules and placed a copy of the tar content in node_modules everything worked fine again.

So I think it has something to do with symlinks and typescript and not really with this package, but if you have an idea why this issue occures, it would be very helpful. I think typescript needs to handle the dependencies relative to the folder, where the symlink is placed and not to where the original folder is, but I don't know how to tell typescript this.