zet-lib 1.5.13 → 1.5.14

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 +103 -3
  2. package/package.json +1 -1
package/lib/connection.js CHANGED
@@ -324,7 +324,7 @@ connection.delete = async (obj) => {
324
324
  }
325
325
  wherequery = arr.length ? wherequery.join(' AND ') : ''
326
326
  const wheres = arr.length ? ' WHERE ' + wherequery : ''
327
- const sql = `DELETE FROM "${table}" ${wheres}`
327
+ const sql = `DELETE FROM "${table}" ${wheres} RETURNING *`
328
328
  /*console.log(sql);
329
329
  console.log(arr)*/
330
330
  try {
@@ -337,14 +337,114 @@ connection.delete = async (obj) => {
337
337
  }
338
338
  }
339
339
 
340
+ connection.insertData = async(tableName, data) => {
341
+ let pgPool;
342
+ try {
343
+ // Validasi input
344
+ if (!data || typeof data !== 'object') {
345
+ throw new Error('Invalid data provided for insert');
346
+ }
347
+
348
+ pgPool = new Pool(configPG);
349
+
350
+ const columns = Object.keys(data);
351
+ if (columns.length === 0) {
352
+ throw new Error('No columns found in the data object');
353
+ }
354
+
355
+ const placeholders = columns.map((_, index) => `$${index + 1}`).join(',');
356
+ const insertQuery = `
357
+ INSERT INTO "${tableName}" (${columns.map(col => `"${col}"`).join(',')})
358
+ VALUES (${placeholders})
359
+ RETURNING *
360
+ `;
361
+ const values = columns.map(col => data[col]);
362
+ const result = await pgPool.query(insertQuery, values);
363
+ console.log(`Successfully inserted 1 record into ${tableName}`);
364
+ return result.rows[0];
365
+ } catch (error) {
366
+ console.error(`Error inserting record into ${tableName}:`, error);
367
+ throw error;
368
+ } finally {
369
+ if (pgPool) {
370
+ await pgPool.end();
371
+ }
372
+ }
373
+ }
374
+
375
+ connection.deleteData = async(tableName, where) => {
376
+ let pgPool;
377
+ try {
378
+ // Validasi input
379
+ if (!where || typeof where !== 'object') {
380
+ throw new Error('Invalid where clause provided for delete');
381
+ }
382
+
383
+ pgPool = new Pool(configPG);
384
+
385
+ const whereColumns = Object.keys(where);
386
+ if (whereColumns.length === 0) {
387
+ throw new Error('No where conditions provided for delete');
388
+ }
389
+
390
+ const whereConditions = whereColumns.map((col, index) => `"${col}" = $${index + 1}`).join(' AND ');
391
+ const deleteQuery = `
392
+ DELETE FROM "${tableName}"
393
+ WHERE ${whereConditions}
394
+ RETURNING *
395
+ `;
396
+ const values = whereColumns.map(col => where[col]);
397
+ const result = await pgPool.query(deleteQuery, values);
398
+ console.log(`Successfully deleted ${result.rowCount} records from ${tableName}`);
399
+ return result.rowCount;
400
+ } catch (error) {
401
+ console.error(`Error deleting records from ${tableName}:`, error);
402
+ throw error;
403
+ } finally {
404
+ if (pgPool) {
405
+ await pgPool.end();
406
+ }
407
+ }
408
+ }
409
+
340
410
  connection.insertMultipleRecords = async(tableName,records) => {
341
411
  let pgPool;
342
412
  try {
413
+ // Validasi input
414
+ if (!records || !Array.isArray(records) || records.length === 0) {
415
+ console.warn(`No records to insert into ${tableName}`);
416
+ return [];
417
+ }
418
+
343
419
  pgPool = new Pool(configPG);
344
- const columns = Object.keys(records[0]);
420
+
421
+ // Validasi bahwa semua records memiliki struktur yang sama
422
+ const firstRecord = records[0];
423
+ if (!firstRecord || typeof firstRecord !== 'object') {
424
+ throw new Error('First record is invalid or empty');
425
+ }
426
+
427
+ const columns = Object.keys(firstRecord);
428
+ if (columns.length === 0) {
429
+ throw new Error('No columns found in the first record');
430
+ }
431
+
432
+ // Validasi bahwa semua records memiliki kolom yang sama
433
+ for (let i = 1; i < records.length; i++) {
434
+ const record = records[i];
435
+ if (!record || typeof record !== 'object') {
436
+ throw new Error(`Record at index ${i} is invalid`);
437
+ }
438
+ const recordColumns = Object.keys(record);
439
+ if (recordColumns.length !== columns.length || !recordColumns.every(col => columns.includes(col))) {
440
+ throw new Error(`Record at index ${i} has different structure than the first record`);
441
+ }
442
+ }
443
+
345
444
  const placeholders = records.map((_, rowIndex) =>
346
445
  `(${columns.map((_, colIndex) => `$${rowIndex * columns.length + colIndex + 1}`).join(',')})`
347
446
  ).join(',');
447
+
348
448
  // Create the INSERT query
349
449
  const insertQuery = `
350
450
  INSERT INTO "${tableName}" (${columns.map(col => `"${col}"`).join(',')})
@@ -353,7 +453,7 @@ connection.insertMultipleRecords = async(tableName,records) => {
353
453
  `;
354
454
  const values = records.flatMap(record => columns.map(col => record[col]));
355
455
  const result = await pgPool.query(insertQuery, values);
356
- //console.log(`Successfully inserted ${records.length} records into ${tableName}`);
456
+ console.log(`Successfully inserted ${records.length} records into ${tableName}`);
357
457
  return result.rows;
358
458
  } catch (error) {
359
459
  console.error(`Error inserting multiple records into ${tableName}:`, error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.5.13",
3
+ "version": "1.5.14",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"