@noormdev/sdk 1.0.0-alpha.15 → 1.0.0-alpha.16

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.
@@ -0,0 +1,81 @@
1
+ import { Kysely, MssqlDialect, sql } from 'kysely';
2
+
3
+ // src/core/connection/dialects/mssql.ts
4
+ function buildTediousConfig(Tedious, config, database) {
5
+ return new Tedious.Connection({
6
+ server: config.host ?? "localhost",
7
+ authentication: {
8
+ type: "default",
9
+ options: {
10
+ userName: config.user,
11
+ password: config.password
12
+ }
13
+ },
14
+ options: {
15
+ port: config.port ?? 1433,
16
+ database: database ?? config.database,
17
+ trustServerCertificate: !config.ssl,
18
+ encrypt: true
19
+ }
20
+ });
21
+ }
22
+ async function verifyDatabaseExists(Tedious, Tarn, config) {
23
+ const masterDb = new Kysely({
24
+ dialect: new MssqlDialect({
25
+ tarn: {
26
+ ...Tarn,
27
+ options: {
28
+ min: 0,
29
+ max: 1,
30
+ propagateCreateError: true
31
+ }
32
+ },
33
+ tedious: {
34
+ ...Tedious,
35
+ connectionFactory: () => buildTediousConfig(Tedious, config, "master")
36
+ }
37
+ })
38
+ });
39
+ try {
40
+ const { rows } = await sql`
41
+ SELECT name FROM sys.databases WHERE name = ${config.database}
42
+ `.execute(masterDb);
43
+ if (rows.length === 0) {
44
+ throw new Error(
45
+ `Database '${config.database}' does not exist on ${config.host ?? "localhost"}:${config.port ?? 1433}`
46
+ );
47
+ }
48
+ } finally {
49
+ await masterDb.destroy();
50
+ }
51
+ }
52
+ async function createMssqlConnection(config) {
53
+ const Tedious = await import('tedious');
54
+ const Tarn = await import('tarn');
55
+ await verifyDatabaseExists(Tedious, Tarn, config);
56
+ const db = new Kysely({
57
+ dialect: new MssqlDialect({
58
+ tarn: {
59
+ ...Tarn,
60
+ options: {
61
+ min: config.pool?.min ?? 0,
62
+ max: config.pool?.max ?? 10,
63
+ propagateCreateError: true
64
+ }
65
+ },
66
+ tedious: {
67
+ ...Tedious,
68
+ connectionFactory: () => buildTediousConfig(Tedious, config)
69
+ }
70
+ })
71
+ });
72
+ return {
73
+ db,
74
+ dialect: "mssql",
75
+ destroy: () => db.destroy()
76
+ };
77
+ }
78
+
79
+ export { createMssqlConnection };
80
+ //# sourceMappingURL=mssql-7VB7EHRX.js.map
81
+ //# sourceMappingURL=mssql-7VB7EHRX.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/core/connection/dialects/mssql.ts"],"names":[],"mappings":";;;AAmBA,SAAS,kBAAA,CACL,OAAA,EACA,MAAA,EACA,QAAA,EACF;AAEE,EAAA,OAAO,IAAI,QAAQ,UAAA,CAAW;AAAA,IAC1B,MAAA,EAAQ,OAAO,IAAA,IAAQ,WAAA;AAAA,IACvB,cAAA,EAAgB;AAAA,MACZ,IAAA,EAAM,SAAA;AAAA,MACN,OAAA,EAAS;AAAA,QACL,UAAU,MAAA,CAAO,IAAA;AAAA,QACjB,UAAU,MAAA,CAAO;AAAA;AACrB,KACJ;AAAA,IACA,OAAA,EAAS;AAAA,MACL,IAAA,EAAM,OAAO,IAAA,IAAQ,IAAA;AAAA,MACrB,QAAA,EAAU,YAAY,MAAA,CAAO,QAAA;AAAA,MAC7B,sBAAA,EAAwB,CAAC,MAAA,CAAO,GAAA;AAAA,MAChC,OAAA,EAAS;AAAA;AACb,GACH,CAAA;AAEL;AASA,eAAe,oBAAA,CACX,OAAA,EACA,IAAA,EACA,MAAA,EACa;AAEb,EAAA,MAAM,QAAA,GAAW,IAAI,MAAA,CAAgB;AAAA,IACjC,OAAA,EAAS,IAAI,YAAA,CAAa;AAAA,MACtB,IAAA,EAAM;AAAA,QACF,GAAG,IAAA;AAAA,QACH,OAAA,EAAS;AAAA,UACL,GAAA,EAAK,CAAA;AAAA,UACL,GAAA,EAAK,CAAA;AAAA,UACL,oBAAA,EAAsB;AAAA;AAC1B,OACJ;AAAA,MACA,OAAA,EAAS;AAAA,QACL,GAAG,OAAA;AAAA,QACH,iBAAA,EAAm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AM;AAAA;AAC/D,KACH;AAAA,GACJ,CAAA;AAED,EAAA,OAAO;AAAA,IACH,EAAA;AAAA,IACA,OAAA,EAAS,OAAA;AAAA,IACT,OAAA,EAAS,MAAM,EAAA,CAAG,OAAA;AAAQ,GAC9B;AAEJ","file":"mssql-7VB7EHRX.js","sourcesContent":["/**\n * SQL Server (MSSQL) dialect adapter.\n *\n * Uses 'tedious' and 'tarn' packages for MSSQL connections.\n * Install with: npm install tedious tarn\n *\n * Verifies database existence via sys.databases before connecting\n * to the target database, avoiding cryptic ECONNRESET errors when\n * the database doesn't exist.\n */\nimport { Kysely, MssqlDialect, sql } from 'kysely';\nimport type { ConnectionConfig, ConnectionResult } from '../types.js';\n\n/**\n * Build tedious connection options from noorm config.\n *\n * Centralizes the tedious config so both the preflight check\n * and the real pool use the same settings.\n */\nfunction buildTediousConfig(\n Tedious: typeof import('tedious'),\n config: ConnectionConfig,\n database?: string,\n) {\n\n return new Tedious.Connection({\n server: config.host ?? 'localhost',\n authentication: {\n type: 'default',\n options: {\n userName: config.user,\n password: config.password,\n },\n },\n options: {\n port: config.port ?? 1433,\n database: database ?? config.database,\n trustServerCertificate: !config.ssl,\n encrypt: true,\n },\n });\n\n}\n\n/**\n * Verify the target database exists by querying sys.databases on master.\n *\n * Connects to 'master' first and checks sys.databases. Throws a clear\n * error if the database is missing, instead of letting tedious hang\n * with a cryptic ECONNRESET.\n */\nasync function verifyDatabaseExists(\n Tedious: typeof import('tedious'),\n Tarn: typeof import('tarn'),\n config: ConnectionConfig,\n): Promise<void> {\n\n const masterDb = new Kysely<unknown>({\n dialect: new MssqlDialect({\n tarn: {\n ...Tarn,\n options: {\n min: 0,\n max: 1,\n propagateCreateError: true,\n },\n },\n tedious: {\n ...Tedious,\n connectionFactory: () => buildTediousConfig(Tedious, config, 'master'),\n },\n }),\n });\n\n try {\n\n const { rows } = await sql<{ name: string }>`\n SELECT name FROM sys.databases WHERE name = ${config.database}\n `.execute(masterDb);\n\n if (rows.length === 0) {\n\n throw new Error(\n `Database '${config.database}' does not exist on ${config.host ?? 'localhost'}:${config.port ?? 1433}`,\n );\n\n }\n\n }\n finally {\n\n await masterDb.destroy();\n\n }\n\n}\n\n/**\n * Create a SQL Server connection.\n *\n * Verifies the target database exists via master before opening\n * the connection pool. This avoids the tedious/tarn hang that\n * occurs when MSSQL rejects login for a non-existent database.\n *\n * @example\n * ```typescript\n * const conn = createMssqlConnection({\n * dialect: 'mssql',\n * host: 'localhost',\n * port: 1433,\n * database: 'myapp',\n * user: 'sa',\n * password: 'secret',\n * })\n * ```\n */\nexport async function createMssqlConnection(config: ConnectionConfig): Promise<ConnectionResult> {\n\n // Dynamic import to avoid compile-time dependency\n const Tedious = await import('tedious');\n const Tarn = await import('tarn');\n\n // Preflight: verify database exists via master\n await verifyDatabaseExists(Tedious, Tarn, config);\n\n const db = new Kysely<unknown>({\n dialect: new MssqlDialect({\n tarn: {\n ...Tarn,\n options: {\n min: config.pool?.min ?? 0,\n max: config.pool?.max ?? 10,\n propagateCreateError: true,\n },\n },\n tedious: {\n ...Tedious,\n connectionFactory: () => buildTediousConfig(Tedious, config),\n },\n }),\n });\n\n return {\n db,\n dialect: 'mssql',\n destroy: () => db.destroy(),\n };\n\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noormdev/sdk",
3
- "version": "1.0.0-alpha.15",
3
+ "version": "1.0.0-alpha.16",
4
4
  "description": "Database schema & changeset manager SDK",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,46 +0,0 @@
1
- import { Kysely, MssqlDialect } from 'kysely';
2
-
3
- // src/core/connection/dialects/mssql.ts
4
- async function createMssqlConnection(config) {
5
- const Tedious = await import('tedious');
6
- const Tarn = await import('tarn');
7
- const db = new Kysely({
8
- dialect: new MssqlDialect({
9
- tarn: {
10
- ...Tarn,
11
- options: {
12
- min: config.pool?.min ?? 0,
13
- max: config.pool?.max ?? 10
14
- }
15
- },
16
- tedious: {
17
- ...Tedious,
18
- connectionFactory: () => new Tedious.Connection({
19
- server: config.host ?? "localhost",
20
- authentication: {
21
- type: "default",
22
- options: {
23
- userName: config.user,
24
- password: config.password
25
- }
26
- },
27
- options: {
28
- port: config.port ?? 1433,
29
- database: config.database,
30
- trustServerCertificate: !config.ssl,
31
- encrypt: !!config.ssl
32
- }
33
- })
34
- }
35
- })
36
- });
37
- return {
38
- db,
39
- dialect: "mssql",
40
- destroy: () => db.destroy()
41
- };
42
- }
43
-
44
- export { createMssqlConnection };
45
- //# sourceMappingURL=mssql-T4M7OGEC.js.map
46
- //# sourceMappingURL=mssql-T4M7OGEC.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/core/connection/dialects/mssql.ts"],"names":[],"mappings":";;;AAwBA,eAAsB,sBAAsB,MAAA,EAAqD;AAG7F,EAAA,MAAM,OAAA,GAAU,MAAM,OAAO,SAAS,CAAA;AACtC,EAAA,MAAM,IAAA,GAAO,MAAM,OAAO,MAAM,CAAA;AAEhC,EAAA,MAAM,EAAA,GAAK,IAAI,MAAA,CAAgB;AAAA,IAC3B,OAAA,EAAS,IAAI,YAAA,CAAa;AAAA,MACtB,IAAA,EAAM;AAAA,QACF,GAAG,IAAA;AAAA,QACH,OAAA,EAAS;AAAA,UACL,GAAA,EAAK,MAAA,CAAO,IAAA,EAAM,GAAA,IAAO,CAAA;AAAA,UACzB,GAAA,EAAK,MAAA,CAAO,IAAA,EAAM,GAAA,IAAO;AAAA;AAC7B,OACJ;AAAA,MACA,OAAA,EAAS;AAAA,QACL,GAAG,OAAA;AAAA,QACH,iBAAA,EAAmB,MACf,IAAI,OAAA,CAAQ,UAAA,CAAW;AAAA,UACnB,MAAA,EAAQ,OAAO,IAAA,IAAQ,WAAA;AAAA,UACvB,cAAA,EAAgB;AAAA,YACZ,IAAA,EAAM,SAAA;AAAA,YACN,OAAA,EAAS;AAAA,cACL,UAAU,MAAA,CAAO,IAAA;AAAA,cACjB,UAAU,MAAA,CAAO;AAAA;AACrB,WACJ;AAAA,UACA,OAAA,EAAS;AAAA,YACL,IAAA,EAAM,OAAO,IAAA,IAAQ,IAAA;AAAA,YACrB,UAAU,MAAA,CAAO,QAAA;AAAA,YACjB,sBAAA,EAAwB,CAAC,MAAA,CAAO,GAAA;AAAA,YAChC,OAAA,EAAS,CAAC,CAAC,MAAA,CAAO;AAAA;AACtB,SACH;AAAA;AACT,KACH;AAAA,GACJ,CAAA;AAED,EAAA,OAAO;AAAA,IACH,EAAA;AAAA,IACA,OAAA,EAAS,OAAA;AAAA,IACT,OAAA,EAAS,MAAM,EAAA,CAAG,OAAA;AAAQ,GAC9B;AAEJ","file":"mssql-T4M7OGEC.js","sourcesContent":["/**\n * SQL Server (MSSQL) dialect adapter.\n *\n * Uses 'tedious' and 'tarn' packages for MSSQL connections.\n * Install with: npm install tedious tarn\n */\nimport { Kysely, MssqlDialect } from 'kysely';\nimport type { ConnectionConfig, ConnectionResult } from '../types.js';\n\n/**\n * Create a SQL Server connection.\n *\n * @example\n * ```typescript\n * const conn = createMssqlConnection({\n * dialect: 'mssql',\n * host: 'localhost',\n * port: 1433,\n * database: 'myapp',\n * user: 'sa',\n * password: 'secret',\n * })\n * ```\n */\nexport async function createMssqlConnection(config: ConnectionConfig): Promise<ConnectionResult> {\n\n // Dynamic import to avoid compile-time dependency\n const Tedious = await import('tedious');\n const Tarn = await import('tarn');\n\n const db = new Kysely<unknown>({\n dialect: new MssqlDialect({\n tarn: {\n ...Tarn,\n options: {\n min: config.pool?.min ?? 0,\n max: config.pool?.max ?? 10,\n },\n },\n tedious: {\n ...Tedious,\n connectionFactory: () =>\n new Tedious.Connection({\n server: config.host ?? 'localhost',\n authentication: {\n type: 'default',\n options: {\n userName: config.user,\n password: config.password,\n },\n },\n options: {\n port: config.port ?? 1433,\n database: config.database,\n trustServerCertificate: !config.ssl,\n encrypt: !!config.ssl,\n },\n }),\n },\n }),\n });\n\n return {\n db,\n dialect: 'mssql',\n destroy: () => db.destroy(),\n };\n\n}\n"]}