-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
280 lines (231 loc) · 8.64 KB
/
database.js
File metadata and controls
280 lines (231 loc) · 8.64 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { createPool } from 'mysql2';
const pool = createPool({
host: "localhost",
user: "root",
password: "admin", // Update with your MySQL password
database: "spotifyblend",
connectionLimit: 10,
multipleStatements: true
});
const createUserTable = `
CREATE TABLE IF NOT EXISTS User (
UserID INT AUTO_INCREMENT PRIMARY KEY,
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL,
Username VARCHAR(50) NOT NULL UNIQUE,
EmailID VARCHAR(100) NOT NULL UNIQUE,
Phoneno VARCHAR(20)
);
`;
const createArtistTable = `
CREATE TABLE IF NOT EXISTS Artist (
ArtistID INT AUTO_INCREMENT PRIMARY KEY,
ArtistName VARCHAR(100) NOT NULL
);
`;
const createGenreTable = `
CREATE TABLE IF NOT EXISTS Genre (
GenreID INT AUTO_INCREMENT PRIMARY KEY,
GenreName VARCHAR(50) NOT NULL
);
`;
const createSongTable = `
CREATE TABLE IF NOT EXISTS Song (
SongID INT AUTO_INCREMENT PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
ArtistID INT NOT NULL,
GenreID INT NOT NULL,
FOREIGN KEY (ArtistID) REFERENCES Artist(ArtistID) ON DELETE CASCADE,
FOREIGN KEY (GenreID) REFERENCES Genre(GenreID) ON DELETE CASCADE
);
`;
const createPlaylistTable=`
CREATE TABLE IF NOT EXISTS Playlist (
PlaylistID INT AUTO_INCREMENT PRIMARY KEY,
PlaylistName VARCHAR(100) NOT NULL,
Privacy ENUM('Public', 'Private') DEFAULT 'Public',
UserArtistID INT NOT NULL,
UserArtistType ENUM('User', 'Artist') NOT NULL,
FOREIGN KEY (UserArtistID) REFERENCES User(UserID) ON DELETE CASCADE
-- Note: For Artist, foreign key constraint will be handled in application logic
);
`;
const createPlaylistSongs=`
CREATE TABLE IF NOT EXISTS PlaylistSongs (
PlaylistID INT NOT NULL,
SongID INT NOT NULL,
FOREIGN KEY (PlaylistID) REFERENCES Playlist(PlaylistID) ON DELETE CASCADE,
FOREIGN KEY (SongID) REFERENCES Song(SongID) ON DELETE CASCADE
);
`;
const createFriendTable=`
CREATE TABLE IF NOT EXISTS Friend (
UserID1 INT NOT NULL,
UserID2 INT NOT NULL,
PRIMARY KEY (UserID1, UserID2),
FOREIGN KEY (UserID1) REFERENCES User(UserID) ON DELETE CASCADE,
FOREIGN KEY (UserID2) REFERENCES User(UserID) ON DELETE CASCADE
);
`;
const tables = [
createUserTable,
createArtistTable,
createGenreTable,
createSongTable,
createPlaylistTable,
createFriendTable,
createPlaylistSongs,
];
tables.forEach((query) => {
pool.query(query, (err, results) => {
if (err) {
console.error('Error creating table:', err);
} else {
console.log('Table created or already exists');
}
});
});
const insertSampleUsers = `
INSERT INTO User (Firstname, Lastname, Username, EmailID, Phoneno) VALUES
('John', 'Doe', 'johndoe', 'john@example.com', '1234567890'),
('Jane', 'Smith', 'janesmith', 'jane@example.com', '0987654321'),
('Alice', 'Johnson', 'alicej', 'alice@example.com', '5555555555');
`;
pool.query(insertSampleUsers, (err, results) => {
if (err) {
console.error('Error inserting sample users:', err);
} else {
console.log('Sample users inserted successfully');
// Insert sample artists into Artist table
const insertSampleArtists = `
INSERT INTO Artist (ArtistName) VALUES
('Artist 1'),
('Artist 2'),
('Artist 3');
`;
pool.query(insertSampleArtists, (err, results) => {
if (err) {
console.error('Error inserting sample artists:', err);
} else {
console.log('Sample artists inserted successfully');
// Insert sample genres into Genre table
const insertSampleGenres = `
INSERT INTO Genre (GenreName) VALUES
('Pop'),
('Rock'),
('Jazz');
`;
pool.query(insertSampleGenres, (err, results) => {
if (err) {
console.error('Error inserting sample genres:', err);
} else {
console.log('Sample genres inserted successfully');
// Insert sample songs into Song table
const insertSampleSongs = `
INSERT INTO Song (Title, ArtistID, GenreID) VALUES
('Song A', 1, 1),
('Song B', 1, 2),
('Song C', 2, 1),
('Song D', 2, 3),
('Song E', 3, 2),
('Song F', 3, 3),
('Song G', 1, 1),
('Song H', 2, 2),
('Song I', 3, 3),
('Song J', 1, 1);
`;
pool.query(insertSampleSongs, (err, results) => {
if (err) {
console.error('Error inserting sample songs:', err);
} else {
console.log('Sample songs inserted successfully');
// Insert sample playlists into Playlist table
const insertSamplePlaylists = `
INSERT INTO Playlist (PlaylistName, Privacy, UserArtistID, UserArtistType) VALUES
('Chill Vibes', 'Public', 1, 'User'),
('Workout Hits', 'Private', 2, 'User'),
('Top 50', 'Public', 1, 'Artist'),
('Indie Mix', 'Public', 4, 'User');
`;
pool.query(insertSamplePlaylists, (err, results) => {
if (err) {
console.error('Error inserting sample playlists:', err);
} else {
console.log('Sample playlists inserted successfully');
// Insert sample PlaylistSongs for user playlists
const insertSamplePlaylistSongs = `
INSERT INTO PlaylistSongs (PlaylistID, SongID) VALUES
(1, 1), (1, 2), (1, 3), (1, 4), (1, 5),
(2, 6), (2, 7), (2, 8), (2, 9), (2, 10),
(4, 1), (4, 3), (4, 5), (4, 7), (4, 9);
`;
pool.query(insertSamplePlaylistSongs, (err, results) => {
if (err) {
console.error('Error inserting sample playlist songs:', err);
} else {
console.log('Sample playlist songs inserted successfully');
// Insert sample blends into Blend table
const insertSampleBlends = `
INSERT INTO Blend (UserID, SongID) VALUES
(1, 1), (1, 2), (2, 3), (2, 4), (3, 5);
`;
// Insert sample friends into Friend table
const insertSampleFriends = `
INSERT INTO Friend (UserID1, UserID2) VALUES
(1, 2),
(2, 3),
(1, 3);
`;
pool.query(insertSampleFriends, (err, results) => {
if (err) {
console.error('Error inserting sample friends:', err);
} else {
console.log('Sample friends inserted successfully');
}
});
}
});
}
});
}
});
}
});
}
});
}
});
;
const insertFriendSuggestionData = `
INSERT INTO User (Firstname, Lastname, Username, EmailID, Phoneno) VALUES
('Alice', 'Smith', 'alice1', 'alice1@example.com', '1234567890'),
('Bob', 'Johnson', 'bob', 'bob@example.com', '2345678901'),
('Charlie', 'Williams', 'charlie', 'charlie@example.com', '3456789012'),
('Diana', 'Brown', 'diana', 'diana@example.com', '4567890123');
INSERT INTO Song (Title, ArtistID, GenreID) VALUES
('Pop Song 1', 1, 1),
('Pop Song 2', 1, 1),
('Rock Song 1', 2, 2),
('Jazz Song 1', 3, 3),
INSERT INTO Playlist (PlaylistName, UserArtistID, UserArtistType) VALUES
('Alice Playlist', 4, 'User'),
('Bob Playlist', 5, 'User'),
('Charlie Playlist', 6, 'User'),
('Diana Playlist', 7, 'User');
INSERT INTO PlaylistSongs (PlaylistID, SongID) VALUES
(4, 1),
(4, 2),
(5, 1),
(5, 3),
(6, 4),
(7, 5);
INSERT INTO Friend (UserID1, UserID2) VALUES
(1, 3);
`;
pool.query(insertFriendSuggestionData, (err, results) => {
if (err) {
console.error('Error inserting friend suggestion sample data:', err);
} else {
console.log('Friend suggestion sample data inserted successfully');
}
});