SQLite integration plugin for Fastify
Effortlessly incorporate SQLite databases into your Fastify applications using fastify-sqlite-typed
. This plugin leverages node-sqlite3
and node-sqlite
for effective database operations.
node-sqlite3
Documentation: https://github.com/TryGhost/node-sqlite3node-sqlite
Documentation: https://github.com/kriasoft/node-sqlite- To work effectively with the database, it's recommended to read node-sqlite3 Wiki.
- Seamless integration with Fastify applications
- Configurable SQLite driver
- Multiple database modes supported
- Debugging capabilities with query tracing
- Support for both in-memory and disk-based databases
npm install fastify-sqlite-typed
# or
yarn add fastify-sqlite-typed
Import and register the plugin with your Fastify instance, and execute a sample query:
import fastify from "fastify";
import sqlitePlugin from "fastify-sqlite-typed";
const app = fastify();
app.register(sqlitePlugin, {
dbFilename: "./myDB.db",
// additional options
});
// Example query
app.get("/users", async (request, reply) => {
const users = await app.db.all("SELECT * FROM users");
reply.send(users);
});
app.listen(3000, (err) => {
if (err) throw err;
console.log(`Server listening on ${app.server.address().port}`);
});
Option | Description | Type | Default |
---|---|---|---|
dbFilename |
Path to the database file (:memory: for in-memory, "" for disk-based). |
String | None |
mode |
Database mode, combining Dbmode values. |
Dbmode |
READWRITE | CREATE | FULLMUTEX |
driverSettings |
Settings for the SQLite driver (see DriverSettings below). |
Object | Default settings |
Option | Description | Type | Default |
---|---|---|---|
verbose |
Enables verbose mode for detailed stack traces. | Boolean | false |
cached |
Enables database object caching. | Boolean | false |
trace |
Function to run on each query execution. | Function | None |
Licensed under MIT.