diff --git a/db.js b/db.js new file mode 100644 index 0000000..da5017a --- /dev/null +++ b/db.js @@ -0,0 +1,9 @@ +var config = require('./knexfile.js'); //requiring the knexfile that contains our connection object. +var env = 'development'; // specify the config enviroment +var knex = require('knex')(config[env]); // imports knex with our connection object (found in knexfile). + +// Export the knex library for use. All knex commands remain the same. +module.exports = knex; + +// Run the latest DB migrations whenever the server starts. +knex.migrate.latest([config]); diff --git a/db/matterwiki.sqlite b/db/matterwiki.sqlite index ba619a0..473e2f5 100644 Binary files a/db/matterwiki.sqlite and b/db/matterwiki.sqlite differ diff --git a/index.js b/index.js index 4cb7007..b92879d 100644 --- a/index.js +++ b/index.js @@ -4,12 +4,29 @@ Once we have enough endpoints defined we start breaking them into modules for be */ var express = require('express'); +var bodyParser = require('body-parser'); //body parser to parse the request body +var db = require('./db.js'); //this file contains the knex file import. it's equal to knex=require('knex') var app = express(); +app.use(bodyParser()); + app.get('/api',function(req,res){ + // this is just a sample endpoint I build to see if things are working res.send("Hey! You're looking at the matterwiki API"); }); +app.post('/new/article',function(req,res){ + /* + This endpoint takes the article title and article body from the request body. + It then saves those values in the database using the insert query. + After the operation is complete the endpoint returns the success object. + TODO: create formal guidelines for different object structures and follow that throughout the API. + */ + db('articles').insert({title: req.body.title, body: req.body.body}).then( function (result) { + res.json({ success: true, message: 'ok' }); // responds back to request + }) +}); + app.use(express.static(__dirname + '/client')); app.listen(5000 || process.env.PORT, function(){ diff --git a/install.js b/install.js index 673664d..2749b81 100755 --- a/install.js +++ b/install.js @@ -6,21 +6,7 @@ This is where we will write the complete setup script. Creating tables. Filling them with initial data. Creating the first user. We're still looking for better names for the command. Should be matterwiki or just wiki? -*/ -var sqlite3 = require('sqlite3').verbose(); -var db = new sqlite3.Database('./db/matterwiki.sqlite'); - -console.log('Installation Started!'); -db.serialize(function() { - db.run("CREATE TABLE articles (title TEXT)"); - var stmt = db.prepare("INSERT INTO lorem VALUES (?)"); - for (var i = 0; i < 10; i++) { - stmt.run("Ipsum " + i); - } - stmt.finalize(); - db.each("SELECT rowid AS id, info FROM lorem", function(err, row) { - console.log(row.id + ": " + row.info); - }); -}); -db.close(); +TODO: Figure out how to create the database setup script integrating with knex and bookshelf. +*/ +console.log("Installation started!"); diff --git a/knexfile.js b/knexfile.js new file mode 100644 index 0000000..f4f6032 --- /dev/null +++ b/knexfile.js @@ -0,0 +1,16 @@ +module.exports = { + development: { + client: 'sqlite3', + connection: { + filename: "./db/matterwiki.sqlite" + }, + useNullAsDefault: true, + debug: true + } + } + +/* +The development object is the connection object for the development database. +We need to create more for different environments (production, testing, staging). +This environment is being used in the db.js file in the root directory. Check there. +*/ diff --git a/migrations/20160823145016_articles.js b/migrations/20160823145016_articles.js new file mode 100644 index 0000000..7568dd5 --- /dev/null +++ b/migrations/20160823145016_articles.js @@ -0,0 +1,13 @@ + +exports.up = function(knex, Promise) { + return knex.schema.createTable('articles', function (table) { + table.increments(); + table.string('title'); + table.text('body'); + table.timestamps(); +}) +}; + +exports.down = function(knex, Promise) { + +}; diff --git a/package.json b/package.json index fa9164d..8dc5b2b 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,9 @@ "babel-loader": "^6.2.5", "babel-preset-es2015": "^6.13.2", "babel-preset-react": "^6.11.1", + "body-parser": "^1.15.2", "express": "^4.14.0", + "knex": "^0.11.10", "react": "^15.3.0", "react-dom": "^15.3.0", "react-router": "^2.6.1",