From 2efe70741686d9fd31a1d45923223302f6c67145 Mon Sep 17 00:00:00 2001 From: Mark Tyers Date: Mon, 3 Jul 2017 10:53:23 +0100 Subject: [PATCH] installing mongoDB --- 00 Introduction.md | 7 +++++ 07 Data Persistence.md | 59 ++++++++++++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/00 Introduction.md b/00 Introduction.md index d6719e8..5eb5537 100644 --- a/00 Introduction.md +++ b/00 Introduction.md @@ -115,3 +115,10 @@ Close the terminal and re-launch. Now you can open any directory. - `code ~/Documents/bookshop/` opens Visual Studio Code with the contents of the directory specified. - `code .` opens Visual Studio Code with the contents of the _current directory_. + +### Using a Mac for Development + +Computers running MacOS are popular with NodeJS developers. MacOS is a flavour of Unix just like Linux and so all of the commands used in this book will work just fine. The biggest issue is the lack of a decent package manager such as the `apt` package manager found on Ubuntu. To get round this you should install the [Homebrew](https://brew.sh) package manager. +``` +/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" +``` diff --git a/07 Data Persistence.md b/07 Data Persistence.md index ba84bb9..09cc46e 100644 --- a/07 Data Persistence.md +++ b/07 Data Persistence.md @@ -44,34 +44,32 @@ exports.addBook = function(isbn, callback) { } ``` -## Relational Database - -Familiar data storage +## 2 Document Database -Supports complex queries +Stores JavaScript objects -Difficult to scale +Can be queried using JavaScript -Data needs normalising -```javascript -const mysql = require('mysql') -const request = require('request') +### 2.1 Installing MongoDB -const connection = mysql.createConnection({ host: 'xxx', port: '3306', user: 'xxx', password: 'xxx', database: 'xxx' }) +There are several ways to access a MongoDB database. You can make use of a cloud provider such as [mLab](https://mlab.com) or you can install the database locally on your development machine. -const sql = 'INSERT INTO books (id, title, authors, description) VALUES (NULL, "The Hobbit, "J.R.R. Tolkien", "Ring found")' +#### 2.1.1 Installing on Ubuntu -connection.query(sql, (err, rows) => { - if (err) callback( Error(`database error: ${err}`) ) - return callback(null, rows) -}) -``` -## Document Database -Stores JavaScript objects +#### 2.1.2 Installing on MacOS -Can be queried using JavaScript +The best way to install it on a Mac is to use the [Homebrew](https://brew.sh) package manager. The installation instructions can be found in the introductory chapter of this book. To install MongoDB +``` +brew install mongodb +``` +You need to create a data directory and change its permissions before starting the database server. +``` +$ mkdir -p /data/db +$ sudo chown -R `id -un` /data/db +$ mongod +``` Mongoose Example. @@ -109,6 +107,29 @@ if (err) { Good for modelling relationships +## Relational Database + +Familiar data storage + +Supports complex queries + +Difficult to scale + +Data needs normalising +```javascript +const mysql = require('mysql') +const request = require('request') + +const connection = mysql.createConnection({ host: 'xxx', port: '3306', user: 'xxx', password: 'xxx', database: 'xxx' }) + +const sql = 'INSERT INTO books (id, title, authors, description) VALUES (NULL, "The Hobbit, "J.R.R. Tolkien", "Ring found")' + +connection.query(sql, (err, rows) => { + if (err) callback( Error(`database error: ${err}`) ) + return callback(null, rows) +}) +``` + ## Choosing a Database Factors: