Skip to content

Commit 8d9f37c

Browse files
authored
Extract compiled JS from SQL for #2169 (#2170)
1 parent af9da89 commit 8d9f37c

File tree

14 files changed

+86723
-24
lines changed

14 files changed

+86723
-24
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ Try AlaSQL JSON objects in Console [sample](http://alasql.org/console?drop table
515515
516516
_Useful stuff, but there might be dragons_
517517
518+
519+
518520
### Graphs
519521
520522
AlaSQL is a multi-paradigm database with support for graphs that can be searched or manipulated.
@@ -569,6 +571,8 @@ then open <http://127.0.0.1:1337/?SELECT%20VALUE%20(2*2)> in your browser
569571
Warning: Alaserver is not multi-threaded, not concurrent, and not secured.
570572
571573
574+
575+
572576
## Tests
573577
574578
### Regression tests
@@ -699,6 +703,6 @@ and other people for useful tools, which make our work much easier.
699703
700704
----
701705
<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>
702-
© 2014-2024, Andrey Gershun ([email protected]) & Mathias Rangel Wulff ([email protected])
706+
© 2014-2025, Andrey Gershun ([email protected]) & Mathias Rangel Wulff ([email protected])
703707
704708
See [this article](https://console.substack.com/p/console-187) for a bit of information about the motivation and background.

build.sh

100644100755
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ x esbuild --minify --outfile="$outfile_min" "$outfile" --allow-overwrite
172172

173173

174174

175+
echo '\nBuild precompile files'
176+
mkdir -p dist/precompile
177+
178+
echo '# Copy precompile module'
179+
x esbuild --outfile="dist/precompile/index.js" "src/precompile/index.js" --format=cjs
180+
181+
182+
183+
175184
echo '\nBuild worker files'
176185
outfile="dist/alasql-worker.js"
177186
outfile_min="dist/alasql-worker.js"

docs/PRECOMPILE.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
### Transform SQL to JavaScript code
3+
4+
If you want to fiddle with speed you can pre-generate the JS code that will be running your query.
5+
6+
The returned function has the signature `function(params, cb)` and is designed to be called with `alasql` as context.
7+
8+
```js
9+
import alasql from 'alasql';
10+
import {compileToJS} from 'alasql/precompile';
11+
12+
const jsCode = compileToJS('SELECT * FROM ? WHERE pop > 1000000', 'my_db');
13+
14+
const selectPop = new Function('return ' + jsCode)().bind(alasql);
15+
16+
// Now you can call it like a regular alasql function, but without the SQL
17+
let data = [{city: 'Copenhagen', pop: 1300000}, {city: 'Aarhus', pop: 300000}];
18+
let res = selectPop([data]);
19+
// res will be [{city: 'Copenhagen', pop: 1300000}]
20+
```
21+
22+
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)
23+
24+
25+
---------
26+
27+
28+
# AlaSQL Precompile Module
29+
30+
This module provides SQL compilation functionality that allows you to pre-compile SQL queries into JavaScript code, skipping SQL parsing on execution.
31+
32+
The functionality is experimental - so proceed with caution and expect changes.
33+
34+
## Installation
35+
36+
```javascript
37+
import { compileToJS } from 'alasql/precompile';
38+
```
39+
40+
## Functions
41+
42+
### `compileToJS(sql, databaseid?)` - Precompile
43+
44+
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.
45+
46+
**Parameters:**
47+
- `sql` (string): SQL statement to compile
48+
- `databaseid` (string, optional): Database identifier
49+
50+
**Returns:** Generated JavaScript source code string
51+
52+
## Usage Example
53+
54+
```javascript
55+
import { compileToJS } from 'alasql/precompile';
56+
57+
const sql = 'SELECT name, age FROM users WHERE age > ?';
58+
const jsCode = compileToJS(sql);
59+
60+
// Create a function from the compiled code
61+
const queryFn = new Function('return ' + jsCode)().bind(alasql);
62+
63+
// Execute the function (requires AlaSQL)
64+
const result = queryFn([users, 18]);
65+
```
66+
67+
## Build-time Compilation
68+
69+
You can use this function with build tools like Bun, Vite, or Webpack to compile SQL queries at build time:
70+
71+
```javascript
72+
// Using Bun macro for precompile
73+
import { compileToJS } from './my-queries.js' with { type: 'macro' };
74+
75+
const queryFn = new Function('return ' + compileToJS('SELECT * FROM users'))().bind(alasql);
76+
```
77+
78+
## Benefits
79+
80+
- **Performance**: Eliminate SQL parsing overhead at runtime
81+
- **Flexibility**: Still has access to full AlaSQL functionality
82+
- **Debugging**: Can still use AlaSQL debugging tools
83+
- **Works with database tables**: Supports both parameterized queries and database tables

examples/precompile/build.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4+
cd "$SCRIPT_DIR"
5+
6+
rm -fr build/
7+
mkdir build/
8+
9+
10+
bun build myCode.js --target node --outfile build/myCode.bundle.js --external "react-native-*"
11+
12+
bun build myCodeIsolate.js --target node --outfile build/myCodeIsolate.bundle.js
13+
14+
echo "Built precompileJS examples into ./build:" >&2
15+
ls -1 build >&2

0 commit comments

Comments
 (0)