-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_todo.js
More file actions
43 lines (35 loc) · 1.07 KB
/
Copy pathinsert_todo.js
File metadata and controls
43 lines (35 loc) · 1.07 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
// Use Sequelize to insert a new row in the todo table
// Enter your code below
import { Sequelize, DataTypes } from "sequelize";
// 1. Establish the connection using your working port
const sequelize = new Sequelize("mysql://root:secret@localhost:3307/practice");
// 2. Define the Todo model so the script knows what "Todo" means
const Todo = sequelize.define("Todo", {
title: {
type: DataTypes.STRING,
allowNull: false
},
completed: {
type: DataTypes.BOOLEAN,
defaultValue: false
}
});
// 3. Wrap your execution logic in an async block
async function run() {
try {
// Sync model with database (creates table if it doesn't exist)
await sequelize.sync();
// YOUR ASSIGNMENT CODE GOES HERE:
const newTodo = await Todo.create({
title: "Finish Sequelize Assignment",
completed: false
});
console.log("Successfully inserted todo:", newTodo.toJSON());
} catch (error) {
console.error("Error executing script:", error);
} finally {
// Always close the connection when done
await sequelize.close();
}
}
run();