{"library":"relational-schema","title":"Relational Schema Generator","description":"Relational-schema is a utility package designed to introspect and generate a comprehensive, semantic schema representation of a relational database (currently supporting PostgreSQL and MySQL) into various developer-friendly formats. It outputs schema definitions as JavaScript, TypeScript, CommonJS, or JSON files. Currently at version 0.6.3, the package receives regular updates, primarily bug fixes and feature enhancements, though minor version increments (0.x.x) may introduce breaking changes without a major version bump. Unlike simplified ORM models, it provides a detailed schema including full table definitions, columns with types, default values, nullability, keys, constraints, unique key combinations, and intricate table relations with human-readable aliases, soft-delete identification, and enums. This tool aims to leverage the strictness of relational schemas for building more robust and automated tooling, such as the Gybson query client.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install relational-schema"],"cli":{"name":"relational-schema","version":null}},"imports":["import { introspectSchema } from 'relational-schema';","import { generateSchema } from 'relational-schema';","import type { DatabaseSchema } from 'relational-schema';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { generateSchema, introspectSchema } from 'relational-schema';\nimport fs from 'node:fs/promises';\nimport path from 'node:path';\n\nasync function runSchemaGeneration() {\n  const config = {\n    host: process.env.DB_HOST ?? '127.0.0.1',\n    client: process.env.DB_CLIENT ?? 'postgres', // or 'mysql'\n    port: parseInt(process.env.DB_PORT ?? '5432', 10), // 3306 for mysql\n    user: process.env.DB_USER ?? 'postgres',\n    password: process.env.DB_PASSWORD ?? 'secure_password',\n    database: process.env.DB_NAME ?? 'mydb',\n    outdir: './src/schemas',\n    format: 'typescript'\n  };\n\n  try {\n    console.log('Introspecting database schema...');\n    const schema = await introspectSchema(config);\n    console.log('Schema introspected successfully. Generating files...');\n\n    // The generateSchema function returns an object of { filePath: content }\n    const generatedFiles = await generateSchema(schema, config);\n\n    await fs.mkdir(config.outdir, { recursive: true });\n\n    for (const [fileName, content] of Object.entries(generatedFiles)) {\n      const fullPath = path.join(config.outdir, fileName);\n      await fs.writeFile(fullPath, content);\n      console.log(`Generated: ${fullPath}`);\n    }\n    console.log('Schema generation complete!');\n  } catch (error) {\n    console.error('Failed to generate schema:', error);\n    process.exit(1);\n  }\n}\n\n// To run this example, ensure you have a database running and set environment variables.\n// Example of typical CLI usage:\n// 1. Create a `relation-config.json` in your project root:\n//    {\n//        \"host\": \"127.0.0.1\", \"client\": \"postgres\", \"port\": 5432,\n//        \"user\": \"postgres\", \"password\": \"secure_password\",\n//        \"database\": \"mydb\", \"outdir\": \"src/schemas\", \"format\": \"typescript\"\n//    }\n// 2. Run from your terminal:\n//    npx relations introspect\n\nrunSchemaGeneration();","lang":"typescript","description":"Demonstrates programmatic database introspection and schema file generation using the library's core functions, including setting environment variables for credentials and handling output files. It also outlines the more common CLI usage.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}