-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathusers.js
66 lines (62 loc) · 1.59 KB
/
users.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
54
55
56
57
58
59
60
61
62
63
64
65
66
const { Table } = require('rethink-table')
const uuid = require('uuid/v4')
const assert = require('assert')
module.exports = async con => {
const schema = {
table: 'users',
indices: ['created', 'type', 'username', 'userid'],
compound: [
{
//Compound index
name: 'userid_type',
fields: ['userid', 'type'],
},
],
}
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)
},
async create(username, type = 'user', meta = {}) {
assert(username, 'username required')
username = username.toLowerCase()
const u = await table.hasBy('username', username)
assert(!u, 'User already exists with that username')
return table.upsert({
...meta,
verfied: false,
username,
type,
id: uuid(),
created: Date.now(),
updated: Date.now(),
})
}
// listTopSites() {
// const query = table
// .table()
// .getAll('case-opened', { index: 'type' })
// .group('caseOpeningSite')
// .count()
// .ungroup()
// .orderBy(table.r.desc('reduction'))
// .limit(100)
// return table.run(query)
// },
}
}