2

How to Configure Sequelize and Sequelize-CLI to work with TypeScript

I have a project to maintain using sequelize and typescript and I tried as hard as possible to configure them, but I couldn't. I cannot generate models using the cli, and sometimes when I find some configurations online the cli generates a js file, or sometimes generates a ts file but cannot migrate.

Note: when I work using sequelize and javascript everything works fine, but for this I have to work with typescript.

I need an explanation for how to configure sequelize and typescript from scratch, or the configuration code if possible.

I even used sequelize-typescript, sequelize-cli-typescript dependencies and I couldn't configure the application to create a model or migrate the database using sequelize.

starball
  • 20,030
  • 7
  • 43
  • 238

2 Answers2

0

create a new folder inside execute:

npm init -y 



npm install sequelize  sequelize-typescript

touch .sequelizerc // create an empty file

copy follwing code inside

const path = require('path');

module.exports = {
  config: path.resolve('.', 'config.js'),
  'models-path': path.resolve('./database/models'),
  'seeders-path': path.resolve('./database/seeders'),
  'migrations-path': path.resolve('./database/migrations')
}

run inside the rootfolder

npx sequelize-cli init
npx sequelize-cli migration:generate --name test

It will create the migration file in database/migration folder

Ralle Mc Black
  • 1,065
  • 1
  • 8
  • 16
-1

There is no need to use sequelize-cli with TypeScript. Just go with JavaScript and your good. For utilizing TypeScript using Sequelize I'd recommend reading the docs: https://sequelize.org/docs/v6/other-topics/typescript/