Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ Try AlaSQL JSON objects in Console [sample](http://alasql.org/console?drop table

_Useful stuff, but there might be dragons_



### Graphs

AlaSQL is a multi-paradigm database with support for graphs that can be searched or manipulated.
Expand Down Expand Up @@ -569,6 +571,8 @@ then open <http://127.0.0.1:1337/?SELECT%20VALUE%20(2*2)> in your browser
Warning: Alaserver is not multi-threaded, not concurrent, and not secured.




## Tests

### Regression tests
Expand Down Expand Up @@ -699,6 +703,6 @@ and other people for useful tools, which make our work much easier.

----
<a href="http://alasql.org"><img src="https://cloud.githubusercontent.com/assets/1063454/14003946/d6e5c076-f156-11e5-8238-e62d2a8d20dc.png" align="right" alt="AlaSQL logo"/></a>
© 2014-2024, Andrey Gershun (agershun@gmail.com) & Mathias Rangel Wulff (m@rawu.dk)
© 2014-2025, Andrey Gershun (agershun@gmail.com) & Mathias Rangel Wulff (m@rawu.dk)

See [this article](https://console.substack.com/p/console-187) for a bit of information about the motivation and background.
9 changes: 9 additions & 0 deletions build.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ x esbuild --minify --outfile="$outfile_min" "$outfile" --allow-overwrite



echo '\nBuild precompile files'
mkdir -p dist/precompile

echo '# Copy precompile module'
x esbuild --outfile="dist/precompile/index.js" "src/precompile/index.js" --format=cjs




echo '\nBuild worker files'
outfile="dist/alasql-worker.js"
outfile_min="dist/alasql-worker.js"
Expand Down
83 changes: 83 additions & 0 deletions docs/PRECOMPILE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

### Transform SQL to JavaScript code

If you want to fiddle with speed you can pre-generate the JS code that will be running your query.

The returned function has the signature `function(params, cb)` and is designed to be called with `alasql` as context.

```js
import alasql from 'alasql';
import {compileToJS} from 'alasql/precompile';

const jsCode = compileToJS('SELECT * FROM ? WHERE pop > 1000000', 'my_db');

const selectPop = new Function('return ' + jsCode)().bind(alasql);

// Now you can call it like a regular alasql function, but without the SQL
let data = [{city: 'Copenhagen', pop: 1300000}, {city: 'Aarhus', pop: 300000}];
let res = selectPop([data]);
// res will be [{city: 'Copenhagen', pop: 1300000}]
```

This example is not useful in it self, as it is all done during exeuction time (you could just as well have used alasql.compile() directly). However, if you precompile the function at build time, it can be a significant performance boost as the execution don't have to parse the SQL. There is an example of how to do this with Bun in [examples/precompileJS](https://github.com/AlaSQL/alasql/tree/develop/examples/precompileJS)


---------


# AlaSQL Precompile Module

This module provides SQL compilation functionality that allows you to pre-compile SQL queries into JavaScript code, skipping SQL parsing on execution.

The functionality is experimental - so proceed with caution and expect changes.

## Installation

```javascript
import { compileToJS } from 'alasql/precompile';
```

## Functions

### `compileToJS(sql, databaseid?)` - Precompile

Compiles a SQL statement to JavaScript source code that expects an AlaSQL engine as `this`. This is useful for performance optimization as it eliminates SQL parsing overhead at runtime.

**Parameters:**
- `sql` (string): SQL statement to compile
- `databaseid` (string, optional): Database identifier

**Returns:** Generated JavaScript source code string

## Usage Example

```javascript
import { compileToJS } from 'alasql/precompile';

const sql = 'SELECT name, age FROM users WHERE age > ?';
const jsCode = compileToJS(sql);

// Create a function from the compiled code
const queryFn = new Function('return ' + jsCode)().bind(alasql);

// Execute the function (requires AlaSQL)
const result = queryFn([users, 18]);
```

## Build-time Compilation

You can use this function with build tools like Bun, Vite, or Webpack to compile SQL queries at build time:

```javascript
// Using Bun macro for precompile
import { compileToJS } from './my-queries.js' with { type: 'macro' };

const queryFn = new Function('return ' + compileToJS('SELECT * FROM users'))().bind(alasql);
```

## Benefits

- **Performance**: Eliminate SQL parsing overhead at runtime
- **Flexibility**: Still has access to full AlaSQL functionality
- **Debugging**: Can still use AlaSQL debugging tools
- **Works with database tables**: Supports both parameterized queries and database tables
15 changes: 15 additions & 0 deletions examples/precompile/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

rm -fr build/
mkdir build/


bun build myCode.js --target node --outfile build/myCode.bundle.js --external "react-native-*"

bun build myCodeIsolate.js --target node --outfile build/myCodeIsolate.bundle.js

echo "Built precompileJS examples into ./build:" >&2
ls -1 build >&2
Loading
Loading