sequelize / express-example

A proposal for the usage of Sequelize within an Express.JS application.
MIT License
2.5k stars 773 forks source link

Please provide an example using sequelize v5 and TypeScript. #97

Closed mrdulin closed 4 years ago

mrdulin commented 4 years ago

In particular, how to organize the models when using sequelize v5 and TypeScript?

I wonder can we remove this setup file: https://github.com/sequelize/express-example/blob/master/models/index.js and organize the models file just using export and import, and combine these models using some method.

The setup file executes immediately when importing it, or import some modules depend on it. Sometimes, It will affect the unit test when importing some modules into test files.

E.g.

models/index.ts:

import {User} from './user';
import {Role} from './Role';
import {Project} from './Project';

const models = combineModels([User, Role, Project])
// ...
export {models};
papb commented 4 years ago

I wonder can we remove this setup file: https://github.com/sequelize/express-example/blob/master/models/index.js and organize the models file just using export and import, and combine these models using some method.

Yes, definitely, this is something I want to do in the near future.

mrdulin commented 4 years ago

@papb I made a example using sequelize v5 and TypeScript. But I met an issue, it blocks me one week. Can you help me solve this issue? I also submitted a post on stackoverflow, but it still has no working answer.

I am thinking if the organization of models is correct or not.

Here is the post on SO: https://stackoverflow.com/questions/60054969/belongs-to-many-associations-add-foreign-keys-to-source-model-sequelizedatabase

Here is the example repo: https://github.com/mrdulin/node-sequelize-examples/tree/master/src/db

Thanks!

mrdulin commented 4 years ago

I figured it out.

papb commented 4 years ago

Hi @mrdulin sorry I couldn't give you more attention, I have too little time hehe. Glad you figured it out. Can you please share it? Or update your github repository? When I have time I would like to take a look and incorporate it as an example for this repository, of course giving all credits to you. Thank you very much.

mrdulin commented 4 years ago

@papb I made a stupid mistake. I passed a wrong modelName(Category) to the Language model. I copied the code from the Category model file, forgot to changing the modelName. After fixing it, the integration tests passed. I already updated the examples in the repo.

papb commented 4 years ago

Hi @mrdulin, we have another issue (#104) also requesting a TypeScript example, so I will close this one as a duplicate for now. The current JS example has been updated to use v6 and should work with v5 as well.

Hopefully I will add a TS example in the near future. Thank you again!