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
 
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
 
luiz_fernandoreal_8737ba profile image
Luiz Fernando Real

Thanks for the post! It's a good direction for me.

I'd like also to say that the problem you had putting each association in a different file is probably because of the circular reference it creates between the model files. Since you import Author model inside Books and vice-versa, Node needs to give an empty object to one of them so that they can both be loaded. I've done that myself also in the project I'm working on.

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
 
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