Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8753217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:23:09+00:00 2026-06-13T13:23:09+00:00

I am looking for an example nodejs app that uses the sequelize ORM. My

  • 0

I am looking for an example nodejs app that uses the sequelize ORM.

My main concern is that it seems next to impossible to define your models in separate js files if those models have complex relationships to one another because of require() dependency loops. Maybe people define all their models in one file that is very very long?

I am mainly interested in how the models are defined and use through out the app. I would like to have some validation that what i am doing on my own is the “good” way to do things.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:23:10+00:00Added an answer on June 13, 2026 at 1:23 pm

    The short story

    The trick in this case is not to initialize the model in the file but just to provide the necesary information for its initialization and let a centralized module take care of the models setup and instantiation.

    So the steps are:

    • Have several Model files with data about the model, like fields, relationships and options.
    • Have a singleton module which loads all those files and setup all the model classes and relationships.
    • Setup your singleton module at the app.js file.
    • Get the model classes from the singleton module do not use require on your model files, load the models from the singleton instead.

    The longer story

    Here is a more detailed description of this solution with the corresponding source code:

    http://jeydotc.github.io/blog/2012/10/30/EXPRESS-WITH-SEQUELIZE.html

    EDIT: This is a very old answer! (read down for info)

    It’s old and limited in many ways!

    • First, as @jinglesthula mentioned in comments (and I experienced it too) – there are problems with requiring those files. It’s because require doesn’t work the same way as readdirSync!

    • Second – you are very limited in relations – the code doesn’t provide options to those associations so you are UNABLE to create belongsToMany as it needs through property. You can make the most basic assocs.

    • Third – you are very limited in model relations! If you read closely the code, you will see that relations is an Object instead of an Array, so if you want to make more than one associations of the same type (like having two times belongsTo) – you cannot!

    • Fourth – You don’t need that singleton thingy. Every module in nodejs is singleton by itself, so all this makes is pretty complex for no reason.

    You should see Farm’s answer! (The link to the article is broken, but I’ll fix it with this official sample from sequelize: https://github.com/sequelize/express-example/blob/master/models/index.js – you can browse the whole project to get an idea of what’s going on).

    p.s.
    I’m editing this post as it’s so upvoted that people won’t even see any new answers (as I did).

    Edit: Just changed the link to a copy of the same post, but in a Github Page

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking at this example that uses cluster layout to assign the X and
I am looking for a java example that shows how to avoid exceptions with
I'm looking for example code for setting up a connection to a bluetooth device
I'm looking for example of how I would solve the scenario below: Imagine my
I'm trying to learn Rails better by looking at example applications, and while looking
Looking for an example or documentation links as to how to implement a method
looking at this example of the jquery ui slider http://jqueryui.com/demos/slider/#steps i want to be
im looking for an example script. I saw one yesterday but for the life
I was looking at the example in Microsoft KB318804 but they use the threadId
I am looking for simple example code for setting up a Message in an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.