sequelize-cli-typescript
sequelize-cli-typescript copied to clipboard
Config File not working well
What you are doing?
I try to run migration but I keep getting an error which I believe concerns the use of es6
Config.ts Code
import dotEnv from "dotenv";
dotEnv.config();
const {
DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, DATABASE_HOST
} = process.env;
interface SequelizeConfig {
username: string;
password: string;
database: string;
host: string;
dialect: "mysql" | "postgres" | "sqlite" | "mariadb" | "mssql" | undefined;
}
interface DbEnvConfig {
[key: string]: SequelizeConfig;
}
const config: DbEnvConfig = {
development: {
username: DATABASE_USER!,
password: DATABASE_PASSWORD!,
database: DATABASE_NAME!,
host: DATABASE_HOST!,
dialect: "postgres",
},
production: {
username: DATABASE_USER!,
password: DATABASE_PASSWORD!,
database: DATABASE_NAME!,
host: DATABASE_HOST!,
dialect: "postgres",
},
test: {
username: DATABASE_USER!,
password: DATABASE_PASSWORD!,
database: DATABASE_NAME!,
host: DATABASE_HOST!,
dialect: "postgres",
},
};
export default config;
What do you expect to happen?
I expect my migrations to run without errors.
What is actually happening?
I am getting Error
ERROR: Error reading "src/dbConfig/config.ts". Error: SyntaxError: Cannot use import statement outside a module
__Dialect: postgres __Database version: 10 __Sequelize CLI version:^3.2.0-c __Sequelize version:^5.21.13
you have to compile these files into dist folder and point them from .sequelizerc file.
Just do
module.exports = {
development: {
username: DATABASE_USER!,
password: DATABASE_PASSWORD!,
database: DATABASE_NAME!,
host: DATABASE_HOST!,
dialect: "postgres",
},
production: {
username: DATABASE_USER!,
password: DATABASE_PASSWORD!,
database: DATABASE_NAME!,
host: DATABASE_HOST!,
dialect: "postgres",
},
test: {
username: DATABASE_USER!,
password: DATABASE_PASSWORD!,
database: DATABASE_NAME!,
host: DATABASE_HOST!,
dialect: "postgres",
},
}
and run npx tsc -outDir ./build-migrations ./src/**/*.ts
in .sequelizerc
var path = require('path');
module.exports = {
"config": path.resolve('build-migrations', 'config.js'),
"seeders-path": path.resolve('build-migrations', 'seeders'),
"migrations-source-path": path.resolve('src', 'migrations'),
'migrations-compiled-path': path.resolve('build-migrations', 'migrations')
}