'Migration from dataSource get the error: Cannot execute operation on "default" connection because connection is not yet established
My typeorm version is 0.3.6.
Whenever i tried to use dataSource.runMigration() i get this error,
MIGRATION ERROR:CannotExecuteNotConnectedError: Cannot execute operation on "default" connection because connection is not yet established.
i checked the url to migration files, it right. What am i missing?
data-source.ts
export const dataSource = new DataSource({
host:__prod__ ? process.env.HOST_PROD : process.env.HOST_DEV,
type: "postgres",
...(__prod__
? {
url: process.env.DATABASE_URL,
username: process.env.PG_USERNAME_PROD,
password: process.env.PG_PASSWORD_PROD,
database: process.env.DATABASE_NAME_PROD,
}
: {
username: process.env.PG_USERNAME_DEV,
password: process.env.PG_PASSWORD_DEV,
database: process.env.DATABASE_NAME_DEV,
}),
...(__prod__
? {
extra: {
ssl: {
rejectUnauthorized: false,
},
},
ssl: true,
}
: {}),
// ...(__prod__ ? {} : { synchronize: true }),
logging: true,
entities: [
Admin,
...
],
migrations: [join(__dirname,"/migrations/*")],
});
with prod variable is a boolean variable to check if app in production or not.
And i call it in index.ts
try {
try {
await dataSource.runMigrations();
} catch (error) {
console.log(`MIGRATION ERROR:${error}`)
}
await dataSource.initialize();
} catch (error) {
console.log(`TypeORM STARTING ERROR:${error}`);
}
My file structure
server
---dist
------migrations/*.js
...
---src
...
------migrations/*.ts
Solution 1:[1]
This could happen because you cannot read from ENV from data-source.ts.
try include require("dotenv").config();
at the top of the file. Also, test your configuration by storing it in a const and console log it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Phirun Tara |