Last active
August 20, 2017 21:17
-
-
Save ericwooley/89e001e17f4c5de65c0c4b4c098892ba to your computer and use it in GitHub Desktop.
snapshot migrations
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
// migrations/20170819134815_products.js | |
// Initial migration which adds the products table | |
exports.up = function (knex, Promise) { | |
return knex.schema.createTable('products', function (t) { | |
t.increments('id').unsigned().primary() | |
t.string('name').notNull() | |
t.text('description').nullable() | |
t.decimal('price', 6, 2).notNull() | |
}) | |
} | |
exports.down = function (knex, Promise) { | |
return knex.schema.dropTable('products') | |
} |
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
// /index.js | |
// Initial express setup, which connects to the database and serves products. | |
// The port should be set by the enviornment variable, | |
// so that we can run one for dev, one for integration, etc... | |
const port = process.env.API_PORT || 3000 | |
const express = require('express') | |
const app = express() | |
var bodyParser = require('body-parser') | |
// development config from knex file. | |
// You probably want to use different ones based on environment variables | |
const knexConfig = require('./knexfile').development | |
// Our connected knex | |
const knex = require('knex')(knexConfig) | |
// use the body parser | |
app.use(bodyParser.json({ type: 'application/json' })) | |
// maybe show some documentation here? up to you! | |
app.get('/', function (req, res) { | |
res.send('Hello World!') | |
}) | |
// returns a list of all products | |
app.get('/products', (req, res) => | |
knex.select('*').from('products').then(products => res.send(products)) | |
) | |
// create a product, req.body should be have a name, description, and price | |
app.post('/products', (req, res) => | |
knex | |
.table('products') | |
// Please take not that this is a TERRIBLE practice, and is only | |
// for demo purposes. Always validate and sanitize your input. | |
.insert(req.body) | |
// Send the created product as a response. | |
.then(([id]) => knex.first('*').from('products').where({ id })) | |
.then(product => res.send(product)) | |
// You also shouldn't return a raw error, an attacker could use this | |
// info to gain more insight into your app. | |
// This is useful in development though. | |
.catch(e => res.status(500).send(e)) | |
) | |
app.listen(port, function () { | |
console.log('example api running on port ' + 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
// /knexfile.js | |
// Allow the env to set the database file, | |
// for integration test to use a different db file. | |
module.exports = { | |
development: { | |
client: 'sqlite3', | |
connection: { | |
// dev.sqlite3 for development | |
// integration.sqlite3 for integration tests | |
filename: process.env.DATABASE_FILE || './dev.sqlite3' | |
} | |
}, | |
staging: { | |
// ... | |
}, | |
production: { | |
// ... | |
} | |
} |
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
// seeds/products.js | |
// This is a basic knex seed, which adds 3 products | |
// see http://knexjs.org/#Seeds-CLI | |
exports.seed = function (knex, Promise) { | |
// delete all products | |
return knex('products').del().then(function () { | |
// Inserts seed entries | |
return knex('products').insert([ | |
{ id: 1, name: 'p1', description: 'product 1', price: 3.93 }, | |
{ id: 2, name: 'p1', description: 'product 2', price: 2.62 }, | |
{ id: 3, name: 'p1', description: 'product 3', price: 14.97 } | |
]) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment