simpleBookshelfApp
simpleBookshelfApp copied to clipboard
Simple Bookshelf App
Simple Application that uses Node.js(Express) and Bookshelf.js ORM
Description
This is a simple application that implements bookshelf.js JavaScript ORM(Object Relational Mapping) with Node.js
There is included an example that create, list, delete and list details of an user.
File Structure
simpleBookshelfApp/ - Location for simpleBookshelfApp specific files.
simpleBookshelfApp/config/ - Location for configuration files.
simpleBookshelfApp/config/db.js - Database configuratin file.
simpleBookshelfApp/models/ - Location for Models.
simpleBookshelfApp/models/User.js - User model file.
simpleBookshelfApp/public/ - Location for all client-side code (views, assets(js, css etc))
simpleBookshelfApp/routes/ - Location for application logic.
simpleBookshelfApp/routes/index.js - Main source
simpleBookshelfApp/routes/users.js - User logic.
simpleBookshelfApp/router.js - Application router.
package.json - npm package descriptor.
server.js - Main application file.
README.md - This file.
Packages (package.json)
This store node modules dependencies used in application.
express(v4.10.4) - This package is a MVC framework for Node.js
bookshelf(v0.4.9) - This package is javascript ORM for Node.js
knex(v0.7.3) - This package is for SQL Query Builder for Postgrate, MySQL etc.
mysql(v2.5.3) - This package is a MySQL driver for Node.js
body-parse(v1.10.0) - This package is a Node.js body parser
path(v0.4.9) - This package is a Node.js path module
All these package are installed by npm
(Node Package Manager). You have to type npm install
at directory package.json is stored and all packages will be installed automatically.
Code description
-
In the
config/db.js
is stored database configuration,First, there is declared an object
DBConfig
, in this object are stored data for database client and database optionsvar DBConfig = { client: 'mysql', connection: { host: 'localhost', user: 'root', password: 'mysql_password', database: 'database_name', charset: 'utf8' } };
Then, there is included
knex
package passingDBConfig
object, after that there is includedbookshelf
package passingknex
var knex = require('knex')(DBConfig); var bookshelf = require('bookshelf')(knex);
And finally, is exported
bookshelf
and we are done withconfig/db.js
file.module.exports.bookshelf = bookshelf;
-
Let's describe next file
models/User.js
First, is included
bookshelf
that we exported abovevar bookshelf = require('./../config/db').bookshelf;
Then,is declared a
User
object that extends built-inbookshelf Model
with a table name, in this caseusers
tablevar User = bookshelf.Model.extend({ tableName: 'users' });
Finally, export
User
and we are done with model.module.exports = { User: User };
-
The directory
routes
contains two files:index.js
andusers.js
.In index.js, first is included
path
package needed resolve relative path. Then, we export the main view file (index.html).var path = require('path'); var index = function (req, res) { res.sendFile(path.resolve(__dirname + '/../public/index.html')); }; module.exports.index = index;
Code below belongs to
users.js
file. First of all, there is included User model. To save a user, inputs are set by a request from client side, and withsave
method ofbookshelf
it is saved to db. If it will be saved,then
method repond user asjson
, othervise it fires and error.var Model = require('./../models/User'); /* Save a user */ var saveUser = function (req, res) { new Model.User({ username: req.body.username, email: req.body.email, name: req.body.name, age: req.body.age, location: req.body.location }).save() .then(function (user) { res.json(user); }).catch(function (error) { res.json(error); }); };
Below are returnded all users from database, useing another method of
bookshelf - fetchAll()
./* Get all users */ var getAllUsers = function (req, res) { new Model.User().fetchAll() .then(function (users) { res.json(users); }).catch(function (error) { res.json(error); }); };
First, this get the user id from url parameters and then using
destroy
method, it deletes the user in database./* Delete a user */ var deleteUser = function (req, res) { var userId = req.params.id; new Model.User().where('id', userId) .destroy() .catch(function (error) { res.json(error); }); };
The code below gets all details for a specified user.
/* Get a user */ var getUser = function (req, res) { var userId = req.params.id; new Model.User().where('id', userId) .fetch() .then(function (user) { res.json(user); }).catch(function (error) { res.json(error); }); };
Using
module.export
it exports all methods explained above./* Exports all methods */ module.exports = { saveUser: saveUser, getAllUsers: getAllUsers, deleteUser: deleteUser, getUser: getUser };
-
router.js
This is router. It included logic from routes collection and uses methods from these routes, and at least it export url routes for basic http methods using expressjs. This method will be used to
server.js
var user = require('./routes/users'); var index = require('./routes/index'); module.exports = function (app) { /* Index(main) route */ app.get('/', index.index); /* User Routes */ app.post('/users', user.saveUser); app.get('/users', user.getAllUsers); app.delete('/user/:id', user.deleteUser); app.get('/user/:id', user.getUser); };
-
server.js
Here the server is started and the routes are called. The
express
framework andbody-parser
are included.body-parser
is used to parsejson
.var express = require('express'); var app = express(); var bodyParser = require('body-parser'); app.use(bodyParser.json()); app.use('/public', express.static(__dirname + '/public')); /* Router */ require('./router')(app); app.listen(3000, function () { console.log('Go to localhost:3000'); });
Finally the simple app is done. Enjoy it!