DEV Community

Cover image for Sequelize + TypeScript - What you need to know

Sequelize + TypeScript - What you need to know

Jean Carlos Taveras on March 08, 2021

Hi Dev community 👋🏾! I just decided that I want to write a post so here I AM. I didn't know what to write about and I landed on the idea of Sequel...
Collapse
 
eeeee profile image
E

Sequelize-typescript package is very helpful, it allows to define model with decorators and transpiles into easily usable objects to pass through graphql, without creating additional boilerplate DTO.
Also, check out Nestjs. It's very flexible, code first, easy to use and
"federationable" typescript based, backend framework.
nestjs.com

Collapse
 
jctaveras profile image
Jean Carlos Taveras • Edited

Totally agree with you! That's why I added the note at the end.

I'm changing a lot of stuff both in the backend and the frontend. It's something kinda like an extreme makeover for the project. 😅

Collapse
 
joaopedromatias profile image
João Pedro Matias

How would the sequelize-cli db:migrate command work with typescript?

Collapse
 
alechuu profile image
Alechuu

Just dropping by the comments to say that you saved me! Was having the same problem related to the Sequelize subclass and didn't find anything useful in Google until I came across your post.
Really appreciated!

Collapse
 
demostheneslld profile image
Nathaniel Clement

Maybe a bit silly, but I was starting without sequelize installed, and needed to run npm i sequelize before npx sequelize-cli init

Collapse
 
hcancelik profile image
Can Celik

Your model files are ending with .ts but db/models/index.js is looking for .js files so this probably doesn't work.

Collapse
 
20101997 profile image
Ah_ch_97 • Edited

I think it will be better to keep the db Object that is being generated based on the files inside the models so you can avoid :
Book.belongsTo called with something that's not a subclass of Sequelize.Model error

Collapse
 
okumujustine profile image
okumujustine

Nice heads up, i love it

Collapse
 
unlocomqx profile image
Mohamed Jbeli

Thank you! This improves DX quite significantly

Collapse
 
minhdc998 profile image
MinhDC998

Awesome post, thank you.
But I wondering how to add or remove column to existed model.

Collapse
 
raitono profile image
Ben Painter

Hey @minhdc998, you should be able to remove the property from your model and then use the sequelize-cli to generate a migration in which you would write a table alter statement to remove it from the database.

There is supposed to be an alter property you can pass to the sync method which might attempt to do this for you, but I've had issues with it creating duplicate foreign key constraints, so I haven't used it much.

Collapse
 
dani_sanomads profile image
dani-sanomads

Thanks a lot.

Collapse
 
gsavvidis96 profile image
Giannis Savvidis

Hi,
Awesome article I am actually using it the exact way. Do you have any repo that using this approach? Also how is it possible to create with associations? Typescript seems to not like it.

Thanks