zet-lib 3.3.5 → 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.
- package/lib/connection.js +3 -21
- package/lib/zRoute.js +45 -5
- 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
|
|
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
|
|
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
|
|
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/lib/zRoute.js
CHANGED
|
@@ -205,12 +205,52 @@ zRoute.post = (req, res, MYMODEL, routeName, body) => {
|
|
|
205
205
|
|
|
206
206
|
case "dragdrop":
|
|
207
207
|
let cleaning2;
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
208
|
+
let dragdropValue = post[routeName] && post[routeName][key] !== undefined ? post[routeName][key] : null;
|
|
209
|
+
|
|
210
|
+
// Helper function to convert object with numeric keys to array
|
|
211
|
+
const convertToArray = (value) => {
|
|
212
|
+
if (Array.isArray(value)) {
|
|
213
|
+
return value;
|
|
214
|
+
}
|
|
215
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
216
|
+
// Check if it's an object with numeric keys (like {"0":"111","1":"138"})
|
|
217
|
+
const keys = Object.keys(value);
|
|
218
|
+
const numericKeys = keys.filter(k => /^\d+$/.test(k));
|
|
219
|
+
if (numericKeys.length === keys.length && numericKeys.length > 0) {
|
|
220
|
+
// Convert object with numeric keys to array
|
|
221
|
+
return numericKeys.map(k => value[k]).filter(item => item !== null && item !== undefined && item !== '');
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return null;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
// Handle case where value might be string (JSON) or already an array
|
|
228
|
+
if (dragdropValue === null || dragdropValue === undefined) {
|
|
229
|
+
cleaning2 = null;
|
|
230
|
+
} else if (typeof dragdropValue === 'string') {
|
|
231
|
+
try {
|
|
232
|
+
dragdropValue = JSON.parse(dragdropValue);
|
|
233
|
+
let arr = convertToArray(dragdropValue);
|
|
234
|
+
if (arr) {
|
|
235
|
+
cleaning2 = arr.filter((item) => item !== null && item !== undefined && item !== '');
|
|
236
|
+
} else {
|
|
237
|
+
cleaning2 = dragdropValue ? [dragdropValue] : null;
|
|
238
|
+
}
|
|
239
|
+
} catch (e) {
|
|
240
|
+
// If not valid JSON, treat as single value array
|
|
241
|
+
cleaning2 = dragdropValue ? [dragdropValue] : null;
|
|
242
|
+
}
|
|
243
|
+
} else if (Array.isArray(dragdropValue)) {
|
|
244
|
+
cleaning2 = dragdropValue.filter((item) => item !== null && item !== undefined && item !== '');
|
|
245
|
+
} else if (dragdropValue && typeof dragdropValue === 'object') {
|
|
246
|
+
// Handle object with numeric keys (converted by qs.parse for large arrays)
|
|
247
|
+
let arr = convertToArray(dragdropValue);
|
|
248
|
+
cleaning2 = arr ? arr.filter((item) => item !== null && item !== undefined && item !== '') : null;
|
|
249
|
+
} else {
|
|
250
|
+
// Single value, convert to array
|
|
251
|
+
cleaning2 = dragdropValue ? [dragdropValue] : null;
|
|
212
252
|
}
|
|
213
|
-
post[routeName][key] = cleaning2 ? JSON.stringify(cleaning2) : null;
|
|
253
|
+
post[routeName][key] = cleaning2 && cleaning2.length > 0 ? JSON.stringify(cleaning2) : null;
|
|
214
254
|
break;
|
|
215
255
|
|
|
216
256
|
case "lexical":
|