-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.js
55 lines (47 loc) · 1.19 KB
/
connection.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
import { MongoClient, ObjectId } from "mongodb"
import "dotenv/config"
const uri = process.env.URI;
const client = new MongoClient(uri);
const db = client.db(process.env.DB);
const col = db.collection(process.env.COLLECTION);
export async function main() {
try {
await client.connect();
} catch (error) {
console.error(error);
}
}
export const getTodoItems = () => {
try {
return col.find().toArray();
} catch (error) {
console.log(error);
}
}
export const getTodoItem = (id) => {
try {
return col.findOne({ _id: new ObjectId(id) });
} catch (error) {
console.log(error);
}
}
export async function createTodoItem(item) {
await col.insertOne(item);
return col.find().toArray();
}
export async function updateTodoItem(update) {
const { _id, title, completed } = update;
await col.updateOne(
{ _id: ObjectId(_id) },
{ $set: { title: title, completed: completed } }
);
return col.find().toArray();
}
export async function deleteTodoItem(id) {
await col.deleteOne({ _id: new ObjectId(id) });
return col.find().toArray();
}
export async function deleteAllCompletedTodos() {
await col.deleteMany({ completed: true });
return col.find().toArray();
}