zet-lib 1.5.4 → 1.5.5

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 +28 -0
  2. package/package.json +1 -1
package/lib/connection.js CHANGED
@@ -318,6 +318,34 @@ connection.delete = async (obj) => {
318
318
  }
319
319
  }
320
320
 
321
+ connection.insertMultipleRecords = async(tableName,records) => {
322
+ let pgPool;
323
+ try {
324
+ pgPool = new Pool(configPG);
325
+ const columns = Object.keys(records[0]);
326
+ const placeholders = records.map((_, rowIndex) =>
327
+ `(${columns.map((_, colIndex) => `$${rowIndex * columns.length + colIndex + 1}`).join(',')})`
328
+ ).join(',');
329
+ // Create the INSERT query
330
+ const insertQuery = `
331
+ INSERT INTO "${tableName}" (${columns.map(col => `"${col}"`).join(',')})
332
+ VALUES ${placeholders}
333
+ RETURNING *
334
+ `;
335
+ const values = records.flatMap(record => columns.map(col => record[col]));
336
+ const result = await pgPool.query(insertQuery, values);
337
+ //console.log(`Successfully inserted ${records.length} records into ${tableName}`);
338
+ return result.rows;
339
+ } catch (error) {
340
+ console.error(`Error inserting multiple records into ${tableName}:`, error);
341
+ throw error;
342
+ } finally {
343
+ if (pgPool) {
344
+ await pgPool.end();
345
+ }
346
+ }
347
+ }
348
+
321
349
  connection.driver = config.driver
322
350
  connection.showTables = "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema'"
323
351
  connection.showFullFields = (tableRelations) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.5.4",
3
+ "version": "1.5.5",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"