typeorm: CLI does not support async DataSource
Issue Description
Starting from the 0.3.0 version, CLI requires to specify the file with DataSource instance instead of ormconfig. When I am trying to export async created DataSource it fails
Expected Behavior
Command ran successfully
Actual Behavior
Command failed with error: Error: Given data source file must contain export of a DataSource instance
Steps to Reproduce
Have your DataSource defined in the file:
const buildDataSource = async () => {
// Provided config-like functionality just for example, it will fail anyway with just async function
const dbConfig = await config.get('database');
return new DataSource({
type: 'mysql',
host: dbConfig.host,
port: 3306,
username: dbConfig.username,
password: dbConfig.password,
database: dbConfig.database,
logging: true,
entities: typeormEntities,
migrations: [ './src/components/database/migrations/**/*.ts' ]
})
};
export default buildDataSource();
This works though:
const buildDataSource = () => {
return new DataSource({
type: 'mysql',
host: '127.0.0.1',
port: 3306,
username: 'test',
password: 'test',
database: 'test',
logging: true,
entities: typeormEntities,
migrations: [ './src/components/database/migrations/**/*.ts' ]
})
};
export default buildDataSource();
My Environment
| Dependency | Version |
|---|---|
| Operating System | Ubuntu 20.04.4 LTS |
| Node.js version | v16.13.0 |
| Typescript version | 4.6.3 |
| TypeORM version | 0.3.4 |
Relevant Database Driver(s)
I tested with mysql only but I do not think it is a driver problem at all
| DB Type | Reproducible |
|---|---|
aurora-mysql |
no |
aurora-postgres |
no |
better-sqlite3 |
no |
cockroachdb |
no |
cordova |
no |
expo |
no |
mongodb |
no |
mysql |
yes |
nativescript |
no |
oracle |
no |
postgres |
no |
react-native |
no |
sap |
no |
spanner |
no |
sqlite |
no |
sqlite-abstract |
no |
sqljs |
no |
sqlserver |
no |
Are you willing to resolve this issue by submitting a Pull Request?
- ✖️ 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.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 5
- Comments: 17 (7 by maintainers)
Links to this issue
Commits related to this issue
- fix: support async import for DataSource in CLI #8914 (#8917) * Fix: await DataSource from export file to support async loading * fix: prettier errors * updated code style Co-authored-by: Um... — committed to typeorm/typeorm by anton-pavlov-deel 4 years ago
- fix: support async import for DataSource in CLI #8914 (#8917) * Fix: await DataSource from export file to support async loading * fix: prettier errors * updated code style Co-authored-by: Um... — committed to wirekang/typeorm by anton-pavlov-deel 4 years ago
- fix: support async import for DataSource in CLI #8914 (#8917) * Fix: await DataSource from export file to support async loading * fix: prettier errors * updated code style Co-authored-by: Um... — committed to nordinh/typeorm by anton-pavlov-deel 4 years ago
A quick and very dirty hack which can be used until this gets resolved:
You can create a ormconfig.ts file like this that will allow that:
Hello again, sorry for the inconvenience. It was indeed a quite obvious solution. What I had to do was the following: even though I’m using Nest, this DataSource has no integration with it, and so this ConfigModule hook to await for the env to load would not work. I needed to install dotenv and dotenv expand manually, even though the ConfigModule already uses them under the hood. And so applying both of them manually, it finally worked. Even the factory became unnecessary.
I’m leaving this here just in case someone passes through a similar situation. Sorry once again due to the obvious question, and best wishes to all.
—CORRECTION—
It is not necessary to install dotenv and dotenv-expand again. You can just import them as described above, however autocompletion of the module won’t work, probably because it’s a “sub-dependency”.
Thanks @Ginden, but still I don’t understand exactly the proper way to setup the async DataSource, sorry if it’s something kind of obvious. Do you think you could help me?
I’m using TypeORM with NestJS, so I imagined this setup would work
When I execute a migration generation, it stops giving an error. However, it appears to simply run and do nothing.
Would you be able to tell if I am in the right track? I’m also thinking about trying to use a static class, to be able to leverage a configuration namespace and partial registration through Nest, though I’m not sure if it’s possible in this case.