-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathboth.js
53 lines (49 loc) · 1.51 KB
/
both.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
/* global check, ValidatedMethod */
export const makePagedRun = (collection, query) => ({ limit = 5, offset = 0, order = -1, orderBy = 'createdAt', ...rest }) => (
collection.find(query(rest), {
limit,
skip: offset,
sort: {
[orderBy]: order
}
}).fetch()
)
export const makeSingleRun = (collection, query) => (args) => (
collection.findOne(query(args))
)
export const makeDataMethod = (name, validate, run) => new ValidatedMethod({
name: 'pixdata:' + name,
validate ({ limit = 5, offset = 0, order = -1, orderBy = 'createdAt', ...rest }) {
check(limit, Number)
check(offset, Number)
check(order, Number)
check(orderBy, String)
validate(rest)
},
run
})
export const makePruneMethod = (name, collection, validate, query) => new ValidatedMethod({
name: `pixdata:${name}:checkExtant`,
validate ({ IDs, limit = 5, offset = 0, order = -1, orderBy = 'createdAt', ...rest }) {
// We're mostly pulling out the
check(limit, Number)
check(offset, Number)
check(order, Number)
check(orderBy, String)
check(IDs, [String])
validate(rest)
},
run (args) {
const { IDs } = args
// Find all documents in provided list, then match the missing ones
const extantDocs = collection.find({
$and: [
{ _id: { $in: IDs } },
query(args)
]
}, { fields: { _id: 1 } }).fetch()
const extantIDs = extantDocs.map(doc => doc._id)
const deletedIDs = IDs.filter((idToCheck) => !extantIDs.includes(idToCheck))
return deletedIDs
}
})