postgresdk 0.19.1 → 0.19.2
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 +31 -57
- package/dist/index.js +31 -57
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -2823,7 +2823,7 @@ function emitZod(table, opts, enums) {
|
|
|
2823
2823
|
return `z.enum([${values}])`;
|
|
2824
2824
|
}
|
|
2825
2825
|
if (t === "uuid")
|
|
2826
|
-
return `z.string()`;
|
|
2826
|
+
return `z.string().uuid()`;
|
|
2827
2827
|
if (t === "bool" || t === "boolean")
|
|
2828
2828
|
return `z.boolean()`;
|
|
2829
2829
|
if (t === "int2" || t === "int4" || t === "int8") {
|
|
@@ -5951,6 +5951,18 @@ const log = {
|
|
|
5951
5951
|
error: (...args: any[]) => console.error("[sdk]", ...args),
|
|
5952
5952
|
};
|
|
5953
5953
|
|
|
5954
|
+
/**
|
|
5955
|
+
* Checks if a Postgres error is a client input error (invalid syntax, out of range, etc.)
|
|
5956
|
+
* PG error class 22 = "data exception" — covers invalid input syntax for uuid, int, json, etc.
|
|
5957
|
+
* PG error code 23502 = "not_null_violation", 23505 = "unique_violation", 23503 = "foreign_key_violation"
|
|
5958
|
+
*/
|
|
5959
|
+
function pgErrorStatus(e: any): number {
|
|
5960
|
+
const code = e?.code;
|
|
5961
|
+
if (typeof code === "string" && code.startsWith("22")) return 400;
|
|
5962
|
+
if (code === "23502" || code === "23505" || code === "23503") return 409;
|
|
5963
|
+
return 500;
|
|
5964
|
+
}
|
|
5965
|
+
|
|
5954
5966
|
/**
|
|
5955
5967
|
* Builds SQL column list from select/exclude parameters
|
|
5956
5968
|
* @param select - Columns to include (mutually exclusive with exclude)
|
|
@@ -6051,22 +6063,13 @@ export async function createRecord(
|
|
|
6051
6063
|
|
|
6052
6064
|
return { data: parsedRows[0] ?? null, status: parsedRows[0] ? 201 : 500 };
|
|
6053
6065
|
} catch (e: any) {
|
|
6054
|
-
|
|
6055
|
-
|
|
6056
|
-
const isJsonError = errorMsg.includes("invalid input syntax for type json");
|
|
6057
|
-
|
|
6058
|
-
if (isJsonError) {
|
|
6059
|
-
log.error(\`POST \${ctx.table} - Invalid JSON input detected!\`);
|
|
6060
|
-
log.error("Input data that caused error:", JSON.stringify(data, null, 2));
|
|
6061
|
-
log.error("PostgreSQL error:", errorMsg);
|
|
6062
|
-
} else {
|
|
6063
|
-
log.error(\`POST \${ctx.table} error:\`, e?.stack ?? e);
|
|
6064
|
-
}
|
|
6066
|
+
const status = pgErrorStatus(e);
|
|
6067
|
+
log.error(\`POST \${ctx.table} error:\`, e?.stack ?? e);
|
|
6065
6068
|
|
|
6066
6069
|
return {
|
|
6067
6070
|
error: e?.message ?? "Internal error",
|
|
6068
6071
|
...(DEBUG ? { stack: e?.stack } : {}),
|
|
6069
|
-
status
|
|
6072
|
+
status
|
|
6070
6073
|
};
|
|
6071
6074
|
}
|
|
6072
6075
|
}
|
|
@@ -6141,22 +6144,12 @@ export async function upsertRecord(
|
|
|
6141
6144
|
|
|
6142
6145
|
return { data: parsedRows[0], status: 200 };
|
|
6143
6146
|
} catch (e: any) {
|
|
6144
|
-
const
|
|
6145
|
-
|
|
6146
|
-
if (isJsonError) {
|
|
6147
|
-
log.error(\`UPSERT \${ctx.table} - Invalid JSON input detected!\`);
|
|
6148
|
-
log.error("Input args that caused error:", JSON.stringify(args, null, 2));
|
|
6149
|
-
log.error("Filtered update data (sent to DB):", JSON.stringify(Object.fromEntries(
|
|
6150
|
-
Object.entries(args.update).filter(([k]) => !ctx.pkColumns.includes(k))
|
|
6151
|
-
), null, 2));
|
|
6152
|
-
log.error("PostgreSQL error:", errorMsg);
|
|
6153
|
-
} else {
|
|
6154
|
-
log.error(\`UPSERT \${ctx.table} error:\`, e?.stack ?? e);
|
|
6155
|
-
}
|
|
6147
|
+
const status = pgErrorStatus(e);
|
|
6148
|
+
log.error(\`UPSERT \${ctx.table} error:\`, e?.stack ?? e);
|
|
6156
6149
|
return {
|
|
6157
6150
|
error: e?.message ?? "Internal error",
|
|
6158
6151
|
...(DEBUG ? { stack: e?.stack } : {}),
|
|
6159
|
-
status
|
|
6152
|
+
status
|
|
6160
6153
|
};
|
|
6161
6154
|
}
|
|
6162
6155
|
}
|
|
@@ -6189,11 +6182,12 @@ export async function getByPk(
|
|
|
6189
6182
|
|
|
6190
6183
|
return { data: parsedRows[0], status: 200 };
|
|
6191
6184
|
} catch (e: any) {
|
|
6185
|
+
const status = pgErrorStatus(e);
|
|
6192
6186
|
log.error(\`GET \${ctx.table} error:\`, e?.stack ?? e);
|
|
6193
|
-
return {
|
|
6194
|
-
error: e?.message ?? "Internal error",
|
|
6187
|
+
return {
|
|
6188
|
+
error: e?.message ?? "Internal error",
|
|
6195
6189
|
...(DEBUG ? { stack: e?.stack } : {}),
|
|
6196
|
-
status
|
|
6190
|
+
status
|
|
6197
6191
|
};
|
|
6198
6192
|
}
|
|
6199
6193
|
}
|
|
@@ -6742,22 +6736,13 @@ export async function listRecords(
|
|
|
6742
6736
|
log.debug(\`LIST \${ctx.table} result: \${rows.length} rows, \${total} total, hasMore=\${hasMore}\`);
|
|
6743
6737
|
return metadata;
|
|
6744
6738
|
} catch (e: any) {
|
|
6745
|
-
|
|
6746
|
-
|
|
6747
|
-
const isJsonError = errorMsg.includes("invalid input syntax for type json");
|
|
6748
|
-
|
|
6749
|
-
if (isJsonError) {
|
|
6750
|
-
log.error(\`LIST \${ctx.table} - Invalid JSON input detected in query!\`);
|
|
6751
|
-
log.error("WHERE clause:", JSON.stringify(params.where, null, 2));
|
|
6752
|
-
log.error("PostgreSQL error:", errorMsg);
|
|
6753
|
-
} else {
|
|
6754
|
-
log.error(\`LIST \${ctx.table} error:\`, e?.stack ?? e);
|
|
6755
|
-
}
|
|
6739
|
+
const status = pgErrorStatus(e);
|
|
6740
|
+
log.error(\`LIST \${ctx.table} error:\`, e?.stack ?? e);
|
|
6756
6741
|
|
|
6757
6742
|
return {
|
|
6758
6743
|
error: e?.message ?? "Internal error",
|
|
6759
6744
|
...(DEBUG ? { stack: e?.stack } : {}),
|
|
6760
|
-
status
|
|
6745
|
+
status
|
|
6761
6746
|
};
|
|
6762
6747
|
}
|
|
6763
6748
|
}
|
|
@@ -6809,25 +6794,13 @@ export async function updateRecord(
|
|
|
6809
6794
|
|
|
6810
6795
|
return { data: parsedRows[0], status: 200 };
|
|
6811
6796
|
} catch (e: any) {
|
|
6812
|
-
|
|
6813
|
-
|
|
6814
|
-
const isJsonError = errorMsg.includes("invalid input syntax for type json");
|
|
6815
|
-
|
|
6816
|
-
if (isJsonError) {
|
|
6817
|
-
log.error(\`PATCH \${ctx.table} - Invalid JSON input detected!\`);
|
|
6818
|
-
log.error("Input data that caused error:", JSON.stringify(updateData, null, 2));
|
|
6819
|
-
log.error("Filtered data (sent to DB):", JSON.stringify(Object.fromEntries(
|
|
6820
|
-
Object.entries(updateData).filter(([k]) => !ctx.pkColumns.includes(k))
|
|
6821
|
-
), null, 2));
|
|
6822
|
-
log.error("PostgreSQL error:", errorMsg);
|
|
6823
|
-
} else {
|
|
6824
|
-
log.error(\`PATCH \${ctx.table} error:\`, e?.stack ?? e);
|
|
6825
|
-
}
|
|
6797
|
+
const status = pgErrorStatus(e);
|
|
6798
|
+
log.error(\`PATCH \${ctx.table} error:\`, e?.stack ?? e);
|
|
6826
6799
|
|
|
6827
6800
|
return {
|
|
6828
6801
|
error: e?.message ?? "Internal error",
|
|
6829
6802
|
...(DEBUG ? { stack: e?.stack } : {}),
|
|
6830
|
-
status
|
|
6803
|
+
status
|
|
6831
6804
|
};
|
|
6832
6805
|
}
|
|
6833
6806
|
}
|
|
@@ -6862,11 +6835,12 @@ export async function deleteRecord(
|
|
|
6862
6835
|
|
|
6863
6836
|
return { data: parsedRows[0], status: 200 };
|
|
6864
6837
|
} catch (e: any) {
|
|
6838
|
+
const status = pgErrorStatus(e);
|
|
6865
6839
|
log.error(\`DELETE \${ctx.table} error:\`, e?.stack ?? e);
|
|
6866
6840
|
return {
|
|
6867
6841
|
error: e?.message ?? "Internal error",
|
|
6868
6842
|
...(DEBUG ? { stack: e?.stack } : {}),
|
|
6869
|
-
status
|
|
6843
|
+
status
|
|
6870
6844
|
};
|
|
6871
6845
|
}
|
|
6872
6846
|
}
|
package/dist/index.js
CHANGED
|
@@ -1863,7 +1863,7 @@ function emitZod(table, opts, enums) {
|
|
|
1863
1863
|
return `z.enum([${values}])`;
|
|
1864
1864
|
}
|
|
1865
1865
|
if (t === "uuid")
|
|
1866
|
-
return `z.string()`;
|
|
1866
|
+
return `z.string().uuid()`;
|
|
1867
1867
|
if (t === "bool" || t === "boolean")
|
|
1868
1868
|
return `z.boolean()`;
|
|
1869
1869
|
if (t === "int2" || t === "int4" || t === "int8") {
|
|
@@ -4991,6 +4991,18 @@ const log = {
|
|
|
4991
4991
|
error: (...args: any[]) => console.error("[sdk]", ...args),
|
|
4992
4992
|
};
|
|
4993
4993
|
|
|
4994
|
+
/**
|
|
4995
|
+
* Checks if a Postgres error is a client input error (invalid syntax, out of range, etc.)
|
|
4996
|
+
* PG error class 22 = "data exception" — covers invalid input syntax for uuid, int, json, etc.
|
|
4997
|
+
* PG error code 23502 = "not_null_violation", 23505 = "unique_violation", 23503 = "foreign_key_violation"
|
|
4998
|
+
*/
|
|
4999
|
+
function pgErrorStatus(e: any): number {
|
|
5000
|
+
const code = e?.code;
|
|
5001
|
+
if (typeof code === "string" && code.startsWith("22")) return 400;
|
|
5002
|
+
if (code === "23502" || code === "23505" || code === "23503") return 409;
|
|
5003
|
+
return 500;
|
|
5004
|
+
}
|
|
5005
|
+
|
|
4994
5006
|
/**
|
|
4995
5007
|
* Builds SQL column list from select/exclude parameters
|
|
4996
5008
|
* @param select - Columns to include (mutually exclusive with exclude)
|
|
@@ -5091,22 +5103,13 @@ export async function createRecord(
|
|
|
5091
5103
|
|
|
5092
5104
|
return { data: parsedRows[0] ?? null, status: parsedRows[0] ? 201 : 500 };
|
|
5093
5105
|
} catch (e: any) {
|
|
5094
|
-
|
|
5095
|
-
|
|
5096
|
-
const isJsonError = errorMsg.includes("invalid input syntax for type json");
|
|
5097
|
-
|
|
5098
|
-
if (isJsonError) {
|
|
5099
|
-
log.error(\`POST \${ctx.table} - Invalid JSON input detected!\`);
|
|
5100
|
-
log.error("Input data that caused error:", JSON.stringify(data, null, 2));
|
|
5101
|
-
log.error("PostgreSQL error:", errorMsg);
|
|
5102
|
-
} else {
|
|
5103
|
-
log.error(\`POST \${ctx.table} error:\`, e?.stack ?? e);
|
|
5104
|
-
}
|
|
5106
|
+
const status = pgErrorStatus(e);
|
|
5107
|
+
log.error(\`POST \${ctx.table} error:\`, e?.stack ?? e);
|
|
5105
5108
|
|
|
5106
5109
|
return {
|
|
5107
5110
|
error: e?.message ?? "Internal error",
|
|
5108
5111
|
...(DEBUG ? { stack: e?.stack } : {}),
|
|
5109
|
-
status
|
|
5112
|
+
status
|
|
5110
5113
|
};
|
|
5111
5114
|
}
|
|
5112
5115
|
}
|
|
@@ -5181,22 +5184,12 @@ export async function upsertRecord(
|
|
|
5181
5184
|
|
|
5182
5185
|
return { data: parsedRows[0], status: 200 };
|
|
5183
5186
|
} catch (e: any) {
|
|
5184
|
-
const
|
|
5185
|
-
|
|
5186
|
-
if (isJsonError) {
|
|
5187
|
-
log.error(\`UPSERT \${ctx.table} - Invalid JSON input detected!\`);
|
|
5188
|
-
log.error("Input args that caused error:", JSON.stringify(args, null, 2));
|
|
5189
|
-
log.error("Filtered update data (sent to DB):", JSON.stringify(Object.fromEntries(
|
|
5190
|
-
Object.entries(args.update).filter(([k]) => !ctx.pkColumns.includes(k))
|
|
5191
|
-
), null, 2));
|
|
5192
|
-
log.error("PostgreSQL error:", errorMsg);
|
|
5193
|
-
} else {
|
|
5194
|
-
log.error(\`UPSERT \${ctx.table} error:\`, e?.stack ?? e);
|
|
5195
|
-
}
|
|
5187
|
+
const status = pgErrorStatus(e);
|
|
5188
|
+
log.error(\`UPSERT \${ctx.table} error:\`, e?.stack ?? e);
|
|
5196
5189
|
return {
|
|
5197
5190
|
error: e?.message ?? "Internal error",
|
|
5198
5191
|
...(DEBUG ? { stack: e?.stack } : {}),
|
|
5199
|
-
status
|
|
5192
|
+
status
|
|
5200
5193
|
};
|
|
5201
5194
|
}
|
|
5202
5195
|
}
|
|
@@ -5229,11 +5222,12 @@ export async function getByPk(
|
|
|
5229
5222
|
|
|
5230
5223
|
return { data: parsedRows[0], status: 200 };
|
|
5231
5224
|
} catch (e: any) {
|
|
5225
|
+
const status = pgErrorStatus(e);
|
|
5232
5226
|
log.error(\`GET \${ctx.table} error:\`, e?.stack ?? e);
|
|
5233
|
-
return {
|
|
5234
|
-
error: e?.message ?? "Internal error",
|
|
5227
|
+
return {
|
|
5228
|
+
error: e?.message ?? "Internal error",
|
|
5235
5229
|
...(DEBUG ? { stack: e?.stack } : {}),
|
|
5236
|
-
status
|
|
5230
|
+
status
|
|
5237
5231
|
};
|
|
5238
5232
|
}
|
|
5239
5233
|
}
|
|
@@ -5782,22 +5776,13 @@ export async function listRecords(
|
|
|
5782
5776
|
log.debug(\`LIST \${ctx.table} result: \${rows.length} rows, \${total} total, hasMore=\${hasMore}\`);
|
|
5783
5777
|
return metadata;
|
|
5784
5778
|
} catch (e: any) {
|
|
5785
|
-
|
|
5786
|
-
|
|
5787
|
-
const isJsonError = errorMsg.includes("invalid input syntax for type json");
|
|
5788
|
-
|
|
5789
|
-
if (isJsonError) {
|
|
5790
|
-
log.error(\`LIST \${ctx.table} - Invalid JSON input detected in query!\`);
|
|
5791
|
-
log.error("WHERE clause:", JSON.stringify(params.where, null, 2));
|
|
5792
|
-
log.error("PostgreSQL error:", errorMsg);
|
|
5793
|
-
} else {
|
|
5794
|
-
log.error(\`LIST \${ctx.table} error:\`, e?.stack ?? e);
|
|
5795
|
-
}
|
|
5779
|
+
const status = pgErrorStatus(e);
|
|
5780
|
+
log.error(\`LIST \${ctx.table} error:\`, e?.stack ?? e);
|
|
5796
5781
|
|
|
5797
5782
|
return {
|
|
5798
5783
|
error: e?.message ?? "Internal error",
|
|
5799
5784
|
...(DEBUG ? { stack: e?.stack } : {}),
|
|
5800
|
-
status
|
|
5785
|
+
status
|
|
5801
5786
|
};
|
|
5802
5787
|
}
|
|
5803
5788
|
}
|
|
@@ -5849,25 +5834,13 @@ export async function updateRecord(
|
|
|
5849
5834
|
|
|
5850
5835
|
return { data: parsedRows[0], status: 200 };
|
|
5851
5836
|
} catch (e: any) {
|
|
5852
|
-
|
|
5853
|
-
|
|
5854
|
-
const isJsonError = errorMsg.includes("invalid input syntax for type json");
|
|
5855
|
-
|
|
5856
|
-
if (isJsonError) {
|
|
5857
|
-
log.error(\`PATCH \${ctx.table} - Invalid JSON input detected!\`);
|
|
5858
|
-
log.error("Input data that caused error:", JSON.stringify(updateData, null, 2));
|
|
5859
|
-
log.error("Filtered data (sent to DB):", JSON.stringify(Object.fromEntries(
|
|
5860
|
-
Object.entries(updateData).filter(([k]) => !ctx.pkColumns.includes(k))
|
|
5861
|
-
), null, 2));
|
|
5862
|
-
log.error("PostgreSQL error:", errorMsg);
|
|
5863
|
-
} else {
|
|
5864
|
-
log.error(\`PATCH \${ctx.table} error:\`, e?.stack ?? e);
|
|
5865
|
-
}
|
|
5837
|
+
const status = pgErrorStatus(e);
|
|
5838
|
+
log.error(\`PATCH \${ctx.table} error:\`, e?.stack ?? e);
|
|
5866
5839
|
|
|
5867
5840
|
return {
|
|
5868
5841
|
error: e?.message ?? "Internal error",
|
|
5869
5842
|
...(DEBUG ? { stack: e?.stack } : {}),
|
|
5870
|
-
status
|
|
5843
|
+
status
|
|
5871
5844
|
};
|
|
5872
5845
|
}
|
|
5873
5846
|
}
|
|
@@ -5902,11 +5875,12 @@ export async function deleteRecord(
|
|
|
5902
5875
|
|
|
5903
5876
|
return { data: parsedRows[0], status: 200 };
|
|
5904
5877
|
} catch (e: any) {
|
|
5878
|
+
const status = pgErrorStatus(e);
|
|
5905
5879
|
log.error(\`DELETE \${ctx.table} error:\`, e?.stack ?? e);
|
|
5906
5880
|
return {
|
|
5907
5881
|
error: e?.message ?? "Internal error",
|
|
5908
5882
|
...(DEBUG ? { stack: e?.stack } : {}),
|
|
5909
|
-
status
|
|
5883
|
+
status
|
|
5910
5884
|
};
|
|
5911
5885
|
}
|
|
5912
5886
|
}
|