Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sequelize cli with typescript

I'm trying to use sequelize with typescript, but don't know how. I installed a package named sequelize-cli-typescript, but it does not work with sequelize v6. And I know it is better to use migrations to perform my database. How can I do that?

like image 297
Hadi Ahmadi Avatar asked May 15 '26 09:05

Hadi Ahmadi


2 Answers

You pretty much need to transpile your code into javascript if you're going to use migrations, because sequelize-cli doesn't know anything about typescript. We compile our stuff to ./dist right before running npx sequelize-cli db:migrate via npx tsc -p . with a tsconfig.json file that has these lines:

{
  "compilerOptions": {
    "target": "es6",
    // ...
    "outDir": "dist",
    // ...
  }
}

The .sequelizerc file (which is used by sequelize-cli only) will also point to dist, e.g.:

const path = require('path');

module.exports = {
   'config': path.resolve('./dist/src/config', 'config.js'),
   'models-path': path.resolve('./dist/src/db', 'models'),
   'seeders-path': path.resolve('./dist/src/db', 'seeders'),
   'migrations-path': path.resolve('./dist/src/db', 'migrations')
}
like image 155
mikebridge Avatar answered May 17 '26 22:05

mikebridge


Use umzug. It powers sequelize cli.

like image 35
Alfredo R Avatar answered May 17 '26 21:05

Alfredo R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!