diff --git a/code/server/db.js b/code/server/db.js index 1846c97b4..6fe6c55ce 100644 --- a/code/server/db.js +++ b/code/server/db.js @@ -12,9 +12,29 @@ let Schema = mongoose.Schema mongoose.Promise = global.Promise; +let progressSchema = new Schema({ + date: { type: Date, required: true }, + value: { type: Number, required: true } +}); + +let goalSchema = new Schema({ + goalId: { type: String, required: true }, + type: { type: String, required: true }, + targetValue: { type: String, required: true }, + unit: { type: String, required: true }, + createdAt: { type: Date, default: Date.now }, + progress: {type: [progressSchema], required: false } //type: [goalSchema] once that is created +}, { + collection: 'goals' +}) + let userSchema = new Schema({ - firstname: { type: String, required: true }, - lastname: { type: String, required: true } + userId: { type: String, required: true }, + email: { type: String, required: true }, + passwordHashed: { type: String, required: true }, + name: { type: String, required: true }, + createdAt: { type: Date, default: Date.now }, + goals: {type: [goalSchema], required: false } //type: [goalSchema] once that is created }, { collection: 'users' }) @@ -25,7 +45,8 @@ module.exports = { connection = mongoose.createConnection(credentials.MONGO_URI) console.log("Connected to MongoDB!") userModel = connection.model("User", userSchema); - models = {userModel: userModel} + goalModel = connection.model("Goal", goalSchema); + models = {userModel: userModel, goalModel: goalModel} } return models }, diff --git a/code/server/initdb.js b/code/server/initdb.js index 92c609230..97ca1a545 100644 --- a/code/server/initdb.js +++ b/code/server/initdb.js @@ -3,40 +3,58 @@ const db = require("./db.js") const User = db.getModel().userModel +const Goal = db.getModel().goalModel async function init() { - try { - - // //Option to delete existing data for full reset + // Option to delete existing data for full reset // await User.deleteMany({}) - - let user3 = new User({ - firstname: "Rebecca", - lastname: "Albright" + // await Goal.deleteMany({}) + + let goal1 = new Goal({ + goalId: "G10001", + type: "sleep", + targetValue: 8, + unit: "hours", + createdAt: "2024-09-10", + progress: [{date: new Date("2024-09-11"), value: 7.5}, + {date: new Date("2024-09-12"), value: 8}] + }) + + let user1 = new User({ + userId: "U10001", + email: "test@gmail.com", + passwordHashed: "pa55w0rd", + name: "John Smith", + createdAt: "2024-09-01", + goals: [goal1] }) - let user4 = new User({ - firstname: "James", - lastname: "Madison" + + let user2 = new User({ + userId: "U10002", + email: "hello@bu.edu", + passwordHashed: "b0st0nuniversity", + name: "Jane Doe", + // createdAt: "2024-09-01", // testing the default param in schema works + goals: [] }) await Promise.all([ - user3.save(), - user4.save() + user1.save(), + user2.save(), + goal1.save() ]) let users = await User.find({}) - console.log(users) - } catch(error) { + let goals = await Goal.find({}) + console.log(goals) + } catch(error) { console.log(error) - } finally { - db.closeConnection() - } }