Last active
November 17, 2015 07:04
-
-
Save Schniz/6fe237d2af38c4818631 to your computer and use it in GitHub Desktop.
Using RethinkDB for testing. Kill the last server, start a new server with a random port, migrate, seed.
This file contains 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
import rethinkdbdash from 'rethinkdbdash'; | |
import { readFileSync } from 'fs'; | |
import { resolve } from 'path'; | |
let port = undefined; | |
if (process.env.NODE_ENV === 'test') { | |
port = parseInt(readFileSync(resolve(__dirname, '../.testData/rethink.port'))); | |
} | |
export default rethinkdbdash({ db: 'shiftlift', port }); |
This file contains 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
kill -9 $(cat .testData/rethink.pid) &> /dev/null | |
rm -rf .testData/rethink{.pid,.port,_data} &> /dev/null | |
RETHINK_PORT=$(get-random-port) | |
echo $RETHINK_PORT > .testData/rethink.port | |
rethinkdb --no-http-admin --cluster-port $(get-random-port) --driver-port $RETHINK_PORT --pid-file .testData/rethink.pid -d .testData/rethink_data & | |
sleep 0.5 | |
rethink-migrate up --port $RETHINK_PORT --db shiftlift | |
NODE_ENV=test babel-node scripts/seed.js |
This file contains 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
import { readdirSync } from 'fs'; | |
import { resolve } from 'path'; | |
const r = require(resolve(__dirname, '../lib/rethinkdb')); | |
let seedPath = resolve(__dirname, './seed'); | |
let tables = readdirSync(seedPath); | |
let regex = /(.+)\.json$/; | |
const log = (data) => console.log(`seed: ${data}`); //eslint-disable-line | |
Promise.all(tables.map(tableFile => { | |
let table = regex.exec(tableFile)[1]; | |
let data = require(resolve(seedPath, tableFile)); | |
log(data); | |
return r.table(table).insert(data).run().then(() => log(`succeed seeding ${table}`)); | |
})).then(() => log('seed complete')).then(() => process.exit(0)); // eslint-disable-line |
npm install --save-dev rethink-migrate get-random-port
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
when i connect to
rethinkdb
I check ifNODE_ENV==='test'
, and if its true I fetch the port from therethink.port
file