forked from thinkb4/a-walk-in-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskill.js
53 lines (50 loc) · 1.21 KB
/
skill.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
const DATASET_KEY = 'skills';
/**
* @param {Object} Ø
* @param {Object} Ø.db
* @param {Function} Ø.prepareFilter
* @param {String} Ø.datasetKey
*/
const model = ({ db, prepareFilter, datasetKey = DATASET_KEY } = {}) => {
return {
/**
*
* @returns {Object}
*/
randomRecord() {
/**
* @see https://openbase.io/js/lowdb
*/
const records = db.get(datasetKey).value();
return Reflect.get(records, Math.floor(Math.random() * records.length));
},
/**
*
* @param {Object|Function} filter { key: value [, key: value] } | predicate
*
* @returns {Object|Undefined}
*/
find: prepareFilter((filter) => {
return filter && db.get(datasetKey)
.find(filter)
.value()
}),
/**
*
* @param {Object|Function} filter { key: value [, key: value] } | predicate
* @param {Array<String>} subset an array of records id
*
* @returns {Array<Object>}
*/
filter: prepareFilter((filter, subset) => {
return db.get(datasetKey)
.filter(record => !subset || subset.includes(record.id))
.filter(filter)
.value()
}),
}
}
module.exports = {
model,
DATASET_KEY
};