Installed knex and built a basic create article endpoint

pull/1/head
Nishant Arora 9 years ago
parent 6798039f32
commit 0ac1e61e4b

@ -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]);

Binary file not shown.

@ -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(){

@ -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!");

@ -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.
*/

@ -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) {
};

@ -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",

Loading…
Cancel
Save