|
|
@@ -1,6 +1,12 @@ |
|
|
|
let r = require('rethinkdb'); |
|
|
|
let serverConfig = require('../config/serverConfig'); |
|
|
|
|
|
|
|
const dbConfig = { |
|
|
|
host: serverConfig.dbHost, |
|
|
|
port: serverConfig.dbPort, |
|
|
|
db: 'live' |
|
|
|
}; |
|
|
|
|
|
|
|
const tables = [ |
|
|
|
'character', |
|
|
|
'characterList', |
|
|
@@ -17,38 +23,39 @@ const tables = [ |
|
|
|
]; |
|
|
|
|
|
|
|
module.exports = { |
|
|
|
connection: null, |
|
|
|
useDb: 'live', |
|
|
|
staticCon: null, |
|
|
|
|
|
|
|
init: async function (cbReady) { |
|
|
|
const dbConfig = { |
|
|
|
host: serverConfig.dbHost, |
|
|
|
port: serverConfig.dbPort |
|
|
|
}; |
|
|
|
|
|
|
|
this.connection = await r.connect(dbConfig); |
|
|
|
this.staticCon = await this.getConnection(); |
|
|
|
|
|
|
|
await this.connection.use(this.useDb); |
|
|
|
await this.create(); |
|
|
|
|
|
|
|
cbReady(); |
|
|
|
}, |
|
|
|
|
|
|
|
getConnection: async function () { |
|
|
|
return await r.connect(dbConfig); |
|
|
|
}, |
|
|
|
|
|
|
|
create: async function () { |
|
|
|
const con = await this.getConnection(); |
|
|
|
|
|
|
|
try { |
|
|
|
await r.dbCreate(this.useDb).run(this.connection); |
|
|
|
await r.dbCreate(this.useDb).run(con); |
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (const table of tables) { |
|
|
|
try { |
|
|
|
await r.tableCreate(table).run(this.connection); |
|
|
|
await r.tableCreate(table).run(con); |
|
|
|
} catch (e) { |
|
|
|
if (!e.message.includes('already exists')) |
|
|
|
_.log(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
con.close(); |
|
|
|
}, |
|
|
|
|
|
|
|
getAsync: async function ({ |
|
|
@@ -57,15 +64,19 @@ module.exports = { |
|
|
|
isArray, |
|
|
|
noDefault |
|
|
|
}) { |
|
|
|
const con = await this.getConnection(); |
|
|
|
|
|
|
|
let res = await r.table(table) |
|
|
|
.get(key) |
|
|
|
.run(this.connection); |
|
|
|
.run(con); |
|
|
|
|
|
|
|
if (res) |
|
|
|
return res.value; |
|
|
|
else if (isArray && !noDefault) |
|
|
|
return []; |
|
|
|
|
|
|
|
con.close(); |
|
|
|
|
|
|
|
return res; |
|
|
|
}, |
|
|
|
|
|
|
@@ -75,8 +86,10 @@ module.exports = { |
|
|
|
isArray, |
|
|
|
noDefault |
|
|
|
}) { |
|
|
|
const con = await this.getConnection(); |
|
|
|
|
|
|
|
let res = await r.table(table) |
|
|
|
.run(this.connection); |
|
|
|
.run(con); |
|
|
|
|
|
|
|
res = await res.toArray(); |
|
|
|
|
|
|
@@ -85,6 +98,8 @@ module.exports = { |
|
|
|
else if (isArray && !noDefault) |
|
|
|
return []; |
|
|
|
|
|
|
|
con.close(); |
|
|
|
|
|
|
|
return res; |
|
|
|
}, |
|
|
|
|
|
|
@@ -94,6 +109,8 @@ module.exports = { |
|
|
|
value, |
|
|
|
conflict = 'update' |
|
|
|
}) { |
|
|
|
const con = await this.getConnection(); |
|
|
|
|
|
|
|
await r.table(table) |
|
|
|
.insert({ |
|
|
|
id, |
|
|
@@ -101,23 +118,29 @@ module.exports = { |
|
|
|
}, { |
|
|
|
conflict |
|
|
|
}) |
|
|
|
.run(this.connection); |
|
|
|
.run(con); |
|
|
|
|
|
|
|
con.close(); |
|
|
|
}, |
|
|
|
|
|
|
|
deleteAsync: async function ({ |
|
|
|
key, |
|
|
|
table |
|
|
|
}) { |
|
|
|
const con = await this.getConnection(); |
|
|
|
|
|
|
|
await r.table(table) |
|
|
|
.get(key) |
|
|
|
.delete() |
|
|
|
.run(this.connection); |
|
|
|
.run(con); |
|
|
|
|
|
|
|
con.close(); |
|
|
|
}, |
|
|
|
|
|
|
|
subscribe: function (table) { |
|
|
|
return r.table(table) |
|
|
|
.changes() |
|
|
|
.run(this.connection); |
|
|
|
.run(this.staticCon); |
|
|
|
}, |
|
|
|
|
|
|
|
append: async function ({ |
|
|
@@ -126,6 +149,8 @@ module.exports = { |
|
|
|
value, |
|
|
|
field |
|
|
|
}) { |
|
|
|
const con = await this.getConnection(); |
|
|
|
|
|
|
|
await r.table(table) |
|
|
|
.get(key) |
|
|
|
.update(row => { |
|
|
@@ -139,16 +164,22 @@ module.exports = { |
|
|
|
} |
|
|
|
); |
|
|
|
}) |
|
|
|
.run(this.connection); |
|
|
|
.run(con); |
|
|
|
|
|
|
|
con.close(); |
|
|
|
}, |
|
|
|
|
|
|
|
exists: async function ({ |
|
|
|
table, |
|
|
|
key |
|
|
|
}) { |
|
|
|
const con = await this.getConnection(); |
|
|
|
|
|
|
|
let res = await r.table(table) |
|
|
|
.get(key) |
|
|
|
.run(this.connection); |
|
|
|
.run(con); |
|
|
|
|
|
|
|
con.close(); |
|
|
|
|
|
|
|
return !!res; |
|
|
|
} |
|
|
|