forked from Keya-Moradi/Animation_Cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
89 lines (81 loc) · 2.28 KB
/
database.js
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
const db = require('./models');
// Implement CRUD for user model
// CREATE
async function createUser() {
try {
const newUser = await db.user.create({
name: "My Name",
email: "[email protected]"
});
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: '[email protected]' },
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: '[email protected]'
}
});
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: '[email protected]' }
});
console.log('number of rows deleted >>>', numOfRowsDeleted);
} catch (error) {
console.log('did not delete user(s) because of >>>', error);
}
}
// @todo run deleteUser function below