9

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?

4
  • sequelize.org/master/manual/typescript.html? Commented Jan 17, 2021 at 22:44
  • 1
    Wanna use migrations, not sync Commented Jan 18, 2021 at 2:17
  • This question leaves us to ask so many other questions we don't know where to start to help. Commented Jan 18, 2021 at 10:47
  • OK I just want to create my migrations with sequelize-cli, and output a ts file instead of js file. That's it, I don't wanna add models manually and then sync them. I just want to add models using migrations. sequelize-cli package is for js, not for ts. It generates js file,not ts. Commented Jan 18, 2021 at 11:30

2 Answers 2

8

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')
}
Sign up to request clarification or add additional context in comments.

1 Comment

gives me this Error: Cannot find module 'babel-plugin-module-resolver' stackoverflow.com/questions/74231128/…
7

Use umzug. It powers sequelize cli.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.