在TypeScript中使用Sequelize模型getter

时间:2018-01-22 02:45:17

标签: typescript sequelize.js

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

这是我得到的错误:

  

属性' getDataValue'类型'字符串|中不存在DataTypeAbstract | DefineAttributeColumnOptions'

     

属性' 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;

3 个答案:

答案 0 :(得分:4)

您需要在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,这只是为了打字稿编译器的好处。

答案 1 :(得分:0)

这是一个可行的示例>它将列数据中的基础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};

答案 2 :(得分:0)

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

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类中声明属性。