knowledge-kitchen

Data Storage For Apps - Cookies, Local Storage, File Storage, Databases, APIs, and More

Various mechanisms for storing data in front- and back-ends.

  1. Overview
  2. Cookies
  3. Local Storage
  4. IndexedDB
  5. File Storage
  6. Relational Databases
  7. MongoDB
  8. Mongoose
  9. Conclusions

Overview

Concept

There are a variety of mechanisms apps can use to store data. These can be generally broken-down into two categories:

Client-server paradigm

A reminder of the “bread-and-butter” of the Internet: clients and servers:

Front- and back-ends

Client-server paradigm

… and the technologies that often power each:

Front- and back-ends

Client-server paradigm

… often with a database attached to the server.

Front- and back-ends

Client-server paradigm

… or so-called serverless architectures.

Front- and back-ends

Persistence within a single client session

HTTP is a stateless protocol. This means that each HTTP request is ignorant of any data produced by requests before it - each request is made from a blank slate.

Offline data

A sophisticated app can sometimes be used (to a limited extent) even when the client is disconnected from the Internet.

Persistence across clients

In addition to maintaining state within a single client session, it is often desireable to have data persist across more than one client session.

Client-side

Server-side

Cookies

Concept

Cookies are simple text key/value pairs stored in a client web browser. With every request, these key value pairs are sent in the HTTP headers to the server.

Set-Cookie: userid=22
Cookie: userid=22

Limitations

Cookies are intended to be read by servers and have a few limitations that make their use limited.

Local Storage

Concept

Local storage (a.k.a. Web Storage) is another client-side data storage mechanism that serves a different use case to cookies.

IndexedDB

Concept

IndexedDB is an alternative and successor to Local Storage for web-browser-based data storage.

Comparison to Local Storage

Compared to Local Storage, IndexedDB offers:

Which to use?

In short, use IndexDB when…

For most other cases, Local Storage is probably simpler.

Serverless Databases

Concept

A new(ish) breed of serverless databases provide storage facilities for both client- and server-side needs.

File Storage

Concept

Sometimes simple file system storage is all that is needed by an app, for example for user-generated images, audio tracks, videos, etc.

Example: multer

The multer Express.js middleware makes handling uploaded files simple.

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    // store files into a directory named 'uploads'
    cb(null, "/uploads")
  },
  filename: function (req, file, cb) {
    // rename the files to include the current time and date
    cb(null, file.fieldname + "-" + Date.now())
  },
})

var upload = multer({ storage: storage })

Example: multer (continued)

Once set up, any route can pass the request object through the multer middleware prior to handling the response.

// assuming multiple files labeled as 'files' were included in the HTTP request
router.post('/upload-route-example', upload.array('files'), (req, res) => {
  console.log( JSON.stringify(req.files, null, 2) )
  if (req.files.length) res.json( { success: true, message: 'thanks!' })
  else res.status(500).send({ success: false, message: 'Oops... something went wrong!' })
}

Example: multer (continued)

Now, the client, for example a web browser, can allow a user to upload a file and send it to the server to be handled by this route.

Relational Databases

Concept

Relational databases stored data in a row/column-type format. They are essentially glorified spreadsheets.

id common_name big_or_not image_path
1 giraffe big uploads/giraffe.jpg
2 squirrel not big uploads/squirrel.png
SELECT common_name, image_path FROM animals WHERE id=2

CRUD

Databases, relational or not, are typically used to do one of four tasks, affectionately referred to with the acronym, CRUD.

Joins

The bane of every relational database developer’s existence is the concept of joins.

id animal_id name
1 1 Bob
2 1 Sheila
SELECT * FROM inventory INNER JOIN animals ON inventory.animal_id = animals.id

MongoDB

Concept

MongoDB is one of many increasingly-popular NoSQL databases that do not use SQL and do not store data in row/column format.

Example document

For example, a document that represents an animal in my personal zoo might look like:

{
  id: 1,
  name: 'Bob',
  type: {
    commonName: 'giraffe',
    bigOrNot: 'big',
    imagePath: 'uploads/giraffe.jpg'
  }
}

MongoDB Atlas

MongoDB offers a cloud-based database server named Atlas with a free tier for trying it out…

Mongoose

When connecting an Express.js web server to a MongoDB server, it is wise to use mongoose, a 3rd-party module.

Credentials

Storing database credentials in version control is a big mistake!

Conclusions

You have seen a brief overview of various data storage mechanisms.