AlaSQL in Action: The JavaScript SQL Database
Tags: sql, webdev, codenewbie, alasql
Published: 2020-06-09 17:43:10

<Enter>
### _What’s AlaSQL?_ 
For starters, this open source project garners __11k weekly downloads__ off npm and has over __5K stars on GitHub__.

I was surprised to see that there aren’t more posts about this popular __lightweight client-side in-memory SQL database__ on dev.to, apart from [this awesome article I found] (https://dev.to/jorge_rockr/alasql-a-real-database-for-web-browsers-and-nodejs-24gj). However, [AlaSQL’s website] (http://alasql.org/) gets straight to the point:

* JavaScript SQL database for browser and Node.js.
* Handles both traditional relational tables and nested JSON data (NoSQL).
* Export, store, and import data from localStorage, IndexedDB, or Excel.

_What’s not to love?_ AlaSQL was designed to work in browser and Node.js, is fast, and super easy to use. [Some say that] (https://medium.com/@danacodes/alasql-a-query-language-for-javascript-e1540ac238b4#:~:text=AlaSQL%20is%20an%20open%2Dsource,and%20a%20few%20other%20sources.) when you need to find data fast, and want a better alternative to a full database or Redis, check out AlaSQL.

### _We love AlaSQL too._ 
HarperDB has had the pleasure of using AlaSQL for its backend SQL functionality. We chose to use AlaSQL because it has extensive language support, is well supported and extensible, has the ability to execute SQL against data sets (JSON or Arrays) and the ability to generate reusable functions. AlaSQL enables us to convert SQL language into an Abstract Syntax Tree (AST) that we can programmatically interpret into our data model. We are able to feed data into a processor that can perform the calculations native to SQL, using the functions defined in the library throughout our project as APIs and not just in the context of a SQL call.

AlaSQL is extensible and allows us to create custom functions. For example, we created a custom function called SEARCH_JSON that allows HarperDB users to search and transform nested documents. This function wraps a popular npm package called JSONata. With AlaSQL we were able to embed another open source package as a simple function call.  Our implementation was as easy as defining the function (Note this is sample code):
```javascript
const jsonata = require('jsonata');
/**
 * wrapper function that implements the JSONata library, which performs searches, transforms, etc... on JSON
 * @param {String} jsonata_expression - the JSONata expression to execute
 * @param {any} data - data which will be evaluated
 * @returns {any}
 */
function searchJSON(jsonata_expression, data){
    if(typeof jsonata_expression !== 'string' || jsonata_expression.length === 0){
        throw new Error('search json expression must be a non-empty string');
    }

    let alias = '__' + jsonata_expression + '__';

    if(hdb_utils.isEmpty(this.__ala__.res)){
        this.__ala__.res = {};
    }

    if(hdb_utils.isEmpty(this.__ala__.res[alias])) {
        let expression = jsonata(jsonata_expression);
        this.__ala__.res[alias] = expression;
    }
    return this.__ala__.res[alias].evaluate(data);
}
//Then define a custom function in AlaSQL:
const alasql = require('alasql');
alasql.fn.search_json = alasql.fn.SEARCH_JSON = searchJSON;
```

<Enter>
_Clearly_ we are happy with our decision to use AlaSQL to interpret SQL into our data model and run performant queries against as much SQL as possible. [That’s why we hosted the creators of AlaSQL on June 16th for a showcase] (https://www.youtube.com/watch?v=ARfWrjXzWSg&t&utm_content=132680797&utm_medium=social&utm_source=twitter&hss_channel=tw-825352688518959105) to get an inside look at how AlaSQL was created, how it grew in popularity, and real world use cases and products. It was awesome to hear from AlaSQL creators Mathias Rangel Wulff and Andrey Gershun. After Q&A on AlaSQL, our CTO Kyle Bernhardy shared more about using AlaSQL as HarperDB’s engine to interpret and parse complex SQL into our data model and perform simple to complex SQL CRUD operations, as well as exposing other libraries like Turf.js, JSONata and more.


<Enter>
<Enter>
Looking for more resources on this innovative client-side in-memory SQL database? Check out [this JavaScript library] (https://www.javascripting.com/view/alasql) designed for:

* Fast in-memory SQL data processing for BI and ERP applications on fat clients
* Easy ETL and options for persistence by data import / manipulation / export of several formats
* All major browsers, Node.js, and mobile applications