Skip to content
Permalink
49e43975ae
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

Data Persistence

Data from other APIs / Screen scraping read only

Need to add value to this

Demonstrate CRUD

So need to store data

Script restart needed if code changes

Filesystem

Relational database

Document database

Graph database

Filesystem

Stores text in file against key

Formatted as JSON data

Easy to store and retrieve by key

Simple to implement

Difficult to query

const storage = require('node-persist')
storage.initSync()

exports.addBook = function(isbn, callback) {
	const data = {
		title: 'The Hobbit',
		authors: 'J.R.R. Tolkien',
		description: 'Hobbit finds ring...'
	}
	storage.setItemSync(isbn, data)
}

Relational Database

Familiar data storage

Supports complex queries

Difficult to scale

Data needs normalising

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)
})

Document Database

Stores JavaScript objects

Can be queried using JavaScript

Mongoose Example.

Start by defining a schema file:

const mongoose = require('mongoose')
mongoose.Promise = global.Promise
const Schema = mongoose.Schema

const bookSchema = new Schema({
	title: String,
	authors: String,
	description: String
})
const Book = mongoose.model('Book', bookSchema)
module.exports = Book

Storing a document.

const book = new Book({
	title: data.title,
authors: data.authors,
	description: data.description
})

book.save( function(err, book) {
if (err) {
		callback( Error(`database error: ${err}`) )
	}
	return callback(null, book)
})

Document Databases

Good for modelling relationships

Choosing a Database

Factors:

  • Type of data to be stored
  • How this will be retrieved
  • Complexity

Online Hosting

  • Relational MySQL freemysqlhosting.net
  • Document MongoDB mlab.com
  • Graph Neo4J graphenedb.com