model.ts
2.65 KB
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
import { Association, BelongsToManyGetAssociationsMixin, DataTypes, HasOne, Model, Sequelize } from 'sequelize';
class MyModel extends Model {
public num!: number;
public static associations: {
other: HasOne;
};
public static async customStuff() {
return this.sequelize!.query('select 1');
}
}
class OtherModel extends Model {}
const assoc: Association = MyModel.associations.other;
const Instance: MyModel = new MyModel({ int: 10 });
const num: number = Instance.get('num');
MyModel.findOne({
include: [
{
through: {
as: "OtherModel",
attributes: ['num']
}
}
]
});
MyModel.findOne({
include: [
{ model: OtherModel, paranoid: true }
]
});
MyModel.hasOne(OtherModel, { as: 'OtherModelAlias' });
MyModel.findOne({ include: ['OtherModelAlias'] });
MyModel.findOne({ include: OtherModel });
MyModel.count({ include: OtherModel });
MyModel.build({ int: 10 }, { include: OtherModel });
MyModel.bulkCreate([{ int: 10 }], { include: OtherModel });
MyModel.update({}, { where: { foo: 'bar' }, paranoid: false});
const sequelize = new Sequelize('mysql://user:user@localhost:3306/mydb');
MyModel.init({
virtual: {
type: new DataTypes.VIRTUAL(DataTypes.BOOLEAN, ['num']),
get() {
return this.getDataValue('num') + 2;
},
set(value: number) {
this.setDataValue('num', value - 2);
}
}
}, {
indexes: [
{
fields: ['foo'],
using: 'gin',
operator: 'jsonb_path_ops',
}
],
sequelize,
tableName: 'my_model',
getterMethods: {
multiply: function() {
return this.num * 2;
}
}
});
/**
* Tests for findCreateFind() type.
*/
class UserModel extends Model {}
UserModel.init({
username: { type: DataTypes.STRING, allowNull: false },
beta_user: { type: DataTypes.BOOLEAN, allowNull: false }
}, {
sequelize: sequelize
})
UserModel.findCreateFind({
where: {
username: "new user username"
},
defaults: {
beta_user: true
}
})
/**
* Test for primaryKeyAttributes.
*/
class TestModel extends Model {};
TestModel.primaryKeyAttributes;
/**
* Test for joinTableAttributes on BelongsToManyGetAssociationsMixin
*/
class SomeModel extends Model {
public getOthers!: BelongsToManyGetAssociationsMixin<OtherModel>
}
const someInstance = new SomeModel()
someInstance.getOthers({
joinTableAttributes: { include: [ 'id' ] }
})
/**
* Test for through options in creating a BelongsToMany association
*/
class Film extends Model {}
class Actor extends Model {}
Film.belongsToMany(Actor, {
through: {
model: 'FilmActors',
paranoid: true
}
})
Actor.belongsToMany(Film, {
through: {
model: 'FilmActors',
paranoid: true
}
})