sequelize / sequelize-auto

Automatically generate bare sequelize models from your database.
2.91k stars 529 forks source link

feature request: Allow for custom model names #430

Open donecarlo opened 4 years ago

donecarlo commented 4 years ago

In my current setup, there are tables with same names under different schemas. As a workaround, I temporarily prefixed the schema name to the model name (which in this case defaults to the table name):

module.exports = function(sequelize, DataTypes, schema) {
    return sequelize.define(schema + '_' +'TABLE_NAME', {
        ID: {
            type: DataTypes.STRING,
            allowNull: false,
            primaryKey: true
        },
        SAMPLE_COLUMN: {
            type: DataTypes.STRING,
            allowNull: false
        },
    }, {
        tableName: 'TABLE_NAME',
        timestamps: false
    });
};

Would it be possible to provide an api that will allow the user to customize the model name, or provide a prefix / suffix to the default model name.

Thanks

steveschmitt commented 4 years ago

You could use the --output and --schema flags together, to put the models for each schema in their own directory.

donecarlo commented 4 years ago

You could use the --output and --schema flags together, to put the models for each schema in their own directory.

How does output differ from the directory option? Would that also change the model names within sequelize?

My concern is with the model names being registered/defined with Sequelize -- where I intend for all models in Sequelize to be something like: sequelize.schema_TABLE_NAME, instead of the default sequelize.TABLE_NAME

steveschmitt commented 4 years ago

Oh right, the concern is with the model names, not the file names. Sorry!

Maybe we could change the SequelizeAuto API to allow you to pass a function to convert the model names.

We couldn't do that from the command line, but we could give a --prefixModelNameWithSchemaName option.

donecarlo commented 4 years ago

Oh right, the concern is with the model names, not the file names. Sorry!

Maybe we could change the SequelizeAuto API to allow you to pass a function to convert the model names.

We couldn't do that from the command line, but we could give a --prefixModelNameWithSchemaName option.

Yes, that would be really helpful 😀

unconfident commented 3 years ago

Maybe we could change the SequelizeAuto API to allow you to pass a function to convert the model names.

This would be nice.

It would also allow us to bypass conversion from plural form for some tables ("singularize": true) if we disagree with some of the conversion results or want to introduce sequelize-auto to an existing code base and preserve Model names that were previously used

Akxe commented 3 years ago

@steveschmitt Is this coming I would love to translate my DB from Czech to English names. I would love to use pluralization too.

My take on API:

interface SingularAndPlural {
  readonly singular: string;
  readonly plural: string;
}

/** method from `inflection.inflect` -  */
type inflectionInflect = (
  /** String to transform (provide english string, other may result in wrong results) */
  enText: string,
  /** 1 for singular, 2 for plural */
  number: 1 | 2,
  /** Override for singular */
  singular: string,
  /** Override for plural */
  plural: string,
) => string;

interface AutoOptions {
  // current options...
  renameFile(
    originalModelName: string, 
    singularAndPluratTransform: inflectionInflect,
  ): string;
  renameModel(
    originalModelName: string, 
    singularAndPluratTransform: inflectionInflect,
  ): SingularAndPlural;
  renameProperty(
    originalModelName: string, 
    originalPropertyName: string, 
    singularAndPluratTransform: inflectionInflect,
  ): SingularAndPlural;
}

PS: Make this work with TypeScript generator too 😇

dejaime commented 3 years ago

Oh right, the concern is with the model names, not the file names. Sorry! Maybe we could change the SequelizeAuto API to allow you to pass a function to convert the model names. We couldn't do that from the command line, but we could give a --prefixModelNameWithSchemaName option.

Honestly, even a static --suffixModelNameWith="MySuffix" would be very helpful for (su | pre)ffixing the names. Is that already a thing?