Skip to content

Commit b3a0299

Browse files
committed
Integrando una base de datos
1 parent bca9a4e commit b3a0299

2 files changed

Lines changed: 26 additions & 28 deletions

File tree

lib/db.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { MongoClient } = require('mongo')
3+
const { MongoClient } = require('mongodb')
44

55
const {
66
DB_USER,

lib/resolvers.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
11
'use strict'
22

3-
const courses = [
4-
{
5-
_id: 'oqeded',
6-
title: 'My title',
7-
teacher: 'My teacher',
8-
description: 'A description',
9-
topic: 'Programming'
10-
},
11-
{
12-
_id: 'oqeded2',
13-
title: 'My title 2',
14-
teacher: 'My teacher',
15-
description: 'A description',
16-
topic: 'Programming'
17-
},
18-
{
19-
_id: 'oqeded3',
20-
title: 'My title 3',
21-
teacher: 'My teacher',
22-
description: 'A description',
23-
topic: 'Programming'
24-
}
25-
]
3+
const connectDb = require('./db')
4+
const { ObjectID } = require('mongodb')
265

276
module.exports = {
287
Query: {
29-
getCourses: () => {
8+
getCourses: async () => {
9+
let db
10+
let courses = []
11+
12+
try {
13+
db = await connectDb()
14+
courses = await db.collection('courses').find().toArray()
15+
} catch (error) {
16+
console.error(error)
17+
}
18+
3019
return courses
3120
},
32-
getCourse: (root, args) => {
33-
const course = courses.filter(course => course._id === args.id)
34-
return course.pop()
21+
getCourse: async (root, { id }) => {
22+
let db
23+
let course
24+
25+
try {
26+
db = await connectDb()
27+
course = await db.collection('courses').findOne({ _id: ObjectID(id)})
28+
} catch (error) {
29+
console.error(error)
30+
}
31+
32+
return course
3533
}
3634
}
3735
}

0 commit comments

Comments
 (0)