_AlaSQL is an unfunded open source project installed 650k+ times each month. Please donate your time. We appreciate any and all contributions we can get._ _Have a question? Ask The…
- _AlaSQL is an unfunded open source project installed 650k+ times each month. Please donate your time. We appreciate any and all contributions we can get._
- _Have a question? Ask The AlaSQL Bot or post on Stack Overflow._
AlaSQL
AlaSQL - _( à la SQL ) [ælæ ɛskju:ɛl]_ - is an open source SQL database for JavaScript with a strong focus on query speed and data source flexibility for both relational data and schemaless data. It works in the web browser, Node.js, and mobile apps.
This library is perfect 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
.xls and .xlsx), CSV, JSON, TAB, IndexedDB, LocalStorage, and SQLite files.
The library adds the comfort of a full database engine to your JavaScript app. No, really - it's working towards a full database engine complying with most of the SQL-99 language, spiced up with additional syntax for NoSQL (schema-less) data and graph networks.
Traditional SQL Table
js
/* create SQL Table and add data */
alasql('CREATE TABLE cities (city string, pop number)');
alasql("INSERT INTO cities VALUES ('Paris',2249975),('Berlin',3517424),('Madrid',3041579)");
/* execute query */
var res = alasql('SELECT * FROM cities WHERE pop < 3500000 ORDER BY pop DESC');
// res = [ { "city": "Madrid", "pop": 3041579 }, { "city": "Paris", "pop": 2249975 } ]
Array of Objects
js
var data = [
{a: 1, b: 10},
{a: 2, b: 20},
{a: 1, b: 30},
];
var res = alasql('SELECT a, SUM(b) AS b FROM ? GROUP BY a', [data]);
// res = [ { "a": 1, "b": 40},{ "a": 2, "b": 20 } ]
Spreadsheet
js
// file is read asynchronously (Promise returned when SQL given as array)
alasql([
'SELECT * FROM XLS("./data/mydata") WHERE lastname LIKE "A%" and city = "London" GROUP BY name ',
])
.then(function (res) {
console.log(res); // output depends on mydata.xls
})
.catch(function (err) {
console.log('Does the file exist? There was an error:', err);
});
Bulk Data Load
js
alasql('CREATE TABLE example1 (a INT, b INT)');
// alasql's data store for a table can be assigned directly
alasql.tables.example1.data = [
{a: 2, b: 6},
{a: 3, b: 4},
];
// ... or manipulated with normal SQL
alasql('INSERT INTO example1 VALUES (1,5)');
var res = alasql('SELECT * FROM example1 ORDER BY b DESC');
console.log(res); // [{a:2,b:6},{a:1,b:5},{a:3,b:4}]
If you are familiar with SQL, it should be no surprise that proper use of indexes on your tables is essential for good performance.
Options
AlaSQL has several configuration options which change the behavior. It can be set via SQL statements or via the options object before using alasql.
If you're using NOW() in queries often, setting alasql.options.dateAsString to false speed things up. It will just return a JS Date object instead of a string representation of a date.
Installation
bash
yarn add alasql # yarn
npm install alasql # npm
npm install -g alasql # global install of command line tool
For the browsers: include alasql.min.js
html
Getting started
See the "Getting started" section of the wiki
More advanced topics are covered in other wiki sections like "Data manipulation" and in questions on Stack Overflow
Other links:
- Documentation: Github wiki
- Library CDN: jsDelivr.com
- Feedback: Open an issue
- Try online: Playground
- Website: alasql.org
Please note
All contributions are extremely welcome and greatly appreciated(!) -
The project has never received any funding and is based on unpaid voluntary work: We really (really) love pull requests
The AlaSQL project depends on your contribution of code and may have bugs. So please, submit any bugs and suggestions as an issue.
Please check out the limitations of the library.
Performance
AlaSQL is designed for speed and includes some of the classic SQL engine optimizations:
- Queries are cached as compiled functions
- Joined tables are pre-indexed
WHEREexpressions are pre-filtered for joins
Features you might like
Traditional SQL
Use "good old" SQL on your data with multiple levels of: JOIN, VIEW, GROUP BY, UNION, PRIMARY KEY, ANY, ALL, IN, ROLLUP(), CUBE(), GROUPING SETS(), CROSS APPLY, OUTER APPLY, WITH SELECT, and subqueries. The wiki lists supported SQL statements and keywords.
User-Defined Functions in your SQL
You can use all benefits of SQL and JavaScript together by defining your own custom functions. Just add new functions to the alasql.fn object:
js
alasql.fn.myfn = function (a, b) {
return a * b + 1;
};
var res = alasql('SELECT myfn(a,b) FROM one');
You can also define your own aggregator functions (like your own SUM(...)). See more in the wiki
Compiled statements and functions
js
var ins = alasql.compile('INSERT INTO one VALUES (?,?)');
ins(1, 10);
ins(2, 20);
See more in the wiki
SELECT against your JavaScript data
Group your JavaScript array of objects by field and count number of records in each group:
js
var data = [
{a: 1, b: 1, c: 1},
{a: 1, b: 2, c: 1},
{a: 1, b: 3, c: 1},
{a: 2, b: 1, c: 1},
];
var res = alasql('SELECT a, COUNT(*) AS b FROM ? GROUP BY a', [data]);
See more ideas for creative data manipulation in the wiki
JavaScript Sugar
AlaSQL extends "good old" SQL to make it closer to JavaScript. The "sugar" includes:
- Write Json objects -
{a:'1',b:@['1','2','3']}
- Access object properties -
obj->property->subproperty - Access object and arrays elements -
obj->(a*1) - Access JavaScript functions -
obj->valueOf() - Format query output with
SELECT VALUE, ROW, COLUMN, MATRIX - Output nested objects with
INTO OBJECT()- converts arrow notation columns back to nested structure - ES5 multiline SQL with
var SQL = function(){/*SELECT 'MY MULTILINE SQL'*/}and pass instead of SQL string (will not work if you compress your code)
Extracting Nested Properties with INTO OBJECT()
When selecting nested properties using arrow notation (->), results are normally flattened with the arrow path as the key. Use INTO OBJECT() to restore the nested structure:
js
var data = [{name: 'Oslo', info: {country: 'Norway', population: 700000}}];
// Standard output (flattened)
alasql('SELECT name, info->country FROM ?', [data]);
// [{ "name": "Oslo", "info->country": "Norway" }]
// With INTO OBJECT() (nested)
alasql('SELECT name, info->country INTO OBJECT() FROM ?', [data]);
// [{ "name": "Oslo", "info": { "country": "Norway" } }]
Read and write Excel and raw data files
You can import from and export to CSV, TAB, TXT, and JSON files. File extensions can be omitted. Calls to files will always be asynchronous so multi-file queries should be chained:
js
var tabFile = 'mydata.tab';
alasql
.promise([
"SELECT * FROM txt('MyFile.log') WHERE [0] LIKE 'M%'", // parameter-less query
['SELECT * FROM tab(?) ORDER BY [1]', [tabFile]], // [query, array of params]
"SELECT [3] AS city,[4] AS population FROM csv('./data/cities')",
"SELECT * FROM json('../config/myJsonfile')",
])
.then(function (results) {
console.log(results);
})
.catch(console.error);
Read SQLite database files
AlaSQL can read (but not write) SQLite data files using SQL.js library:
html
alasql([
'ATTACH SQLITE DATABASE Chinook("Chinook_Sqlite.sqlite")',
'USE Chinook',
'SELECT * FROM Genre',
]).then(function (res) {
console.log('Genres:', res.pop());
});
sql.js calls will always be asynchronous.
AlaSQL works in the console - CLI
The node module ships with an alasql command-line tool:
bash
$ npm install -g alasql ## install the module globally
$ alasql -h ## shows usage information
$ alasql "SET @data = @[{a:'1',b:?},{a:'2',b:?}]; SELECT a, b FROM @data;" 10 20
[ 1, [ { a: 1, b: 10 }, { a: 2, b: 20 } ] ]
$ alasql "VALUE OF SELECT COUNT(*) AS abc FROM TXT('README.md') WHERE LENGTH([0]) > ?" 140
// Number of lines with more than 140 characters in README.md
More examples are included in the wiki
Features you might love
AlaSQL ♥ D3.js
AlaSQL plays nice with d3.js and gives you a convenient way to integrate a specific subset of your data with the visual powers of D3. See more about D3.js and AlaSQL in the wiki
AlaSQL ♥ Excel
AlaSQL can export data to both Excel 2003 (.xls) and Excel 2007 (.xlsx) formats with coloring of cells and other Excel formatting functions.
AlaSQL ♥ Meteor
Meteor is amazing. You can query directly on your Meteor collections with SQL - simple and easy. See more about Meteor and AlaSQL in the wiki
AlaSQL ♥ Angular.js
Angular is great. In addition to normal data manipulation, AlaSQL works like a charm for exporting your present scope to Excel. See more about Angular and AlaSQL in the wiki
AlaSQL ♥ Google Maps
Pinpointing data on a map should be easy. AlaSQL is great to prepare source data for Google Maps from, for example, Excel or CSV, making it one unit of work for fetching and identifying what's relevant. See more about Google Maps and AlaSQL in the wiki
AlaSQL ♥ Google Spreadsheets
AlaSQL can query data directly from a Google spreadsheet. A good "partnership" for easy editing and powerful data manipulation. See more about Google Spreadsheets and AlaSQL in the wiki
Miss a feature?
Take charge and add your idea or vote for your favorite feature to be implemented:

Limitations
Please be aware that AlaSQL has bugs. Beside having some bugs, there are a number of limitations:
0. AlaSQL has a (long) list of keywords that must be escaped if used for column names. When selecting a field named key please write `SELECT key FROM ... instead. This is also the case for words like value , read , count , by , top , path , deleted , work and offset . Please consult the full list of keywords.
1. It is OK to SELECT 1000000 records or to JOIN two tables with 10000 records in each (You can use streaming functions to work with longer datasources - see [test/test143.js](test/test143.js)) but be aware that the workload is multiplied so SELECTing from more than 8 tables with just 100 rows in each will show bad performance. This is one of our top priorities to make better.
2. Limited functionality for transactions (supports only for localStorage) - Sorry, transactions are limited, because AlaSQL switched to more complex approach for handling PRIMARY KEYs / FOREIGN KEYs. Transactions will be fully turned on again in a future version.
3. A (FULL) OUTER JOIN and RIGHT JOIN of more than 2 tables will not produce expected results. INNER JOIN and LEFT JOIN are OK.
4. Please use aliases when you want fields with the same name from different tables (SELECT a.id AS a_id, b.id AS b_id FROM ?).
5. At the moment AlaSQL does not work with JSZip 3.0.0 - please use version 2.x.
6. JOINing a sub-SELECT does not work. Please use a with structure (Example here) or fetch the sub-SELECT` to a variable and pass it as an argument (Example here).
7
…
Members
-
alasql
AlaSQL.js - 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.
JavaScript ★ 7.3k 19h agoExplain → -
node-red-contrib-alasql
A Node-RED node wrapping AlaSQL for fast SQL based in-memory data processing for BI and ERP applications.
HTML ★ 11 23d agoExplain → -
alasql-org
No description.
CSS ★ 4 2y agoExplain → -
alasql-docs
Documentation for the AlaSQL library
Shell ★ 2 3y agoExplain → -
alasql-sqllogictest
sqllogictest for AlaSQL
JavaScript ★ 1 1d agoExplain → -
alasql-ionic ⑂
Example on how to use AlaSQL with ionic
JavaScript ★ 1 2y agoExplain → -
recast ⑂
JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator
★ 1 6y agoExplain → -
jisons ⑂
bison / YACC / LEX in JavaScript (LALR(1), SLR(1), etc. lexer/parser generator)
JavaScript ★ 1 6y agoExplain → -
ala-wiki
No description.
Shell ★ 0 2y agoExplain → -
historical-performance
Historical performance for the AlaSQL lib
TypeScript ★ 0 5mo agoExplain → -
noflo-alasql
AlaSQL component for NoFlo
JavaScript ★ 0 2mo agoExplain → -
alasql-gfx
No description.
★ 0 9y agoExplain → -
alasql-codex
Source of coding examples and learning lab
★ 0 2y agoExplain → -
alasql.github.io
Source of alasql.js.org
HTML ★ 0 10y agoExplain → -
alasql.org
Source of alasql.org
★ 0 10y agoExplain →
No repos match these filters.