Last active
September 12, 2016 17:13
-
-
Save chuck0523/8526426d5b2643d9d18f2a6ebe24e413 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Reference : https://github.com/rethinkdb/rethinkdb-example-nodejs/blob/master/todo-angular-express/app.js | |
#!/usr/bin/env node | |
'use strict' | |
const express = require('express') | |
const horizon = require('@horizon/server') | |
const async = require('async') | |
const r = horizon.r | |
const app = express() | |
app.get('/warning/:id', (req, res) => { | |
const id = parseInt(req.params.id) | |
r.connect({ | |
db: 'messages' | |
}, (err, conn) => { | |
if(!err) { | |
const warning = r.table('warning').get(id).run(conn) | |
warning.then((result) => { | |
res.json(result) | |
}) | |
} else { | |
console.log(err) | |
} | |
}) | |
}) | |
app.get('/warnings', (req, res) => { | |
r.connect({ | |
db: 'messages' | |
}, (err, conn) => { | |
r.table('warning').between(0, 101).run(conn).then((cursor) => { | |
return cursor.toArray() | |
}).then((result) => { | |
res.json(result) | |
}).error((err) => { | |
console.log(err) | |
}) | |
}) | |
}) | |
app.get('/warning/create/:id', (req, res) => { | |
const id = parseInt(req.params.id) | |
r.connect({ | |
db: 'messages' | |
}, (err, conn) => { | |
if(!err) { | |
r.table('warning').insert({ | |
id: id, | |
text: 'warning!' | |
}).run(conn).then(() => { | |
res.send('posted') | |
}) | |
} else { | |
console.log(err) | |
} | |
}) | |
}) | |
app.get('/warning/delete/:id', (req, res) => { | |
const id = parseInt(req.params.id) | |
r.connect({ | |
db: 'messages' | |
}, (err, conn) => { | |
if(!err) { | |
r.table('warning').get(id).delete().run(conn) | |
res.send('deleted') | |
} else { | |
console.log(err) | |
} | |
}) | |
}) | |
const startExpress = (conn) => { | |
const http_server = app.listen(8181) | |
const options = { rdb_host: 'localhost', auth: { token_secret: 'my_super_secret_secret' } } | |
horizon(http_server, options) | |
} | |
// 接続 | |
const connectRethinkDB = (cb) => { | |
r.connect({ | |
host: 'localhost', | |
port: 28015, | |
authKey: '', | |
db: 'trialDB' | |
}, cb) | |
} | |
// 無ければDBを作成 | |
const createDB = (conn, cb) => { | |
r.dbList().contains('messages').do((containsDb) => { | |
return r.branch( | |
containsDb, | |
{created: 0}, | |
r.dbCreate('messages') | |
) | |
}).run(conn, (err) => { | |
cb(err, conn) | |
}) | |
} | |
// 無ければTableを作成 | |
const createTable = (conn, cb) => { | |
r.tableList().contains('messages').do((containsTable) => { | |
return r.branch( | |
containsTable, | |
{created: 0}, | |
r.tableCreate('messages') | |
) | |
}).run(conn, (err) => { | |
cb(err, conn) | |
}) | |
} | |
// 複数処理を非同期で実行 | |
async.waterfall([ | |
connectRethinkDB, | |
createDB, | |
createTable | |
], (err, conn) => { | |
if(err) { | |
console.log(err) | |
process.exit(1) | |
return | |
} | |
startExpress(conn) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment