MCPcopy Create free account
hub / github.com/denodrivers/postgres / insertRow

Method insertRow

query/query.ts:307–375  ·  view source on GitHub ↗

* Insert a row into the result

(row_data: Uint8Array[], controls?: ClientControls)

Source from the content-addressed store, hash-verified

305 * Insert a row into the result
306 */
307 insertRow(row_data: Uint8Array[], controls?: ClientControls) {
308 if (!this.rowDescription) {
309 throw new Error(
310 "The row description required to parse the result data wasn't initialized",
311 );
312 }
313
314 // This will only run on the first iteration after row descriptions have been set
315 if (!this.columns) {
316 if (this.query.fields) {
317 if (this.rowDescription.columns.length !== this.query.fields.length) {
318 throw new RangeError(
319 "The fields provided for the query don't match the ones returned as a result " +
320 `(${this.rowDescription.columns.length} expected, ${this.query.fields.length} received)`,
321 );
322 }
323
324 this.columns = this.query.fields;
325 } else {
326 let column_names: string[];
327 if (this.query.camelCase) {
328 column_names = this.rowDescription.columns.map((column) =>
329 snakecaseToCamelcase(column.name)
330 );
331 } else {
332 column_names = this.rowDescription.columns.map(
333 (column) => column.name,
334 );
335 }
336
337 // Check field names returned by the database are not duplicated
338 const duplicates = findDuplicatesInArray(column_names);
339 if (duplicates.length) {
340 throw new Error(
341 `Field names ${
342 duplicates
343 .map((str) => `"${str}"`)
344 .join(", ")
345 } are duplicated in the result of the query`,
346 );
347 }
348
349 this.columns = column_names;
350 }
351 }
352
353 // It's safe to assert columns as defined from now on
354 const columns = this.columns!;
355
356 if (columns.length !== row_data.length) {
357 throw new RangeError(
358 "The result fields returned by the database don't match the defined structure of the result",
359 );
360 }
361
362 const row = row_data.reduce((row, raw_value, index) => {
363 const current_column = this.rowDescription!.columns[index];
364

Callers

nothing calls this directly

Calls 5

decodeFunction · 0.90
snakecaseToCamelcaseFunction · 0.85
findDuplicatesInArrayFunction · 0.85
joinMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected