sequelize-auto
sequelize-auto copied to clipboard
added typeOverrides option
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
*/
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.
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"
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": { // ... ?
Any updates? This would fix an issue I have.