-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
34 lines (28 loc) · 770 Bytes
/
index.js
File metadata and controls
34 lines (28 loc) · 770 Bytes
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
const { model } = require('./schema')
const electionsModel = {
create: (data) => {
return model.create(data).catch((err) => {
if (err.code === 11000) {
const error = new Error('unique error on key candidate')
error.name = 'DUPLICATE_KEY_ERROR'
error.param = Object.keys(err.keyPattern)[0]
error.value = err.keyValue[error.param]
throw error
}
throw err;
});
},
fetch: ({ query } = {}) => {
return model.find(query)
},
get: ({ query }) => {
return model.findOne({ query })
},
update: ({ query, update }) => {
return model.findOneAndUpdate(query, update, { new: true })
},
remove: ({ query }) => {
return model.deleteOne(query)
}
}
module.exports = electionsModel