Skip to content

Commit 66a017a

Browse files
committed
models
0 parents  commit 66a017a

File tree

12 files changed

+3567
-0
lines changed

12 files changed

+3567
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
*.log
3+
yarn-error.log
4+
dist
5+
DS_Store

README.md

Whitespace-only changes.

exercises/connect.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const mongoose = require('mongoose')
2+
3+
const connect = (url) => mongoose.connect(url, {
4+
useNewUrlParser: true
5+
})
6+
7+
module.exports = connect
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const User = require('../user')
2+
const crud = require('../crud')
3+
4+
describe('user crud', () => {
5+
describe('getUserById', () => {
6+
test('get user by object id', async () => {
7+
const user = await User.create({
8+
firstName: 'Nemo',
9+
lastName: 'Nemo',
10+
email: 'nemo@nemo.com'
11+
})
12+
const match = await crud.getUserById(user.id)
13+
14+
expect(match.id).toBe(user.id)
15+
})
16+
})
17+
describe('getAllUsers', () => {
18+
test('get all users in the DB', async () => {
19+
const usersToCreate = [
20+
{
21+
firstName: 'Nemo',
22+
lastName: 'Nemo',
23+
email: 'nemo@nemo.com'
24+
},
25+
{
26+
firstName: 'Nemo1',
27+
lastName: 'Nemo1',
28+
email: 'nemo1@nemo.com'
29+
},
30+
{
31+
firstName: 'Nemo3',
32+
lastName: 'Nemo3',
33+
email: 'nemo3@nemo.com'
34+
}
35+
]
36+
const users = await User.create(usersToCreate)
37+
const matchedUsers = await crud.getAllUsers()
38+
39+
expect(matchedUsers).toHaveLength(users.length)
40+
})
41+
})
42+
describe('createUser', () => {
43+
test('create a user', async () => {
44+
const userConfig = {
45+
firstName: 'Nemo',
46+
lastName: 'Nemo',
47+
email: 'nemo@nemo.com'
48+
}
49+
const {id} = await crud.createUser(userConfig)
50+
const match = await User.findById(id).exec()
51+
expect(match.id).toBe(id)
52+
})
53+
})
54+
describe('removeUserById', () => {
55+
test('remove user by id', async () => {
56+
const {id} = await User.create({
57+
firstName: 'Nemo',
58+
lastName: 'Nemo',
59+
email: 'nemo@nemo.com'
60+
})
61+
await crud.removeUserById(id)
62+
const match = await User.findById(id).exec()
63+
expect(match).toBe(null)
64+
})
65+
})
66+
describe('updateUserById', () => {
67+
test('update user by id', async () => {
68+
const {id} = await User.create({
69+
firstName: 'Nemo',
70+
lastName: 'Nemo',
71+
email: 'nemo@nemo.com'
72+
})
73+
const user = await crud.updateUserById(id, {betaUser: true})
74+
expect(user.id).toBe(id)
75+
expect(user.betaUser).toBe(true)
76+
})
77+
})
78+
})
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
const User = require('../user')
2+
3+
describe('user', () => {
4+
test('first name must be required', async () => {
5+
expect.assertions(1)
6+
7+
try {
8+
await User.create({
9+
lastName: 'Williams',
10+
email: 'sasha@gmail.com'
11+
})
12+
} catch (e) {
13+
expect(e).toBeTruthy()
14+
}
15+
})
16+
test('last name must be required', async () => {
17+
expect.assertions(1)
18+
19+
try {
20+
await User.create({
21+
firstName: 'Williams',
22+
email: 'sasha@gmail.com'
23+
})
24+
} catch (e) {
25+
expect(e).toBeTruthy()
26+
}
27+
})
28+
test('email must be required', async () => {
29+
expect.assertions(1)
30+
31+
try {
32+
await User.create({
33+
lastName: 'Williams',
34+
firstName: 'Sasha'
35+
})
36+
} catch (e) {
37+
expect(e).toBeTruthy()
38+
}
39+
})
40+
41+
test('email must be unique', async () => {
42+
expect.assertions(1)
43+
44+
try {
45+
await User.init() // wait for index to build
46+
await User.create([
47+
{
48+
lastName: 'Williams',
49+
firstName: 'Sasha',
50+
email: 'email@gmail.com'
51+
},
52+
{
53+
lastName: 'Haas',
54+
firstName: 'Mel',
55+
email: 'email@gmail.com'
56+
}
57+
])
58+
} catch (e) {
59+
expect(e).toBeTruthy()
60+
}
61+
})
62+
63+
test('betaUser should default to false', async () => {
64+
const user = await User.create({
65+
firstName: 'Tilly',
66+
lastName: 'Mills',
67+
email: 'tg@gmail.com'
68+
})
69+
70+
expect(user.betaUser).toBe(false)
71+
})
72+
73+
test('should have correct fields', async () => {
74+
const now = Date.now()
75+
const {_id, __v, ...user} = (await User.create({
76+
firstName: 'Tilly',
77+
lastName: 'Mills',
78+
email: 'tg@gmail.com',
79+
birthDate: now, // they were born today 😎
80+
address: {
81+
street: 'Heming way',
82+
houseNumber: 1234,
83+
zip: 91917,
84+
city: 'SF',
85+
State: 'CA'
86+
}
87+
})).toObject()
88+
89+
expect(user).toEqual({
90+
firstName: 'Tilly',
91+
lastName: 'Mills',
92+
email: 'tg@gmail.com',
93+
birthDate: new Date(now),
94+
betaUser: false,
95+
address: {
96+
street: 'Heming way',
97+
houseNumber: 1234,
98+
zip: 91917,
99+
city: 'SF',
100+
State: 'CA'
101+
}
102+
})
103+
})
104+
})

exercises/models/crud.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const User = require('./user')
2+
3+
const getUserById = (id) => {
4+
return User.findById(id)
5+
.exec()
6+
}
7+
8+
const getAllUsers = () => {
9+
return User.find({})
10+
.exec()
11+
}
12+
13+
const createUser = (userDetails) => {
14+
return User.create(userDetails)
15+
}
16+
const removeUserById = (id) => {
17+
return User.findByIdAndDelete(id).exec()
18+
}
19+
20+
const updateUserById = (id, update) => {
21+
return User.findByIdAndUpdate(id, update, {new: true}).exec()
22+
}
23+
24+
module.exports = {
25+
getUserById,
26+
getAllUsers,
27+
createUser,
28+
removeUserById,
29+
updateUserById
30+
}

exercises/models/user.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const mongoose = require('mongoose')
2+
3+
const userSchema = new mongoose.Schema({
4+
firstName: {
5+
type: String,
6+
required: true
7+
},
8+
lastName: {
9+
type: String,
10+
required: true
11+
},
12+
email: {
13+
type: String,
14+
required: true,
15+
unique: true
16+
},
17+
betaUser: {
18+
type: Boolean,
19+
default: false
20+
},
21+
birthDate: Date,
22+
address: {
23+
other: Boolean,
24+
street: String,
25+
houseNumber: Number,
26+
zip: Number,
27+
city: String,
28+
State: String
29+
}
30+
})
31+
32+
module.exports = mongoose.model('user', userSchema)

exercises/queries/index.js

Whitespace-only changes.

exercises/queries/post.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const mongoose = require('mongoose')
2+
3+
const postSchema = new mongoose.Schema({
4+
title: {
5+
type: String,
6+
required: true,
7+
unique: true,
8+
sparse: true
9+
},
10+
11+
12+
}, {timestamps: true})

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "intro-to-mongodb",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"author": "Scott Moss <willscottmoss@gmail.com>",
6+
"license": "MIT",
7+
"scripts": {
8+
"test": "jest"
9+
},
10+
"dependencies": {
11+
"cuid": "^2.1.4",
12+
"mongoose": "^5.3.2"
13+
},
14+
"jest": {
15+
"verbose": true,
16+
"testURL": "http://localhost/",
17+
"testEnvironment": "node",
18+
"setupTestFrameworkScriptFile": "<rootDir>/testconfig.js",
19+
"restoreMocks": true,
20+
"testPathIgnorePatterns": [
21+
"dist/"
22+
]
23+
},
24+
"devDependencies": {
25+
"jest": "^23.6.0"
26+
}
27+
}

0 commit comments

Comments
 (0)