Skip to content

fix(model): potential fix for #6423 - #9260

Closed
rogerc wants to merge 4 commits into
sequelize:masterfrom
rogerc:master
Closed

rogerc wants to merge 4 commits into
sequelize:masterfrom
rogerc:master

Conversation

@rogerc

@rogerc rogerc commented Apr 2, 2018 •

Copy link
Copy Markdown
Contributor

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

Can provide a fix for #6423 to match what most users expect when defining underscored in the options.

Provides snake_case for all property names by setting field to snake_case
but keeping the name as camelCase.

The alternative is to manually define a hook which most users won't.

sequelize.addHook('beforeDefine', (attributes) => {
    Object.keys(attributes).forEach((name) => {
        if (typeof attributes[name] !== 'function') {
            attribute = attributes[name];
            const _underscored = attribute.underscored === undefined ? sequelize.options.define.underscored : attribute.underscored;
            if (attribute.field === undefined && _underscored !== undefined) {
                attribute.field = sequelize.Utils.underscoredIf(name, _underscored);
            }
        }
    });
});

NOTE: I run npm run test-docker-unit successfully.

rcastells added 2 commits April 1, 2018 18:43
Provides snake_case for all property names by setting field to snake_case
but keeping the name as camelCase.

The alternative is to manually define a hook.

sequelize.addHook('beforeDefine', (attributes) => {
    Object.keys(attributes).forEach((name) => {
        if (typeof attributes[name] !== 'function') {
            attribute = attributes[name];
            const _underscored = attribute.underscored === undefined ? sequelize.options.define.underscored : attribute.underscored;
            console.log("NAME:", name, _underscored, attribute._underscored === undefined, sequelize.options.define.underscored, attribute.underscored);
            if (attribute.field === undefined && _underscored !== undefined) {
                attribute.field = sequelize.Utils.underscoredIf(name, _underscored);
            }
        }
    });
});
Added define tests to validate correct output using underscored.
@codecov

codecov Bot commented Apr 2, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #9260 into master will increase coverage by <.01%.
The diff coverage is 100%.

rcastells added 2 commits April 2, 2018 13:04
Updated docs to make reference to new behaviour.
…elize#6423 - PR sequelize#9260

Provides consistent behaviour with new implementation for createdAt, updatedAt and deletedAt.
Added checks to test.
Removed unnecessary manipulation during init and just doing it in refreshAttributes

@sushantdhiman sushantdhiman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See if you can cover full proposal from #6423 (comment)

Otherwise I will give it a shot when I get some time

@sushantdhiman

Copy link
Copy Markdown
Contributor

I have worked on this feature as foreignKey related work required some familiarity with current codebase. Thank you for this contribution.

#9304

@rogerc

rogerc commented Apr 15, 2018

Copy link
Copy Markdown
Contributor Author

Thanks for working on the foreign key work. Hopefully it will go in the v5 release.

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.

2 participants