Hans Sprecher

Meet RecordScript

Hans Sprecher, @honzie

28.1.2013

I recently created my first open source project hosted on GitHub: RecordScript. Use it to read, write, and query HTML5's local storage.

RecordScript is a standalone micro-library that weighs a hefty 5.56kb with comments, but squeezes to 1.33kb minified. The API takes queues from ActiveRecord and the project is meant to make life easier for webapp development.

Learning Avoiding SQL

One of the things that I liked best about Ruby on Rails is it meant I never had to learn SQL. Instead, ActiveRecord abstracts database querying by replacing hard SQL strings with a few methods in a powerful and succinct language. Abstractions like this create APIs by decoupling two normally dependent systems. In this case, the server side language is abstracted from the database language.

At its best, this means that the database technology can be swapped out or upgraded without updating a single line of code. At the very least, it means that web developers don't have to learn the arcane syntax of garbled modem commands that power databases.

Either way, that's a win.

Local Storage

When developing webapps, Local Storage is an awesome way to keep track of user data. It's a data store that is pretty large (you get 5mb, which is nearly enough to hold all of Shakespeare's works, which weigh in at 5.3mb) and relatively permanent. It stays on the user's computer until they clear their local storage. It's the storage technology I chose when I wrote He Owe Me and it's what powers the Android version of the same.

Local storage rocks.

But, local storage is a key value store at heart. Nothing more. This means it can be hard to do model complex relationships, and fails at doing many things that relational databases are good at.

Hold On! What About Web SQL?

You may be thinking: HTML5 promised Web SQL Databases, which means that we get local relational databases—so, I can forget about using local storage unless I really only need a KVP store.

You'd be right. Except when it comes to the current state of browser support. Web SQL is supported in Webkit and Opera, but not FireFox or IE. Local Storage, on the other hand, enjoys support in FireFox, Chrome, and—yes&mash;IE 8+.

Back to Local Storage

So, one technique to handle complex data in Local Storage is to store it as JSON objects and then map keys to these objects. This seems a lot like SQL. You get to index a key, and then get data off of the object once you've gotten the key. However, with Local Storage, you have to stringify and parse JSON in order to store it.

That's where RecordScript comes in.

RecordScript creates your objects, automatically saving them to local storage. You initialize RecordScript on a type of object, just like you would create a model in Rails to handle a certain type of data. Then, you can save, query, edit, and delete records to your hearts content. The full API is documented on GitHub.

Currently, RecordScript enjoys support of the following:

This would have been enough to get me through the coding of He Owe Me. However, RecordScript needs some further development. Optimally, it would support additional use cases. Here is a set of future features:

I hope to use ReecordScript on future webapps, and I hope some of you might play with it, offer feedback, and maybe help extend the library.

Use RecordScript? Like it? Want to improve it? Send a pull request via GitHub or drop a line to @honzie.