From ac0dc201e88f7cb30aa5e90f17d740008e1d7b45 Mon Sep 17 00:00:00 2001 From: Yukha Dharmeswara Date: Fri, 13 Jan 2023 18:24:23 +0700 Subject: [PATCH 1/2] feat: add typescript support --- index.js | 3 +++ package.json | 10 +++++++-- types/index.d.ts | 50 +++++++++++++++++++++++++++++++++++++++++++ types/index.test-d.ts | 20 +++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 types/index.d.ts create mode 100644 types/index.test-d.ts diff --git a/index.js b/index.js index 1f7a226..e500695 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/package.json b/package.json index d86cace..4a6c318 100644 --- a/package.json +++ b/package.json @@ -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", @@ -29,9 +32,12 @@ }, "homepage": "https://github.com/Eomm/fastify-sqlite#readme", "devDependencies": { + "@fastify/pre-commit": "^2.0.2", + "@types/node": "^18.0.0", "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", diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..8897875 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,50 @@ +import type { FastifyPluginAsync } from "fastify"; +import { sqlite3 } from "sqlite3"; +import { Database } from "sqlite"; + +declare module "fastify" { + interface FastifyInstance { + sqlite: Database & { [name: string]: Database }; + } +} + +type FastifySqlite = FastifyPluginAsync; + +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 +): ReturnType; +export = fastifySqlite; diff --git a/types/index.test-d.ts b/types/index.test-d.ts new file mode 100644 index 0000000..bfc6599 --- /dev/null +++ b/types/index.test-d.ts @@ -0,0 +1,20 @@ +import fastify from "fastify"; +import fastifySqlite, { sqlite3 as reExportedSqlite3 } from "."; +import { expectType } from "tsd"; +import { sqlite3 } from "sqlite3"; +import { Database } from "sqlite"; + +const app = fastify(); + +app + .register(fastifySqlite, { + dbFile: "foo.db", + name: "foo", + }) + .after((err) => { + app.sqlite; + expectType(app.sqlite); + expectType(app.sqlite.foo); + }); + +expectType(reExportedSqlite3); From b1f16e5ce85b172da50dab0ba4a17fe0c4ad5642 Mon Sep 17 00:00:00 2001 From: Yoav Gal Date: Tue, 9 Jan 2024 13:19:26 +0200 Subject: [PATCH 2/2] Addded native support to sqlite3 Database --- package.json | 1 + types/index.d.ts | 3 +-- types/index.test-d.ts | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 4a6c318..cece4a9 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "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", diff --git a/types/index.d.ts b/types/index.d.ts index 8897875..0efd1da 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,6 +1,5 @@ import type { FastifyPluginAsync } from "fastify"; -import { sqlite3 } from "sqlite3"; -import { Database } from "sqlite"; +import { sqlite3, Database } from "sqlite3"; declare module "fastify" { interface FastifyInstance { diff --git a/types/index.test-d.ts b/types/index.test-d.ts index bfc6599..4773192 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -1,8 +1,7 @@ import fastify from "fastify"; import fastifySqlite, { sqlite3 as reExportedSqlite3 } from "."; import { expectType } from "tsd"; -import { sqlite3 } from "sqlite3"; -import { Database } from "sqlite"; +import { sqlite3, Database } from "sqlite3"; const app = fastify();