/
model.js
executable file
·47 lines (39 loc) · 1010 Bytes
/
model.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
import r from 'rethinkdb'
import getConn from './db'
export class Model {
constructor (table) {
this.table = table
this.conn = getConn()
this.r = r
}
find (id) {
return getConn()
.then((conn) => r.table(this.table).get(id).run(conn))
}
all () {
return getConn()
.then((conn) => r.table(this.table).run(conn))
.then((cursor) => cursor.toArray())
}
findBy (by) {
return getConn()
.then((conn) => r.table(this.table).filter(by).run(conn))
.then((cursor) => cursor.toArray())
}
changes () {
return getConn()
.then((conn) => r.table(this.table).changes().run(conn))
}
create (payload) {
return getConn()
.then((conn) => r.table(this.table).insert(payload).run(conn))
}
update (id, payload) {
return getConn()
.then((conn) => r.table(this.table).get(id).update(payload).run(conn))
}
remove (id) {
return getConn()
.then((conn) => r.table(this.table).get(id).delete().run(conn))
}
}