A lightweight TypeScript wrapper for SQLite WebAssembly, providing a simple interface for in-memory SQLite database operations in the browser.
- In-memory SQLite database operations
- Database export and import functionality
- Asynchronous API
- TypeScript support
- Event callback support
- Zero configuration required
npm install @agnosticeng/sqlite
import { SQLite } from '@agnosticeng/sqlite';
const db = new SQLite();
// Execute queries
async function example() {
// Create a table
await db.exec(`
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER
)
`);
// Insert data
await db.exec(`
INSERT INTO users (name, age) VALUES
('John Doe', 30),
('Jane Smith', 25)
`);
// Or use binding
await db.exec(`
INSERT INTO users (name, age) VALUES
(?, ?)
`, ['Johnny Appleseed', 70])
// Query data
const results = await db.exec('SELECT * FROM users');
console.log(results);
// Add callback for exec events with unsubscribe
const unsubscribe = db.on('exec', (args) => {
console.log('Query executed:', args[0]);
console.log('Bindings:', args[1]);
});
// Later unsubscribe when done
unsubscribe();
}
Export the current database state as a Uint8Array:
const bytes = await db.export_db();
// Store or transmit the bytes as needed
Load a database from a Uint8Array:
// Load database with modification capabilities
await db.load_db(bytes, false);
// Load database in immutable mode
await db.load_db(bytes, true);
const db = new SQLite();
Creates a new SQLite instance with an in-memory database.
Executes an SQL query and returns the results as an array of objects.
- Parameters:
query
: SQL query stringbind
: Optional binding parameters
- Returns: Promise resolving to an array of row objects
Subscribes to database events and returns an unsubscribe function.
- Parameters:
event
: Event type ("exec")callback
: Function called when event occurs
- Returns: Function that when called will unsubscribe the callback
Exports the current database state.
- Returns: Promise resolving to a Uint8Array containing the database
Loads a database from a Uint8Array.
- Parameters:
bytes
: Uint8Array containing the databaseimmutable
: If true, loads the database in read-only mode
@sqlite.org/sqlite-wasm
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a new branch:
git checkout -b feature/amazing-feature
- Make your changes
- Commit your changes:
git commit -m 'Add some amazing feature'
- Push to the branch:
git push origin feature/amazing-feature
- Open a Pull Request
Please make sure to:
- Update the README.md with details of changes if applicable
- Update any documentation if needed
- Add tests for new features
- Follow the existing code style
- Reference any relevant issues in your PR
MIT License
Copyright (c) 2024 Agnostic
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.