@vibeorm/runtime 1.1.0 → 1.1.1
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/package.json +1 -1
- package/src/client.ts +97 -65
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -1032,70 +1032,86 @@ export function createClient(params: {
|
|
|
1032
1032
|
): Promise<T | unknown[]> {
|
|
1033
1033
|
// Array-of-promises style
|
|
1034
1034
|
if (Array.isArray(fnOrPromises)) {
|
|
1035
|
-
|
|
1036
|
-
return
|
|
1037
|
-
|
|
1035
|
+
try {
|
|
1036
|
+
return await adapter.transaction(async () => {
|
|
1037
|
+
return Promise.all(fnOrPromises);
|
|
1038
|
+
}, txOptions);
|
|
1039
|
+
} catch (err) {
|
|
1040
|
+
throw normalizeError({ error: err });
|
|
1041
|
+
}
|
|
1038
1042
|
}
|
|
1039
1043
|
|
|
1040
1044
|
// Callback style
|
|
1041
1045
|
const fn = fnOrPromises as (tx: Record<string, unknown>) => Promise<T>;
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1046
|
+
try {
|
|
1047
|
+
return await adapter.transaction(async (txAdapter) => {
|
|
1048
|
+
// Create a transactional executor — normalizes DB errors
|
|
1049
|
+
async function txExecutor(txParams: { text: string; values: unknown[] }): Promise<Record<string, unknown>[]> {
|
|
1050
|
+
const values = txParams.values.map((v) => (v instanceof PgArray ? txAdapter.formatArrayParam(v.values) : v));
|
|
1051
|
+
if (shouldLog) {
|
|
1052
|
+
console.log(`[vibeorm:tx] ${txParams.text}`);
|
|
1053
|
+
if (values.length > 0) {
|
|
1054
|
+
console.log(`[vibeorm:tx] params:`, values);
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
try {
|
|
1058
|
+
return await txAdapter.execute({ text: txParams.text, values });
|
|
1059
|
+
} catch (err) {
|
|
1060
|
+
throw normalizeError({ error: err });
|
|
1050
1061
|
}
|
|
1051
1062
|
}
|
|
1052
|
-
try {
|
|
1053
|
-
return await txAdapter.execute({ text: txParams.text, values });
|
|
1054
|
-
} catch (err) {
|
|
1055
|
-
throw normalizeError({ error: err });
|
|
1056
|
-
}
|
|
1057
|
-
}
|
|
1058
|
-
|
|
1059
|
-
// Build transactional delegates
|
|
1060
|
-
const txClient: Record<string, unknown> = {};
|
|
1061
|
-
for (const [key, meta] of Object.entries(allModelsMeta)) {
|
|
1062
|
-
txClient[key] = createDelegate({
|
|
1063
|
-
modelKey: key,
|
|
1064
|
-
modelMeta: meta,
|
|
1065
|
-
executor: txExecutor,
|
|
1066
|
-
schemas: params.schemas?.[key],
|
|
1067
|
-
});
|
|
1068
|
-
}
|
|
1069
1063
|
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1064
|
+
// Build transactional delegates
|
|
1065
|
+
const txClient: Record<string, unknown> = {};
|
|
1066
|
+
for (const [key, meta] of Object.entries(allModelsMeta)) {
|
|
1067
|
+
txClient[key] = createDelegate({
|
|
1068
|
+
modelKey: key,
|
|
1069
|
+
modelMeta: meta,
|
|
1070
|
+
executor: txExecutor,
|
|
1071
|
+
schemas: params.schemas?.[key],
|
|
1072
|
+
});
|
|
1079
1073
|
}
|
|
1080
|
-
const result = await txAdapter.executeUnsafe({ text, values: sqlParams });
|
|
1081
|
-
return result.rows as T[];
|
|
1082
|
-
};
|
|
1083
1074
|
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1075
|
+
// Add $queryRaw and $executeRaw to transactional client
|
|
1076
|
+
txClient.$queryRaw = async function <T = unknown>(
|
|
1077
|
+
strings: TemplateStringsArray,
|
|
1078
|
+
...values: unknown[]
|
|
1079
|
+
): Promise<T[]> {
|
|
1080
|
+
const { text, params: sqlParams } = buildTaggedTemplateSql({ strings, values });
|
|
1081
|
+
if (shouldLog) {
|
|
1082
|
+
console.log(`[vibeorm:tx] ${text}`);
|
|
1083
|
+
if (sqlParams.length > 0) console.log(`[vibeorm:tx] params:`, sqlParams);
|
|
1084
|
+
}
|
|
1085
|
+
try {
|
|
1086
|
+
const result = await txAdapter.executeUnsafe({ text, values: sqlParams });
|
|
1087
|
+
return result.rows as T[];
|
|
1088
|
+
} catch (err) {
|
|
1089
|
+
throw normalizeError({ error: err });
|
|
1090
|
+
}
|
|
1091
|
+
};
|
|
1096
1092
|
|
|
1097
|
-
|
|
1098
|
-
|
|
1093
|
+
txClient.$executeRaw = async function (
|
|
1094
|
+
strings: TemplateStringsArray,
|
|
1095
|
+
...values: unknown[]
|
|
1096
|
+
): Promise<number> {
|
|
1097
|
+
const { text, params: sqlParams } = buildTaggedTemplateSql({ strings, values });
|
|
1098
|
+
if (shouldLog) {
|
|
1099
|
+
console.log(`[vibeorm:tx] ${text}`);
|
|
1100
|
+
if (sqlParams.length > 0) console.log(`[vibeorm:tx] params:`, sqlParams);
|
|
1101
|
+
}
|
|
1102
|
+
try {
|
|
1103
|
+
const result = await txAdapter.executeUnsafe({ text, values: sqlParams });
|
|
1104
|
+
return result.affectedRows;
|
|
1105
|
+
} catch (err) {
|
|
1106
|
+
throw normalizeError({ error: err });
|
|
1107
|
+
}
|
|
1108
|
+
};
|
|
1109
|
+
|
|
1110
|
+
return fn(txClient);
|
|
1111
|
+
}, txOptions);
|
|
1112
|
+
} catch (err) {
|
|
1113
|
+
throw normalizeError({ error: err });
|
|
1114
|
+
}
|
|
1099
1115
|
};
|
|
1100
1116
|
|
|
1101
1117
|
// $queryRaw — tagged template literal for safe parameterized queries
|
|
@@ -1110,8 +1126,12 @@ export function createClient(params: {
|
|
|
1110
1126
|
console.log(`[vibeorm:raw] params:`, sqlParams);
|
|
1111
1127
|
}
|
|
1112
1128
|
}
|
|
1113
|
-
|
|
1114
|
-
|
|
1129
|
+
try {
|
|
1130
|
+
const result = await adapter.executeUnsafe({ text, values: sqlParams });
|
|
1131
|
+
return result.rows as T[];
|
|
1132
|
+
} catch (err) {
|
|
1133
|
+
throw normalizeError({ error: err });
|
|
1134
|
+
}
|
|
1115
1135
|
};
|
|
1116
1136
|
|
|
1117
1137
|
// $executeRaw — tagged template literal for INSERT/UPDATE/DELETE returning affected count
|
|
@@ -1126,8 +1146,12 @@ export function createClient(params: {
|
|
|
1126
1146
|
console.log(`[vibeorm:raw] params:`, sqlParams);
|
|
1127
1147
|
}
|
|
1128
1148
|
}
|
|
1129
|
-
|
|
1130
|
-
|
|
1149
|
+
try {
|
|
1150
|
+
const result = await adapter.executeUnsafe({ text, values: sqlParams });
|
|
1151
|
+
return result.affectedRows;
|
|
1152
|
+
} catch (err) {
|
|
1153
|
+
throw normalizeError({ error: err });
|
|
1154
|
+
}
|
|
1131
1155
|
};
|
|
1132
1156
|
|
|
1133
1157
|
// $queryRawUnsafe — accepts a plain SQL string + params array
|
|
@@ -1141,12 +1165,16 @@ export function createClient(params: {
|
|
|
1141
1165
|
console.log(`[vibeorm:raw] params:`, values);
|
|
1142
1166
|
}
|
|
1143
1167
|
}
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1168
|
+
try {
|
|
1169
|
+
if (values.length === 0) {
|
|
1170
|
+
const result = await adapter.executeUnsafe({ text: query });
|
|
1171
|
+
return result.rows as T[];
|
|
1172
|
+
}
|
|
1173
|
+
const rows = await adapter.execute({ text: query, values });
|
|
1174
|
+
return rows as T[];
|
|
1175
|
+
} catch (err) {
|
|
1176
|
+
throw normalizeError({ error: err });
|
|
1147
1177
|
}
|
|
1148
|
-
const rows = await adapter.execute({ text: query, values });
|
|
1149
|
-
return rows as T[];
|
|
1150
1178
|
};
|
|
1151
1179
|
|
|
1152
1180
|
// $executeRawUnsafe — accepts a plain SQL string + params array, returns affected count
|
|
@@ -1160,8 +1188,12 @@ export function createClient(params: {
|
|
|
1160
1188
|
console.log(`[vibeorm:raw] params:`, values);
|
|
1161
1189
|
}
|
|
1162
1190
|
}
|
|
1163
|
-
|
|
1164
|
-
|
|
1191
|
+
try {
|
|
1192
|
+
const result = await adapter.executeUnsafe({ text: query, values: values.length > 0 ? values : undefined });
|
|
1193
|
+
return result.affectedRows;
|
|
1194
|
+
} catch (err) {
|
|
1195
|
+
throw normalizeError({ error: err });
|
|
1196
|
+
}
|
|
1165
1197
|
};
|
|
1166
1198
|
|
|
1167
1199
|
// $connect
|