forked from du5rte/graphql-mongodb-projection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoDBMockup.js
40 lines (33 loc) · 963 Bytes
/
MongoDBMockup.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
import users from './users.json'
export default class MongoDBMockup {
static findOne(query, projection) {
let result = users.filter(user => user._id === query._id );
result = result.length ? result.shift() : {};
if ( result.friends ) {
result.friends = result.friends.map(friend => (users.filter(user => user._id === friend ).shift()))
}
if (projection) {
return this.projection(result, projection)
} else {
return result
}
}
static find(query, projection) {
let result = users;
if (projection) {
return result.map(entry => this.projection(entry, projection))
} else {
return result
}
}
static projection(result, projection) {
return Object.entries(result).reduce((filtered, entry, index, array) => {
let [key, value] = entry
if (projection[key]) {
return {...filtered, [key]: value}
} else {
return filtered
}
}, {})
}
}