Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,8 @@ module.exports = fp(fastifySqlite, {
fastify: '^4.x'
})

module.exports.default = fastifySqlite
module.exports.fastifySqlite = fastifySqlite

// let the user access the sqlite3 mode constants eg: sqlite3.OPEN_READONLY
module.exports.sqlite3 = sqlite3
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
"version": "1.1.0",
"description": "Fastify plugin to connect to a SQLite database",
"main": "index.js",
"types": "types/index.d.ts",
"scripts": {
"lint": "standard",
"lint:fix": "standard --fix",
"test": "tap test/**/*.test.js"
"test": "npm run test:unit && npm run test:typescript",
"test:typescript": "tsd",
"test:unit": "tap test/**/*.test.js"
},
"repository": {
"type": "git",
Expand All @@ -29,9 +32,13 @@
},
"homepage": "https://github.com/Eomm/fastify-sqlite#readme",
"devDependencies": {
"@fastify/pre-commit": "^2.0.2",
"@types/node": "^18.0.0",
"@types/sqlite3": "^3.1.11",
"fastify": "^4.5.3",
"standard": "^17.0.0",
"tap": "^16.3.0"
"tap": "^16.3.0",
"tsd": "^0.25.0"
},
"dependencies": {
"fastify-plugin": "^4.2.1",
Expand Down
49 changes: 49 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { FastifyPluginAsync } from "fastify";
import { sqlite3, Database } from "sqlite3";

declare module "fastify" {
interface FastifyInstance {
sqlite: Database & { [name: string]: Database };
}
}

type FastifySqlite = FastifyPluginAsync<fastifySqlite.FastifySqliteOptions>;

declare namespace fastifySqlite {
export interface FastifySqliteOptions {
/**
* Enable Promise API
* @default false
*/
promiseApi?: boolean;
/**
* Optional decorator name
* @default null
*/
name?: string;
/**
* Log sqlite3 queries as trace
* @default false
*/
verbose?: boolean;
/**
* Select the database file
* @default ':memory:'
*/
dbFile?: string;
/**
* How to connect to the DB
* @default OPEN_READWRITE | OPEN_CREATE | OPEN_FULLMUTEX
*/
mode?: number;
}

export const sqlite3: sqlite3;
export const fastifySqlite: FastifySqlite;
export { fastifySqlite as default };
}

declare function fastifySqlite(
...params: Parameters<FastifySqlite>
): ReturnType<FastifySqlite>;
export = fastifySqlite;
19 changes: 19 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fastify from "fastify";
import fastifySqlite, { sqlite3 as reExportedSqlite3 } from ".";
import { expectType } from "tsd";
import { sqlite3, Database } from "sqlite3";

const app = fastify();

app
.register(fastifySqlite, {
dbFile: "foo.db",
name: "foo",
})
.after((err) => {
app.sqlite;
expectType<Database & { [name: string]: Database }>(app.sqlite);
expectType<Database>(app.sqlite.foo);
});

expectType<sqlite3>(reExportedSqlite3);