Skip to content

Export generated JS #2169

Closed
Closed

Description

@mathiasrw

Let the function alasql.compileToJS export the generated JS code used in the alasql engine.

Activity

  1. self-assigned this
    on Nov 14, 2025
  2. mathiasrw commented on Nov 17, 2025

    @mathiasrw
    MemberAuthor

    Got implemented in a way that does not put the code in the main lib

    import alasql from 'alasql.js';
    
    // Import the pre-compiled function source code using a Bun macro.
    // This will execute compileToJS(sql) at build time and inline the result.
    import {compileToJS} from 'alasql/precompile' with {type: 'macro'};
    
    const sql =
    	'SELECT product, price*100 AS calculated_price FROM ? where price > ? ORDER BY calculated_price DESC';
    
    // Create a function from the source code.
    // We bind `alasql` to `this` so the compiled function can find it.
    const selectProducts = new Function('return ' + compileToJS(sql))().bind(alasql);
    
    const data = [
    	{product: 'Ball', price: 3},
    	{product: 'Pen', price: 1.5},
    ];
    
    // Example of howto pass parameters to the compiled function
    const minPrice = 2;
    
    // Execute the function
    const result = selectProducts([data, minPrice]);
    
    console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions