Skip to content

Commit c23a36b

Browse files
committed
Create SSCCE
1 parent 2b38801 commit c23a36b

4 files changed

Lines changed: 159 additions & 29 deletions

File tree

dev/create-sequelize-instance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function createSequelize6Instance(options?: Sequelize6Options): Sequelize
77
return new Sequelize6(wrapOptions(options));
88
}
99

10-
export function createSequelize7Instance(options?: Sequelize7Options): Sequelize7 {
10+
export function createSequelize7Instance(options?: Sequelize7Options<any>): Sequelize7 {
1111
// not compatible with node 10
1212
const { Sequelize: Sequelize7Constructor } = require('@sequelize/core');
1313
// @ts-expect-error -- wrapOptions expect sequelize 6.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
"type": "commonjs",
77
"license": "MIT",
88
"dependencies": {
9+
"@sequelize/core": "alpha",
910
"chai": "^4",
1011
"chai-as-promised": "^7",
1112
"chai-datetime": "^1",
1213
"chalk": "^4.1.2",
1314
"cross-env": "^7",
1415
"fs-jetpack": "^4",
1516
"sequelize": "^6",
16-
"@sequelize/core": "alpha",
1717
"sinon": "^13",
18-
"sinon-chai": "^3"
18+
"sinon-chai": "^3",
19+
"yarn": "^1.22.22"
1920
},
2021
"scripts": {
2122
"_test": "ts-node dev/runner.ts",

src/sscce-sequelize-6.ts

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import sinon from 'sinon';
66
// if your issue is dialect specific, remove the dialects you don't need to test on.
77
export const testingOnDialects = new Set(['mssql', 'sqlite', 'mysql', 'mariadb', 'postgres', 'postgres-native']);
88

9-
// You can delete this file if you don't want your SSCCE to be tested against Sequelize 6
10-
119
// Your SSCCE goes inside this function.
1210
export async function run() {
1311
// This function should be used instead of `new Sequelize()`.
@@ -18,24 +16,90 @@ export async function run() {
1816
define: {
1917
// For less clutter in the SSCCE
2018
timestamps: false,
19+
underscored: true
2120
},
2221
});
2322

24-
class Foo extends Model {}
25-
26-
Foo.init({
23+
class Parents extends Model {};
24+
Parents.init(
25+
{
26+
id: {
27+
type: DataTypes.INTEGER,
28+
autoIncrement: true,
29+
primaryKey: true,
30+
},
2731
name: DataTypes.TEXT,
28-
}, {
29-
sequelize,
30-
modelName: 'Foo',
31-
});
32+
},
33+
{sequelize},
34+
);
35+
36+
class Children extends Model {};
37+
Children.init(
38+
{
39+
id: {
40+
type: DataTypes.INTEGER,
41+
autoIncrement: true,
42+
primaryKey: true,
43+
},
44+
parentId: {
45+
type: DataTypes.INTEGER,
46+
references: {
47+
model: Parents,
48+
key: 'id'
49+
}
50+
}
51+
},
52+
{sequelize}
53+
54+
)
55+
56+
class GrandChildren extends Model {};
57+
GrandChildren.init(
58+
{
59+
id: {
60+
type: DataTypes.INTEGER,
61+
autoIncrement: true,
62+
primaryKey: true,
63+
},
64+
parentId: {
65+
type: DataTypes.INTEGER,
66+
references: {
67+
model: Children,
68+
key: 'id'
69+
}
70+
}
71+
},
72+
{sequelize}
73+
);
74+
75+
Parents.hasMany(Children)
76+
Children.belongsTo(Parents)
77+
Children.hasMany(GrandChildren)
78+
GrandChildren.belongsTo(Children)
3279

3380
// You can use sinon and chai assertions directly in your SSCCE.
3481
const spy = sinon.spy();
3582
sequelize.afterBulkSync(() => spy());
3683
await sequelize.sync({ force: true });
3784
expect(spy).to.have.been.called;
3885

39-
console.log(await Foo.create({ name: 'TS foo' }));
40-
expect(await Foo.count()).to.equal(1);
86+
const created = await Parents.create()
87+
const {rows, count} = await Parents.findAndCountAll({
88+
include: {
89+
model: Children,
90+
required: false,
91+
include: [{
92+
model: GrandChildren,
93+
required: true,
94+
}]
95+
},
96+
limit: 1
97+
})
98+
99+
console.log(rows);
100+
expect(created).to.not.be.undefined;
101+
expect(created).to.not.be.null;
102+
expect(count).to.equal(1);
41103
}
104+
105+

src/sscce-sequelize-7.ts

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CreationOptional, DataTypes, InferAttributes, InferCreationAttributes, Model } from '@sequelize/core';
1+
import { CreationOptional, DataTypes, InferAttributes, InferCreationAttributes, Model, DialectOptions } from '@sequelize/core';
22
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
33
import { createSequelize7Instance } from '../dev/create-sequelize-instance';
44
import { expect } from 'chai';
@@ -13,31 +13,96 @@ export const testingOnDialects = new Set(['mssql', 'sqlite', 'mysql', 'mariadb',
1313
export async function run() {
1414
// This function should be used instead of `new Sequelize()`.
1515
// It applies the config for your SSCCE to work on CI.
16+
1617
const sequelize = createSequelize7Instance({
1718
logQueryParameters: true,
1819
benchmark: true,
1920
define: {
2021
// For less clutter in the SSCCE
2122
timestamps: false,
23+
underscored: true
2224
},
25+
dialect: 'sqlite3'
2326
});
2427

25-
class Foo extends Model<InferAttributes<Foo>, InferCreationAttributes<Foo>> {
26-
declare id: CreationOptional<number>;
28+
class Parents extends Model {};
29+
Parents.init(
30+
{
31+
id: {
32+
type: DataTypes.INTEGER,
33+
autoIncrement: true,
34+
primaryKey: true,
35+
},
36+
name: DataTypes.TEXT,
37+
},
38+
{sequelize},
39+
);
40+
41+
class Children extends Model {};
42+
Children.init(
43+
{
44+
id: {
45+
type: DataTypes.INTEGER,
46+
autoIncrement: true,
47+
primaryKey: true,
48+
},
49+
parentId: {
50+
type: DataTypes.INTEGER,
51+
references: {
52+
model: Parents,
53+
key: 'id'
54+
}
55+
}
56+
},
57+
{sequelize}
58+
59+
)
60+
61+
class GrandChildren extends Model {};
62+
GrandChildren.init(
63+
{
64+
id: {
65+
type: DataTypes.INTEGER,
66+
autoIncrement: true,
67+
primaryKey: true,
68+
},
69+
parentId: {
70+
type: DataTypes.INTEGER,
71+
references: {
72+
model: Children,
73+
key: 'id'
74+
}
75+
}
76+
},
77+
{sequelize}
78+
);
2779

28-
@Attribute(DataTypes.TEXT)
29-
@NotNull
30-
declare name: string;
31-
}
80+
Parents.hasMany(Children)
81+
Children.belongsTo(Parents)
82+
Children.hasMany(GrandChildren)
83+
GrandChildren.belongsTo(Children)
3284

33-
sequelize.addModels([Foo]);
85+
// You can use sinon and chai assertions directly in your SSCCE.
86+
const spy = sinon.spy();
87+
sequelize.afterBulkSync(() => spy());
88+
await sequelize.sync({ force: true });
89+
expect(spy).to.have.been.called;
3490

35-
// You can use sinon and chai assertions directly in your SSCCE.
36-
const spy = sinon.spy();
37-
sequelize.afterBulkSync(() => spy());
38-
await sequelize.sync({ force: true });
39-
expect(spy).to.have.been.called;
91+
const created = await Parents.create()
92+
const {rows, count} = await Parents.findAndCountAll({
93+
include: {
94+
model: Children,
95+
required: false,
96+
include: [{
97+
model: GrandChildren,
98+
required: true,
99+
}]
100+
},
101+
limit: 1
102+
})
40103

41-
console.log(await Foo.create({ name: 'TS foo' }));
42-
expect(await Foo.count()).to.equal(1);
104+
console.log(rows);
105+
expect(created).to.not.be.undefined;
106+
expect(created).to.not.be.null;
107+
expect(count).to.equal(1);
43108
}

0 commit comments

Comments
 (0)