sequelize-auto icon indicating copy to clipboard operation
sequelize-auto copied to clipboard

feature request: Allow for custom model names

Open donecarlo opened this issue 3 years ago • 7 comments

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

donecarlo avatar Sep 22 '20 07:09 donecarlo

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

steveschmitt avatar Sep 22 '20 17:09 steveschmitt

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

donecarlo avatar Sep 23 '20 02:09 donecarlo

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.

steveschmitt avatar Sep 23 '20 03:09 steveschmitt

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 😀

donecarlo avatar Sep 23 '20 06:09 donecarlo

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

unconfident avatar Mar 07 '21 04:03 unconfident

@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 😇

Akxe avatar Jul 12 '21 10:07 Akxe

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?

dejaime avatar Aug 16 '21 16:08 dejaime