postgresdk 0.7.1 → 0.7.3

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/dist/cli.js CHANGED
@@ -1688,6 +1688,57 @@ export const Update${Type}Schema = Insert${Type}Schema.partial();
1688
1688
  `;
1689
1689
  }
1690
1690
 
1691
+ // src/emit-params-zod.ts
1692
+ init_utils();
1693
+ function emitParamsZod(table, graph) {
1694
+ const Type = pascal(table.name);
1695
+ const columnNames = table.columns.map((c) => `"${c.name}"`).join(", ");
1696
+ const pkCols = Array.isArray(table.pk) ? table.pk : table.pk ? [table.pk] : [];
1697
+ const safePk = pkCols.length ? pkCols : ["id"];
1698
+ const hasCompositePk = safePk.length > 1;
1699
+ const includeSpecSchema = `z.any()`;
1700
+ const pkSchema = hasCompositePk ? `z.object({ ${safePk.map((col) => `${col}: z.string().min(1)`).join(", ")} })` : `z.string().min(1)`;
1701
+ return `import { z } from "zod";
1702
+
1703
+ // Schema for primary key parameters
1704
+ export const ${Type}PkSchema = ${pkSchema};
1705
+
1706
+ // Schema for list query parameters
1707
+ export const ${Type}ListParamsSchema = z.object({
1708
+ include: ${includeSpecSchema}.optional(),
1709
+ limit: z.number().int().positive().max(1000).optional(),
1710
+ offset: z.number().int().nonnegative().optional(),
1711
+ where: z.any().optional(),
1712
+ orderBy: z.enum([${columnNames}]).optional(),
1713
+ order: z.enum(["asc", "desc"]).optional()
1714
+ }).strict();
1715
+
1716
+ // Schema for ordering parameters
1717
+ export const ${Type}OrderParamsSchema = z.object({
1718
+ orderBy: z.enum([${columnNames}]).optional(),
1719
+ order: z.enum(["asc", "desc"]).optional()
1720
+ }).strict();
1721
+
1722
+ export type ${Type}Pk = z.infer<typeof ${Type}PkSchema>;
1723
+ export type ${Type}ListParams = z.infer<typeof ${Type}ListParamsSchema>;
1724
+ export type ${Type}OrderParams = z.infer<typeof ${Type}OrderParamsSchema>;
1725
+ `;
1726
+ }
1727
+
1728
+ // src/emit-shared-params-zod.ts
1729
+ function emitSharedParamsZod() {
1730
+ return `import { z } from "zod";
1731
+
1732
+ // Shared pagination schema (used across all tables)
1733
+ export const PaginationParamsSchema = z.object({
1734
+ limit: z.number().int().positive().max(1000).optional(),
1735
+ offset: z.number().int().nonnegative().optional()
1736
+ }).strict();
1737
+
1738
+ export type PaginationParams = z.infer<typeof PaginationParamsSchema>;
1739
+ `;
1740
+ }
1741
+
1691
1742
  // src/emit-routes-hono.ts
1692
1743
  init_utils();
1693
1744
  function emitHonoRoutes(table, _graph, opts) {
@@ -1951,6 +2002,16 @@ export type { AuthConfig, HeaderMap, AuthHeadersProvider } from "./base-client${
1951
2002
  `;
1952
2003
  for (const t of tables) {
1953
2004
  out += `export { Insert${pascal(t.name)}Schema, Update${pascal(t.name)}Schema } from "./zod/${t.name}${ext}";
2005
+ `;
2006
+ }
2007
+ out += `
2008
+ // Zod schemas for query parameters
2009
+ `;
2010
+ out += `export { PaginationParamsSchema } from "./params/shared${ext}";
2011
+ `;
2012
+ for (const t of tables) {
2013
+ const Type = pascal(t.name);
2014
+ out += `export { ${Type}PkSchema, ${Type}ListParamsSchema, ${Type}OrderParamsSchema } from "./params/${t.name}${ext}";
1954
2015
  `;
1955
2016
  }
1956
2017
  return out;
@@ -3318,27 +3379,57 @@ else
3318
3379
  fi
3319
3380
 
3320
3381
  echo ""
3321
- echo "\uD83D\uDE80 Starting API server..."
3322
- echo "⚠️ TODO: Uncomment and customize the API server startup command below:"
3382
+ echo "\uD83D\uDE80 API Server Setup"
3383
+ echo "════════════════════════════════════════════════════════════════"
3323
3384
  echo ""
3324
- echo " # Example for Node.js/Bun using PROJECT_ROOT:"
3325
- echo " # cd \\$PROJECT_ROOT && npm run dev &"
3326
- echo " # SERVER_PID=\\$!"
3385
+ echo "⚠️ IMPORTANT: This script expects your API server to be running."
3327
3386
  echo ""
3328
- echo " # Example for custom server file:"
3329
- echo " # DATABASE_URL=\\$TEST_DATABASE_URL node \\$PROJECT_ROOT/src/server.js &"
3330
- echo " # SERVER_PID=\\$!"
3387
+ echo " Option 1: Start manually (recommended for development)"
3388
+ echo " - Start your API server in another terminal"
3389
+ echo " - Make sure it uses TEST_DATABASE_URL: $TEST_DATABASE_URL"
3390
+ echo " - Make sure it's accessible at: $TEST_API_URL"
3331
3391
  echo ""
3332
- echo " # Example for Bun:"
3333
- echo " # DATABASE_URL=\\$TEST_DATABASE_URL bun \\$PROJECT_ROOT/src/index.ts &"
3334
- echo " # SERVER_PID=\\$!"
3392
+ echo " Option 2: Auto-start (edit this script)"
3393
+ echo " - Uncomment and customize one of these commands:"
3394
+ echo " # cd $PROJECT_ROOT && DATABASE_URL=$TEST_DATABASE_URL npm run dev &"
3395
+ echo " # DATABASE_URL=$TEST_DATABASE_URL bun $PROJECT_ROOT/src/server.ts &"
3396
+ echo " # SERVER_PID=$! # (uncomment this line too)"
3397
+ echo " # sleep 3 # (wait for server to start)"
3335
3398
  echo ""
3336
- echo " Please edit this script to start your API server."
3399
+ echo "════════════════════════════════════════════════════════════════"
3337
3400
  echo ""
3338
- # cd $PROJECT_ROOT && npm run dev &
3401
+
3402
+ # Uncomment these lines to auto-start your API server:
3403
+ # cd $PROJECT_ROOT && DATABASE_URL=$TEST_DATABASE_URL npm run dev &
3339
3404
  # SERVER_PID=$!
3340
3405
  # sleep 3
3341
3406
 
3407
+ # Check if API server is running before starting tests
3408
+ echo "\uD83D\uDD0D Checking if API server is running..."
3409
+ API_URL="\${TEST_API_URL:-http://localhost:3000}"
3410
+
3411
+ # Simple health check - just try to connect to the port
3412
+ if ! curl -f -s --connect-timeout 3 "\${API_URL}/health" > /dev/null 2>&1; then
3413
+ # Try a generic endpoint or just check if port is open
3414
+ if ! nc -z localhost 3000 2>/dev/null; then
3415
+ echo "❌ ERROR: API server is not running on \${API_URL}"
3416
+ echo ""
3417
+ echo " The API server must be running before tests can execute."
3418
+ echo " Please:"
3419
+ echo " 1. Start your API server manually, OR"
3420
+ echo " 2. Edit this script to start the server automatically (see comments above)"
3421
+ echo ""
3422
+ echo " Uncomment and customize one of these server startup commands:"
3423
+ echo " # cd $PROJECT_ROOT && npm run dev &"
3424
+ echo " # DATABASE_URL=$TEST_DATABASE_URL bun $PROJECT_ROOT/src/server.ts &"
3425
+ echo ""
3426
+ exit 1
3427
+ fi
3428
+ fi
3429
+
3430
+ echo "✅ API server is responding"
3431
+ echo ""
3432
+
3342
3433
  echo "\uD83E\uDDEA Running tests..."
3343
3434
  TIMESTAMP=$(date +%Y%m%d_%H%M%S)
3344
3435
  TEST_RESULTS_DIR="$SCRIPT_DIR/test-results"
@@ -3361,8 +3452,14 @@ echo " $TEST_RESULTS_DIR/"
3361
3452
  echo ""
3362
3453
  echo "\uD83D\uDCA1 Tips:"
3363
3454
  echo " - Database will be stopped automatically on script exit"
3455
+ echo " - API server (if started by this script) will also be stopped automatically"
3364
3456
  echo " - To manually stop the database: docker-compose -f $SCRIPT_DIR/docker-compose.yml down"
3365
3457
  echo " - To reset the database: docker-compose -f $SCRIPT_DIR/docker-compose.yml down -v"
3458
+ echo ""
3459
+ echo "\uD83D\uDD27 If tests failed due to server issues:"
3460
+ echo " - Check that your API server is configured to use TEST_DATABASE_URL"
3461
+ echo " - Verify your API server is accessible at $API_URL"
3462
+ echo " - Make sure all required environment variables are set"
3366
3463
 
3367
3464
  exit $TEST_EXIT_CODE
3368
3465
  `;
@@ -3769,7 +3866,8 @@ async function generate(configPath) {
3769
3866
  join(serverDir, "routes"),
3770
3867
  clientDir,
3771
3868
  join(clientDir, "types"),
3772
- join(clientDir, "zod")
3869
+ join(clientDir, "zod"),
3870
+ join(clientDir, "params")
3773
3871
  ];
3774
3872
  if (generateTests) {
3775
3873
  dirs.push(testDir);
@@ -3779,6 +3877,7 @@ async function generate(configPath) {
3779
3877
  const includeSpec = emitIncludeSpec(graph);
3780
3878
  files.push({ path: join(serverDir, "include-spec.ts"), content: includeSpec });
3781
3879
  files.push({ path: join(clientDir, "include-spec.ts"), content: includeSpec });
3880
+ files.push({ path: join(clientDir, "params", "shared.ts"), content: emitSharedParamsZod() });
3782
3881
  files.push({ path: join(clientDir, "base-client.ts"), content: emitBaseClient() });
3783
3882
  files.push({
3784
3883
  path: join(serverDir, "include-builder.ts"),
@@ -3806,6 +3905,8 @@ async function generate(configPath) {
3806
3905
  const zodSrc = emitZod(table, { numericMode: "string" });
3807
3906
  files.push({ path: join(serverDir, "zod", `${table.name}.ts`), content: zodSrc });
3808
3907
  files.push({ path: join(clientDir, "zod", `${table.name}.ts`), content: zodSrc });
3908
+ const paramsZodSrc = emitParamsZod(table, graph);
3909
+ files.push({ path: join(clientDir, "params", `${table.name}.ts`), content: paramsZodSrc });
3809
3910
  let routeContent;
3810
3911
  if (serverFramework === "hono") {
3811
3912
  routeContent = emitHonoRoutes(table, graph, {
@@ -0,0 +1,3 @@
1
+ import type { Table } from "./introspect";
2
+ import type { Graph } from "./rel-classify";
3
+ export declare function emitParamsZod(table: Table, graph: Graph): string;
@@ -0,0 +1 @@
1
+ export declare function emitSharedParamsZod(): string;
package/dist/index.js CHANGED
@@ -1425,6 +1425,57 @@ export const Update${Type}Schema = Insert${Type}Schema.partial();
1425
1425
  `;
1426
1426
  }
1427
1427
 
1428
+ // src/emit-params-zod.ts
1429
+ init_utils();
1430
+ function emitParamsZod(table, graph) {
1431
+ const Type = pascal(table.name);
1432
+ const columnNames = table.columns.map((c) => `"${c.name}"`).join(", ");
1433
+ const pkCols = Array.isArray(table.pk) ? table.pk : table.pk ? [table.pk] : [];
1434
+ const safePk = pkCols.length ? pkCols : ["id"];
1435
+ const hasCompositePk = safePk.length > 1;
1436
+ const includeSpecSchema = `z.any()`;
1437
+ const pkSchema = hasCompositePk ? `z.object({ ${safePk.map((col) => `${col}: z.string().min(1)`).join(", ")} })` : `z.string().min(1)`;
1438
+ return `import { z } from "zod";
1439
+
1440
+ // Schema for primary key parameters
1441
+ export const ${Type}PkSchema = ${pkSchema};
1442
+
1443
+ // Schema for list query parameters
1444
+ export const ${Type}ListParamsSchema = z.object({
1445
+ include: ${includeSpecSchema}.optional(),
1446
+ limit: z.number().int().positive().max(1000).optional(),
1447
+ offset: z.number().int().nonnegative().optional(),
1448
+ where: z.any().optional(),
1449
+ orderBy: z.enum([${columnNames}]).optional(),
1450
+ order: z.enum(["asc", "desc"]).optional()
1451
+ }).strict();
1452
+
1453
+ // Schema for ordering parameters
1454
+ export const ${Type}OrderParamsSchema = z.object({
1455
+ orderBy: z.enum([${columnNames}]).optional(),
1456
+ order: z.enum(["asc", "desc"]).optional()
1457
+ }).strict();
1458
+
1459
+ export type ${Type}Pk = z.infer<typeof ${Type}PkSchema>;
1460
+ export type ${Type}ListParams = z.infer<typeof ${Type}ListParamsSchema>;
1461
+ export type ${Type}OrderParams = z.infer<typeof ${Type}OrderParamsSchema>;
1462
+ `;
1463
+ }
1464
+
1465
+ // src/emit-shared-params-zod.ts
1466
+ function emitSharedParamsZod() {
1467
+ return `import { z } from "zod";
1468
+
1469
+ // Shared pagination schema (used across all tables)
1470
+ export const PaginationParamsSchema = z.object({
1471
+ limit: z.number().int().positive().max(1000).optional(),
1472
+ offset: z.number().int().nonnegative().optional()
1473
+ }).strict();
1474
+
1475
+ export type PaginationParams = z.infer<typeof PaginationParamsSchema>;
1476
+ `;
1477
+ }
1478
+
1428
1479
  // src/emit-routes-hono.ts
1429
1480
  init_utils();
1430
1481
  function emitHonoRoutes(table, _graph, opts) {
@@ -1688,6 +1739,16 @@ export type { AuthConfig, HeaderMap, AuthHeadersProvider } from "./base-client${
1688
1739
  `;
1689
1740
  for (const t of tables) {
1690
1741
  out += `export { Insert${pascal(t.name)}Schema, Update${pascal(t.name)}Schema } from "./zod/${t.name}${ext}";
1742
+ `;
1743
+ }
1744
+ out += `
1745
+ // Zod schemas for query parameters
1746
+ `;
1747
+ out += `export { PaginationParamsSchema } from "./params/shared${ext}";
1748
+ `;
1749
+ for (const t of tables) {
1750
+ const Type = pascal(t.name);
1751
+ out += `export { ${Type}PkSchema, ${Type}ListParamsSchema, ${Type}OrderParamsSchema } from "./params/${t.name}${ext}";
1691
1752
  `;
1692
1753
  }
1693
1754
  return out;
@@ -3055,27 +3116,57 @@ else
3055
3116
  fi
3056
3117
 
3057
3118
  echo ""
3058
- echo "\uD83D\uDE80 Starting API server..."
3059
- echo "⚠️ TODO: Uncomment and customize the API server startup command below:"
3119
+ echo "\uD83D\uDE80 API Server Setup"
3120
+ echo "════════════════════════════════════════════════════════════════"
3060
3121
  echo ""
3061
- echo " # Example for Node.js/Bun using PROJECT_ROOT:"
3062
- echo " # cd \\$PROJECT_ROOT && npm run dev &"
3063
- echo " # SERVER_PID=\\$!"
3122
+ echo "⚠️ IMPORTANT: This script expects your API server to be running."
3064
3123
  echo ""
3065
- echo " # Example for custom server file:"
3066
- echo " # DATABASE_URL=\\$TEST_DATABASE_URL node \\$PROJECT_ROOT/src/server.js &"
3067
- echo " # SERVER_PID=\\$!"
3124
+ echo " Option 1: Start manually (recommended for development)"
3125
+ echo " - Start your API server in another terminal"
3126
+ echo " - Make sure it uses TEST_DATABASE_URL: $TEST_DATABASE_URL"
3127
+ echo " - Make sure it's accessible at: $TEST_API_URL"
3068
3128
  echo ""
3069
- echo " # Example for Bun:"
3070
- echo " # DATABASE_URL=\\$TEST_DATABASE_URL bun \\$PROJECT_ROOT/src/index.ts &"
3071
- echo " # SERVER_PID=\\$!"
3129
+ echo " Option 2: Auto-start (edit this script)"
3130
+ echo " - Uncomment and customize one of these commands:"
3131
+ echo " # cd $PROJECT_ROOT && DATABASE_URL=$TEST_DATABASE_URL npm run dev &"
3132
+ echo " # DATABASE_URL=$TEST_DATABASE_URL bun $PROJECT_ROOT/src/server.ts &"
3133
+ echo " # SERVER_PID=$! # (uncomment this line too)"
3134
+ echo " # sleep 3 # (wait for server to start)"
3072
3135
  echo ""
3073
- echo " Please edit this script to start your API server."
3136
+ echo "════════════════════════════════════════════════════════════════"
3074
3137
  echo ""
3075
- # cd $PROJECT_ROOT && npm run dev &
3138
+
3139
+ # Uncomment these lines to auto-start your API server:
3140
+ # cd $PROJECT_ROOT && DATABASE_URL=$TEST_DATABASE_URL npm run dev &
3076
3141
  # SERVER_PID=$!
3077
3142
  # sleep 3
3078
3143
 
3144
+ # Check if API server is running before starting tests
3145
+ echo "\uD83D\uDD0D Checking if API server is running..."
3146
+ API_URL="\${TEST_API_URL:-http://localhost:3000}"
3147
+
3148
+ # Simple health check - just try to connect to the port
3149
+ if ! curl -f -s --connect-timeout 3 "\${API_URL}/health" > /dev/null 2>&1; then
3150
+ # Try a generic endpoint or just check if port is open
3151
+ if ! nc -z localhost 3000 2>/dev/null; then
3152
+ echo "❌ ERROR: API server is not running on \${API_URL}"
3153
+ echo ""
3154
+ echo " The API server must be running before tests can execute."
3155
+ echo " Please:"
3156
+ echo " 1. Start your API server manually, OR"
3157
+ echo " 2. Edit this script to start the server automatically (see comments above)"
3158
+ echo ""
3159
+ echo " Uncomment and customize one of these server startup commands:"
3160
+ echo " # cd $PROJECT_ROOT && npm run dev &"
3161
+ echo " # DATABASE_URL=$TEST_DATABASE_URL bun $PROJECT_ROOT/src/server.ts &"
3162
+ echo ""
3163
+ exit 1
3164
+ fi
3165
+ fi
3166
+
3167
+ echo "✅ API server is responding"
3168
+ echo ""
3169
+
3079
3170
  echo "\uD83E\uDDEA Running tests..."
3080
3171
  TIMESTAMP=$(date +%Y%m%d_%H%M%S)
3081
3172
  TEST_RESULTS_DIR="$SCRIPT_DIR/test-results"
@@ -3098,8 +3189,14 @@ echo " $TEST_RESULTS_DIR/"
3098
3189
  echo ""
3099
3190
  echo "\uD83D\uDCA1 Tips:"
3100
3191
  echo " - Database will be stopped automatically on script exit"
3192
+ echo " - API server (if started by this script) will also be stopped automatically"
3101
3193
  echo " - To manually stop the database: docker-compose -f $SCRIPT_DIR/docker-compose.yml down"
3102
3194
  echo " - To reset the database: docker-compose -f $SCRIPT_DIR/docker-compose.yml down -v"
3195
+ echo ""
3196
+ echo "\uD83D\uDD27 If tests failed due to server issues:"
3197
+ echo " - Check that your API server is configured to use TEST_DATABASE_URL"
3198
+ echo " - Verify your API server is accessible at $API_URL"
3199
+ echo " - Make sure all required environment variables are set"
3103
3200
 
3104
3201
  exit $TEST_EXIT_CODE
3105
3202
  `;
@@ -3506,7 +3603,8 @@ async function generate(configPath) {
3506
3603
  join(serverDir, "routes"),
3507
3604
  clientDir,
3508
3605
  join(clientDir, "types"),
3509
- join(clientDir, "zod")
3606
+ join(clientDir, "zod"),
3607
+ join(clientDir, "params")
3510
3608
  ];
3511
3609
  if (generateTests) {
3512
3610
  dirs.push(testDir);
@@ -3516,6 +3614,7 @@ async function generate(configPath) {
3516
3614
  const includeSpec = emitIncludeSpec(graph);
3517
3615
  files.push({ path: join(serverDir, "include-spec.ts"), content: includeSpec });
3518
3616
  files.push({ path: join(clientDir, "include-spec.ts"), content: includeSpec });
3617
+ files.push({ path: join(clientDir, "params", "shared.ts"), content: emitSharedParamsZod() });
3519
3618
  files.push({ path: join(clientDir, "base-client.ts"), content: emitBaseClient() });
3520
3619
  files.push({
3521
3620
  path: join(serverDir, "include-builder.ts"),
@@ -3543,6 +3642,8 @@ async function generate(configPath) {
3543
3642
  const zodSrc = emitZod(table, { numericMode: "string" });
3544
3643
  files.push({ path: join(serverDir, "zod", `${table.name}.ts`), content: zodSrc });
3545
3644
  files.push({ path: join(clientDir, "zod", `${table.name}.ts`), content: zodSrc });
3645
+ const paramsZodSrc = emitParamsZod(table, graph);
3646
+ files.push({ path: join(clientDir, "params", `${table.name}.ts`), content: paramsZodSrc });
3546
3647
  let routeContent;
3547
3648
  if (serverFramework === "hono") {
3548
3649
  routeContent = emitHonoRoutes(table, graph, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postgresdk",
3
- "version": "0.7.1",
3
+ "version": "0.7.3",
4
4
  "description": "Generate a typed server/client SDK from a Postgres schema (includes, Zod, Hono).",
5
5
  "type": "module",
6
6
  "bin": {