|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Generate TypeScript types for postgrest-js tests from the test database |
| 5 | + * |
| 6 | + * This script spins up the Docker test infrastructure, generates types |
| 7 | + * from the database schema, and writes them to test/types.generated.ts |
| 8 | + */ |
| 9 | + |
| 10 | +const { execSync } = require('child_process') |
| 11 | +const path = require('path') |
| 12 | + |
| 13 | +const POSTGREST_DIR = path.join(__dirname, '../packages/core/postgrest-js') |
| 14 | +const DB_DIR = path.join(POSTGREST_DIR, 'test/db') |
| 15 | +const OUTPUT_FILE = path.join(POSTGREST_DIR, 'test/types.generated.ts') |
| 16 | + |
| 17 | +function exec(command, options = {}) { |
| 18 | + try { |
| 19 | + return execSync(command, { |
| 20 | + stdio: 'inherit', |
| 21 | + ...options, |
| 22 | + }) |
| 23 | + } catch (error) { |
| 24 | + console.error(`❌ Command failed: ${command}`) |
| 25 | + process.exit(1) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function main() { |
| 30 | + console.log('🔄 Generating postgrest-js test types...\n') |
| 31 | + |
| 32 | + // Start Docker containers |
| 33 | + console.log('📦 Starting Docker containers...') |
| 34 | + exec('docker compose up --detach', { cwd: DB_DIR }) |
| 35 | + |
| 36 | + // Wait for services to be ready |
| 37 | + console.log('⏳ Waiting for services to be ready...') |
| 38 | + exec('npx wait-for-localhost 8080') |
| 39 | + exec('npx wait-for-localhost 3000') |
| 40 | + |
| 41 | + // Generate types from database |
| 42 | + console.log('🔧 Generating types from database...') |
| 43 | + exec( |
| 44 | + `curl --location 'http://0.0.0.0:8080/generators/typescript?included_schemas=public,personal&detect_one_to_one_relationships=true' > ${OUTPUT_FILE}`, |
| 45 | + { cwd: POSTGREST_DIR, stdio: 'inherit' } |
| 46 | + ) |
| 47 | + |
| 48 | + // Run post-generation script to update JSON type |
| 49 | + console.log('🔧 Post-processing generated types...') |
| 50 | + exec('node scripts/update-json-type.js', { cwd: POSTGREST_DIR }) |
| 51 | + |
| 52 | + // Clean up Docker containers |
| 53 | + console.log('🧹 Cleaning up Docker containers...') |
| 54 | + exec('docker compose down --volumes', { cwd: DB_DIR }) |
| 55 | + |
| 56 | + console.log('\n✅ Type generation complete!') |
| 57 | + console.log(` Output: ${OUTPUT_FILE}`) |
| 58 | +} |
| 59 | + |
| 60 | +main() |
0 commit comments