postgresdk 0.6.7 → 0.6.8

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.
Files changed (3) hide show
  1. package/dist/cli.js +78 -97
  2. package/dist/index.js +78 -97
  3. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -1005,7 +1005,7 @@ function emitZod(table, opts) {
1005
1005
  const Type = pascal(table.name);
1006
1006
  const zFor = (pg) => {
1007
1007
  if (pg === "uuid")
1008
- return `z.string().uuid()`;
1008
+ return `z.string()`;
1009
1009
  if (pg === "bool" || pg === "boolean")
1010
1010
  return `z.boolean()`;
1011
1011
  if (pg === "int2" || pg === "int4" || pg === "int8")
@@ -2732,28 +2732,24 @@ function generateSampleDataFromSchema(table, hasForeignKeys = false) {
2732
2732
  }
2733
2733
  }
2734
2734
  for (const col of table.columns) {
2735
- if (col.hasDefault) {
2736
- const autoGenerated = ["id", "created_at", "updated_at", "created", "updated", "modified_at"];
2737
- if (autoGenerated.includes(col.name.toLowerCase())) {
2738
- continue;
2739
- }
2740
- }
2741
- if (col.name === "deleted_at" || col.name === "deleted") {
2735
+ const isAutoGenerated = col.hasDefault && ["id", "created_at", "updated_at", "created", "updated", "modified_at"].includes(col.name.toLowerCase());
2736
+ if (isAutoGenerated) {
2742
2737
  continue;
2743
2738
  }
2744
- if (!col.nullable) {
2745
- const foreignTable = foreignKeyColumns.get(col.name);
2746
- if (foreignTable) {
2747
- fields.push(` ${col.name}: ${foreignTable}Id`);
2739
+ if (col.name === "deleted_at" || col.name === "deleted") {
2740
+ if (col.nullable) {
2741
+ fields.push(` ${col.name}: null`);
2748
2742
  } else {
2749
2743
  const value = generateValueForColumn(col);
2750
2744
  fields.push(` ${col.name}: ${value}`);
2751
2745
  }
2752
- } else {
2753
- const foreignTable = foreignKeyColumns.get(col.name);
2746
+ continue;
2747
+ }
2748
+ const foreignTable = foreignKeyColumns.get(col.name);
2749
+ if (!col.nullable || foreignTable || col.hasDefault && !isAutoGenerated || shouldIncludeNullableColumn(col)) {
2754
2750
  if (foreignTable) {
2755
2751
  fields.push(` ${col.name}: ${foreignTable}Id`);
2756
- } else if (shouldIncludeNullableColumn(col)) {
2752
+ } else {
2757
2753
  const value = generateValueForColumn(col);
2758
2754
  fields.push(` ${col.name}: ${value}`);
2759
2755
  }
@@ -2813,80 +2809,15 @@ function shouldIncludeNullableColumn(col) {
2813
2809
  return importantPatterns.some((pattern) => name.includes(pattern));
2814
2810
  }
2815
2811
  function generateValueForColumn(col, isUpdate = false) {
2816
- const name = col.name.toLowerCase();
2817
2812
  const type = col.pgType.toLowerCase();
2818
- if (name.includes("email")) {
2819
- return `'test${isUpdate ? ".updated" : ""}@example.com'`;
2820
- }
2821
- if (name.includes("phone")) {
2822
- return `'555-${isUpdate ? "0200" : "0100"}'`;
2823
- }
2824
- if (name.includes("url") || name.includes("website")) {
2825
- return `'https://example.com${isUpdate ? "/updated" : ""}'`;
2826
- }
2827
- if (name.includes("password")) {
2828
- return `'hashedPassword123'`;
2829
- }
2830
- if (name === "name" || name.includes("_name") || name.includes("name_")) {
2831
- return `'Test ${pascal(col.name)}${isUpdate ? " Updated" : ""}'`;
2832
- }
2833
- if (name === "title" || name.includes("title")) {
2834
- return `'Test Title${isUpdate ? " Updated" : ""}'`;
2835
- }
2836
- if (name.includes("description") || name === "bio" || name === "about") {
2837
- return `'Test description${isUpdate ? " updated" : ""}'`;
2838
- }
2839
- if (name === "slug") {
2840
- return `'test-slug${isUpdate ? "-updated" : ""}'`;
2841
- }
2842
- if (name === "status") {
2843
- return `'${isUpdate ? "updated" : "active"}'`;
2844
- }
2845
- if (name === "type" || name === "kind" || name === "category") {
2846
- return `'${isUpdate ? "type2" : "type1"}'`;
2847
- }
2848
- if (name === "color" || name === "colour") {
2849
- return `'${isUpdate ? "#FF0000" : "#0000FF"}'`;
2850
- }
2851
- if (name === "gender") {
2852
- return `'${isUpdate ? "F" : "M"}'`;
2853
- }
2854
- if (name.includes("price") || name === "cost" || name === "amount") {
2855
- return isUpdate ? "99.99" : "10.50";
2856
- }
2857
- if (name === "quantity" || name === "count" || name.includes("qty")) {
2858
- return isUpdate ? "5" : "1";
2859
- }
2860
- if (name === "age") {
2861
- return isUpdate ? "30" : "25";
2862
- }
2863
- if (name.includes("percent") || name === "rate" || name === "ratio") {
2864
- return isUpdate ? "0.75" : "0.5";
2865
- }
2866
- if (name.includes("latitude") || name === "lat") {
2867
- return "40.7128";
2868
- }
2869
- if (name.includes("longitude") || name === "lng" || name === "lon") {
2870
- return "-74.0060";
2871
- }
2872
- if (type.includes("date") || type.includes("timestamp")) {
2873
- if (name.includes("birth") || name === "dob") {
2874
- return `new Date('1990-01-01')`;
2875
- }
2876
- if (name.includes("end") || name.includes("expire")) {
2877
- return `new Date('2025-12-31')`;
2878
- }
2879
- if (name.includes("start") || name.includes("begin")) {
2880
- return `new Date('2024-01-01')`;
2881
- }
2882
- return `new Date()`;
2883
- }
2884
2813
  switch (type) {
2885
2814
  case "text":
2886
2815
  case "varchar":
2887
2816
  case "char":
2888
2817
  case "character varying":
2889
- return `'test_value${isUpdate ? "_updated" : ""}'`;
2818
+ case "bpchar":
2819
+ case "name":
2820
+ return `'str_${Math.random().toString(36).substring(7)}'`;
2890
2821
  case "int":
2891
2822
  case "int2":
2892
2823
  case "int4":
@@ -2894,6 +2825,8 @@ function generateValueForColumn(col, isUpdate = false) {
2894
2825
  case "integer":
2895
2826
  case "smallint":
2896
2827
  case "bigint":
2828
+ case "serial":
2829
+ case "bigserial":
2897
2830
  return isUpdate ? "42" : "1";
2898
2831
  case "decimal":
2899
2832
  case "numeric":
@@ -2902,37 +2835,85 @@ function generateValueForColumn(col, isUpdate = false) {
2902
2835
  case "float":
2903
2836
  case "float4":
2904
2837
  case "float8":
2838
+ case "money":
2905
2839
  return isUpdate ? "99.99" : "10.50";
2906
2840
  case "boolean":
2907
2841
  case "bool":
2908
2842
  return isUpdate ? "false" : "true";
2843
+ case "date":
2844
+ case "timestamp":
2845
+ case "timestamptz":
2846
+ case "timestamp without time zone":
2847
+ case "timestamp with time zone":
2848
+ case "time":
2849
+ case "timetz":
2850
+ case "time without time zone":
2851
+ case "time with time zone":
2852
+ return `new Date()`;
2853
+ case "interval":
2854
+ return `'1 day'`;
2909
2855
  case "json":
2910
2856
  case "jsonb":
2911
- return `{ key: '${isUpdate ? "updated" : "value"}' }`;
2857
+ return `{}`;
2912
2858
  case "uuid":
2913
- return `'${isUpdate ? "550e8400-e29b-41d4-a716-446655440001" : "550e8400-e29b-41d4-a716-446655440000"}'`;
2859
+ return `'${generateUUID()}'`;
2914
2860
  case "inet":
2915
- return `'${isUpdate ? "192.168.1.2" : "192.168.1.1"}'`;
2861
+ return `'192.168.1.1'`;
2916
2862
  case "cidr":
2917
2863
  return `'192.168.1.0/24'`;
2918
2864
  case "macaddr":
2919
- return `'08:00:2b:01:02:0${isUpdate ? "4" : "3"}'`;
2865
+ case "macaddr8":
2866
+ return `'08:00:2b:01:02:03'`;
2867
+ case "point":
2868
+ return `'(1,2)'`;
2869
+ case "line":
2870
+ return `'{1,2,3}'`;
2871
+ case "lseg":
2872
+ return `'[(0,0),(1,1)]'`;
2873
+ case "box":
2874
+ return `'((0,0),(1,1))'`;
2875
+ case "path":
2876
+ return `'[(0,0),(1,1),(2,0)]'`;
2877
+ case "polygon":
2878
+ return `'((0,0),(1,1),(1,0))'`;
2879
+ case "circle":
2880
+ return `'<(0,0),1>'`;
2881
+ case "bit":
2882
+ case "bit varying":
2883
+ case "varbit":
2884
+ return `'101'`;
2885
+ case "bytea":
2886
+ return `'\\\\x0102'`;
2920
2887
  case "xml":
2921
- return `'<root>${isUpdate ? "updated" : "value"}</root>'`;
2888
+ return `'<root/>'`;
2889
+ case "tsvector":
2890
+ return `'a fat cat'`;
2891
+ case "tsquery":
2892
+ return `'fat & cat'`;
2893
+ case "oid":
2894
+ case "regproc":
2895
+ case "regprocedure":
2896
+ case "regoper":
2897
+ case "regoperator":
2898
+ case "regclass":
2899
+ case "regtype":
2900
+ case "regconfig":
2901
+ case "regdictionary":
2902
+ return "1";
2922
2903
  default:
2923
- if (type.endsWith("[]")) {
2924
- const baseType = type.slice(0, -2);
2925
- if (baseType === "text" || baseType === "varchar") {
2926
- return `['item1', 'item2${isUpdate ? "_updated" : ""}']`;
2927
- }
2928
- if (baseType === "int" || baseType === "integer") {
2929
- return `[1, 2, ${isUpdate ? "3" : ""}]`;
2930
- }
2904
+ if (type.endsWith("[]") || type.startsWith("_")) {
2931
2905
  return `[]`;
2932
2906
  }
2933
- return `'test${isUpdate ? "_updated" : ""}'`;
2907
+ return `'value1'`;
2934
2908
  }
2935
2909
  }
2910
+ function generateUUID() {
2911
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
2912
+ const r = Math.random() * 16 | 0;
2913
+ const v = c === "x" ? r : r & 3 | 8;
2914
+ return v.toString(16);
2915
+ });
2916
+ }
2936
2917
  function generateTestCases(table, sampleData, updateData, hasForeignKeys = false) {
2937
2918
  const Type = pascal(table.name);
2938
2919
  const hasData = sampleData !== "{}";
package/dist/index.js CHANGED
@@ -735,7 +735,7 @@ function emitZod(table, opts) {
735
735
  const Type = pascal(table.name);
736
736
  const zFor = (pg) => {
737
737
  if (pg === "uuid")
738
- return `z.string().uuid()`;
738
+ return `z.string()`;
739
739
  if (pg === "bool" || pg === "boolean")
740
740
  return `z.boolean()`;
741
741
  if (pg === "int2" || pg === "int4" || pg === "int8")
@@ -2462,28 +2462,24 @@ function generateSampleDataFromSchema(table, hasForeignKeys = false) {
2462
2462
  }
2463
2463
  }
2464
2464
  for (const col of table.columns) {
2465
- if (col.hasDefault) {
2466
- const autoGenerated = ["id", "created_at", "updated_at", "created", "updated", "modified_at"];
2467
- if (autoGenerated.includes(col.name.toLowerCase())) {
2468
- continue;
2469
- }
2470
- }
2471
- if (col.name === "deleted_at" || col.name === "deleted") {
2465
+ const isAutoGenerated = col.hasDefault && ["id", "created_at", "updated_at", "created", "updated", "modified_at"].includes(col.name.toLowerCase());
2466
+ if (isAutoGenerated) {
2472
2467
  continue;
2473
2468
  }
2474
- if (!col.nullable) {
2475
- const foreignTable = foreignKeyColumns.get(col.name);
2476
- if (foreignTable) {
2477
- fields.push(` ${col.name}: ${foreignTable}Id`);
2469
+ if (col.name === "deleted_at" || col.name === "deleted") {
2470
+ if (col.nullable) {
2471
+ fields.push(` ${col.name}: null`);
2478
2472
  } else {
2479
2473
  const value = generateValueForColumn(col);
2480
2474
  fields.push(` ${col.name}: ${value}`);
2481
2475
  }
2482
- } else {
2483
- const foreignTable = foreignKeyColumns.get(col.name);
2476
+ continue;
2477
+ }
2478
+ const foreignTable = foreignKeyColumns.get(col.name);
2479
+ if (!col.nullable || foreignTable || col.hasDefault && !isAutoGenerated || shouldIncludeNullableColumn(col)) {
2484
2480
  if (foreignTable) {
2485
2481
  fields.push(` ${col.name}: ${foreignTable}Id`);
2486
- } else if (shouldIncludeNullableColumn(col)) {
2482
+ } else {
2487
2483
  const value = generateValueForColumn(col);
2488
2484
  fields.push(` ${col.name}: ${value}`);
2489
2485
  }
@@ -2543,80 +2539,15 @@ function shouldIncludeNullableColumn(col) {
2543
2539
  return importantPatterns.some((pattern) => name.includes(pattern));
2544
2540
  }
2545
2541
  function generateValueForColumn(col, isUpdate = false) {
2546
- const name = col.name.toLowerCase();
2547
2542
  const type = col.pgType.toLowerCase();
2548
- if (name.includes("email")) {
2549
- return `'test${isUpdate ? ".updated" : ""}@example.com'`;
2550
- }
2551
- if (name.includes("phone")) {
2552
- return `'555-${isUpdate ? "0200" : "0100"}'`;
2553
- }
2554
- if (name.includes("url") || name.includes("website")) {
2555
- return `'https://example.com${isUpdate ? "/updated" : ""}'`;
2556
- }
2557
- if (name.includes("password")) {
2558
- return `'hashedPassword123'`;
2559
- }
2560
- if (name === "name" || name.includes("_name") || name.includes("name_")) {
2561
- return `'Test ${pascal(col.name)}${isUpdate ? " Updated" : ""}'`;
2562
- }
2563
- if (name === "title" || name.includes("title")) {
2564
- return `'Test Title${isUpdate ? " Updated" : ""}'`;
2565
- }
2566
- if (name.includes("description") || name === "bio" || name === "about") {
2567
- return `'Test description${isUpdate ? " updated" : ""}'`;
2568
- }
2569
- if (name === "slug") {
2570
- return `'test-slug${isUpdate ? "-updated" : ""}'`;
2571
- }
2572
- if (name === "status") {
2573
- return `'${isUpdate ? "updated" : "active"}'`;
2574
- }
2575
- if (name === "type" || name === "kind" || name === "category") {
2576
- return `'${isUpdate ? "type2" : "type1"}'`;
2577
- }
2578
- if (name === "color" || name === "colour") {
2579
- return `'${isUpdate ? "#FF0000" : "#0000FF"}'`;
2580
- }
2581
- if (name === "gender") {
2582
- return `'${isUpdate ? "F" : "M"}'`;
2583
- }
2584
- if (name.includes("price") || name === "cost" || name === "amount") {
2585
- return isUpdate ? "99.99" : "10.50";
2586
- }
2587
- if (name === "quantity" || name === "count" || name.includes("qty")) {
2588
- return isUpdate ? "5" : "1";
2589
- }
2590
- if (name === "age") {
2591
- return isUpdate ? "30" : "25";
2592
- }
2593
- if (name.includes("percent") || name === "rate" || name === "ratio") {
2594
- return isUpdate ? "0.75" : "0.5";
2595
- }
2596
- if (name.includes("latitude") || name === "lat") {
2597
- return "40.7128";
2598
- }
2599
- if (name.includes("longitude") || name === "lng" || name === "lon") {
2600
- return "-74.0060";
2601
- }
2602
- if (type.includes("date") || type.includes("timestamp")) {
2603
- if (name.includes("birth") || name === "dob") {
2604
- return `new Date('1990-01-01')`;
2605
- }
2606
- if (name.includes("end") || name.includes("expire")) {
2607
- return `new Date('2025-12-31')`;
2608
- }
2609
- if (name.includes("start") || name.includes("begin")) {
2610
- return `new Date('2024-01-01')`;
2611
- }
2612
- return `new Date()`;
2613
- }
2614
2543
  switch (type) {
2615
2544
  case "text":
2616
2545
  case "varchar":
2617
2546
  case "char":
2618
2547
  case "character varying":
2619
- return `'test_value${isUpdate ? "_updated" : ""}'`;
2548
+ case "bpchar":
2549
+ case "name":
2550
+ return `'str_${Math.random().toString(36).substring(7)}'`;
2620
2551
  case "int":
2621
2552
  case "int2":
2622
2553
  case "int4":
@@ -2624,6 +2555,8 @@ function generateValueForColumn(col, isUpdate = false) {
2624
2555
  case "integer":
2625
2556
  case "smallint":
2626
2557
  case "bigint":
2558
+ case "serial":
2559
+ case "bigserial":
2627
2560
  return isUpdate ? "42" : "1";
2628
2561
  case "decimal":
2629
2562
  case "numeric":
@@ -2632,37 +2565,85 @@ function generateValueForColumn(col, isUpdate = false) {
2632
2565
  case "float":
2633
2566
  case "float4":
2634
2567
  case "float8":
2568
+ case "money":
2635
2569
  return isUpdate ? "99.99" : "10.50";
2636
2570
  case "boolean":
2637
2571
  case "bool":
2638
2572
  return isUpdate ? "false" : "true";
2573
+ case "date":
2574
+ case "timestamp":
2575
+ case "timestamptz":
2576
+ case "timestamp without time zone":
2577
+ case "timestamp with time zone":
2578
+ case "time":
2579
+ case "timetz":
2580
+ case "time without time zone":
2581
+ case "time with time zone":
2582
+ return `new Date()`;
2583
+ case "interval":
2584
+ return `'1 day'`;
2639
2585
  case "json":
2640
2586
  case "jsonb":
2641
- return `{ key: '${isUpdate ? "updated" : "value"}' }`;
2587
+ return `{}`;
2642
2588
  case "uuid":
2643
- return `'${isUpdate ? "550e8400-e29b-41d4-a716-446655440001" : "550e8400-e29b-41d4-a716-446655440000"}'`;
2589
+ return `'${generateUUID()}'`;
2644
2590
  case "inet":
2645
- return `'${isUpdate ? "192.168.1.2" : "192.168.1.1"}'`;
2591
+ return `'192.168.1.1'`;
2646
2592
  case "cidr":
2647
2593
  return `'192.168.1.0/24'`;
2648
2594
  case "macaddr":
2649
- return `'08:00:2b:01:02:0${isUpdate ? "4" : "3"}'`;
2595
+ case "macaddr8":
2596
+ return `'08:00:2b:01:02:03'`;
2597
+ case "point":
2598
+ return `'(1,2)'`;
2599
+ case "line":
2600
+ return `'{1,2,3}'`;
2601
+ case "lseg":
2602
+ return `'[(0,0),(1,1)]'`;
2603
+ case "box":
2604
+ return `'((0,0),(1,1))'`;
2605
+ case "path":
2606
+ return `'[(0,0),(1,1),(2,0)]'`;
2607
+ case "polygon":
2608
+ return `'((0,0),(1,1),(1,0))'`;
2609
+ case "circle":
2610
+ return `'<(0,0),1>'`;
2611
+ case "bit":
2612
+ case "bit varying":
2613
+ case "varbit":
2614
+ return `'101'`;
2615
+ case "bytea":
2616
+ return `'\\\\x0102'`;
2650
2617
  case "xml":
2651
- return `'<root>${isUpdate ? "updated" : "value"}</root>'`;
2618
+ return `'<root/>'`;
2619
+ case "tsvector":
2620
+ return `'a fat cat'`;
2621
+ case "tsquery":
2622
+ return `'fat & cat'`;
2623
+ case "oid":
2624
+ case "regproc":
2625
+ case "regprocedure":
2626
+ case "regoper":
2627
+ case "regoperator":
2628
+ case "regclass":
2629
+ case "regtype":
2630
+ case "regconfig":
2631
+ case "regdictionary":
2632
+ return "1";
2652
2633
  default:
2653
- if (type.endsWith("[]")) {
2654
- const baseType = type.slice(0, -2);
2655
- if (baseType === "text" || baseType === "varchar") {
2656
- return `['item1', 'item2${isUpdate ? "_updated" : ""}']`;
2657
- }
2658
- if (baseType === "int" || baseType === "integer") {
2659
- return `[1, 2, ${isUpdate ? "3" : ""}]`;
2660
- }
2634
+ if (type.endsWith("[]") || type.startsWith("_")) {
2661
2635
  return `[]`;
2662
2636
  }
2663
- return `'test${isUpdate ? "_updated" : ""}'`;
2637
+ return `'value1'`;
2664
2638
  }
2665
2639
  }
2640
+ function generateUUID() {
2641
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
2642
+ const r = Math.random() * 16 | 0;
2643
+ const v = c === "x" ? r : r & 3 | 8;
2644
+ return v.toString(16);
2645
+ });
2646
+ }
2666
2647
  function generateTestCases(table, sampleData, updateData, hasForeignKeys = false) {
2667
2648
  const Type = pascal(table.name);
2668
2649
  const hasData = sampleData !== "{}";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postgresdk",
3
- "version": "0.6.7",
3
+ "version": "0.6.8",
4
4
  "description": "Generate a typed server/client SDK from a Postgres schema (includes, Zod, Hono).",
5
5
  "type": "module",
6
6
  "bin": {