1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| @Column(DataType.VIRTUAL) accessToken: string;
@Column(DataType.JSON) data: any
@Column({field: 'user_id'}) userID: number;
class PostModel extends Model { public id: number public name: string public getUser! BelongsToGetAssociationMixin<BandModel> static initModel (sequelize: Sequelize): void { id: { autoIncrement: true, primaryKey: true, type: INTEGER }, name: { type: STRING, allowNull: false, defaultValue: 'test', validate: { is: /^[a-z]+$/i, is: ["^[a-z]+$",'i'], not: /^[a-z]+$/i, not: ["^[a-z]+$",'i'], isEmail: true, isUrl: true, isIP: true, isIPv4: true, isIPv6: true, isAlpha: true, isAlphanumeric: true, isNumeric: true, isInt: true, isFloat: true, isDecimal: true, isLowercase: true, isUppercase: true, notNull: true, isNull: true, notEmpty: true, equals: 'specific value', contains: 'foo', notIn: [['foo', 'bar']], isIn: [['foo', 'bar']], notContains: 'bar', len: [2,10], isUUID: 4, isDate: true, isAfter: "2011-11-05", isBefore: "2011-11-05", max: 23, min: 23, isCreditCard: true, isEven(value) { if (parseInt(value) % 2 !== 0) { throw new Error('Only even values are allowed!'); } } }, isIn: { args: [['en', 'zh']], msg: "Must be English or Chinese" } } }
static relate (): void { PostModel.belongsTo(UserModel, { as: 'user', foreignKey: 'user_id' }) } @AfterUpdate static createUser(user: UserModel) { console.log(user.id); }
@AfterDestroy static async afterDestroy(use: UserModel) {} }
const Post = sequelize.define('post', { id: { autoIncrement: true, primaryKey: true, type: INTEGER }, name: { type: STRING, allowNull: false, defaultValue: 'test' }, firstName: { type: STRING, field: 'first_name' comment: '列注释' }, fullName: { type: VIRTUAL, get: function (this: UserModel) { return this.firstName + this.name }, set: function (val) { this.setDataValue('name', val) } }, date: { type: DATE, defaultValue: NOW }, uniqueOne: { type: DataTypes.STRING, unique: 'compositeIndex' }, uniqueTwo: { type: DataTypes.INTEGER, unique: 'compositeIndex' },
data: { type: JSON }, created_at: { type: DATE } }, { indexes: [{ unique: true, fields: ['field1']}], timestamps: true, tableName: 'MyPosts' paranoid: true, deletedAt: 'mydelete', validate: { bothCoordsOrNone() { if ((this.latitude === null) !== (this.longitude === null)) { throw new Error('Either both latitude and longitude, or neither!'); } } } } );
Post.associate = () => { Post.User = Post.belongsTo(app.model.Post) }
User.associate = () => { User.hasMany(Post) }
Post.customQuery = () => {}
Post.prototype.customQuery = () => {}
|