【问题标题】:Sequelize model getters in TypeScript在 TypeScript 中序列化模型获取器
【发布时间】:2018-06-30 15:19:31
【问题描述】:

在使用 TypeScript 时,对于 Sequelize 模型,在 getter function 中使用 this.getDataValue 的正确方法是什么?

这是我得到的错误:

“字符串 | 类型”上不存在属性“getDataValue”数据类型摘要 |定义AttributeColumnOptions'。

“字符串”类型上不存在属性“getDataValue”。

我的模型定义:

import * as Sequelize from 'sequelize';
import sequelize from '../db-connection';

export interface IUserAttributes {
    date_of_birth: Date;
    name: string;
}

export interface IUserInstance extends Sequelize.Instance<IUserAttributes> {
    date_of_birth: Date;
    name: string;
}

const User = sequelize.define<IUserAttributes, IUserInstance>('user', {
    name: {
        type: Sequelize.STRING,
        validate: {
            notEmpty: true,
        },
    },
    date_of_birth: {
        get(): Date {
            return new Date(this.getDataValue('date_of_birth'));
        },
        type: Sequelize.DATEONLY,
        validate: {
            isDate: true,
            notEmpty: true,
        },
    },
});

export default User;

【问题讨论】:

    标签: typescript sequelize.js


    【解决方案1】:

    你需要在getter函数中指定this的类型

    const User = sequelize.define<IUserAttributes, IUserInstance>('user', {
      name: {
          type: Sequelize.STRING,
          validate: {
              notEmpty: true,
          },
      },
      date_of_birth: {
          get(this: IUserInstance): Date {
              return new Date(this.getDataValue('date_of_birth'));
          },
          type: Sequelize.DATEONLY,
          validate: {
              isDate: true,
              notEmpty: true,
          },
      },
    });
    

    注意 this 参数不会被发送到 JS 它只是为了打字稿编译器的好处。

    【讨论】:

    • 谢谢!有这方面的文件吗? (没有双关语)我能找到的只是关于这个问题的讨论:github.com/Microsoft/TypeScript/issues/3694
    • @rink.attendant.6 不......这是我所知道的文档的范围:(
    • 知道你会如何处理二传手吗?我想添加 this 的类型注释作为第二个参数,但 TS 不代表那个。
    【解决方案2】:

    这是一个工作示例> 它将列数据中的基础 TEXT 值转换为称为有效负载的新 JSONB 类型列:

    import {DataTypes, Model, Sequelize} from 'sequelize';
    
    export class Book extends Model {
        public id!: number; 
        public name!: string;
        public payload!: any;
        private data!: string;
    }
    
    export const initBook = (sequelize: Sequelize) => {
        Book.init(
            {
                id: {
                    type: DataTypes.INTEGER,
                    primaryKey: true
                },
                name: {
                    type: DataTypes.STRING(100),
                    primaryKey: false
                },
                data: {
                    type: DataTypes.TEXT,
                    primaryKey: false
                },               
                payload: {
                    type: DataTypes.JSONB,
                    allowNull: false,
                    field: 'data',
                    get(this: any) {
                        const j = this.getDataValue('payload');
                        return JSON.parse(j);
                    }
                }               
            },
            {
                tableName: 'book',
                sequelize // this bit is important
            }
        );
    };
    
    export default {Book, initBook};
    

    【讨论】:

      【解决方案3】:

      对于"sequelize": "^5.21.3""typescript": "^3.7.5" 并使用基于 TypeScript 类的样式定义 sequelize 模型。

      index.ts:

      import { sequelize } from '../../db';
      import Sequelize, { Model } from 'sequelize';
      
      class User extends Model {
        public date_of_birth!: Date;
        public name!: string;
      }
      User.init(
        {
          name: {
            type: Sequelize.STRING,
            validate: {
              notEmpty: true,
            },
          },
          date_of_birth: {
            get(this: User): Date {
              return new Date(this.getDataValue('date_of_birth'));
            },
            type: Sequelize.DATEONLY,
            validate: {
              isDate: true,
              notEmpty: true,
            },
          },
        },
        { sequelize, modelName: 'user' },
      );
      

      this 类型转换为User 类将消除错误。不要忘记在User 类中声明属性。

      【讨论】:

        猜你喜欢
        • 2015-03-12
        • 2022-01-06
        • 1970-01-01
        • 1970-01-01
        • 2020-12-24
        • 2018-01-30
        • 1970-01-01
        • 2014-01-05
        • 1970-01-01
        相关资源
        最近更新 更多