Home > Article > Web Front-end > How to Achieve Multi-Row Inserts with Efficiency Using pg-promise?
Problem Statement:
How to efficiently insert multiple rows into a database using a single INSERT query in pg-promise?
Solution:
The preferred approach in pg-promise is to use the helpers namespace for high performance and flexibility.
<code class="javascript">const {ColumnSet, insert} = pgp.helpers; const cs = new ColumnSet(['col_a', 'col_b'], {table: 'tmp'}); const values = [{col_a: 'a1', col_b: 'b1'}, {col_a: 'a2', col_b: 'b2'}]; const query = insert(values, cs); // => INSERT INTO "tmp"("col_a","col_b") VALUES('a1','b1'),('a2','b2') await db.none(query);</code>
Additional Considerations:
Extras:
The above is the detailed content of How to Achieve Multi-Row Inserts with Efficiency Using pg-promise?. For more information, please follow other related articles on the PHP Chinese website!