typeorm: Class constructor BaseEntity cannot be invoked without 'new'
Issue Description
Entities extending BaseEntity cause TypeORM to throw when calling DataSource.initialize
Expected Behavior
The database is initialized without issue.
Actual Behavior
TypeORM throws:
TypeError: Class constructor BaseEntity cannot be invoked without 'new'
at new YoutubeFeed (/Users/dustinlacewell/ext/yt-playground/src/services/FeedService/models/YoutubeFeed.ts:33:42)
at EntityMetadata.create (/Users/dustinlacewell/ext/yt-playground/src/metadata/EntityMetadata.ts:563:23)
at EntityMetadataValidator.validate (/Users/dustinlacewell/ext/yt-playground/src/metadata-builder/EntityMetadataValidator.ts:172:47)
at /Users/dustinlacewell/ext/yt-playground/src/metadata-builder/EntityMetadataValidator.ts:43:18
at Array.forEach (<anonymous>)
at EntityMetadataValidator.validateMany (/Users/dustinlacewell/ext/yt-playground/src/metadata-builder/EntityMetadataValidator.ts:42:25)
at DataSource.buildMetadatas (/Users/dustinlacewell/ext/yt-playground/src/data-source/DataSource.ts:682:33)
at DataSource.initialize (/Users/dustinlacewell/ext/yt-playground/src/data-source/DataSource.ts:241:13)
Steps to Reproduce
- Use
DataSource - Add entities extending
BaseEntity - Call
.initializeon theDataSourceinstance
let db = new DataSource({
type: "sqlite",
synchronize: true,
database: "test.sqlite",
entities: [ YoutubeFeed, YoutubeChannel]
})
db = await db.initialize()
Entities:
@Entity()
export class YoutubeFeed extends BaseEntity {
@PrimaryGeneratedColumn()
id!: number
@Column()
guildId!: string
@Column()
channelId!: string
@ManyToOne(to => YoutubeChannel, info => info.feeds)
info!: YoutubeChannel
}
@Entity()
@Unique(["id"])
export class YoutubeChannel extends BaseEntity {
@PrimaryColumn()
id!: string
@Column()
name!: string
@Column()
thumbnail!: string
@Column()
description!: string
@OneToMany(to => YoutubeFeed, feed => feed.info)
feeds!: YoutubeFeed[]
}
```
### My Environment
<!--
Please add any other relevant dependencies to this table at the end.
For example: Electron, React Native, or NestJS.
-->
| Dependency | Version |
| --- | --- |
| Operating System | OSX |
| Node.js version | v18.3.0 | <!-- run `node -v` to obtain this -->
| Typescript version | 4.7.4 | <!-- run `npm list typescript` to obtain this -->
| TypeORM version | 0.3.6 | <!-- run `npm list typeorm` to obtain this -->
### Relevant Database Driver(s)
<!-- Put "yes" instead of "no" to your issue's relevant databases -->
| DB Type | Reproducible |
|-------------------| --- |
| `aurora-mysql` | no |
| `aurora-postgres` | no |
| `better-sqlite3` | no |
| `cockroachdb` | no |
| `cordova` | no |
| `expo` | no |
| `mongodb` | no |
| `mysql` | no |
| `nativescript` | no |
| `oracle` | no |
| `postgres` | no |
| `react-native` | no |
| `sap` | no |
| `spanner` | no |
| `sqlite` | yes |
| `sqlite-abstract` | no |
| `sqljs` | no |
| `sqlserver` | no |
### Are you willing to resolve this issue by submitting a Pull Request?
<!-- Put "✅" (:white_check_mark:) to one of these options, left "✖️" (:heavy_multiplication_x:) others: -->
- ✖️ Yes, I have the time, and I know how to start.
- ✅ Yes, I have the time, but I don't know how to start. I would need guidance.
- ✖️ No, I don’t have the time, but I can support (using donations) development.
- ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
<!--
Remember that first-time contributors are welcome! 🙌
👋 Have a great day and thank you for the bug report!
-->
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 3
- Comments: 16
If you are using typescript make sure to have
compilerOptions.targetises6or neweron
tsconfig.jsonSetting compilation target to ES6 does nothing for me, any help? For those who managed to make it work, what’s your DataSource configuration? Are you using .ts files or the compiled .js files?
same issue, Data-Mapper mode would be a better choice!
Same issue, but on postgresql
This also happens when you do not register the entity class with the
TypeOrmModule, something like this we do in Nest.js module:@metanas love you brother
thanks saved me can you tell me config file for build also that select entity from build file on live server
@alumni Thanks, man. 🥇 💯 Here we created the entity class but we did not add to the datasource initialization.
This issue does not have enough information to figure out what’s wrong (there’s no stack trace from typeorm, just from the author’s own project).
However, there is one of the similar issue (#9692), where these lines throw: https://github.com/typeorm/typeorm/blob/master/src/query-builder/QueryBuilder.ts#L679-L681 because TypeORM expects a function at that point (not an entity). I think it does not have the metadata for the entity, so reaches that place. Maybe the checks could also be improved there.
Probably we need to look in the repo an check if there are others similar.
Probably you should make sure that TypeOrm is aware of the entity.
But yeah, TypeOrm is compiled to ES2021, which was actually released in 2020, already 5 years ago, and has pretty wide support. Using an older ES version in your app should still work if your runtime environment supports ES2021 (not ES5 if you are using classes in TypeOrm), but honestly since we require ES2021 anyway, you might as well pick that (or something newer) for your compilation target and benefit from the runtime environment optimizations for that language version 😉
This change worked for me. Thanks.
changed it from es5 to es6 and it worked