zet-lib 3.3.6 → 3.3.7

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 (2) hide show
  1. package/lib/connection.js +3 -21
  2. package/package.json +1 -1
package/lib/connection.js CHANGED
@@ -414,15 +414,12 @@ connection.delete = async (obj) => {
414
414
  }
415
415
 
416
416
  connection.insertData = async(tableName, data) => {
417
- let pgPool;
418
417
  try {
419
418
  // Validasi input
420
419
  if (!data || typeof data !== 'object') {
421
420
  throw new Error('Invalid data provided for insert');
422
421
  }
423
422
 
424
- pgPool = new Pool(configPG);
425
-
426
423
  const columns = Object.keys(data);
427
424
  if (columns.length === 0) {
428
425
  throw new Error('No columns found in the data object');
@@ -435,29 +432,23 @@ connection.insertData = async(tableName, data) => {
435
432
  RETURNING *
436
433
  `;
437
434
  const values = columns.map(col => data[col]);
438
- const result = await pgPool.query(insertQuery, values);
435
+ const result = await pool.query(insertQuery, values);
439
436
  console.log(`Successfully inserted 1 record into ${tableName}`);
440
437
  return result.rows[0];
441
438
  } catch (error) {
442
439
  console.error(`Error inserting record into ${tableName}:`, error);
443
440
  throw error;
444
441
  } finally {
445
- if (pgPool) {
446
- await pgPool.end();
447
- }
448
442
  }
449
443
  }
450
444
 
451
445
  connection.deleteData = async(tableName, where) => {
452
- let pgPool;
453
446
  try {
454
447
  // Validasi input
455
448
  if (!where || typeof where !== 'object') {
456
449
  throw new Error('Invalid where clause provided for delete');
457
450
  }
458
451
 
459
- pgPool = new Pool(configPG);
460
-
461
452
  const whereColumns = Object.keys(where);
462
453
  if (whereColumns.length === 0) {
463
454
  throw new Error('No where conditions provided for delete');
@@ -470,21 +461,17 @@ connection.deleteData = async(tableName, where) => {
470
461
  RETURNING *
471
462
  `;
472
463
  const values = whereColumns.map(col => where[col]);
473
- const result = await pgPool.query(deleteQuery, values);
464
+ const result = await pool.query(deleteQuery, values);
474
465
  console.log(`Successfully deleted ${result.rowCount} records from ${tableName}`);
475
466
  return result.rowCount;
476
467
  } catch (error) {
477
468
  console.error(`Error deleting records from ${tableName}:`, error);
478
469
  throw error;
479
470
  } finally {
480
- if (pgPool) {
481
- await pgPool.end();
482
- }
483
471
  }
484
472
  }
485
473
 
486
474
  connection.insertMultipleRecords = async(tableName,records) => {
487
- let pgPool;
488
475
  try {
489
476
  // Validasi input
490
477
  if (!records || !Array.isArray(records) || records.length === 0) {
@@ -492,8 +479,6 @@ connection.insertMultipleRecords = async(tableName,records) => {
492
479
  return [];
493
480
  }
494
481
 
495
- pgPool = new Pool(configPG);
496
-
497
482
  // Validasi bahwa semua records memiliki struktur yang sama
498
483
  const firstRecord = records[0];
499
484
  if (!firstRecord || typeof firstRecord !== 'object') {
@@ -528,16 +513,13 @@ connection.insertMultipleRecords = async(tableName,records) => {
528
513
  RETURNING *
529
514
  `;
530
515
  const values = records.flatMap(record => columns.map(col => record[col]));
531
- const result = await pgPool.query(insertQuery, values);
516
+ const result = await pool.query(insertQuery, values);
532
517
  console.log(`Successfully inserted ${records.length} records into ${tableName}`);
533
518
  return result.rows;
534
519
  } catch (error) {
535
520
  console.error(`Error inserting multiple records into ${tableName}:`, error);
536
521
  throw error;
537
522
  } finally {
538
- if (pgPool) {
539
- await pgPool.end();
540
- }
541
523
  }
542
524
  }
543
525
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "3.3.6",
3
+ "version": "3.3.7",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"