diff --git a/.travis.yml b/.travis.yml index 12833cf..902c510 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,9 @@ language: node_js +services: + - postgresql node_js: - "0.10" before_script: - cp ./settings_test.js.dist ./settings_test.js +addons: + postgresql: "9.3" diff --git a/README.md b/README.md index 02ad075..be042b0 100644 --- a/README.md +++ b/README.md @@ -74,51 +74,44 @@ Initialise settings: cp ./settings_test.js.dist ./settings_test.js -Set these environment variables: - - NODE_ENV=test - GALAXY_API_SETTINGS=./settings_test.js - DATABASE_URL='postgres://localhost/galaxy-api' - To run tests: npm test -To run tests without coverage and linting: +To run tests without destroying the database first: - npm run lab + npm run test-keepdb + +To run tests with coverage and linting: + + npm run test-verbose ## Database -If you haven't already, create a PostgreSQL database: +### `gulp` tasks - createdb galaxy-api +These are the available `gulp` tasks for PostgreSQL database and migration operations: + +* `gulp createdb` - create a PostgreSQL database using `settings.POSTGRES_URL`. +* `gulp dropdb` - delete the database. +* `gulp migratedb` - run migrations. +* `gulp migratedb-create --name ` - create a new migration file called ``. +* `gulp migratedb-up` - run all up migrations from the current state. +* `gulp migratedb-up --num ` - run `` up migrations from the current state. +* `gulp migratedb-down` - run a single down migration. +* `gulp migratedb-down --num ` - run `` down migrations from the current state. + +### `psql` commands To access the PostgreSQL prompt: psql -d galaxy-api -To run migrations, run this from the shell: +These are a few helpful PostgreSQL commands: - node node_modules/.bin/pg-migrate up - -To create a new migration: - - node node_modules/.bin/pg-migrate create - -To get a list the tables: - - \dt+ - -To get a table's schema: - - \d+ games - -To delete a table: - - drop table ; - -To view a table in "extended display": - - \x on +* `\h` - view list of available commands. +* `\dt+` - list all tables in the database. +* `\d+ ` - show a table's schema. +* `drop table ` - delete a table. +* `\x on` - view a table in "extended display" mode. diff --git a/gulpfile.js b/gulpfile.js index 855ce0d..6b40a7f 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,19 +1,113 @@ -var gulp = require('gulp'); -var jshint = require('gulp-jshint'); +var exec = require('child_process').exec; + +var argv = require('yargs').argv; // Using instead of deprecated `gulp.env`. +var inquirer = require('inquirer'); +var Promise = require('es6-promise').Promise; var stylish = require('jshint-stylish'); +var gulp = require('gulp'); +var jshint = require('gulp-jshint'); +var runSequence = require('run-sequence'); +var shell = require('gulp-shell'); -var sources = { - scripts: [ - '*.js', - 'api/**/*.js', - 'lib/**/*.js' - ] +var settings = require('./settings'); + + +var internals = { + dbUrl: settings.POSTGRES_URL, + dbName: settings.POSTGRES_URL.substring( + settings.POSTGRES_URL.lastIndexOf('/') + 1 + ), + sources: { + scripts: [ + '*.js', + 'api/**/*.js', + 'lib/**/*.js' + ] + } }; gulp.task('lint', function () { - return gulp.src(sources.scripts) + return gulp.src(internals.sources.scripts) .pipe(jshint({esnext: true})) .pipe(jshint.reporter(stylish)); }); + + +gulp.task('createdb', shell.task([ + 'createdb ' + internals.dbName +])); + + +gulp.task('dropdb', shell.task([ + 'dropdb --if-exists ' + internals.dbName +])); + + +gulp.task('migratedb', ['migratedb-up']); + + +// Sample usage: +// +// gulp migrate-up +// gulp migrate-up --num 3 +// +gulp.task('migratedb-up', shell.task([ + 'DATABASE_URL="' + internals.dbUrl + '" ' + + 'node node_modules/.bin/pg-migrate up ' + + (argv.num || argv.number || argv.n || '') +])); + + +// Sample usage: +// +// gulp migrate-down +// gulp migrate-down --num 3 +// +gulp.task('migratedb-down', shell.task([ + 'DATABASE_URL="' + internals.dbUrl + '" ' + + 'node node_modules/.bin/pg-migrate down ' + + (argv.num || argv.number || argv.n || '') +])); + + +// Sample usage: +// +// gulp migrate-down +// gulp migrate-down --name addTimestampColumn +// +gulp.task('migratedb-create', shell.task([ + 'DATABASE_URL="' + internals.dbUrl + '" ' + + 'node node_modules/.bin/pg-migrate create ' + + (argv.name || argv.n || '') +])); + + +gulp.task('refreshdb', function (cb) { + var runNow = function () { + return runSequence( + 'dropdb', + 'createdb', + 'migratedb', + cb + ); + }; + + if (process.argv.indexOf('--no-prompt') !== -1) { + return runNow(); + } + + inquirer.prompt({ + type: 'confirm', + name: 'val', + message: 'Are you sure you want to drop the database?', + default: false + }, function (res) { + if (!res.val) { + return cb(); + } + + runNow(); + }); +}); diff --git a/index.js b/index.js index 9d16494..3486ff7 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ var Hapi = require('hapi'); var routes = require('./api/routes'); -var server = new Hapi.Server(); +var server = module.exports = new Hapi.Server(); server.connection({ host: settings.HOST, port: settings.PORT, @@ -16,11 +16,15 @@ server.connection({ } } }); + routes(server); -server.start(function () { - console.log('Listening on %s', server.info.uri); -}); +// Do not start the server when this script is required by another script. +if (!module.parent) { + server.start(function () { + console.log('Listening on %s', server.info.uri); + }); +} server.register({ register: require('hapi-node-postgres'), diff --git a/package.json b/package.json index 81dd1d4..04ce6c4 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,11 @@ "jshint-stylish": "^0.4.0", "nodemon": "^1.2.1", "code": "^1.2.1", - "lab": "^5.1.0" + "lab": "^5.1.0", + "run-sequence": "^1.0.2", + "gulp-shell": "^0.2.11", + "inquirer": "^0.8.0", + "yargs": "^1.3.3" }, "engines": { "node": ">= 0.10.x", @@ -31,9 +35,10 @@ "start": "node index.js", "dev": "NODE_ENV=development GALAXY_API_SETTINGS=./settings_dev.js nodemon index.js", "prod": "NODE_ENV=production GALAXY_API_SETTINGS=./settings_prod.js node index.js", - "test": "./node_modules/lab/bin/lab -c -L", - "lab": "./node_modules/lab/bin/lab", - "test-cover": "./node_modules/lab/bin/lab -c -r html -o ./test/artifacts/coverage.html && open ./test/artifacts/coverage.html" + "test": "GALAXY_API_SETTINGS=./settings_test.js gulp refreshdb --no-prompt && GALAXY_API_SETTINGS=./settings_test.js ./node_modules/lab/bin/lab", + "test-keepdb": "GALAXY_API_SETTINGS=./settings_test.js ./node_modules/lab/bin/lab", + "test-verbose": "GALAXY_API_SETTINGS=./settings_test.js ./node_modules/lab/bin/lab -c -L", + "test-cover": "GALAXY_API_SETTINGS=./settings_test.js ./node_modules/lab/bin/lab -c -r html -o ./test/artifacts/coverage.html && open ./test/artifacts/coverage.html" }, "version": "0.0.5" } diff --git a/settings.js b/settings.js index 5d61248..d8d6ff6 100644 --- a/settings.js +++ b/settings.js @@ -19,7 +19,7 @@ exports.HOST = '0.0.0.0'; exports.PORT = 4000; // Usage: postgres://user:password@host/database -exports.POSTGRES_URL = process.env.DATABASE_URL || 'postgres://localhost/galaxy-api'; +exports.POSTGRES_URL = 'postgres://localhost/galaxy-api'; exports.SECRET = 'a secret string'; diff --git a/settings_test.js.dist b/settings_test.js.dist index 7be35b6..21ca03f 100644 --- a/settings_test.js.dist +++ b/settings_test.js.dist @@ -1,2 +1 @@ -module.exports = { -}; +exports.POSTGRES_URL = 'postgres://localhost/galaxy-api_test';