{"library":"schemats","title":"Schemats: TypeScript Interface Generator","description":"Schemats is a command-line interface (CLI) tool designed to automatically generate TypeScript interface definitions directly from existing SQL database schemas, specifically supporting PostgreSQL and MySQL. The current stable version is 3.0.3. While it doesn't adhere to a strict release cadence, updates are made to enhance compatibility and features. Its primary differentiator lies in enabling strong static typing for database queries by bridging the gap between relational databases and TypeScript applications. Users can generate type definitions for entire schemas or specific tables, either through command-line arguments or a `schemats.json` configuration file, which then allows for type-safe database interactions and enhanced developer experience with autocompletion and static checks in their application code.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install schemats"],"cli":{"name":"schemats","version":null}},"imports":["import * as dbTypes from './path/to/generated/db.ts'","import { generate } from 'schemats'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Client } from 'pg';\nimport * as path from 'path';\nimport { execSync } from 'child_process';\n\n// 1. Ensure schemats is installed globally or accessible via npx\n// npm install -g schemats\n\nconst dbUrl = 'postgres://postgres:password@localhost:5432/mytestdb';\nconst outputFile = path.join(process.cwd(), 'db-types.ts');\n\nasync function setupAndGenerate() {\n  // For demonstration, ensure a test database exists\n  const client = new Client({\n    connectionString: 'postgres://postgres:password@localhost:5432/postgres'\n  });\n  try {\n    await client.connect();\n    await client.query(`DROP DATABASE IF EXISTS mytestdb;`);\n    await client.query(`CREATE DATABASE mytestdb;`);\n    await client.end();\n\n    const testClient = new Client({ connectionString: dbUrl });\n    await testClient.connect();\n    await testClient.query(`\n      CREATE TABLE IF NOT EXISTS users (\n        id SERIAL PRIMARY KEY,\n        username VARCHAR(255) NOT NULL UNIQUE,\n        email VARCHAR(255) NOT NULL,\n        created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP\n      );\n      CREATE TABLE IF NOT EXISTS products (\n        product_id UUID DEFAULT gen_random_uuid() PRIMARY KEY,\n        name VARCHAR(255) NOT NULL,\n        price NUMERIC(10, 2) NOT NULL\n      );\n    `);\n    await testClient.end();\n\n    console.log('Database and tables created. Generating types...');\n\n    // 2. Generate TypeScript interfaces from the schema\n    // Using npx for local schemats binary or if not globally installed\n    const command = `npx schemats generate -c \"${dbUrl}\" -s public -o \"${outputFile}\"`;\n    execSync(command, { stdio: 'inherit' });\n\n    console.log(`\nGenerated TypeScript types to ${outputFile}:\n`);\n\n    // 3. Demonstrate usage of generated types\n    // (In a real app, you'd import generated types from outputFile)\n    // For this example, we'll simulate the types:\n    interface Users {\n      id: number;\n      username: string;\n      email: string;\n      created_at: Date;\n    }\n    interface Products {\n      product_id: string; // UUIDs often map to strings in TS\n      name: string;\n      price: string; // NUMERIC can be string or number, safer as string without specific parser\n    }\n\n    const exampleUser: Users = { id: 1, username: 'testuser', email: 'test@example.com', created_at: new Date() };\n    const exampleProduct: Products = { product_id: 'a1b2c3d4-e5f6-7890-1234-567890abcdef', name: 'Example Item', price: '99.99' };\n\n    console.log('Example user with generated type:', exampleUser);\n    console.log('Example product with generated type:', exampleProduct);\n\n  } catch (error) {\n    console.error('Error during setup or generation:', error);\n    process.exit(1);\n  }\n}\n\nsetupAndGenerate();\n","lang":"typescript","description":"This quickstart demonstrates how to set up a PostgreSQL database, create some tables, then use the `schemats` CLI to generate TypeScript interfaces, and finally shows a basic example of how these generated types would be used in application code.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}