Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore default ability to globally set defaults for all models #66

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kinsi55
Copy link

@kinsi55 kinsi55 commented Aug 11, 2019

Allows to define global values to set for all models.

Description, Motivation and Context

I found myself where I have a certain column for all of my Models, repeadetly defining them is not really DRY thus I've tried to copy original functionality of Waterlin where attributes defined in sails.config.models are merged into the Models themselves (Except I'm merging the seperate key sails.config.sequelize.modelDefaults to avoid breaking changes).

What is the current behavior?

Theres no way to globally define default values for models

What is the new behavior?

Any key not defined in the Model that exists in sails.config.sequelize.modelDefaults will get merged into it

What kind of change does this PR introduce?

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
  • I have added tests to cover my changes.
  • Overall test coverage is not decreased.
  • All new and existing tests passed.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.0%) to 79.907% when pulling 04b3ff3 on kinsi55:master into 9123b26 on KSDaemon:master.

@KSDaemon
Copy link
Owner

Hi @kinsi55 ! First of all, thanks for contributing!
I think it's a good feature!

Well, there are a few little bits to make this PR better:

  • Will be great, if you extend README with an example of modelDefaults as object.
  • There is no test for this new feature. I'm trying to cover all code with tests, and especially new ones. So we can be sure it will work as expected in the future.

@kinsi55
Copy link
Author

kinsi55 commented Aug 11, 2019

I was just trying to get this out quickly as I've wanted to use it myself. I'll try to add a test for it / extend the readme when I get around to it. If you happen to be interested in doing it you're ofc free to do it :P Dont know when I'd get around to it.

@KSDaemon
Copy link
Owner

I don't think it's gonna take long! ;)
Just a simple example with a few common options + 1 test, that checks that options are propagated to models ;)

@kinsi55
Copy link
Author

kinsi55 commented Aug 11, 2019

I've a question - when using this hook / Sequelize doesnt it essentially prevent you from using the normal ORM / Waterline alongside? If so I'd just switch it to using sails.config.models instead of the custom key as that makes things more streamlined.

@KSDaemon
Copy link
Owner

Well, it's possible to use both: sequelize and waterline models at the same time.
I don't think it's a good idea to store sequelize configuration in sails.config.models as it is supposed to be used in waterline. Also, keys and options may differ, so let's save it separately.

@prusswan
Copy link

prusswan commented Jul 2, 2020

Found out that Sequelize can do this using the beforeDefine global hook (see sequelize/sequelize#4383)

Example:

   options: {
      dialect: 'postgres',
      host   : 'localhost',
      port   : 5432,
      logging: console.log, // or specify sails log level to use ('info', 'warn', 'verbose', etc)
      hooks: {
        beforeDefine: (attributes) => {
          attributes = Object.assign(attributes, {
            createdAt: { type: Sequelize.BIGINT, field: 'created_at' },
            updatedAt: { type: Sequelize.BIGINT, field: 'updated_at' },
          });
          sails.log("beforeDefine", attributes);
        }
      },
   }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants