Registry / database / ts-sql-codegen

ts-sql-codegen

JSON →
library3.23.1jsnpmunverified

A TypeScript code generator that produces table mapper classes for ts-sql-query by inspecting database schemas via tbls YAML output. Version 3.23.1 is current. It eliminates manual mapping and ensures type safety declaratively. Compared to manual mapping, it reduces errors and automates field type detection, documentation propagation, and naming conventions. Integrates with tbls and supports PostgreSQL, MySQL, SQLite, and more. Designed for build-time code generation with high customizability.

npm install ts-sql-codegen
INSTALL
IMPORT
SIG · TS-SQL-CODEGEN
T
ts-sql-codegen
databasejavascriptv3.23.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Generator
import { Generator } from 'ts-sql-codegen'
const Generator = require('ts-sql-codegen')
CommonJS require returns the module, not a named export; use destructuring or named import.
GeneratorOpts
import type { GeneratorOpts } from 'ts-sql-codegen'
import { GeneratorOpts } from 'ts-sql-codegen' (runtime value, but it's a type)
GeneratorOpts is a TypeScript interface; use 'import type' for correct transpilation and to avoid runtime errors.
default import
import tsSqlCodegen from 'ts-sql-codegen'; const { Generator } = tsSqlCodegen;
import Generator from 'ts-sql-codegen' (no default export exists)
The package exports named exports only; there is no default export.

Shows how to use the Generator class programmatically with a connection source path, then consume the generated DB mapper.

// 1. Generate schema with tbls // $ tbls out postgres://postgres:password@localhost:5432/testdb -t yaml -o schema.yaml // 2. Run ts-sql-codegen programmatically import { Generator } from 'ts-sql-codegen'; const options = { schemaPath: './schema.yaml', connectionSourcePath: './src/db/connection-source.ts' }; const generator = new Generator(options); await generator.generate(); // 3. Use generated mappers in your application import { createConnection } from './db/connection-source'; import { DB } from './generated/db'; const connection = await createConnection(); const db = new DB(connection); const users = await db.selectFrom('user').select(['id', 'name']).execute(); console.log(users);
Debug
Known issues
breakingGenerator constructor options changed between v2 and v3. 'connectionSourcePath' is required in v3; previously 'connectionConfig' was used.
fix
Update to use the new options interface; see GeneratorOpts documentation.
affects: >=3.0.0
gotchaThe package requires tbls binary to be installed globally or in PATH, otherwise generation fails silently.
fix
Install tbls via npm i -g tbls or download from https://github.com/k1LoW/tbls#install.
affects: >=1.0.0
deprecatedThe 'removeExtraneous' option is deprecated as of v3.10.0. Use 'clearOutputDir' or 'prune' instead.
fix
Replace 'removeExtraneous: true' with 'clearOutputDir: true' in Generator options.
affects: >=3.10.0
gotchaGenerated files might have different casing than expected; package uses camelCase by default, but table names are PascalCase.
fix
Customize naming with the 'tableNameMapping' and 'columnNameMapping' options in GeneratorOpts.
affects: >=1.0.0
breakingTypeScript config must have 'esModuleInterop' enabled or the generated import statements may fail.
fix
Add "esModuleInterop": true to tsconfig.json.
affects: >=3.0.0
Errors
Common errors & fixes
Error: Cannot find module 'ts-sql-query'
ts-sql-query is a peer dependency but not installed in the project.
fix
Run 'npm install ts-sql-query' (version compatible with ^1.29.0).
TypeError: Generator is not a constructor
Using CommonJS require without importing the named export correctly.
fix
Use 'const { Generator } = require('ts-sql-codegen')' or switch to ES module import.
Error: tbls command not found
tbls binary is not installed or not in PATH.
fix
Install tbls globally via 'npm i -g tbls' or ensure its location is in PATH.
Error: Schema file not found at path ./schema.yaml
The schemaPath option is incorrect or the file does not exist.
fix
Double-check the path; ensure you have run 'tbls out ...' to generate the YAML file.
Upgrade
Version history
3.23.1latest on npm
Audit
Dependencies
ts-sql-queryrequiredpeer dependency; generated code relies on ts-sql-query's Connection and Table classes
Agent activity
15 hits · last 30 days
node
12
Meta
1
OpenAI (training)
1
Resources
ts-sql-codegen — npm install ts-sql-codegen · libregistry