From 0ac1e61e4b592cb97608c5699c5bd2d9c68c21a8 Mon Sep 17 00:00:00 2001 From: Nishant Arora Date: Tue, 23 Aug 2016 16:04:23 +0530 Subject: [PATCH] Installed knex and built a basic create article endpoint --- db.js | 9 +++++++++ db/matterwiki.sqlite | Bin 3072 -> 5120 bytes index.js | 17 +++++++++++++++++ install.js | 20 +++----------------- knexfile.js | 16 ++++++++++++++++ migrations/20160823145016_articles.js | 13 +++++++++++++ package.json | 2 ++ 7 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 db.js create mode 100644 knexfile.js create mode 100644 migrations/20160823145016_articles.js 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 ba619a07b3678479c858ca03ae52ea104b384a6e..473e2f58dbd08593865c0647454484ec29e6b5f8 100644 GIT binary patch literal 5120 zcmeHLPjAyO6u0BFqeEP{^g^X4Pe_f*pRHSf0|x@?twL-}NSq=sHJfSTbdCe-iESTa zp9Ap`_yk-yA?-E^i7&7NPOEG*6iq^0sQi?~&+on1{{3EF68Yt3UuX_*Qk5_b>&QhI zqbC3ep=EQ{%vlOCid%L}i+|Doq%NboXJ28pDu}f#o_C zGEzL@Qr`ipg^oFecT7clOtrhd?-}b(Iv7*X{GbRi!A$c($TWq4(X>doG3TSAGi&w| zQg8Y`{xDhhQ1XK?5xa_Mk;*KL(`Y!)=FZ}o7c#*%9f4m$^jW+tgP+mabf=1%YEFr%$T&|{i}Qw3;8`AOhdd}ZNFx?pHr)E0`#3;*?pQ?5C&Hw-a literal 3072 zcmeIzKS%;m90%}s?`PJp(*AduDQ{@7AyUu34UxqSf z10@}J@ZhZ45C26amfLNgb1Lav(1SC%6WhPiZ1Db&ghhmsX}|SMJBD%3N2EA{6z9ozROqnEFa~a)9jX;KwlH! zungvpS|@dt^| zF)+b?&|n`J=TR`mBVd$=!7MYF;UO^1gJ6oiV3G&G1baY(35>G@W2}ME&i(KCtM~Og Ox-WARa1;2?1ik=NjE-yo 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",