-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathsscce-sequelize-6.ts
More file actions
84 lines (73 loc) · 2.59 KB
/
sscce-sequelize-6.ts
File metadata and controls
84 lines (73 loc) · 2.59 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
import { BelongsToGetAssociationMixin, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model } from 'sequelize';
import { createSequelize6Instance } from '../setup/create-sequelize-instance';
import { expect } from 'chai';
import sinon from 'sinon';
// if your issue is dialect specific, remove the dialects you don't need to test on.
export const testingOnDialects = new Set(['mssql', 'sqlite', 'mysql', 'mariadb', 'postgres', 'postgres-native']);
// You can delete this file if you don't want your SSCCE to be tested against Sequelize 6
// Your SSCCE goes inside this function.
export async function run() {
// This function should be used instead of `new Sequelize()`.
// It applies the config for your SSCCE to work on CI.
const sequelize = createSequelize6Instance({
logQueryParameters: true,
benchmark: true,
define: {
// For less clutter in the SSCCE
timestamps: false,
},
});
class Foo extends Model<InferAttributes<Foo>, InferCreationAttributes<Foo>> {
declare id?: string;
declare name: string;
declare BarId: ForeignKey<Bar['id']>;
declare getBar: BelongsToGetAssociationMixin<Bar>;
}
class Bar extends Model<InferAttributes<Bar>, InferCreationAttributes<Bar>> {
declare id?: string;
declare name: string;
}
Foo.init({
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: DataTypes.TEXT,
}, {
sequelize,
modelName: 'Foo',
});
Bar.init({
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: DataTypes.TEXT,
}, {
sequelize,
modelName: 'Bar',
});
Bar.hasMany(Foo);
Foo.belongsTo(Bar);
// You can use sinon and chai assertions directly in your SSCCE.
const spy = sinon.spy();
sequelize.afterBulkSync(() => spy());
await sequelize.sync({ force: true });
expect(spy).to.have.been.called;
const theBar = await Bar.create({ name: 'TS bar' });
const theFoo = await Foo.create({ name: 'TS foo', BarId: theBar.id });
// All is fine, BarId is correctly filled in the model, due to the create
expect(await theFoo?.getBar()).to.not.be.null;
// If have a function in my code that gives me the attributes and include automatically. But in some cases, I want to be
// manually able to lazy load a specific relationship
const theFreshFoo = await Foo.findOne({
where: {
name: theFoo.get("name")
},
attributes: ["name"]
})
// And when trying to load the relation, the relationship attribute is missing, so I simply get null
expect(await theFreshFoo?.getBar()).to.not.be.null;
}