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
CLI does not support async DataSource #8914
Comments
If you are using ESM modules, try |
Unfortunately, I am using |
@Ginden I think the solution could be here smth like that: for (let fileExport in dataSourceFileExports) {
const unpromisifiedExport = dataSourceFileExports[fileExport] instanceof Promise
? await dataSourceFileExports[fileExport]
: dataSourceFileExports[fileExport];
if (
InstanceChecker.isDataSource(unpromisifiedExport)
) {
dataSourceExports.push(unpromisifiedExport)
}
} What do you think? |
Yes, it makes sense. Though, you don't need to make const dataSourceExports = []
for (let fileExport in dataSourceFileExports) {
if (
InstanceChecker.isDataSource(dataSourceFileExports[fileExport])
) {
dataSourceExports.push(dataSourceFileExports[fileExport])
}
} with: const dataSourceExports = []
for (let fileExport in dataSourceFileExports) {
const fileExportValue = await dataSourceFileExports[fileExport];
if (
InstanceChecker.isDataSource(fileExportValue)
) {
dataSourceExports.push(fileExportValue)
}
} Please, feel free to open PR for that. @pavlovanton |
@Ginden ^ Check pls |
A quick and very dirty hack which can be used until this gets resolved: import { DataSource, DataSourceOptions, InstanceChecker } from 'typeorm';
import * as dotenv from 'dotenv';
dotenv.config();
function patchAsyncDataSourceSetup() {
const oldIsDataSource = InstanceChecker.isDataSource;
InstanceChecker.isDataSource = function(obj: unknown): obj is DataSource {
if (obj instanceof Promise) {
return true;
}
return oldIsDataSource(obj);
}
}
patchAsyncDataSourceSetup();
const buildDataSource = async () => { ... }
export default buildDataSource(); |
…#8917) * Fix: await DataSource from export file to support async loading * fix: prettier errors * updated code style Co-authored-by: Umed Khudoiberdiev <pleerock.me@gmail.com>
…#8917) * Fix: await DataSource from export file to support async loading * fix: prettier errors * updated code style Co-authored-by: Umed Khudoiberdiev <pleerock.me@gmail.com>
Any news on async DataSource? |
@DanielMaranhao Yes, it was fixed 0.3.8. |
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 const buildDataSource = async () => {
await ConfigModule.envVariablesLoaded;
return new DataSource({
type: 'postgres',
url: process.env.DATABASE_URL,
entities: ['dist/domain/**/*.entity.js'],
migrations: ['dist/database/migrations/*.js'],
});
};
export default buildDataSource(); When I execute a migration generation, it stops giving an error. However, it appears to simply run and do nothing. PS C:\Workspaces\ws-nest\nest-lecture-project> yarn migration:generate src/database/migrations/create-user
yarn run v1.22.17
$ yarn typeorm migration:generate -d dist/database/data-source -p src/database/migrations/create-user
$ C:\Workspaces\ws-nest\nest-lecture-project\node_modules\.bin\typeorm migration:generate -d dist/database/data-source -p src/database/migrations/create-user
Done in 1.89s. 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. |
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. import * as dotenv from 'dotenv';
import * as dotenvExpand from 'dotenv-expand';
import { DataSource } from 'typeorm';
dotenvExpand.expand(dotenv.config());
export default new DataSource({
type: 'postgres',
url: process.env.DATABASE_URL,
entities: ['dist/domain/**/*.entity.js'],
migrations: ['dist/database/migrations/*.js'],
}); 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". |
@DanielMaranhao Thank you very much, my hero !! Very nice of you to come back and give the answer that worked for you |
Why doesn't the TypeORM CLI use the datasource definition that's already in Nest? |
You can create a ormconfig.ts file like this that will allow that:
|
@mattzuba Do you happen to know how to get this working with a
I have this script at
|
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 failsExpected 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:
This works though:
My Environment
Relevant Database Driver(s)
I tested with
mysql
only but I do not think it is a driver problem at allaurora-mysql
aurora-postgres
better-sqlite3
cockroachdb
cordova
expo
mongodb
mysql
nativescript
oracle
postgres
react-native
sap
spanner
sqlite
sqlite-abstract
sqljs
sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
The text was updated successfully, but these errors were encountered: