### Install Absurd-SQL and SQL.js Packages Source: https://github.com/jlongster/absurd-sql/blob/master/README.md This command installs the necessary npm packages: `@jlongster/sql.js` (a fork of sql.js) and `absurd-sql`. These are the core dependencies for integrating SQLite with IndexedDB persistence in your web application. ```Shell yarn add @jlongster/sql.js absurd-sql ``` -------------------------------- ### Basic CSS Styling for Absurd-SQL Demo UI Source: https://github.com/jlongster/absurd-sql/blob/master/src/examples/bench/index.html This CSS snippet defines the fundamental visual layout and appearance for elements within the Absurd-SQL browser demonstration interface. It includes rules for container dimensions, flexbox arrangements, styling of output areas, timing displays, and general text presentation to ensure a consistent and functional user experience. ```CSS html, body, .container { height: 100%; } body { font-family: sans-serif; overflow: hidden; } .container { display: flex; flex-direction: column; } .flex { display: flex; } .flex-fill { flex: 1; overflow: hidden; } .output { padding: 15px 10px; font-size: 14px; overflow: auto; border: 1px solid #d0d0fa; border-radius: 6px; margin: 20px 0; } .timings { margin-left: 10px; width: 100px; text-align: center; } .timings strong { display: block; margin-bottom: 5px; } .output > div { margin-bottom: 10px; } .options { margin-top: 10px; } .options label { margin-right: 15px; } .text { max-width: 800px; line-height: 1.4em; margin-bottom: 10px; } .text.last { margin-bottom: 15px; } .more { display: none; } .more-text { display: inline; font-size: 13px; color: #8080a0; margin-left: 5px; text-decoration: none; } ``` -------------------------------- ### Initialize Absurd-SQL Backend in Main Thread Source: https://github.com/jlongster/absurd-sql/blob/master/README.md This JavaScript snippet demonstrates how to set up the `absurd-sql` backend from the main thread. It creates a Web Worker to offload database operations and uses `initBackend` to proxy worker creation, especially for Safari's limitations with nested workers. This ensures database operations don't block the main UI thread. ```JavaScript import { initBackend } from 'absurd-sql/dist/indexeddb-main-thread'; function init() { let worker = new Worker(new URL('./index.worker.js', import.meta.url)); // This is only required because Safari doesn't support nested // workers. This installs a handler that will proxy creating web // workers through the main thread initBackend(worker); } init(); ``` -------------------------------- ### Configure Absurd-SQL and SQLiteFS in Web Worker Source: https://github.com/jlongster/absurd-sql/blob/master/README.md This JavaScript code runs within the Web Worker and sets up the SQLite environment. It initializes `sql.js`, integrates `SQLiteFS` with `IndexedDBBackend` for persistent storage, mounts the file system, and opens the SQLite database. It also includes a fallback mechanism for browsers without `SharedArrayBuffer` and optimizes performance with a `PRAGMA` statement. ```JavaScript import initSqlJs from '@jlongster/sql.js'; import { SQLiteFS } from 'absurd-sql'; import IndexedDBBackend from 'absurd-sql/dist/indexeddb-backend'; async function run() { let SQL = await initSqlJs({ locateFile: file => file }); let sqlFS = new SQLiteFS(SQL.FS, new IndexedDBBackend()); SQL.register_for_idb(sqlFS); SQL.FS.mkdir('/sql'); SQL.FS.mount(sqlFS, {}, '/sql'); const path = '/sql/db.sqlite'; if (typeof SharedArrayBuffer === 'undefined') { let stream = SQL.FS.open(path, 'a+'); await stream.node.contents.readIfFallback(); SQL.FS.close(stream); } let db = new SQL.Database(path, { filename: true }); // You might want to try `PRAGMA page_size=8192;` too! db.exec(` PRAGMA journal_mode=MEMORY; `); // Your code } ``` -------------------------------- ### Required HTTP Headers for SharedArrayBuffer Source: https://github.com/jlongster/absurd-sql/blob/master/README.md To enable `SharedArrayBuffer` and `Atomics` API, which are crucial for `absurd-sql`'s performance, your web server must respond with these specific HTTP headers. They enforce cross-origin isolation, a security measure required by browsers for these powerful features. ```HTTP Headers Cross-Origin-Opener-Policy: same-origin Cross-Origin-Embedder-Policy: require-corp ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.