A look through the current available solutions for offline support on web and mobile platforms. We will cover the support of each solution and how to get started implementing them into your own projects.
14. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
Cookies
//Set
document.cookie = "username=John Doe";
//Set with expire
document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013
12:00:00 UTC";
//Edit
document.cookie = "username=John Smith; expires=Thu, 18 Dec 2013
12:00:00 UTC; path=/";
//Delete (set the expire date to before now)
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00
UTC";
15. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
Cookies (jCookie)
//Create a cookie, valid across the entire site:
Cookies.set('name', ‘value');
//Create a cookie that expires 7 days from now:
Cookies.set('name', 'value', { expires: 7 });
//Read cookie:
Cookies.get('name'); // => ‘value' or undefined
//Read all visible cookies:
Cookies.get(); // => { name: 'value' }
//Delete cookie:
Cookies.remove('name');
17. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
//Create a local storage item
localStorage.setItem('name', 'value');
//Create a local storage with object
var fullNameObj = {'first': 'John', 'last': ‘Smith'};
localStorage
.setItem(‘fullname',JSON.stringify(fullNameObj));
//Read local storage item:
localStorage.getItem('name'); // => 'value' or undefined if doesnt exist
//Read local storage object
JSON.parse(localStorage.getItem('fullname'));
// => {'first': 'John', 'last': ‘Smith'}
//Delete local storage item:
localStorage.removeItem('name');
19. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
Cookie & Local Storage Gotchas!
• only stores strings. which means anything being stored
gets converted to a string.
8 -> “8”
true -> “true”
{“a”:1} -> [object Object]
JSON.stringify(), JSON.parse()
Solution
20. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
20
But what happens when our data get more complicated?
Both IndexedDB & WebSQL can serve as great solutions for this problem.
WebSQL Indexed DB
Except we have some support issues…..
We need to search?
Join Data?
Add / Edit / Delete Items inside of our object?
Sync Data?
21. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
Client Side Storage Solutions
WebSQL
•Large Storage (50M Max)
•Relational Database (SQL lite)
•Tables and relationships, moderately fast
IndexedDB
•Large Storage (50M Max)
•Document Store DB (NoSQL)
•Indexable, large row storage
•No tables, Super fast
22. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
Client Side Storage Frameworks
Loki JS
javascript embedded / in-memory
database
Lovefield
Lovefield is a relational
database written in pure
JavaScript. It provides SQL-like
AlaSQL.js
JavaScript SQL database for
browser and Node.js. Handles both
traditional relational tables and
nested JSON data (NoSQL)
Lawnchair
A lightweight client side JSON
document store
NoSQL SQL
23. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
Client Side Storage Frameworks
Pouchdb
PouchDB is an open-source
JavaScript database inspired
by Apache CouchDB
Firebase
Firebase provides a realtime
database synchronized across
clients and stored on Firebase's
cloud.
WebSqlSync
Automatically synchronize a local
WebSql database (SQLite of the
browser) with a server
Persistence.js
An asynchronous Javascript
database mapper library.
NoSQL Syncing Frameworks SQL Syncing Frameworks
29. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
The SQL vs NoSQL Holy War
SQL and NoSQL do the same thing: store data. They take different approaches, which may help or
hinder your project. Despite feeling newer and grabbing recent headlines, NoSQL is not a replacement
for SQL — it’s an alternative.
SQLNoSQL
31. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
WebSQL Storage
• Persists after the
browser is closed
• Storage is shared
across all pages on
a domain
Column Types
• TEXT
• NUMERIC
• INTEGER
• REAL
• BLOB
• Sorting
• Grouping
• Compound
SELECTs
• JOINs
• last_insert_rowid(
)
• like, between, in
• indexes
• Primary Key
• Autoincrement
• Limit, Offset
Most Common
Features Supported
32. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
//Create Our Database
var db = openDatabase('mydb', '1.0', 'my first
database', 2 * 1024 * 1024);
//Create a transaction and run our qureies inside of it
db.transaction(function (tx) {
//Create Table
tx.executeSql('CREATE TABLE IF NOT EXISTS foo (id unique, text)');
//Insert Data without params (BAD)
tx.executeSql('INSERT INTO foo (id, text) VALUES (1, "synergies")');
});
WebSQL
33. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
WebSQL
db.transaction(function (tx) {
var id = 2;
var userValue = 'creativity';
//Insert Data with params
tx.executeSql(
'INSERT INTO foo (id, text) VALUES (?, ?)',
[id, userValue]
);
});
db.transaction(function (tx) {
//Delete Data with params
tx.executeSql(‘DELETE FROM foo WHERE id = ?', [id]);
});
34. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
WebSQL
db.transaction(function (tx) {
// Return Data
// executeSql(query,params,callback)
tx.executeSql('SELECT * FROM foo', [], function (tx, results) {
var len = results.rows.length, i;
for (i = 0; i < len; i++) {
alert(results.rows.item(i).text);
}
});
});
Gotchas
Results is not a array of objects.
The results array is very similar to a query object in CF.
It has a length property
and can be iterated over using parens instead of brackets
you can easily turn it into an array of objects, works great for using in
angular, etc.
36. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
So by 2018 we should have full support…. nope
Web SQL was deprecated because standards are really
important and turning Web SQL into a proper standard
would have been prohibitively difficult.
37. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
If it is critical to your project to have support on many
different browsers polyfills have been created to
implement support.
In this case local storage would be used to implement
it,
(since it works cross platform, persists across browser
tabs, and persists after browser restarts)
Lovefield - A relational database written in pure JavaScript. It
provides SQL-like syntax and works cross-browser. Created By
Google,
Web Developers
Cordova/PhoneGap sqlite storage adapter
- Drop-in replacement for HTML5/Web SQL API:
Only Change:
window.openDatabase() -> window.sqlitePlugin.openDatabase()
Mobile Developers
41. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
IndexedDB
//Add a record
var request = db.transaction(["employee"], "readwrite")
.objectStore("employee")
.add({ id: "00-03", name: "Kenny", age: 19, email: "kenny@planet.org" });
request.onsuccess = function(event) {
alert("Kenny has been added to your database.");
};
request.onerror = function(event) {
alert("Unable to add datarnKenny is aready exist in your database! ");
}
//Remove a record
var request = db.transaction(["employee"], "readwrite")
.objectStore("employee")
.delete("00-03");
request.onsuccess = function(event) {
alert("Kenny's entry has been removed from your database.");
};
43. www.agritrackingsystems.com
AGRI TRACKING
SYSTEMS
Agri Tracking Systems keeps track of your
farms, fields, crops, nutrient samples,
budgets all the way down to your daily
operations. Our purpose is simple, we want
to give you the power to become more
efficient by staying informed.
www.agritrackingsystems.com
Agri Mapping makes its simple to build
your maps without having to read through
tons of help guides. We provide all the
information and tools you need, and none
of the ones you don’t. And the best part is
it is accessible from anywhere!
www.agrimapping.com
OUR PRODUCTS
Editor's Notes
so you have just completed your beautiful app and it works flawlessly on your dev environment.
you send it to production sit back and
wait for the everyone to start telling you how amazing it is using your app
right because thats how it works.
no instead you get the site live and you start to gradually increase traffic
simultaneously your site starts slowing down
shortly after you are getting phone calls about how your site takes forever to load
or its broken,
its a piece of crap,
or many other unpleasant comments
and you somehow have to fix it quickly
or maybe your app requirements include “it has to work everywhere”
and suddenly you have to start worrying about Offline Support
for my company this was necessity
and of course offline support has its own set of challenges
but that never stopped anyone
remember your a programmer, you can do anything
thankfully the browser is here to help
I present to you databases in browsers!
the best thing since sliced bread
but lets make sure we've got good browser support
IE
Edge
Firefox
Opera Mini
So as expected IE is here to spoil the party again
but don't worry IE has its strengths
But hey the do rank number one for something
although thats basically like saying you win at losing
but thankfully IE does support some features for offline support
and as for opera mini, well the fact that it doesn't support cookies is a little concerning
but in the same breath, i cant name a single person i know that uses it
so im just going to let that one slide
so why don't we just use cookies or local storage for everything?
so why don't we just use cookies or local storage for everything?
so why don't we just use cookies or local storage for everything?
working with strings, not very extensible by themselves, a pain to work with
thankfully there is a javascript library called jCookie which
allows you to add edit and delete it like local storage
jCookie is an api for working with cookies
it even allows you to store JSON and will parse it back for you
but remember we have a limitation of 4KB so
so for scenarios like caching, storing temporary data, or persisting across browser tabs these work great
so why don't we just use cookies or local storage for everything?