@bdkinc/knex-ibmi 0.5.2 → 0.5.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/dist/index.mjs CHANGED
@@ -6,6 +6,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
6
6
 
7
7
  // src/index.ts
8
8
  import process from "process";
9
+ import { Buffer as Buffer2 } from "buffer";
9
10
  import knex from "knex";
10
11
  import odbc from "odbc";
11
12
 
@@ -804,6 +805,7 @@ var DB2Client = class extends knex.Client {
804
805
  super(config);
805
806
  // Per-connection statement cache (WeakMap so it's GC'd with connections)
806
807
  __publicField(this, "statementCaches", /* @__PURE__ */ new WeakMap());
808
+ __publicField(this, "normalizeBigintToString");
807
809
  this.driverName = "odbc";
808
810
  if (this.dialect && !this.config.client) {
809
811
  this.printWarn(
@@ -829,6 +831,8 @@ var DB2Client = class extends knex.Client {
829
831
  if (config.useNullAsDefault) {
830
832
  this.valueForUndefined = null;
831
833
  }
834
+ const ibmiConfig = config?.ibmi;
835
+ this.normalizeBigintToString = ibmiConfig?.normalizeBigintToString !== false;
832
836
  }
833
837
  // Helper method to safely stringify objects that might have circular references
834
838
  safeStringify(obj, indent = 0) {
@@ -1109,7 +1113,8 @@ var DB2Client = class extends knex.Client {
1109
1113
  obj.bindings
1110
1114
  );
1111
1115
  if (rows) {
1112
- obj.response = { rows, rowCount: rows.length };
1116
+ const normalizedRows = this.maybeNormalizeBigint(rows);
1117
+ obj.response = { rows: normalizedRows, rowCount: normalizedRows.length };
1113
1118
  }
1114
1119
  }
1115
1120
  async executeStatementQuery(connection, obj) {
@@ -1127,7 +1132,9 @@ var DB2Client = class extends knex.Client {
1127
1132
  statement = cache.get(obj.sql);
1128
1133
  if (statement) {
1129
1134
  usedCache = true;
1130
- this.printDebug(`Using cached statement for: ${obj.sql.substring(0, 50)}...`);
1135
+ this.printDebug(
1136
+ `Using cached statement for: ${obj.sql.substring(0, 50)}...`
1137
+ );
1131
1138
  } else {
1132
1139
  statement = await connection.createStatement();
1133
1140
  await statement.prepare(obj.sql);
@@ -1178,6 +1185,44 @@ var DB2Client = class extends knex.Client {
1178
1185
  const msg = String(error?.message || error || "").toLowerCase();
1179
1186
  return msg.includes("02000") || msg.includes("no data") || msg.includes("no rows") || msg.includes("0 rows");
1180
1187
  }
1188
+ shouldNormalizeBigintValues() {
1189
+ return this.normalizeBigintToString;
1190
+ }
1191
+ maybeNormalizeBigint(value) {
1192
+ if (value === null || value === void 0) {
1193
+ return value;
1194
+ }
1195
+ if (!this.shouldNormalizeBigintValues()) {
1196
+ return value;
1197
+ }
1198
+ return this.normalizeBigintValue(value);
1199
+ }
1200
+ normalizeBigintValue(value, seen = /* @__PURE__ */ new WeakSet()) {
1201
+ if (typeof value === "bigint") {
1202
+ return value.toString();
1203
+ }
1204
+ if (Array.isArray(value)) {
1205
+ for (let i = 0; i < value.length; i++) {
1206
+ value[i] = this.normalizeBigintValue(value[i], seen);
1207
+ }
1208
+ return value;
1209
+ }
1210
+ if (value && typeof value === "object") {
1211
+ if (value instanceof Date || Buffer2.isBuffer(value) || ArrayBuffer.isView(value)) {
1212
+ return value;
1213
+ }
1214
+ const obj = value;
1215
+ if (seen.has(obj)) {
1216
+ return value;
1217
+ }
1218
+ seen.add(obj);
1219
+ for (const key of Object.keys(obj)) {
1220
+ obj[key] = this.normalizeBigintValue(obj[key], seen);
1221
+ }
1222
+ return value;
1223
+ }
1224
+ return value;
1225
+ }
1181
1226
  /**
1182
1227
  * Format statement response from ODBC driver
1183
1228
  * Handles special case for IDENTITY_VAL_LOCAL() function
@@ -1185,16 +1230,18 @@ var DB2Client = class extends knex.Client {
1185
1230
  formatStatementResponse(result) {
1186
1231
  const isIdentityQuery = result.statement?.includes("IDENTITY_VAL_LOCAL()");
1187
1232
  if (isIdentityQuery && result.columns?.length > 0) {
1233
+ const identityRows = result.map(
1234
+ (row) => row[result.columns[0].name]
1235
+ );
1236
+ const normalizedIdentityRows = this.maybeNormalizeBigint(identityRows);
1188
1237
  return {
1189
- rows: result.map(
1190
- (row) => row[result.columns[0].name]
1191
- ),
1238
+ rows: normalizedIdentityRows,
1192
1239
  rowCount: result.count
1193
1240
  };
1194
1241
  }
1195
1242
  const rowCount = typeof result?.count === "number" ? result.count : 0;
1196
1243
  return {
1197
- rows: result,
1244
+ rows: this.maybeNormalizeBigint(result),
1198
1245
  rowCount
1199
1246
  };
1200
1247
  }
@@ -1266,7 +1313,7 @@ var DB2Client = class extends knex.Client {
1266
1313
  return;
1267
1314
  }
1268
1315
  if (!cursor.noData) {
1269
- this.push(result);
1316
+ this.push(parentThis.maybeNormalizeBigint(result));
1270
1317
  } else {
1271
1318
  isClosed = true;
1272
1319
  cursor.close((closeError) => {
@@ -1274,7 +1321,7 @@ var DB2Client = class extends knex.Client {
1274
1321
  parentThis.printError(parentThis.safeStringify(closeError, 2));
1275
1322
  }
1276
1323
  if (result) {
1277
- this.push(result);
1324
+ this.push(parentThis.maybeNormalizeBigint(result));
1278
1325
  }
1279
1326
  this.push(null);
1280
1327
  });
@@ -1338,6 +1385,9 @@ var DB2Client = class extends knex.Client {
1338
1385
  throw wrappedError;
1339
1386
  }
1340
1387
  }
1388
+ if (response) {
1389
+ this.maybeNormalizeBigint(response.rows);
1390
+ }
1341
1391
  const validationResult = this.validateResponse(obj);
1342
1392
  if (validationResult !== null) return validationResult;
1343
1393
  return this.processSqlMethod(obj);