-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
149 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const { Table } = require('rethink-table') | ||
|
||
module.exports = async con => { | ||
const schema = { | ||
table: 'providers', | ||
indices: ['created', 'type', 'userid', 'name'], | ||
} | ||
|
||
const table = await Table(con, schema) | ||
|
||
return { | ||
...table, | ||
changes() { | ||
const query = table.table().changes() | ||
return table.streamify(query) | ||
}, | ||
streamSorted() { | ||
const query = table.table().orderBy({ index: 'created' }) | ||
return table.streamify(query) | ||
}, | ||
listSorted() { | ||
const q = table | ||
.table() | ||
.orderBy({ index: table.r.desc('created') }) | ||
.limit(100) | ||
.coerceTo('array') | ||
return table.run(q) | ||
}, | ||
listUserSorted(userid) { | ||
const query = table | ||
.table() | ||
.orderBy({ index: 'created' }) | ||
.filter({ userid }) | ||
.limit(100) | ||
.coerceTo('array') | ||
return table.run(query) | ||
}, | ||
create(name, userid, meta ={}) { | ||
assert(name, 'name required') | ||
assert(userid, 'userid required') | ||
|
||
return table.upsert({ | ||
done: false, | ||
verified: false, | ||
id: uuid(), | ||
name, | ||
userid, | ||
expires: null, | ||
created: Date.now(), | ||
updated: null, | ||
...meta | ||
}) | ||
}, | ||
listGroups(index) { | ||
const q = table | ||
.table() | ||
.group({ index }) | ||
.count() | ||
return table.run(q) | ||
}, | ||
listDone(done=false) { | ||
return table.getBy('done', done) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters