postgresdk 0.6.2 → 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1005,38 +1005,30 @@ When tests are enabled, postgresdk generates:
1005
1005
 
1006
1006
  ### Running Tests with Docker
1007
1007
 
1008
- The generated Docker setup makes it easy to run tests in isolation:
1008
+ The generated test setup includes a Docker Compose file and a test runner script:
1009
1009
 
1010
1010
  ```bash
1011
- # Navigate to test directory
1012
- cd api/tests
1013
-
1014
- # Start test database
1015
- docker-compose up -d
1016
-
1017
- # Wait for database to be ready
1018
- sleep 3
1019
-
1020
- # Set environment variables
1021
- export TEST_DATABASE_URL="postgres://testuser:testpass@localhost:5432/testdb"
1022
- export TEST_API_URL="http://localhost:3000"
1023
-
1024
- # Run your migrations on test database
1025
- # (your migration command here)
1026
-
1027
- # Start your API server
1028
- npm run dev &
1029
-
1030
- # Run tests
1031
- npm test
1032
-
1033
- # Stop database when done
1034
- docker-compose down
1035
-
1036
- # Or use the generated script
1037
- bash run-tests.sh
1011
+ # 1. Make the test script executable (first time only)
1012
+ chmod +x api/tests/run-tests.sh
1013
+
1014
+ # 2. Edit api/tests/run-tests.sh to configure:
1015
+ # - Your API server startup command
1016
+ # - Migration commands (if needed)
1017
+ # - Any other setup specific to your project
1018
+
1019
+ # 3. Run the tests
1020
+ ./api/tests/run-tests.sh
1021
+
1022
+ # The script will:
1023
+ # - Start a PostgreSQL 17 test database in Docker
1024
+ # - Wait for it to be ready
1025
+ # - Run your API server (once configured)
1026
+ # - Execute the test suite
1027
+ # - Provide cleanup instructions
1038
1028
  ```
1039
1029
 
1030
+ The test script includes detailed comments and examples for common setups. You'll need to uncomment and customize the API server startup section for your specific project.
1031
+
1040
1032
  ### Customizing Tests
1041
1033
 
1042
1034
  The generated tests are basic and meant as a starting point. Create your own test files for:
package/dist/cli.js CHANGED
@@ -741,7 +741,7 @@ var init_cli_pull = () => {};
741
741
 
742
742
  // src/index.ts
743
743
  var import_config = __toESM(require_config(), 1);
744
- import { join } from "node:path";
744
+ import { join, relative } from "node:path";
745
745
  import { pathToFileURL } from "node:url";
746
746
 
747
747
  // src/introspect.ts
@@ -2340,15 +2340,15 @@ export async function deleteRecord(
2340
2340
  }
2341
2341
 
2342
2342
  // src/emit-tests.ts
2343
- function emitTableTest(table, framework = "vitest") {
2343
+ function emitTableTest(table, clientPath, framework = "vitest") {
2344
2344
  const Type = pascal(table.name);
2345
2345
  const tableName = table.name;
2346
2346
  const imports = getFrameworkImports(framework);
2347
2347
  const sampleData = generateSampleData(table);
2348
2348
  const updateData = generateUpdateData(table);
2349
2349
  return `${imports}
2350
- import { SDK } from '../client';
2351
- import type { Insert${Type}, Update${Type}, Select${Type} } from '../client/types/${tableName}';
2350
+ import { SDK } from '${clientPath}';
2351
+ import type { Insert${Type}, Update${Type}, Select${Type} } from '${clientPath}/types/${tableName}';
2352
2352
 
2353
2353
  /**
2354
2354
  * Basic tests for ${tableName} table operations
@@ -2371,7 +2371,7 @@ describe('${Type} SDK Operations', () => {
2371
2371
  });
2372
2372
  `;
2373
2373
  }
2374
- function emitTestSetup(framework = "vitest") {
2374
+ function emitTestSetup(clientPath, framework = "vitest") {
2375
2375
  return `/**
2376
2376
  * Test Setup and Utilities
2377
2377
  *
@@ -2390,7 +2390,7 @@ export const TEST_API_KEY = process.env.TEST_API_KEY;
2390
2390
 
2391
2391
  // Utility to create SDK instance
2392
2392
  export function createTestSDK() {
2393
- const { SDK } = require('../client');
2393
+ const { SDK } = require('${clientPath}');
2394
2394
  return new SDK({
2395
2395
  baseUrl: TEST_API_URL,
2396
2396
  auth: TEST_API_KEY ? { apiKey: TEST_API_KEY } : undefined
@@ -2428,7 +2428,7 @@ version: '3.8'
2428
2428
 
2429
2429
  services:
2430
2430
  postgres:
2431
- image: postgres:16-alpine
2431
+ image: postgres:17-alpine
2432
2432
  environment:
2433
2433
  POSTGRES_USER: testuser
2434
2434
  POSTGRES_PASSWORD: testpass
@@ -2452,11 +2452,23 @@ function emitTestScript(framework = "vitest") {
2452
2452
  return `#!/bin/bash
2453
2453
  # Test Runner Script
2454
2454
  #
2455
- # This script sets up and runs tests with a Docker PostgreSQL database
2455
+ # This script sets up and runs tests with a Docker PostgreSQL database.
2456
+ #
2457
+ # Usage:
2458
+ # chmod +x run-tests.sh # Make executable (first time only)
2459
+ # ./run-tests.sh
2460
+ #
2461
+ # Prerequisites:
2462
+ # - Docker installed and running
2463
+ # - Your API server code in the parent directories
2464
+ # - Test framework installed (${framework})
2456
2465
 
2457
2466
  set -e
2458
2467
 
2468
+ SCRIPT_DIR="$( cd "$( dirname "\${BASH_SOURCE[0]}" )" && pwd )"
2469
+
2459
2470
  echo "\uD83D\uDC33 Starting test database..."
2471
+ cd "$SCRIPT_DIR"
2460
2472
  docker-compose up -d --wait
2461
2473
 
2462
2474
  # Export test database URL
@@ -2465,24 +2477,43 @@ export TEST_API_URL="http://localhost:3000"
2465
2477
 
2466
2478
  # Wait for database to be ready
2467
2479
  echo "⏳ Waiting for database..."
2468
- sleep 2
2480
+ sleep 3
2469
2481
 
2470
- # Run migrations if needed (customize this)
2471
- # npm run migrate
2482
+ # TODO: Run your migrations on the test database
2483
+ # Example:
2484
+ # echo "\uD83D\uDCCA Running migrations..."
2485
+ # npm run migrate -- --database-url="$TEST_DATABASE_URL"
2472
2486
 
2473
2487
  echo "\uD83D\uDE80 Starting API server..."
2474
- # Start your API server in the background
2475
- # npm run dev &
2488
+ echo "⚠️ TODO: Uncomment and customize the API server startup command below:"
2489
+ echo ""
2490
+ echo " # Example for Node.js/Bun:"
2491
+ echo " # cd ../.. && npm run dev &"
2492
+ echo " # SERVER_PID=$!"
2493
+ echo ""
2494
+ echo " # Example for custom server file:"
2495
+ echo " # cd ../.. && node server.js &"
2496
+ echo " # SERVER_PID=$!"
2497
+ echo ""
2498
+ echo " Please edit this script to start your API server."
2499
+ echo ""
2500
+ # cd ../.. && npm run dev &
2476
2501
  # SERVER_PID=$!
2477
2502
  # sleep 3
2478
2503
 
2479
2504
  echo "\uD83E\uDDEA Running tests..."
2480
- ${runCommand} $@
2505
+ ${runCommand} "$@"
2481
2506
 
2482
2507
  # Cleanup
2483
- # kill $SERVER_PID 2>/dev/null || true
2508
+ # if [ ! -z "\${SERVER_PID}" ]; then
2509
+ # echo "\uD83D\uDED1 Stopping API server..."
2510
+ # kill $SERVER_PID 2>/dev/null || true
2511
+ # fi
2484
2512
 
2485
2513
  echo "✅ Tests completed!"
2514
+ echo ""
2515
+ echo "To stop the test database, run:"
2516
+ echo " cd $SCRIPT_DIR && docker-compose down"
2486
2517
  `;
2487
2518
  }
2488
2519
  function getFrameworkImports(framework) {
@@ -2779,9 +2810,10 @@ async function generate(configPath) {
2779
2810
  });
2780
2811
  if (generateTests) {
2781
2812
  console.log("\uD83E\uDDEA Generating tests...");
2813
+ const relativeClientPath = relative(testDir, clientDir);
2782
2814
  files.push({
2783
2815
  path: join(testDir, "setup.ts"),
2784
- content: emitTestSetup(testFramework)
2816
+ content: emitTestSetup(relativeClientPath, testFramework)
2785
2817
  });
2786
2818
  files.push({
2787
2819
  path: join(testDir, "docker-compose.yml"),
@@ -2794,7 +2826,7 @@ async function generate(configPath) {
2794
2826
  for (const table of Object.values(model.tables)) {
2795
2827
  files.push({
2796
2828
  path: join(testDir, `${table.name}.test.ts`),
2797
- content: emitTableTest(table, testFramework)
2829
+ content: emitTableTest(table, relativeClientPath, testFramework)
2798
2830
  });
2799
2831
  }
2800
2832
  }
@@ -2806,8 +2838,10 @@ async function generate(configPath) {
2806
2838
  if (generateTests) {
2807
2839
  const testsInSubdir = originalTestDir === serverDir || originalTestDir === originalClientDir;
2808
2840
  console.log(` Tests: ${testsInSubdir ? testDir + " (in tests subdir due to same output dir)" : testDir}`);
2809
- console.log(` \uD83D\uDC33 Run 'cd ${testDir} && docker-compose up -d' to start test database`);
2810
- console.log(` \uD83E\uDDEA Run 'bash ${testDir}/run-tests.sh' to execute tests`);
2841
+ console.log(` \uD83D\uDCDD Test setup:`);
2842
+ console.log(` 1. Make script executable: chmod +x ${testDir}/run-tests.sh`);
2843
+ console.log(` 2. Edit the script to configure your API server startup`);
2844
+ console.log(` 3. Run tests: ${testDir}/run-tests.sh`);
2811
2845
  }
2812
2846
  }
2813
2847
 
@@ -2,11 +2,11 @@ import type { Table } from "./introspect";
2
2
  /**
3
3
  * Generate basic SDK tests for a table
4
4
  */
5
- export declare function emitTableTest(table: Table, framework?: "vitest" | "jest" | "bun"): string;
5
+ export declare function emitTableTest(table: Table, clientPath: string, framework?: "vitest" | "jest" | "bun"): string;
6
6
  /**
7
7
  * Generate a test setup file
8
8
  */
9
- export declare function emitTestSetup(framework?: "vitest" | "jest" | "bun"): string;
9
+ export declare function emitTestSetup(clientPath: string, framework?: "vitest" | "jest" | "bun"): string;
10
10
  /**
11
11
  * Generate docker-compose.yml for test database
12
12
  */
package/dist/index.js CHANGED
@@ -471,7 +471,7 @@ var require_config = __commonJS(() => {
471
471
 
472
472
  // src/index.ts
473
473
  var import_config = __toESM(require_config(), 1);
474
- import { join } from "node:path";
474
+ import { join, relative } from "node:path";
475
475
  import { pathToFileURL } from "node:url";
476
476
 
477
477
  // src/introspect.ts
@@ -2070,15 +2070,15 @@ export async function deleteRecord(
2070
2070
  }
2071
2071
 
2072
2072
  // src/emit-tests.ts
2073
- function emitTableTest(table, framework = "vitest") {
2073
+ function emitTableTest(table, clientPath, framework = "vitest") {
2074
2074
  const Type = pascal(table.name);
2075
2075
  const tableName = table.name;
2076
2076
  const imports = getFrameworkImports(framework);
2077
2077
  const sampleData = generateSampleData(table);
2078
2078
  const updateData = generateUpdateData(table);
2079
2079
  return `${imports}
2080
- import { SDK } from '../client';
2081
- import type { Insert${Type}, Update${Type}, Select${Type} } from '../client/types/${tableName}';
2080
+ import { SDK } from '${clientPath}';
2081
+ import type { Insert${Type}, Update${Type}, Select${Type} } from '${clientPath}/types/${tableName}';
2082
2082
 
2083
2083
  /**
2084
2084
  * Basic tests for ${tableName} table operations
@@ -2101,7 +2101,7 @@ describe('${Type} SDK Operations', () => {
2101
2101
  });
2102
2102
  `;
2103
2103
  }
2104
- function emitTestSetup(framework = "vitest") {
2104
+ function emitTestSetup(clientPath, framework = "vitest") {
2105
2105
  return `/**
2106
2106
  * Test Setup and Utilities
2107
2107
  *
@@ -2120,7 +2120,7 @@ export const TEST_API_KEY = process.env.TEST_API_KEY;
2120
2120
 
2121
2121
  // Utility to create SDK instance
2122
2122
  export function createTestSDK() {
2123
- const { SDK } = require('../client');
2123
+ const { SDK } = require('${clientPath}');
2124
2124
  return new SDK({
2125
2125
  baseUrl: TEST_API_URL,
2126
2126
  auth: TEST_API_KEY ? { apiKey: TEST_API_KEY } : undefined
@@ -2158,7 +2158,7 @@ version: '3.8'
2158
2158
 
2159
2159
  services:
2160
2160
  postgres:
2161
- image: postgres:16-alpine
2161
+ image: postgres:17-alpine
2162
2162
  environment:
2163
2163
  POSTGRES_USER: testuser
2164
2164
  POSTGRES_PASSWORD: testpass
@@ -2182,11 +2182,23 @@ function emitTestScript(framework = "vitest") {
2182
2182
  return `#!/bin/bash
2183
2183
  # Test Runner Script
2184
2184
  #
2185
- # This script sets up and runs tests with a Docker PostgreSQL database
2185
+ # This script sets up and runs tests with a Docker PostgreSQL database.
2186
+ #
2187
+ # Usage:
2188
+ # chmod +x run-tests.sh # Make executable (first time only)
2189
+ # ./run-tests.sh
2190
+ #
2191
+ # Prerequisites:
2192
+ # - Docker installed and running
2193
+ # - Your API server code in the parent directories
2194
+ # - Test framework installed (${framework})
2186
2195
 
2187
2196
  set -e
2188
2197
 
2198
+ SCRIPT_DIR="$( cd "$( dirname "\${BASH_SOURCE[0]}" )" && pwd )"
2199
+
2189
2200
  echo "\uD83D\uDC33 Starting test database..."
2201
+ cd "$SCRIPT_DIR"
2190
2202
  docker-compose up -d --wait
2191
2203
 
2192
2204
  # Export test database URL
@@ -2195,24 +2207,43 @@ export TEST_API_URL="http://localhost:3000"
2195
2207
 
2196
2208
  # Wait for database to be ready
2197
2209
  echo "⏳ Waiting for database..."
2198
- sleep 2
2210
+ sleep 3
2199
2211
 
2200
- # Run migrations if needed (customize this)
2201
- # npm run migrate
2212
+ # TODO: Run your migrations on the test database
2213
+ # Example:
2214
+ # echo "\uD83D\uDCCA Running migrations..."
2215
+ # npm run migrate -- --database-url="$TEST_DATABASE_URL"
2202
2216
 
2203
2217
  echo "\uD83D\uDE80 Starting API server..."
2204
- # Start your API server in the background
2205
- # npm run dev &
2218
+ echo "⚠️ TODO: Uncomment and customize the API server startup command below:"
2219
+ echo ""
2220
+ echo " # Example for Node.js/Bun:"
2221
+ echo " # cd ../.. && npm run dev &"
2222
+ echo " # SERVER_PID=$!"
2223
+ echo ""
2224
+ echo " # Example for custom server file:"
2225
+ echo " # cd ../.. && node server.js &"
2226
+ echo " # SERVER_PID=$!"
2227
+ echo ""
2228
+ echo " Please edit this script to start your API server."
2229
+ echo ""
2230
+ # cd ../.. && npm run dev &
2206
2231
  # SERVER_PID=$!
2207
2232
  # sleep 3
2208
2233
 
2209
2234
  echo "\uD83E\uDDEA Running tests..."
2210
- ${runCommand} $@
2235
+ ${runCommand} "$@"
2211
2236
 
2212
2237
  # Cleanup
2213
- # kill $SERVER_PID 2>/dev/null || true
2238
+ # if [ ! -z "\${SERVER_PID}" ]; then
2239
+ # echo "\uD83D\uDED1 Stopping API server..."
2240
+ # kill $SERVER_PID 2>/dev/null || true
2241
+ # fi
2214
2242
 
2215
2243
  echo "✅ Tests completed!"
2244
+ echo ""
2245
+ echo "To stop the test database, run:"
2246
+ echo " cd $SCRIPT_DIR && docker-compose down"
2216
2247
  `;
2217
2248
  }
2218
2249
  function getFrameworkImports(framework) {
@@ -2509,9 +2540,10 @@ async function generate(configPath) {
2509
2540
  });
2510
2541
  if (generateTests) {
2511
2542
  console.log("\uD83E\uDDEA Generating tests...");
2543
+ const relativeClientPath = relative(testDir, clientDir);
2512
2544
  files.push({
2513
2545
  path: join(testDir, "setup.ts"),
2514
- content: emitTestSetup(testFramework)
2546
+ content: emitTestSetup(relativeClientPath, testFramework)
2515
2547
  });
2516
2548
  files.push({
2517
2549
  path: join(testDir, "docker-compose.yml"),
@@ -2524,7 +2556,7 @@ async function generate(configPath) {
2524
2556
  for (const table of Object.values(model.tables)) {
2525
2557
  files.push({
2526
2558
  path: join(testDir, `${table.name}.test.ts`),
2527
- content: emitTableTest(table, testFramework)
2559
+ content: emitTableTest(table, relativeClientPath, testFramework)
2528
2560
  });
2529
2561
  }
2530
2562
  }
@@ -2536,8 +2568,10 @@ async function generate(configPath) {
2536
2568
  if (generateTests) {
2537
2569
  const testsInSubdir = originalTestDir === serverDir || originalTestDir === originalClientDir;
2538
2570
  console.log(` Tests: ${testsInSubdir ? testDir + " (in tests subdir due to same output dir)" : testDir}`);
2539
- console.log(` \uD83D\uDC33 Run 'cd ${testDir} && docker-compose up -d' to start test database`);
2540
- console.log(` \uD83E\uDDEA Run 'bash ${testDir}/run-tests.sh' to execute tests`);
2571
+ console.log(` \uD83D\uDCDD Test setup:`);
2572
+ console.log(` 1. Make script executable: chmod +x ${testDir}/run-tests.sh`);
2573
+ console.log(` 2. Edit the script to configure your API server startup`);
2574
+ console.log(` 3. Run tests: ${testDir}/run-tests.sh`);
2541
2575
  }
2542
2576
  }
2543
2577
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postgresdk",
3
- "version": "0.6.2",
3
+ "version": "0.6.4",
4
4
  "description": "Generate a typed server/client SDK from a Postgres schema (includes, Zod, Hono).",
5
5
  "type": "module",
6
6
  "bin": {