Recently I had a very intricate and nuanced problem I had to solve. I thought "gee, if only I had an SQL database to run queries on, in the browser, like an SQLite for JS, life would be easy".
Much to my delight, this wasn't that hard. The purpose of this project is to make something that can be described as:
Lets start by doing an SQL query and show you how you can do it here:
select firstname, age from people where age > 30 order by age desc
Pretty straightforward, right? In this library, you have to rearrange your thinking a little bit, but not by much. We can get the same ends by doing the following:
people.find(db('age', '> 30')).order('age', 'desc').select('firstname','age')
The proper way to think about this is
- You first filter your search results to the entries you are interested in. You can do this in a chained way, such as this:
db.find({key: value, key1: value}).find({key: db('!== undefined')})
- Now you have a subset of data that you can work with. You can run a number of familiar commands on it.
I believe in expressive freedom. Really. That means that you can invoke this library in many ways. For instance, if you wanted to update 'key' to be 'value', you could do it like
update('key', 'value')
or
update({key: 'value'})
You can chain this under a find like
db.find().update(blah blah)
or drop it all together
db.update(blah blah)
The basic idea is that you are using this API because you want life to be painless and easy. You certainly don't want to wade through a bunch of documentation or have to remember strange nuances of how to invoke something. You should be able to take the cavalier approach and Get Shit Done(tm).
Now without further ado, moving on:
This will remove the entries from the database but also return them if you want to manipulate them.
This will extract the values of a particular key from the filtered list and then return it as an array or an array of arrays, depending on which is relevant for the query.
In regular SQL you may find yourself doing something like this:
update employees set fired = true where tardydays > 40
Here's how you pull that off here:
employees.find(db('tardydays', '> 40')).update({fired: true})
See again, how you do the noun first, that is, describe the data you want to be working on, and then describe the operations that you want to do to them.
Lets start with a trivial example; we will create a database and then
just add the object {key: value}
into it.
var db = DB(); db.insert({key: value});
Now let's say we want to insert {one: 1, two: 2}
into it
db.insert({one: 1, two: 2})
Alright, let's say that we want to do this all over again and now insert both fields in. We can do this a few ways:
- As two arguments:
db.insert({key: value}, {one: 1, two: 2});
- As an array:
db.insert([{key: value}, {one: 1, two: 2}]);
- Or even chained:
db.insert({key: value}).insert({one: 1, two: 2});
remove from users where lastlogin = false
becomes:
users.find({lastlogin: false}).remove();
There's no notion of joining although it probably wouldn't be that hard.
Since starting this project, people have brought other, similar products to my attention: