sábado, 5 de noviembre de 2022

js model demo


declare @TableName sysname = 'touchTokens'


declare @Result varchar(max) = ' ' + @TableName + '

const { DataTypes } = require(''sequelize'');

module.exports = model;


function model(sequelize) {

    const attributes = 

{'




select @Result = @Result + '


     ,'  +' ' + ColumnName + ': { type: DataTypes.'+ColumnType+', '+  NullableSign +' } 

'


from


(


    select 


        replace(col.name, ' ', '_') ColumnName,


        column_id ColumnId,


        case typ.name 


            when 'bigint' then 'long'


            when 'binary' then 'byte[]'


            when 'bit' then 'BOOLEAN'


            when 'char' then 'STRING'


            when 'date' then 'DATE'


            when 'datetime' then 'DATE'


            when 'datetime2' then 'DATE'


            when 'datetimeoffset' then 'DateTimeOffset'


            when 'decimal' then 'DECIMAL'


            when 'float' then 'double'


            when 'image' then 'byte[]'


            when 'int' then 'INTEGER'


            when 'money' then 'DECIMAL'


            when 'nchar' then 'STRING'


            when 'ntext' then 'STRING'


            when 'numeric' then 'DECIMAL'


            when 'nvarchar' then 'string'


            when 'real' then 'float'


            when 'smalldatetime' then 'DATE'


            when 'smallint' then 'short'


            when 'smallmoney' then 'DECIMAL'


            when 'text' then 'string'


            when 'time' then 'TimeSpan'


            when 'timestamp' then 'long'


            when 'tinyint' then 'byte'


            when 'uniqueidentifier' then 'Guid'


            when 'varbinary' then 'byte[]'


            when 'varchar' then 'STRING'


            else 'UNKNOWN_' + typ.name


        end ColumnType,


        case 


            when col.is_nullable = 1 and typ.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier') 


            then 'allowNull: true' 


            else 'allowNull: false' 


        end NullableSign


    from sys.columns col


        join sys.types typ on


            col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id


    where object_id = object_id(@TableName)


) t


order by ColumnId




set @Result = @Result  + '


    };


    const options = {

        defaultScope: {

            // exclude password hash by default

            attributes: { exclude: [''passwordHash''] }

        },

        scopes: {

            // include hash with this scope

            withHash: { attributes: {}, }

        }

    };


    return sequelize.define(''' + UPPER(LEFT(@TableName, 1)) + LOWER(SUBSTRING(@TableName, 2, LEN(@TableName))) +''', attributes, options);

}


'




print @Result

No hay comentarios:

Publicar un comentario