Structural queries are still a big need for modern web apps.
And with Web SQL deprecated, IndexedDB not having
structural queries, web developers
need a tool to satisfy their structural query needs.
Hi.
My name is Arthur Hsu, and I'm here
to talk about the Lovefield project that
solves the problem.
Lovefield is a relational database for web apps.
Web app developers need structural queries
that work in the mobile world.
Our users need the web app to work either online or offline.
How can we build such apps?
This seems to be the reason IndexedDB was built.
But even with offline support, there is a steep learning curve
to make it useful for your app.
Moreover, IndexedDB does not provide structural queries,
so many of us just write domain specific query functions that
works only for the apps that we were working on.
To address these problems, I built Lovefield, a JavaScript
driven relational database that runs inside your browser.
Lovefield improves on the concept of IndexedDB
with simpler APIs and more control
over persistent storage.
It is build for structural query needs.
IndexedDB does not offer structural query features
such as sorting by multiple columns
or joining the results of multiple tables.
These can be easy done with Lovefield.
Lovefield also makes data persistence way
easier in two ways.
First, the APIs are much easier to use.
And secondly, Lovefield offers transactions
which provide you full control on when to flush data
to persistent stores.
The last and most important part is
Lovefield is designed with developers in mind.
It is open source, easy to use, cross browser-- yes you hear me
correctly.
Lovefield works on IE, Chrome, Firefox, and Safari.
Even better, Lovefield is designed
to work with existing JavaScript frameworks.
And we've tested with jQuery, Angular, and Polymer
with no problem.
Let's move to a quick example to see what Lovefield can do.
Assume we have two different type of objects.
One is called Task that stores a task to be finished.
And the other is called Person which stores
data about a person in our app.
In the good old days, we list all tasks
that are in the works with their owners
using this simple SQL statement.
Select from task and person where
task state equals to working and task owner equals to person.id.
How shall we do this shiny new JavaScript web app?
Let's forget about the code that reads data out of index db.
Let's just concentrate on the select logic.
Say we have an array of tasks objects, a hash table that maps
person ID to person objects.
To do the select, we need this wall code.
This code's problem is not only the size,
but also how much effort needed to maintain or change it.
Lovefield is designed to make the simple SELECT simple again.
In Lovefield, we describe data type of table in schema
To construct a query, we first retrieve the schema
by calling the getSchema function of the database
connection.
Then we construct our query using the builder pattern,
which is very similar to SQL SELECT statement
and easy to learn.
Lovefield's queries are JavaScript functions.
After minifying and uglifying, it
makes SQL injection next to impossible.
This is very good for security reasons.
It also has positive impacts on performance.
You're writing JavaScript functions
that cascade together.
There's no need for a SQL parser, and it just works.
Lovefield can do more than just simple queries.
It has some great functionality such as grew by,
event sorting, pagination using LIMIT and SKIP, and so on.
The code snippet here demonstrates how
to group results, sort the results by multiple columns,
and implement pagination using LIMIT and SKIP.
Lovefield's relational query engine
provides SQL-like functions-- fundamental functions, SELECT,
INSERT, UPDATE, and DELETE, and also atomic transactions
that you have to control to commit or roll back.
I've shown events sorting, LIMIT, and SKIP, and GROUP
BY in previous slides.
Moreover, Lovefield has common aggregators, inner table join,
self table join, left outer join, and aliases.
These features empower you to perform very complex queries
in very few lines of code.
Lovefield provides parameterized queries.
And the idea is to prepare a query
and bind the variables to that query
so that the runtime values of the bound variables
will be used to run the query.
One super cool feature that Lovefield
has is observable query.
Lovefield allows observation of the results of a SELECT query.
Say another asynchronous function
in the code updates table of contents
or the values of the parameterized query findings
change.
The updated query results will be propagated
to the handler of the observer.
This offers a clean way of updating UI contents
in an event driven fashion.
Behavior wise, Lovefield has integrity constraint checks,
B+-Tree for indices, a query optimizer to ensure that
the best indices are used and the queries are run as fast
as possible, a query analyzer to display to the user how
the query is executed so that developers can tune
their database and queries.
Lovefield is all about performance.
The team created a performance dashboard
to detail any performance regressions for daily changes.
A lot of time has spent on optimizing the query engine.
If you do find any query or execution
pass that seems to be slower than expected,
please report on GitHub, and someone in the team
will look into it.
Lovefield makes persisting data inside the browser much easier.
Persisting data inside the browser
is useful when the user go offline and want
to use the app, or to improve the app loading
experience by using local cache before the server data returns.
Lovefield by default uses IndexedDB
as persistent storage.
If you're only interested in using Lovefield's query engine
but not really wanted to persist sensitive data
inside the browser, then Memory DB is for you.
Lovefield cab be used as a pure in memory cache.
Data come from some database sources--
say service in the cloud-- and the app
uses Lovefield's query engine to store the least data in memory
and performs query locally.
Another exciting data persistence layer Lovefield
has is based on Firebase.
Firebase is a cloud based database.
Using Lovefield on top of it makes the best of both worlds,
a fully synchronized cloud database
and the powerful relational query engine running on browser
provided Lovefield.
So if your web app is looking for a simpler relational
database with the power of SQL, Lovefield
is a perfect match for you.
For more information, check out web based code lab from Google
I/O, or browse over to out GitHub page and grab the code
and play around.
Feel free to leave us some feedback.
And if you find a problem, we're more than
happy to accept contributions.
Anyhow, check out the Lovefield project,
and thanks for watching.
[MUSIC PLAYING]