Skip to content

Commit b7885c8

Browse files
committed
rewritten model definition with ModelDefined type
1 parent ad3d13a commit b7885c8

10 files changed

Lines changed: 157 additions & 141 deletions

File tree

express-main-example/src/express/routes/instruments.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import { Request, Response } from "express";
2-
import sequelize from "../../sequelize";
2+
import { Instrument } from "../../sequelize/models/instrument.model";
33
import { getIdParam } from "../helpers";
44

5-
const { models } = sequelize;
6-
75
export async function getAll(req: Request, res: Response) {
8-
const instruments = await models.instrument.findAll();
6+
const instruments = await Instrument.findAll();
97
res.status(200).json(instruments);
108
}
119

1210
export async function getById(req: Request, res: Response) {
1311
const id = getIdParam(req);
14-
const instrument = await models.instrument.findByPk(id);
12+
const instrument = await Instrument.findByPk(id);
1513
if (instrument) {
1614
res.status(200).json(instrument);
1715
} else {
@@ -27,7 +25,7 @@ export async function create(req: Request, res: Response) {
2725
`Bad request: ID should not be provided, since it is determined automatically by the database.`
2826
);
2927
} else {
30-
await models.instrument.create(req.body);
28+
await Instrument.create(req.body);
3129
res.status(201).end();
3230
}
3331
}
@@ -37,9 +35,9 @@ export async function update(req: Request, res: Response) {
3735

3836
// We only accept an UPDATE request if the `:id` param matches the body `id`
3937
if (req.body.id === id) {
40-
await models.instrument.update(req.body, {
38+
await Instrument.update(req.body, {
4139
where: {
42-
id: id,
40+
id,
4341
},
4442
});
4543
res.status(200).end();
@@ -54,9 +52,9 @@ export async function update(req: Request, res: Response) {
5452

5553
export async function remove(req: Request, res: Response) {
5654
const id = getIdParam(req);
57-
await models.instrument.destroy({
55+
await Instrument.destroy({
5856
where: {
59-
id: id,
57+
id,
6058
},
6159
});
6260
res.status(200).end();

express-main-example/src/express/routes/orchestras.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import { Request, Response } from "express";
2-
import sequelize from "../../sequelize";
2+
import { Instrument } from "../../sequelize/models/instrument.model";
3+
import { Orchestra } from "../../sequelize/models/orchestra.model";
34
import { getIdParam } from "../helpers";
45

5-
const { models } = sequelize;
6-
76
export async function getAll(req: Request, res: Response) {
87
const orchestras =
98
"includeInstruments" in req.query
10-
? await models.orchestra.findAll({ include: models.instrument })
11-
: await models.orchestra.findAll();
9+
? await Orchestra.findAll({ include: Instrument })
10+
: await Orchestra.findAll();
1211
res.status(200).json(orchestras);
1312
}
1413

1514
export async function getById(req: Request, res: Response) {
1615
const id = getIdParam(req);
1716
const orchestra =
1817
"includeInstruments" in req.query
19-
? await models.orchestra.findByPk(id, { include: models.instrument })
20-
: await models.orchestra.findByPk(id);
18+
? await Orchestra.findByPk(id, { include: Instrument })
19+
: await Orchestra.findByPk(id);
2120
if (orchestra) {
2221
res.status(200).json(orchestra);
2322
} else {
@@ -33,7 +32,7 @@ export async function create(req: Request, res: Response) {
3332
`Bad request: ID should not be provided, since it is determined automatically by the database.`
3433
);
3534
} else {
36-
await models.orchestra.create(req.body);
35+
await Orchestra.create(req.body);
3736
res.status(201).end();
3837
}
3938
}
@@ -43,9 +42,9 @@ export async function update(req: Request, res: Response) {
4342

4443
// We only accept an UPDATE request if the `:id` param matches the body `id`
4544
if (req.body.id === id) {
46-
await models.orchestra.update(req.body, {
45+
await Orchestra.update(req.body, {
4746
where: {
48-
id: id,
47+
id,
4948
},
5049
});
5150
res.status(200).end();
@@ -60,9 +59,9 @@ export async function update(req: Request, res: Response) {
6059

6160
export async function remove(req: Request, res: Response) {
6261
const id = getIdParam(req);
63-
await models.orchestra.destroy({
62+
await Orchestra.destroy({
6463
where: {
65-
id: id,
64+
id,
6665
},
6766
});
6867
res.status(200).end();

express-main-example/src/express/routes/users.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import { Request, Response } from "express";
2-
import sequelize from "../../sequelize";
2+
import { User } from "../../sequelize/models/user.model";
33
import { getIdParam } from "../helpers";
44

5-
const { models } = sequelize;
6-
75
export async function getAll(req: Request, res: Response) {
8-
const users = await models.user.findAll();
6+
const users = await User.findAll();
97
res.status(200).json(users);
108
}
119

1210
export async function getById(req: Request, res: Response) {
1311
const id = getIdParam(req);
14-
const user = await models.user.findByPk(id);
12+
const user = await User.findByPk(id);
1513
if (user) {
1614
res.status(200).json(user);
1715
} else {
@@ -27,7 +25,7 @@ export async function create(req: Request, res: Response) {
2725
`Bad request: ID should not be provided, since it is determined automatically by the database.`
2826
);
2927
} else {
30-
await models.user.create(req.body);
28+
await User.create(req.body);
3129
res.status(201).end();
3230
}
3331
}
@@ -37,7 +35,7 @@ export async function update(req: Request, res: Response) {
3735

3836
// We only accept an UPDATE request if the `:id` param matches the body `id`
3937
if (req.body.id === id) {
40-
await models.user.update(req.body, {
38+
await User.update(req.body, {
4139
where: {
4240
id: id,
4341
},
@@ -54,7 +52,7 @@ export async function update(req: Request, res: Response) {
5452

5553
export async function remove(req: Request, res: Response) {
5654
const id = getIdParam(req);
57-
await models.user.destroy({
55+
await User.destroy({
5856
where: {
5957
id: id,
6058
},
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Sequelize } from "sequelize";
2+
3+
// In a real app, you should keep the database connection URL as an environment variable.
4+
// But for this example, we will just use a local SQLite database.
5+
// const sequelize = new Sequelize(process.env.DB_CONNECTION_URL);
6+
const sequelize = new Sequelize({
7+
dialect: "sqlite",
8+
storage: "sqlite-example-database/example-db.sqlite",
9+
logQueryParameters: true,
10+
benchmark: true,
11+
});
12+
13+
export default sequelize;
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { Sequelize } from "sequelize/types";
1+
import { Instrument } from "./models/instrument.model";
2+
import { Orchestra } from "./models/orchestra.model";
23

3-
export function applyExtraSetup(sequelize: Sequelize) {
4-
const { instrument, orchestra } = sequelize.models;
5-
6-
orchestra.hasMany(instrument);
7-
instrument.belongsTo(orchestra);
4+
export function applyExtraSetup() {
5+
Orchestra.hasMany(Instrument);
6+
Instrument.belongsTo(Orchestra);
87
}
Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,13 @@
1-
import { Sequelize } from "sequelize";
21
import { applyExtraSetup } from "./extra-setup";
32

4-
// In a real app, you should keep the database connection URL as an environment variable.
5-
// But for this example, we will just use a local SQLite database.
6-
// const sequelize = new Sequelize(process.env.DB_CONNECTION_URL);
7-
const sequelize = new Sequelize({
8-
dialect: "sqlite",
9-
storage: "sqlite-example-database/example-db.sqlite",
10-
logQueryParameters: true,
11-
benchmark: true,
12-
});
3+
import sequelize from "./connection";
134

14-
const modelDefiners = [
15-
require("./models/user.model"),
16-
require("./models/instrument.model"),
17-
require("./models/orchestra.model"),
18-
// Add more models here...
19-
// require('./models/item'),
20-
];
21-
22-
// We define all models according to their files.
23-
for (const modelDefiner of modelDefiners) {
24-
modelDefiner(sequelize);
25-
}
5+
// must reference all models, after importing the sequelize connection.
6+
import "./models/instrument.model";
7+
import "./models/orchestra.model";
8+
import "./models/user.model";
269

2710
// We execute any extra setup after the models are defined, such as adding associations.
28-
applyExtraSetup(sequelize);
11+
applyExtraSetup();
2912

30-
// We export the sequelize connection instance to be used around our app.
3113
export default sequelize;
Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
1-
import { DataTypes, Sequelize } from "sequelize";
1+
import { DataTypes, ModelDefined } from "sequelize";
2+
import sequelize from "../connection";
23

3-
// We export a function that defines the model.
4-
// This function will automatically receive as parameter the Sequelize connection object.
5-
module.exports = (sequelize: Sequelize) => {
6-
sequelize.define("instrument", {
7-
// The following specification of the 'id' attribute could be omitted
8-
// since it is the default.
9-
id: {
10-
allowNull: false,
11-
autoIncrement: true,
12-
primaryKey: true,
13-
type: DataTypes.INTEGER,
14-
},
15-
type: {
16-
allowNull: false,
17-
type: DataTypes.STRING,
18-
},
19-
// type: {
20-
// allowNull: false,
21-
// type: DataTypes.STRING,
22-
// validate: {
23-
// isIn: [['string', 'wind', 'percussion']]
24-
// }
25-
// },
26-
purchaseDate: {
27-
allowNull: false,
28-
type: DataTypes.DATE,
29-
},
30-
// We also want it to have a 'orchestraId' field, but we don't have to define it here.
31-
// It will be defined automatically when Sequelize applies the associations.
32-
});
33-
};
4+
interface InstrumentAttributes {
5+
id: number;
6+
type: string;
7+
purchaseDate: Date;
8+
orchestraId: number;
9+
}
10+
11+
interface InstrumentCreationAttributes
12+
extends Omit<InstrumentAttributes, "id"> {}
13+
14+
export const Instrument: ModelDefined<
15+
InstrumentAttributes,
16+
InstrumentCreationAttributes
17+
> = sequelize.define("instrument", {
18+
// The following specification of the 'id' attribute could be omitted
19+
// since it is the default.
20+
id: {
21+
allowNull: false,
22+
autoIncrement: true,
23+
primaryKey: true,
24+
type: DataTypes.INTEGER,
25+
},
26+
type: {
27+
allowNull: false,
28+
type: DataTypes.STRING,
29+
},
30+
// type: {
31+
// allowNull: false,
32+
// type: DataTypes.STRING,
33+
// validate: {
34+
// isIn: [['string', 'wind', 'percussion']]
35+
// }
36+
// },
37+
purchaseDate: {
38+
allowNull: false,
39+
type: DataTypes.DATE,
40+
},
41+
// We also want it to have a 'orchestraId' field, but we don't have to define it here.
42+
// It will be defined automatically when Sequelize applies the associations.
43+
});
Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
import { DataTypes, Sequelize } from "sequelize";
1+
import { DataTypes, ModelDefined } from "sequelize";
2+
import sequelize from "../connection";
23

3-
// We export a function that defines the model.
4-
// This function will automatically receive as parameter the Sequelize connection object.
5-
module.exports = (sequelize: Sequelize) => {
6-
sequelize.define("orchestra", {
7-
// The following specification of the 'id' attribute could be omitted
8-
// since it is the default.
9-
id: {
10-
allowNull: false,
11-
autoIncrement: true,
12-
primaryKey: true,
13-
type: DataTypes.INTEGER,
14-
},
15-
name: {
16-
allowNull: false,
17-
type: DataTypes.STRING,
18-
},
19-
});
20-
};
4+
interface OrchestraAttributes {
5+
id: number;
6+
name: string;
7+
}
8+
9+
interface OrchestraCreationAttributes extends Omit<OrchestraAttributes, "id"> {}
10+
11+
export const Orchestra: ModelDefined<
12+
OrchestraAttributes,
13+
OrchestraCreationAttributes
14+
> = sequelize.define("orchestra", {
15+
// The following specification of the 'id' attribute could be omitted
16+
// since it is the default.
17+
id: {
18+
allowNull: false,
19+
autoIncrement: true,
20+
primaryKey: true,
21+
type: DataTypes.INTEGER,
22+
},
23+
name: {
24+
allowNull: false,
25+
type: DataTypes.STRING,
26+
},
27+
});

0 commit comments

Comments
 (0)