-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdb.js
executable file
·80 lines (66 loc) · 1.96 KB
/
db.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Database modules
* The MIT License
* Akhil Pandey
*/
const a = require('assert'),
c = require('./config'),
r = require('rethinkdb')
function pool (callback) {
r.connect({
host: c.rethink.host,
port: c.rethink.port,
db: c.rethink.db,
authkey: c.rethink.key
}, function (err, conn) {
a.ok (err == null, err)
callback (err, conn)
})
}
module.exports.init = function (callback) {
pool (function (err, conn) {
// create the database
r.dbList().run(conn, function (err, cursor) {
if(err) throw err
if(cursor.indexOf(c.rethink.db) > -1) {
console.log("[WARN] : The database exists")
}
else {
r.dbCreate(c.rethink.db).run(conn, function (err, cursor) {
if(err) throw err;
console.log(cursor)
})
}
conn.close()
})
// create the tables
r.tableList().run(conn, function (err, cursor) {
if(err) throw err
if(cursor.indexOf(c.list.table1) > -1) {
console.log("[NOTE] : Table " + c.list.table1 + " exists" )
}
if(cursor.indexOf(c.list.table2) > -1) {
console.log("[NOTE] : Table " + c.list.table2 + " exists" )
}
})
})
}
module.exports.postLinks = function (opt, callback) {
pool (function (err, conn) {
// post the links in the form a URI
r.db(c.rethink.db).table(c.list.table1)
.insert(opt).run(conn, function (err, cursor) {
if(err) throw err
})
})
}
module.exports.deleteLinks = function (opt, callback) {
pool (function (err, conn) {
r.db(c.rethink.db).table(m.list.table1).replace(r.row.without(opt))
.run(conn, function (err, cursor) {
if(err) throw err
console.log(cursor)
conn.close()
})
})
}