{"id":16687,"library":"sql-schema-reader","title":"SQL Server Schema Reader","description":"sql-schema-reader is a JavaScript/TypeScript library designed to programmatically extract detailed schema information from SQL Server databases. It leverages the `Tedious.js` driver for database connectivity. The package, currently at version 3.4.1, focuses on providing programmatic access to metadata such as table names, comprehensive column definitions (including nullability, types, lengths, primary/foreign keys), stored procedures, scalar functions, and table-valued functions. Its release cadence appears stable, with `3.x` being the current major version. Key differentiators include its comprehensive schema introspection capabilities specifically tailored for SQL Server, allowing developers to retrieve definitions, columns, and relationships with ease, contrasting with more generic ORM schema tools or .NET specific solutions.","status":"active","version":"3.4.1","language":"javascript","source_language":"en","source_url":"https://github.com/he3/SqlSchemaReader","tags":["javascript"],"install":[{"cmd":"npm install sql-schema-reader","lang":"bash","label":"npm"},{"cmd":"yarn add sql-schema-reader","lang":"bash","label":"yarn"},{"cmd":"pnpm add sql-schema-reader","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Provides the underlying TDS protocol implementation for connecting to SQL Server databases.","package":"tedious","optional":false}],"imports":[{"note":"While the README shows CommonJS `require`, modern Node.js projects typically use ESM `import`. `sql-schema-reader` is likely designed for both, but `import` is preferred in newer environments.","wrong":"const schemaReader = require('sql-schema-reader');","symbol":"schemaReader","correct":"import schemaReader from 'sql-schema-reader';"},{"note":"Although the primary export is often a default object, some modern libraries also expose functions directly as named exports for better tree-shaking and explicit import patterns. This assumes individual functions like `tableNames` might be directly exported in some versions or configurations.","wrong":"import schemaReader from 'sql-schema-reader'; const names = schemaReader.tableNames(config);","symbol":"tableNames","correct":"import { tableNames } from 'sql-schema-reader';"},{"note":"For TypeScript users, importing the `ConfigObject` type is crucial for defining database connection configurations correctly, ensuring type safety for `server`, `database`, `username`, and `password` properties.","symbol":"ConfigObject","correct":"import type { ConfigObject } from 'sql-schema-reader';"}],"quickstart":{"code":"import schemaReader from 'sql-schema-reader';\n\nasync function go(){\n    const config = {\n        \"server\": process.env.DB_SERVER ?? 'localhost',\n        \"database\": process.env.DB_NAME ?? 'master',\n        \"username\": process.env.DB_USERNAME ?? 'sa',\n        \"password\": process.env.DB_PASSWORD ?? ''  \n    };\n\n    try {\n        console.log(\"Attempting to read SQL Server schema...\");\n\n        // Tables\n        console.log(\"Fetching table names...\");\n        const tableNames = await schemaReader.tableNames(config);\n        console.log(\"Table Names:\", tableNames.map(t => `${t.schema}.${t.name}`));\n\n        if (tableNames.length > 0) {\n            const firstTableName = tableNames[0].name;\n            const firstTableSchema = tableNames[0].schema;\n            console.log(`Fetching details for table: ${firstTableSchema}.${firstTableName}...`);\n            const table = await schemaReader.table(config, `${firstTableSchema}.${firstTableName}`);\n            console.log(\"First Table Details:\", table.columns.map(c => c.columnName));\n        }\n\n        // Stored Procedures\n        console.log(\"Fetching stored procedure names...\");\n        const procNames = await schemaReader.storedProcedureNames(config);\n        console.log(\"Stored Procedure Names:\", procNames.map(p => `${p.schema}.${p.name}`));\n\n        // Table Value Functions\n        console.log(\"Fetching table value function names...\");\n        const functionNames = await schemaReader.tableValueFunctionNames(config);\n        console.log(\"Table Value Function Names:\", functionNames.map(f => `${f.schema}.${f.name}`));\n\n    } catch (error) {\n        console.error(\"Error reading schema:\", error.message);\n        if (error.code === 'ELOGIN') {\n            console.error(\"Check your database server, username, and password.\");\n        } else if (error.code === 'ESOCKET') {\n            console.error(\"Check server address, port, and network connectivity.\");\n        }\n    }\n}\n\ngo();\n","lang":"typescript","description":"This quickstart demonstrates how to connect to a SQL Server database, retrieve a list of all table names, detailed information for the first detected table, and lists of stored procedure and table-valued function names. It includes basic error handling for common connection issues."},"warnings":[{"fix":"Consult the official `sql-schema-reader` and `tedious.js` release notes and changelogs for specific migration instructions for your target version. Update your code to match new API signatures and configuration requirements.","message":"Major version updates (e.g., from v2.x to v3.x) often introduce breaking changes in API signatures or configuration structures, as is common in the `tedious` driver it relies upon. Always review the changelog when upgrading major versions.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure your Node.js environment is at version 18.17 or higher to guarantee compatibility with `tedious` and `sql-schema-reader`.","message":"The underlying `tedious` driver for SQL Server requires Node.js v18.17 or later as of its v19.0.0 release. Using an older Node.js version may lead to runtime errors or compatibility issues.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"Double-check your `config` object for accuracy. Verify network connectivity to the SQL Server and ensure the provided `username` has sufficient permissions (e.g., `VIEW DEFINITION`) to access schema metadata in the specified `database`.","message":"Database connection failures are often due to incorrect configuration, network issues, or insufficient SQL Server permissions. Common problems include wrong `server` address, `username`/`password` issues, or firewall blocks.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always use `await` when calling `sql-schema-reader` methods within an `async` function, or chain `.then()` and `.catch()` to handle the Promise resolution.","message":"All schema-reading methods return Promises and must be awaited. Forgetting to use `await` or handle the Promise result will lead to unhandled promise rejections or incorrect data due to asynchronous operations.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Verify that the `username` and `password` fields in your `config` object exactly match a valid SQL Server login with appropriate database permissions.","cause":"Incorrect username or password in the connection configuration.","error":"Login failed for user 'your_username'."},{"fix":"Ensure the `server` address is correct, the SQL Server instance is running, the port is open (default 1433), and no firewalls are blocking the connection from your application's host.","cause":"The application could not establish a network connection to the SQL Server. This can be due to incorrect server address, port, or network/firewall restrictions.","error":"Failed to connect: Connection Timeout"},{"fix":"If using ESM, ensure `import schemaReader from 'sql-schema-reader';` and call methods as `schemaReader.tableNames()`. If using CommonJS, `const schemaReader = require('sql-schema-reader');` is correct.","cause":"The `schemaReader` object was not correctly imported or initialized, often due to mismatched CommonJS/ESM imports or attempting to destructure from a default export that doesn't expose methods directly.","error":"TypeError: schemaReader.tableNames is not a function"},{"fix":"Check the exact spelling and casing of the object name. Ensure the object exists in the target `database` and `schema`. Verify the database user has `VIEW DEFINITION` permission on the schema and objects.","cause":"The specified table, stored procedure, or function name does not exist in the connected database or schema, or the user lacks permissions to view it.","error":"Invalid object name 'your_tableName'."}],"ecosystem":"npm"}