@fastrack/starter-db-mssql 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Fastrack
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @fastrack/starter-db-mssql
2
+
3
+ MSSQL pooled connections via `mssql` driver. Contract: query(sql, params), transaction(fn), health() with SELECT 1. Parameterized queries only.
4
+
5
+ See [docs/starters.md](../../docs/starters.md).
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @fastrack/starter-db-mssql — MSSQL pool, query/transaction, health SELECT 1.
3
+ */
4
+ export { mssqlStarter } from "./plugin.js";
5
+ export type { MssqlStarterConfig, MssqlClient } from "./plugin.js";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,YAAY,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @fastrack/starter-db-mssql — MSSQL pool, query/transaction, health SELECT 1.
3
+ */
4
+ export { mssqlStarter } from "./plugin.js";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,18 @@
1
+ import { type Static } from "@sinclair/typebox";
2
+ import type { FastrackStarter } from "@fastrack/core";
3
+ export declare const MssqlStarterConfigSchema: import("@sinclair/typebox").TObject<{
4
+ connectionString: import("@sinclair/typebox").TString;
5
+ poolMax: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
6
+ }>;
7
+ export type MssqlStarterConfig = Static<typeof MssqlStarterConfigSchema>;
8
+ export interface MssqlClient {
9
+ query(sql: string, params?: Record<string, unknown>): Promise<unknown[]>;
10
+ transaction<T>(fn: (tx: {
11
+ query: (sql: string, params?: Record<string, unknown>) => Promise<unknown[]>;
12
+ }) => Promise<T>): Promise<T>;
13
+ health(): Promise<{
14
+ ok: boolean;
15
+ }>;
16
+ }
17
+ export declare const mssqlStarter: FastrackStarter<MssqlStarterConfig>;
18
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAGtD,OAAO,KAAK,EAAE,eAAe,EAAmB,MAAM,gBAAgB,CAAC;AAGvE,eAAO,MAAM,wBAAwB;;;EAGnC,CAAC;AAEH,MAAM,MAAM,kBAAkB,GAAG,MAAM,CAAC,OAAO,wBAAwB,CAAC,CAAC;AAEzE,MAAM,WAAW,WAAW;IAC1B,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;IACzE,WAAW,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE;QAAE,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,OAAO,EAAE,CAAC,CAAA;KAAE,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IACrI,MAAM,IAAI,OAAO,CAAC;QAAE,EAAE,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CACpC;AAED,eAAO,MAAM,YAAY,EAAE,eAAe,CAAC,kBAAkB,CAuD5D,CAAC"}
package/dist/plugin.js ADDED
@@ -0,0 +1,71 @@
1
+ import { Type } from "@sinclair/typebox";
2
+ import sql from "mssql";
3
+ import { registerHealthContributor } from "@fastrack/starter-actuator";
4
+ export const MssqlStarterConfigSchema = Type.Object({
5
+ connectionString: Type.String(),
6
+ poolMax: Type.Optional(Type.Number({ default: 10 })),
7
+ });
8
+ export const mssqlStarter = {
9
+ name: "db-mssql",
10
+ configSchema: MssqlStarterConfigSchema,
11
+ defaults: { poolMax: 10 },
12
+ register(app, ctx, config) {
13
+ const pool = new sql.ConnectionPool(config.connectionString, { max: config.poolMax ?? 10 });
14
+ let poolPromise = null;
15
+ async function getPool() {
16
+ if (!poolPromise)
17
+ poolPromise = pool.connect();
18
+ return poolPromise;
19
+ }
20
+ const client = {
21
+ async query(sqlText, params) {
22
+ const p = await getPool();
23
+ const req = p.request();
24
+ if (params)
25
+ for (const [k, v] of Object.entries(params))
26
+ req.input(k, v);
27
+ const result = await req.query(sqlText);
28
+ return result.recordset ?? [];
29
+ },
30
+ async transaction(fn) {
31
+ const p = await getPool();
32
+ const t = new sql.Transaction(p);
33
+ await t.begin();
34
+ try {
35
+ const tx = {
36
+ query: async (sqlText, params) => {
37
+ const req = new sql.Request(t);
38
+ if (params)
39
+ for (const [k, v] of Object.entries(params))
40
+ req.input(k, v);
41
+ const result = await req.query(sqlText);
42
+ return result.recordset ?? [];
43
+ },
44
+ };
45
+ const out = await fn(tx);
46
+ await t.commit();
47
+ return out;
48
+ }
49
+ catch (e) {
50
+ await t.rollback();
51
+ throw e;
52
+ }
53
+ },
54
+ async health() {
55
+ try {
56
+ const rows = await client.query("SELECT 1 AS n");
57
+ return { ok: Array.isArray(rows) && rows.length > 0 };
58
+ }
59
+ catch {
60
+ return { ok: false };
61
+ }
62
+ },
63
+ };
64
+ app.decorate("mssql", client);
65
+ registerHealthContributor(app, "mssql", () => client.health());
66
+ ctx.onShutdown?.(async () => {
67
+ await pool.close();
68
+ });
69
+ },
70
+ };
71
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAe,MAAM,mBAAmB,CAAC;AACtD,OAAO,GAAG,MAAM,OAAO,CAAC;AAGxB,OAAO,EAAE,yBAAyB,EAAE,MAAM,4BAA4B,CAAC;AAEvE,MAAM,CAAC,MAAM,wBAAwB,GAAG,IAAI,CAAC,MAAM,CAAC;IAClD,gBAAgB,EAAE,IAAI,CAAC,MAAM,EAAE;IAC/B,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC;CACrD,CAAC,CAAC;AAUH,MAAM,CAAC,MAAM,YAAY,GAAwC;IAC/D,IAAI,EA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AAI,EAAE;YAC1B,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;QACrB,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAC"}
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@fastrack/starter-db-mssql",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "dependencies": {
15
+ "@sinclair/typebox": "^0.34.34",
16
+ "fastify": "^5.1.0",
17
+ "mssql": "^11.0.1",
18
+ "@fastrack/starter-actuator": "0.1.0",
19
+ "@fastrack/core": "0.1.0"
20
+ },
21
+ "devDependencies": {
22
+ "typescript": "^5.6.3"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public",
26
+ "registry": "https://registry.npmjs.org/"
27
+ },
28
+ "engines": {
29
+ "node": ">=18"
30
+ },
31
+ "scripts": {
32
+ "build": "tsc -p tsconfig.json",
33
+ "clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
34
+ "lint": "echo ok",
35
+ "test": "echo ok"
36
+ }
37
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @fastrack/starter-db-mssql — MSSQL pool, query/transaction, health SELECT 1.
3
+ */
4
+ export { mssqlStarter } from "./plugin.js";
5
+ export type { MssqlStarterConfig, MssqlClient } from "./plugin.js";
package/src/mssql.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ declare module "mssql" {
2
+ export interface ConnectionPool {
3
+ connect(): Promise<ConnectionPool>;
4
+ request(): Request;
5
+ close(): Promise<void>;
6
+ }
7
+ export interface Request {
8
+ input(name: string, value: unknown): Request;
9
+ query(sql: string): Promise<{ recordset: unknown[] }>;
10
+ }
11
+ export interface Transaction {
12
+ begin(): Promise<void>;
13
+ commit(): Promise<void>;
14
+ rollback(): Promise<void>;
15
+ }
16
+ export class ConnectionPool {
17
+ constructor(config: string | Record<string, unknown>, options?: { max?: number });
18
+ }
19
+ export class Transaction {
20
+ constructor(pool: ConnectionPool);
21
+ }
22
+ export class Request {
23
+ constructor(transaction?: Transaction);
24
+ }
25
+ }
package/src/plugin.ts ADDED
@@ -0,0 +1,75 @@
1
+ import { Type, type Static } from "@sinclair/typebox";
2
+ import sql from "mssql";
3
+ import type { FastifyInstance } from "fastify";
4
+ import type { FastrackStarter, FastrackContext } from "@fastrack/core";
5
+ import { registerHealthContributor } from "@fastrack/starter-actuator";
6
+
7
+ export const MssqlStarterConfigSchema = Type.Object({
8
+ connectionString: Type.String(),
9
+ poolMax: Type.Optional(Type.Number({ default: 10 })),
10
+ });
11
+
12
+ export type MssqlStarterConfig = Static<typeof MssqlStarterConfigSchema>;
13
+
14
+ export interface MssqlClient {
15
+ query(sql: string, params?: Record<string, unknown>): Promise<unknown[]>;
16
+ transaction<T>(fn: (tx: { query: (sql: string, params?: Record<string, unknown>) => Promise<unknown[]> }) => Promise<T>): Promise<T>;
17
+ health(): Promise<{ ok: boolean }>;
18
+ }
19
+
20
+ export const mssqlStarter: FastrackStarter<MssqlStarterConfig> = {
21
+ name: "db-mssql",
22
+ configSchema: MssqlStarterConfigSchema,
23
+ defaults: { poolMax: 10 },
24
+ register(app: FastifyInstance, ctx: FastrackContext, config: MssqlStarterConfig) {
25
+ const pool = new sql.ConnectionPool(config.connectionString, { max: config.poolMax ?? 10 });
26
+ let poolPromise: Promise<sql.ConnectionPool> | null = null;
27
+ async function getPool(): Promise<sql.ConnectionPool> {
28
+ if (!poolPromise) poolPromise = pool.connect();
29
+ return poolPromise;
30
+ }
31
+ const client: MssqlClient = {
32
+ async query(sqlText: string, params?: Record<string, unknown>) {
33
+ const p = await getPool();
34
+ const req = p.request();
35
+ if (params) for (const [k, v] of Object.entries(params)) req.input(k, v);
36
+ const result = await req.query(sqlText);
37
+ return result.recordset ?? [];
38
+ },
39
+ async transaction<T>(fn: (tx: { query: (s: string, p?: Record<string, unknown>) => Promise<unknown[]> }) => Promise<T>) {
40
+ const p = await getPool();
41
+ const t = new sql.Transaction(p);
42
+ await t.begin();
43
+ try {
44
+ const tx = {
45
+ query: async (sqlText: string, params?: Record<string, unknown>) => {
46
+ const req = new sql.Request(t);
47
+ if (params) for (const [k, v] of Object.entries(params)) req.input(k, v);
48
+ const result = await req.query(sqlText);
49
+ return result.recordset ?? [];
50
+ },
51
+ };
52
+ const out = await fn(tx);
53
+ await t.commit();
54
+ return out;
55
+ } catch (e) {
56
+ await t.rollback();
57
+ throw e;
58
+ }
59
+ },
60
+ async health() {
61
+ try {
62
+ const rows = await client.query("SELECT 1 AS n");
63
+ return { ok: Array.isArray(rows) && rows.length > 0 };
64
+ } catch {
65
+ return { ok: false };
66
+ }
67
+ },
68
+ };
69
+ app.decorate("mssql", client);
70
+ registerHealthContributor(app, "mssql", () => client.health());
71
+ ctx.onShutdown?.(async () => {
72
+ await pool.close();
73
+ });
74
+ },
75
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": { "outDir": "dist", "rootDir": "src" },
4
+ "include": ["src/**/*"],
5
+ "exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]
6
+ }