-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbTest.js
More file actions
128 lines (107 loc) · 3.2 KB
/
Copy pathdbTest.js
File metadata and controls
128 lines (107 loc) · 3.2 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
const db = require('./models');
// Implement CRUD for user model
// CREATE
async function createUser() {
try {
const newUser = await db.user.create({
name: "My Name",
email: "myemail@gmail.com"
});
console.log('my new user >>>', newUser);
} catch (error) {
console.log('new user was not created b/c of >>>', error);
}
}
// @todo run createUser function below
// READ
// find one user
async function findOneUser() {
try {
const user = await db.user.findOne({
where: { id: 1 }
});
console.log('current user here >>>', user);
} catch (error) {
console.log('did not find user b/c of >>>', error);
}
}
// @todo run findOneUser function below
// find all users
async function findAllUsers() {
try {
const users = await db.user.findAll();
console.log('all users here >>>', users);
} catch (error) {
console.log('did not find all users because of >>>', error);
}
}
// @todo run findAllUsers function below
// find one user
async function findOrCreate() {
try {
const users = await db.user.findOrCreate({
where: { email: 'brainsmith@gmail.com' },
defaults: {
name: 'Brian Smith',
},
});
console.log('all users here >>>', users);
} catch (error) {
console.log('did not find all users because of >>>', error);
}
}
// @todo run findOrCreate function below
// UPDATE
async function updateUser() {
try {
const numRowsUpdated = await db.user.update({
name: 'Brain Taco'
}, {
where: {
email: 'brainsmith@gmail.com'
}
});
console.log('number of users updated', numRowsUpdated);
} catch (error) {
console.log('did not update user(s) because of >>>', error);
}
}
// @todo run updateUser function below
// DELETE
async function deleteUser() {
try {
let numOfRowsDeleted = await db.user.destroy({
where: { email: 'brainsmith@gmail.com' }
});
console.log('number of rows deleted >>>', numOfRowsDeleted);
} catch (error) {
console.log('did not delete user(s) because of >>>', error);
}
}
// @todo run deleteUser function below
////==== chat tests
// const socket = io();
// // The user count. Can change when someone joins/leaves
// socket.on('count', function (data) {
// $('.user-count').html(data);
// });
// // When we receive a message
// // it will be like { user: 'username', message: 'text' }
// socket.on('message', function (data) {
// $('.chat').append('<p><strong>' + data.user + '</strong>: ' + data.message + '</p>');
// });
// // When the form is submitted
// $('form').submit(function (e) {
// // Avoid submitting it through HTTP
// e.preventDefault();
// // Retrieve the message from the user
// var message = $(e.target).find('input').val();
// // Send the message to the server
// socket.emit('message', {
// user: cookie.get('user') || 'Anonymous',
// message: message
// });
// // Clear the input and focus it for a new message
// e.target.reset();
// $(e.target).find('input').focus();
// });