Utilizing Sequelize for executing MySQL queries in my codebase, I have meticulously defined Models and connected them with their associations.
Creating a music playlist structure with:
- Artists who can have multiple songs.
- Playlists that contain multiple songs as well.
- Songs belonging to one or multiple playlists and may have one or multiple associated artists.
The models are structured as follows:
I have created an interface within each model containing the necessary fields for type safety and convenience.
ARTIST
export interface IArtist {
ID_ARTIST: number,
NAME: string
}
export const Artist = seq.define<Model<IArtist>>("Artist", {
ID_ARTIST: {
primaryKey: true,
type: DataTypes.INTEGER,
allowNull: false,
autoIncrement: true
},
NAME: DataTypes.STRING(30)
}, {
timestamps: true,
createdAt: true,
updatedAt: false,
tableName: "ARTISTS"
})
PLAYLIST
export interface IPlaylist {
ID_PLAYLIST: number,
NAME: string
}
export const PlayList = seq.define<Model<IPlaylist>>("Playlist", {
ID_PLAYLIST: {
primaryKey: true,
type: DataTypes.INTEGER,
allowNull: false,
autoIncrement: true,
},
NAME: DataTypes.STRING(20)
}, {
timestamps: true,
createdAt: true,
updatedAt: false,
tableName: "PLAYLISTS"
})
SONG
export interface ISong {
ID_SONG: number,
ID_ARTIST: number,
ID_PLAYLIST: number,
DURATION: number,
NAME: string
}
export const Song = seq.define<Model<ISong>>("Song", {
ID_SONG: {
primaryKey: true,
type:</questionbody>
<exquestionbody>
<div class="question">
<p>Im trying to use Sequelize for MySQL queries, in my code I defined my Models and links them with his respective associations.</p>
<p>Trying to simulate a music playlist that have:
Artists, they can have multiples songs.</p>
<p>Playlist, has multiples songs too.</p>
<p>Songs, they are part of 1 or multiple playlists and have 1 or multiple artists.</p>
<p>That models are defines like this:</p>
<blockquote>
<p>In each model I defined a interface with his respective fields for safe type and comfort</p>
</blockquote>
<p>ARTIST</p>
<pre><code>export interface IArtist {
ID_ARTIST: number,
NAME: string
}
export const Artist = seq.define<Model<IArtist>>("Artist", {
ID_ARTIST: {
primaryKey: true,
type: DataTypes.INTEGER,
allowNull: false,
autoIncrement: true
},
NAME: DataTypes.STRING(30)
}, {
timestamps: true,
createdAt: true,
updatedAt: false,
tableName: "ARTISTS",
})
PLAYLIST
export interface IPlaylist {
ID_PLAYLIST: number,
NAME: string
}
export const PlayList = seq.define<Model<IPlaylist>>("Playlist", {
ID_PLAYLIST: {
primaryKey: true,
type: DataTypes.INTEGER,
allowNull: false,
autoIncrement: true,
},
NAME: DataTypes.STRING(20)
}, {
timestamps: true,
createdAt: true,
updatedAt: false,
tableName: "PLAYLISTS"
})
SONG
export interface ISong {
ID_SONG: number,
ID_ARTIST: number,
ID_PLAYLIST: number,
DURATION: number,
NAME: string
}
export const Song = seq.define<Model<ISong>>("Song", {
ID_SONG: {
primaryKey: true,
type:</exquestionbody>
<answer1>
<div class="answer" i="77628315" l="4.0" c="1702046406" a="QW5hdG9seQ==" ai="1376618">
<p>You just need to define paired associations with the same <code>foreignKey
option, see my answer here