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

added typeOverrides option

Open kurochin143 opened this issue 4 years ago • 4 comments

Added option typeOverrides. It allows overriding the field types of the generated class. For my use case I had to change the type of the roles table column name to a custom enum type. Current output

export interface RolesAttributes {
	name: string;
        description?: string;
}

export class Roles extends Model<RolesAttributes, RolesCreationAttributes> implements RolesAttributes {
	name!: string;
        description?: string;
}

New output

import { RoleNames } from '../RoleNames'; // adds import

export interface RolesAttributes {
	name: RoleNames; // changed type
        description?: string;
}

export class Roles extends Model<RolesAttributes, RolesCreationAttributes> implements RolesAttributes {
	name!: RoleNames;
        description?: string;
}

How to use

options
{
 typeOverrides: {
  tables: {
    roles: { // name of table in db
      name: { // name of column in db
        type: "RoleNames",
        source: "../RoleNames",
        isDefault: false,
        isOptional: false
    }
  }
 }
}
/**
 * @type Optional. Name of the type
 * @source Optional. File path of the type relative to file in the directory.
 *         Leave undefined if overriding with primitive types
 * @isDefault Optional. Whether the type is an export default. Default false
 * @isOptional Optional. Override optionality
 */

kurochin143 avatar Aug 07 '21 06:08 kurochin143

More changes added.

Nullable columns will now be using null instead of ?

Current output

export interface RolesAttributes {
	name: string;
        description?: string;
}

export class Roles extends Model<RolesAttributes, RolesCreationAttributes> implements RolesAttributes {
	name!: string;
        description?: string;
}

New output

export interface RolesAttributes {
	name: string;
        description: string | null;
}

export class Roles extends Model<RolesAttributes, RolesCreationAttributes> implements RolesAttributes {
	name!: string;
        description!: string | null;
}

This is more logical since the value is actually a null rather than undefined.

kurochin143 avatar Sep 03 '21 04:09 kurochin143

More changes added.

Controls the default type of nullable table column. This overrides the previous change of having null column as default. Enabling users to revert back to the current output.

Current output

export interface RolesAttributes {
	name: string;
        description?: string;
}

export class Roles extends Model<RolesAttributes, RolesCreationAttributes> implements RolesAttributes {
	name!: string;
        description?: string;
}

New output

export interface RolesAttributes {
	name: string;
        description?: string | null;
}

export class Roles extends Model<RolesAttributes, RolesCreationAttributes> implements RolesAttributes {
	name!: string;
        description?: string | null;
}

How to use

options
{
 typeOverrides: {
  nullableFieldType: "NULL_AND_OPTIONAL"
 }
}

@nullableFieldType use "NULL", "OPTIONAL", OR "NULL_AND_OPTIONAL" for nullable table columns. Default "NULL_AND_OPTIONAL"

kurochin143 avatar Sep 05 '21 03:09 kurochin143

typeOverrides: { tables: { roles: { // name of table in db name: { // name of column in db

Can one use a DB schema with the table name, like "dbo.roles": { // ... ?

joaoe avatar Dec 14 '21 23:12 joaoe

Any updates? This would fix an issue I have.

Ngreene-mwks avatar Sep 22 '22 19:09 Ngreene-mwks