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

How can I rename a model attribute? #38

Closed
ignotus0001 opened this issue Dec 7, 2016 · 14 comments

Comments

@ignotus0001
Copy link

commented Dec 7, 2016

I have column in a postgres table called "ETA". pg-generator transforms it to "eTA", but I don't like that name; I'd prefer "eta" all lower case.

I know how to alias FK relationships using the custom file. I see from the docs how to have different names for the columns and model attributes, and I've read about using nunjucks filters for custom naming of ALL model attributes, but I can't seem to figure out how to rename this one model attribute. Can I accomplish this somehow in the custom file or by some other means?

Thanks,
-Billy B

ozum added a commit that referenced this issue Dec 8, 2016

Custom data `attributeName` added for `sequelize` template (#38)
Attribute names can be altered via custom-data.js file.

@ozum ozum added the enhancement label Dec 8, 2016

@ozum

This comment has been minimized.

Copy link
Owner

commented Dec 8, 2016

Hi,

I assume that you are using Sequelize template. Follow one of the methods according to your requirements

You want lower case only for one column (Special Case)

I added attributeName attribute to custom-data.js file. Please update pg-generator to latest version and update your custom-data.js as example below:

module.exports = {
    YourTable: {
        attributes: {
            ETA: { attributeName: 'eta' }
        }
    }
};

You want all you columns as lower case (General Case)

After generating sequelize template, you should edit generated template file your-generated-template-folder/partials/helper.nunj.html as below:

Change camelCase nunjucks filter to lower :

{% macro attributeName(column, cc) -%}
    {{ cc.attributeName or column.name | camelCase }}
{%- endmacro -%}

like this:

{% macro attributeName(column, cc) -%}
    {{ cc.attributeName or column.name | lower }}
{%- endmacro -%}

lower is a default filter of nunjucks. On the other hand camelCase is a filter defined in library (pg-generator/lib/util/template-filter.js). You can add your custom filters to this file if you wish.

Best Regards,

@ignotus0001

This comment has been minimized.

Copy link
Author

commented Dec 8, 2016

Yes I tried something like that after reading up in the docs. I tried again just now using your instructions. When I put this in my custom file:

    Stop: {
        attributes: {
            ETA: { attributeName: 'eta' }
        }
    },

No changes occur. I still get "eTA" in generated model:

        eTA: {
            type: DataTypes.DATE,
            field: 'ETA',
            allowNull: true
        },

If I use "eTA" as the property name in the custom file like this:

    Stop: {
        attributes: {
            eTA: { attributeName: 'eta' }
        }
    },

I do get changes in the generated model:

        eTA: {
            type: DataTypes.DATE,
            field: 'ETA',
            allowNull: true,
            attributeName: eta
        },

but at run-time sequelize throws an exception

model.Stop = sequelize.import('./definition/stop.js');

when exporting:

/home/bbarnum/BitBucket/StrataGen/transit-dispatch-service/model/definition/stop.js:51
            attributeName: eta
                           ^

ReferenceError: eta is not defined
    at module.exports (/home/bbarnum/BitBucket/StrataGen/transit-dispatch-service/model/definition/stop.js:51:28)
    at Sequelize.import (/home/bbarnum/BitBucket/StrataGen/transit-dispatch-service/node_modules/sequelize/lib/sequelize.js:691:30)

I tried searching the sequelize docs for this "attributeName" feature but found no results. I hope I'm not misunderstanding your instructions, but for whatever reason I'm not getting it to work.

-B

@ozum

This comment has been minimized.

Copy link
Owner

commented Dec 8, 2016

Which version of pg-generator are you using?

@ignotus0001

This comment has been minimized.

Copy link
Author

commented Dec 8, 2016

3.4.7, and I just reinstalled to be sure.

But something just occurred to me. I have not re-generated the sequelize-template in a while. Could that be the issue? I'm unclear on the relationship between the 2. Should I always be re-generating the sequelize-template directory when I get a new version pg-generator? Sometimes?

@ozum

This comment has been minimized.

Copy link
Owner

commented Dec 8, 2016

In this particular case, you should re-generate template, because I updated template to add feature you requested.

About relationship between them: pg-generator reverse engineers your database and provides database structure to template files. Also it generates output using those template files. It's like generating html files for web pages.

Built-in templates are just for example. The ideal way is that you should create and use your own templates according to your specific needs. You can use built-in templates as a starting point.

@ignotus0001

This comment has been minimized.

Copy link
Author

commented Dec 8, 2016

Got it. And thank you, sir! Worked great.

@ignotus0001 ignotus0001 closed this Dec 8, 2016

@ignotus0001

This comment has been minimized.

Copy link
Author

commented Dec 8, 2016

Um, not quite. I spoke too soon. The model now contains

        eta: {
            type: DataTypes.DATE,
            field: 'ETA',
            allowNull: true
        },

So far so good, but I ALSO get this extra attribute definition

        eTA: {
            attributeName: eta
        }

which blows up at run-time when importing with same exception mentioned above.

@ignotus0001 ignotus0001 reopened this Dec 8, 2016

@ozum

This comment has been minimized.

Copy link
Owner

commented Dec 10, 2016

OK. I found the problem, fixed It and added necessary tests.

  • Update to latest version,
  • Re-generate template,
  • Add data below to custom-data.js
        ETA: {
            attributeName: 'eta'
        },

Please report back if it works.

Regards,

@ignotus0001

This comment has been minimized.

Copy link
Author

commented Dec 12, 2016

I'm sorry but no, that didn't work.

I upgraded to 3.4.8 and re-ran the template, which changed line 2 of columns.nunj.html. I added this to my custom file:

    Stop: {
        ETA: {
            attributeName: 'eta'
        }
    },

The modification solved the problem of blowing up when importing the model file, but it doesn't transform the model attribute definition as desired. What I have now is

        eTA: {
            type: DataTypes.DATE,
            field: 'ETA',
            allowNull: true
        },

in list of attributes, which is unchanged, as if I had no customization. And then at the bottom of table def:

{
        schema: 'public',
        tableName: 'Stop',
        timestamps: true,
        ETA: {
            attributeName: 'eta'
        }
    }

So that extra definition doesn't cause exceptions now that it is at the table level, but it doesn't transform the attribute definition. I tried actually running my app just to be sure, and sure enough it shows "eTA" instead of "eta" in the attributes of the Stop model.

@ozum

This comment has been minimized.

Copy link
Owner

commented Dec 12, 2016

Could you please share a DDL and custom-data.js file which produces this bug? I'll work on it.

@ignotus0001

This comment has been minimized.

Copy link
Author

commented Dec 12, 2016

Of course. And thank you! I realize this is an improvement you are creating, and I'm grateful.
customModelDefinition.zip

@ozum

This comment has been minimized.

Copy link
Owner

commented Dec 13, 2016

Hi,

At last I found problem. The last version works correctly. However you (and me in my later examples) has missed attributes key.

Your customModelDefinition.js is

    Stop: {
        ETA: {
            attributeName: 'eta'
        }
    },

It should be:

    Stop: {
        attributes: {
            ETA: {
                attributeName: 'eta'
            }
        }
    },

I tested your database with corrected customModelDefinition.js. It worked as you expected. Test it with corrected file again please.

Regards,

@ignotus0001

This comment has been minimized.

Copy link
Author

commented Dec 15, 2016

Hey sorry for the delay. I had to hop on a plane for Boston to a customer site and I'm still there. I haven't personally tried the fix, but a colleague back in the office on another project reports that it works. I will confirm in the next day or so and then close the issue.

Many thanks!

-B

@ignotus0001

This comment has been minimized.

Copy link
Author

commented Dec 20, 2016

I confirmed that it worked. Thanks again for maintaining this repo, my friend.

-BillyB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.