Registry / database / nu-schemats

nu-schemats

JSON →
library4.9.1jsnpmunverified

Schemats generates TypeScript interface definitions from PostgreSQL or MySQL database schemas. Version 4.9.1 is the latest stable release, with irregular release cadence. It supports generating types for all tables in a schema or specific tables, and provides both CLI and programmatic API. Key differentiators include config file support, type-safe query results integration with pg-promise, and handling of custom types.

npm install nu-schemats
INSTALL
IMPORT
SIG · NU-SCHEMATS
N
nu-schemats
databasejavascriptv4.9.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.

schemats generate
schemats generate -c postgres://user:pass@localhost/db -t users -o types.ts
schemats generate -c postgres://user:pass@localhost/db -t users -o types.ts --type postgres
The database type is inferred from the connection string. Do not specify --type unless absolutely necessary.
generate
import { generate } from 'schemats'; const config = { conn: 'postgres://...', table: ['users'] }; await generate(config);
const schemats = require('schemats'); schemats.generate({...});
Schemats provides both ESM and CommonJS exports. The default export is the generate function, but named import is recommended for TypeScript.
schemats.json
Place schemats.json in project root with config. Run 'schemats generate' without arguments.
Using schemats.json with a different filename or path without --config option
Config file defaults to schemats.json in current working directory. Use --config to specify a different path.

Installs Schemats globally, generates TypeScript interfaces for a specific table, and imports them for type-safe database queries.

npm install -g schemats # Generate interfaces for table 'users' from PostgreSQL schemats generate -c postgres://postgres@localhost/mydb -t users -o types/users.ts // In your code: import * as users from './types/users'; async function getUsers(): Promise<users.users[]> { const db = await pgp('postgres://postgres@localhost/mydb'); return db.query('SELECT * FROM users'); }
Debug
Known issues
breakingSchemats v4.0.0 dropped support for Node.js < 10
fix
Upgrade Node.js to version 10 or later.
affects: >=4.0.0
breakingSchemats v3.0.0 changed the CLI interface: removed --type flag, connection string now implies database type
fix
Update CLI commands to not use --type. Connection string must include 'postgres://' or 'mysql://'.
affects: >=3.0.0 <3.0.0
gotchaSchemats generates interfaces for ALL tables in schema if -t is omitted, which may generate many unwanted files.
fix
Always specify -t <table> or use -s <schema> to limit scope.
affects: >=1.0.0
deprecatedSchemats v2.x allowed table parameter as a comma-separated list via CLI, which is now deprecated in favor of repeated -t flags.
fix
Use multiple -t flags instead: -t users -t posts
affects: >=1.0.0 <3.0.0
gotchaWhen using MySQL, schemats requires the mysql2 package (not mysql).
fix
Ensure mysql2 is installed: npm install mysql2
affects: >=1.0.0
gotchaGenerated interfaces use snake_case property names matching database columns, which may conflict with TypeScript naming conventions.
fix
Consider renaming properties manually or using a post-processing script.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'pg'
pg package not installed (required for PostgreSQL connections)
fix
npm install pg
schemats: error: connect ECONNREFUSED
Database server is not running or connection string is incorrect
fix
Verify database is running and connection string is correct: postgres://user:pass@host:port/db
TypeError: Cannot read property 'replace' of undefined
Missing or malformed connection string
fix
Provide a valid connection string: -c postgres://localhost/mydb
Warning: No tables found in schema public
Schema specified does not contain tables, or tables are in a different schema
fix
Use -s to specify the correct schema name
Upgrade
Version history
4.9.1latest on npm
Audit
Dependencies
pgrequiredRequired for PostgreSQL database connection
mysql2optionalRequired for MySQL database connection
pg-promiseoptionalCommonly used with Schemats for typed queries, but not a runtime dependency
Agent activity
5 hits · last 30 days
node
4
Resources
nu-schemats — npm install nu-schemats · libregistry