Introduction to node-sqlite3
The node-sqlite3
project provides asynchronous, non-blocking bindings for SQLite3, a popular database engine, to be used within Node.js environments. This library allows Node.js applications to interact with SQLite databases efficiently, offering an easy-to-use interface while leveraging the capabilities of SQLite3.
Features
node-sqlite3
boasts a host of features that make it a powerful tool for developers:
- Straightforward Query and Parameter Binding: Users can easily create queries and bind parameters, facilitating seamless database operations.
- Full Buffer/Blob Support: It handles large data blobs efficiently, making it suitable for applications requiring the storage or processing of multimedia and other binary data.
- Debugging Support: The library provides extensive debugging support, useful for tracking down issues or optimizing performance.
- Query Serialization API: This allows users to control the execution flow of queries, making it possible to serialize operations as needed.
- Extension Support:
node-sqlite3
supports extensions, including built-in support for the JSON1 extension, enabling JSON processing capabilities within SQLite. - Robust Test Suite: The project includes a comprehensive set of tests to ensure reliability and performance.
- Modern Codebase: Written in modern C++, the codebase is designed to avoid memory leaks, ensuring robust performance.
- SQLite Version: The library bundles SQLite version 3.45.0, but users can choose to use a local SQLite build if preferred.
Installation
To install node-sqlite3
, users can use either npm
or yarn
:
npm install sqlite3
# or
yarn add sqlite3
For those who prefer the latest updates directly from the GitHub repository, the master branch can be installed via:
npm install https://github.com/tryghost/node-sqlite3/tarball/master
Prebuilt Binaries
With Node-API compatibility, prebuilt binaries are available across various platforms and architectures for Node.js versions 10 and newer. These binaries ensure quick and easy installation without the need for manual building.
Usage
Here’s a simple example of using node-sqlite3
:
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database(':memory:');
db.serialize(() => {
db.run("CREATE TABLE lorem (info TEXT)");
const stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (let i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
db.each("SELECT rowid AS id, info FROM lorem", (err, row) => {
console.log(row.id + ": " + row.info);
});
});
db.close();
This example demonstrates how to create a table, insert data, and retrieve entries using SQL commands.
Building and Installation Options
Source Installation
For users wanting to build from source or work with custom SQLite configurations, there are options available through command-line arguments during installation. This includes using external SQLite builds or specifying custom file headers.
Building for node-webkit and SQLCipher
Special build instructions are provided for environments such as node-webkit and installations requiring SQLCipher, ensuring compatibility and enhanced security features where needed.
Testing and Contribution
node-sqlite3
includes a test suite executable with the command:
npm test
The project has been contributed to by numerous developers, with key contributors listed in the documentation. Community collaboration continues to play a vital role in its development.
Acknowledgments and Maintenance
Originally created by Mapbox, the project is now maintained by Ghost. The contributions from various developers and community support have been invaluable in its growth and success. For change logs and licensing information, users are directed to the project's GitHub repository.
In summary, node-sqlite3
remains a crucial library for Node.js developers needing efficient, reliable SQLite database integrations with a broad range of features and robust community support.