-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathlist.js
47 lines (39 loc) · 1.03 KB
/
list.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
const { Table } = require('utils').Rethink
const assert = require('assert')
module.exports = name => async con => {
assert(name, 'requires table name')
const schema = {
table: name,
}
const table = await Table(con, schema)
table.create = props => {
assert(props.id, 'requires id')
props.list = props.list || []
return table.upsert(props)
}
table.push = async (id, data) => {
assert(id, 'requires id')
assert(data, 'requires data to push')
const item = await table.get(id)
item.list.push(data)
return table.upsert(item)
}
table.pop = async id => {
const item = await table.get(id)
const data = item.list.pop()
await table.upsert(item)
return data
}
table.dequeue = async id => {
const item = await table.get(id)
const data = item.list.shift()
await table.upsert(item)
return data
}
table.splice = async (id, ...args) => {
const item = await table.get(id)
const data = item.list.splice(...args)
return table.upsert(item)
}
return table
}