model.ts
1.23 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
import { Association, HasOne, Model, Sequelize, DataTypes } 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: [
{ model: OtherModel, paranoid: true }
]
});
MyModel.hasOne(OtherModel, { as: 'OtherModelAlias' });
MyModel.findOne({ include: ['OtherModelAlias'] });
const sequelize = new Sequelize('mysql://user:user@localhost:3306/mydb');
MyModel.init({}, {
indexes: [
{
fields: ['foo'],
using: 'gin',
operator: 'jsonb_path_ops',
}
],
sequelize,
tableName: 'my_model'
});
/**
* 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
}
})