--- title: AlaSQL pagination_prev: demos/desktop/index pagination_next: demos/local/index sidebar_custom_props: sql: true ---
import current from '/version.js'; AlaSQL is a pure JavaScript in-memory SQL database. It has built-in support for SheetJS through the `XLSX` target operator. This demo covers basic concepts pertaining to data import and export. The official documentation includes advanced examples and deployment tips as well as strategies for general data processing in AlaSQL expressions. :::note This demo was tested in the following environments: | Environment | AlaSQL | Date | |:--------------------|:-------|:----------:| | NodeJS | 3.1.0 | 2023-02-23 | | Standalone (Chrome) | 3.0.0 | 2023-04-09 | ::: ## Live Demo This demo fetchesURL: {url}
Import: {q1}
Export: {q2}
Index | Nom |
---|---|
{Index} | {Nom} |
{`\
{
/* add this part before "name" */
/* highlight-start */
"overrides": {
"xlsx": "https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz"
},
/* highlight-end */
"name": "my-project",
/* ... more fields ... */
`}
After adding the override, AlaSQL can be installed through `npm`:
```bash
npm install --save alasql
```
In imports, the SheetJS library must be passed to AlaSQL as shown below:
```js
import * as alasql from 'alasql';
import * as XLSX from 'xlsx';
alasql.utils.isBrowserify = false;
alasql.utils.global.XLSX = XLSX;
```
### Reading Files
The `XLSX` "from" target expects a filename. In the browser, AlaSQL uses object
URLs which can be created from `Blob` or `File` objects.
The following snippet fetches data and passes to AlaSQL:
```js
const blob = await (await fetch("https://sheetjs.com/pres.numbers")).blob();
const data = URL.createOjectURL(blob);
const res = await alasql.promise("SELECT * FROM XLSX(?, {autoExt: false}", [data]);
```
By default, the `XLSX` "from" target automatically adds a `.xlsx` extension. To
read URLs, the `autoExt: false` option should be passed as the second argument:
```sql
SELECT `Name`, `Index` FROM XLSX(
? --<< this will be the URL passed into `alasql.promise`
// highlight-start
, { --<< options are supplied as the second argument to XLSX operator
autoExt: false --<< do not automatically add ".xlsx" extension!
}
// highlight-end
) WHERE `Index` < 45
```
By default the workbook is parsed and `sheet_to_json` is used to pull data:
```js
(async() => {
const blob = await (await fetch("https://sheetjs.com/pres.numbers")).blob();
const data = URL.createOjectURL(blob);
const aoo = await alasql.promise("SELECT * FROM XLSX(?, {autoExt: false}", [data]);
console.log(aoo); // [ { Name: "Bill Clinton", Index: 42 }, ...]
})();
```
### Writing Files
The `XLSX` "into" target calls `XLSX.writeFile` under the hood:
```js
const { promise: alasql } = require("alasql");
(async() => {
const data = [
{ Name: "Bill Clinton", Index: 42 },
{ Name: "Someone Else", Index: 47 }
];
await alasql(`SELECT * INTO XLSX("PresMod5.xlsx") FROM ?`, [data]);
/* PresMod5.xlsx will be created */
})();
```
## NodeJS
:::caution
`alasql` uses an older version of the library. It can be overridden through a
`package.json` override in the latest versions of NodeJS:
{`\
{
"overrides": {
"xlsx": "https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz"
}
}`}
:::
### Reading Files
By default, the `XLSX` "from" target automatically adds a `.xlsx` extension. To
read files with an arbitrary filename, the `autoExt: false` option should be
passed as the second argument:
```sql
SELECT `Name`, `Index` FROM XLSX(
"pres.numbers" --<< filename is "pres.numbers"
// highlight-start
, { --<< options are supplied as the second argument to XLSX operator
autoExt: false --<< do not automatically add ".xlsx" extension!
}
// highlight-end
) WHERE `Index` < 45
```
By default the workbook is parsed and `sheet_to_json` is used to pull data:
```js
const { promise: alasql } = require("alasql");
(async() => {
const aoo = await alasql(`SELECT * from XLSX("pres.xlsx", {autoExt: false})`);
console.log(aoo); // [ { Name: "Bill Clinton", Index: 42 }, ...]
})();
```
### Writing Files
The `XLSX` "into" target calls `XLSX.writeFile` under the hood:
```js
const { promise: alasql } = require("alasql");
(async() => {
const data = [
{ Name: "Bill Clinton", Index: 42 },
{ Name: "Someone Else", Index: 47 }
];
await alasql(`SELECT * INTO XLSX("PresMod5.xlsx") FROM ?`, [data]);
/* PresMod5.xlsx will be created */
})();
```
### NodeJS Example
1) Create an empty folder for the project:
```bash
mkdir alasql
cd alasql
```
2) In the folder, create a stub `package.json` with the `xlsx` override:
```json title="package.json"
{
"overrides": {
"xlsx": "https://cdn.sheetjs.com/xlsx-latest/xlsx-latest.tgz"
}
}
```
3) Install SheetJS and AlaSQL:
```bash
npm i --save alasql@3.1.0 https://cdn.sheetjs.com/xlsx-latest/xlsx-latest.tgz
```
4) Download the test file