Skip to content

Commit

Permalink
SQL JOIN with Sessions now functional. So all tests now pass!
Browse files Browse the repository at this point in the history
  • Loading branch information
malee31 committed Oct 22, 2024
1 parent aed6973 commit 3945bfd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
25 changes: 15 additions & 10 deletions src/database/database-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,35 +102,40 @@ export async function getAllUsersWithStatus() {
attributes: ["id", "first_name", "last_name"],
include: [{
model: Session,
as: "session",
association: User.hasMany(Session, {
as: "session_data",
sourceKey: "session",
foreignKey: "session_id"
}),
attributes: ["session_id", "startTime", "endTime"],
where: {
session_id: {
password: {
[Op.eq]: col('User.password')
}
},
required: false // This is effectively the Sequelize equivalent of a `LEFT JOIN` in SQL
}],
raw: true
});
console.log("DONE")
if(users.length === 0) {
console.warn("No users in the database");
}

// Modify SQL output (self) to fit output schema
for(const userStatus of users) {
if(userStatus.session_id !== null) {
if(userStatus[`session_data.session_id`] !== null) {
userStatus.session = {
session_id: userStatus.session_id,
startTime: userStatus.startTime,
endTime: userStatus.endTime
session_id: userStatus[`session_data.session_id`],
startTime: userStatus[`session_data.startTime`],
endTime: userStatus[`session_data.endTime`]
};
} else {
userStatus.session = null;
}

delete userStatus.session_id;
delete userStatus.startTime;
delete userStatus.endTime;
delete userStatus[`session_data.session_id`];
delete userStatus[`session_data.startTime`];
delete userStatus[`session_data.endTime`];
}
return users;
}
Expand Down
8 changes: 1 addition & 7 deletions src/database/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ User.init({
// WARNING: Will not modify tables with an updated schema if they already exist
async function createTables() {
await sequelize.sync({
alter: true,
alter: false, // Change to true if models have changed
logging: false // Change to `console.log` if debugging or making alterations in development
});
}
Expand All @@ -138,17 +138,11 @@ async function createTables() {
// Restarting the server should regenerate new tables using createTables()
async function dropTables() {
// All logging turned on for this extremely destructive action
try {

await sequelize.drop({
cascade: true,
benchmark: true,
logging: console.info
});
} catch(err) {
console.error(err)
throw err;
}
}


Expand Down
2 changes: 1 addition & 1 deletion src/routes/users/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe("GET /", () => {
id: expect.any(Number),
first_name: expect.any(String),
last_name: expect.any(String),
session: expect.any(Number),
session: expect.toBeOneOf([null, expect.any(Number)])
})
])
});
Expand Down

0 comments on commit 3945bfd

Please sign in to comment.