Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

feat(pieces): add Insert Multiple Rows to Postgres, MySQL and SQL Server (#15946)

Kishan Parmarced084ded5c2
diff --git a/bun.lock b/bun.lock
index ce07ab442ba..e31cb132040 100644
--- a/bun.lock
+++ b/bun.lock
@@ -3954,7 +3954,7 @@
},
"packages/pieces/community/google-slides": {
"name": "@activepieces/piece-google-slides",
- "version": "0.2.0",
+ "version": "0.3.0",
"dependencies": {
"@activepieces/core-piece-types": "workspace:*",
"@activepieces/core-utils": "workspace:*",
@@ -5886,7 +5886,7 @@
},
"packages/pieces/community/microsoft-sql-server": {
"name": "@activepieces/piece-microsoft-sql-server",
- "version": "0.0.3",
+ "version": "0.1.0",
"dependencies": {
"@activepieces/core-piece-types": "workspace:*",
"@activepieces/core-utils": "workspace:*",
@@ -6233,7 +6233,7 @@
},
"packages/pieces/community/mysql": {
"name": "@activepieces/piece-mysql",
- "version": "0.2.7",
+ "version": "0.3.0",
"dependencies": {
"@activepieces/core-piece-types": "workspace:*",
"@activepieces/core-utils": "workspace:*",
@@ -7109,7 +7109,7 @@
},
"packages/pieces/community/postgres": {
"name": "@activepieces/piece-postgres",
- "version": "0.3.1",
+ "version": "0.4.0",
"dependencies": {
"@activepieces/core-piece-types": "workspace:*",
"@activepieces/core-utils": "workspace:*",
@@ -8386,7 +8386,7 @@
},
"packages/pieces/community/slack": {
"name": "@activepieces/piece-slack",
- "version": "0.20.0",
+ "version": "0.21.0",
"dependencies": {
"@activepieces/core-piece-types": "workspace:*",
"@activepieces/core-utils": "workspace:*",
diff --git a/packages/pieces/community/microsoft-sql-server/package.json b/packages/pieces/community/microsoft-sql-server/package.json
index 6f8cc6e3990..4807527ba87 100644
--- a/packages/pieces/community/microsoft-sql-server/package.json
+++ b/packages/pieces/community/microsoft-sql-server/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/piece-microsoft-sql-server",
- "version": "0.0.3",
+ "version": "0.1.0",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"dependencies": {
diff --git a/packages/pieces/community/microsoft-sql-server/src/index.ts b/packages/pieces/community/microsoft-sql-server/src/index.ts
index e9745bbeb79..d88019ec8fa 100644
--- a/packages/pieces/community/microsoft-sql-server/src/index.ts
+++ b/packages/pieces/community/microsoft-sql-server/src/index.ts
@@ -2,6 +2,7 @@ import { PieceCategory, createPiece } from '@activepieces/pieces-framework';
import { mssqlAuth } from './lib/auth';
import { findRowsAction } from './lib/actions/find-rows';
import { insertRowAction } from './lib/actions/insert-row';
+import { insertRowsAction } from './lib/actions/insert-rows';
import { updateRowsAction } from './lib/actions/update-rows';
import { deleteRowsAction } from './lib/actions/delete-rows';
import { getTablesAction } from './lib/actions/get-tables';
@@ -20,6 +21,7 @@ export const mssql = createPiece({
actions: [
findRowsAction,
insertRowAction,
+ insertRowsAction,
updateRowsAction,
deleteRowsAction,
getTablesAction,
diff --git a/packages/pieces/community/microsoft-sql-server/src/lib/actions/insert-rows.ts b/packages/pieces/community/microsoft-sql-server/src/lib/actions/insert-rows.ts
new file mode 100644
index 00000000000..abf33f9a685
--- /dev/null
+++ b/packages/pieces/community/microsoft-sql-server/src/lib/actions/insert-rows.ts
@@ -0,0 +1,318 @@
+import { createAction, InputPropertyMap, Property } from '@activepieces/pieces-framework';
+import sql from 'mssql';
+import { mssqlAuth } from '../auth';
+import { mssqlCommon, MssqlColumnDetail } from '../common';
+import { cursorUtils } from '../common/cursor';
+import { mssqlProps } from '../common/props';
+import { insertRowsActionOutputSchema } from '../output-schemas';
+
+export const insertRowsAction = createAction({
+ auth: mssqlAuth,
+ name: 'insert_rows',
+ classification: 'WRITE',
+ displayName: 'Insert Multiple Rows',
+ description: 'Inserts many rows into a table in a single transaction',
+ audience: 'both',
+ aiMetadata: {
+ description:
+ 'Inserts an array of rows into a SQL Server table, each row a map of column names to values, using multi-row INSERT statements inside one transaction: if any row fails, none are inserted. Columns a row omits get the column default. Returns the inserted row count and, when Return Inserted Rows is on, the stored rows including IDENTITY ids. Use instead of repeated single inserts for bulk loads. Not idempotent: each call appends new rows, so repeating it adds duplicates or fails on a unique-key collision.',
+ idempotent: false,
+ },
+ props: {
+ table: mssqlProps.table(),
+ batch: Property.DynamicProperties({
+ auth: mssqlAuth,
+ displayName: 'Rows',
+ required: true,
+ refreshers: ['table'],
+ props: async ({ auth, table }): Promise<InputPropertyMap> => {
+ const target = mssqlProps.asTable(table);
+ if (!auth || target === null) {
+ return {};
+ }
+ const pool = await mssqlCommon.connect({ auth });
+ try {
+ const columns = await mssqlCommon.getColumnDetails({ pool, table: target });
+ return {
+ rows: Property.Array({
+ displayName: 'Rows',
+ description:
+ 'One item per row. Leave a field empty to use the column default.',
+ required: true,
+ properties: Object.fromEntries(
+ columns.map((column) => [
+ column.name,
+ Property.ShortText({
+ displayName: column.name,
+ description: describeColumn({ column }),
+ required: isRequiredColumn({ column }),
+ }),
+ ])
+ ),
+ }),
+ };
+ } finally {
+ await pool.close().catch(() => undefined);
+ }
+ },
+ }),
+ keep_empty_strings: Property.Checkbox({
+ displayName: 'Keep Empty Strings',
+ description:
+ 'Insert empty fields as empty strings instead of the column default.',
+ required: false,
+ defaultValue: false,
+ }),
+ return_rows: Property.Checkbox({
+ displayName: 'Return Inserted Rows',
+ description:
+ 'Return the inserted rows, including identity ids. Fails on tables with triggers.',
+ required: false,
+ defaultValue: false,
+ }),
+ },
+ outputSchema: insertRowsActionOutputSchema,
+ async run(context) {
+ const { table, batch, keep_empty_strings, return_rows } = context.propsValue;
+ const records = insertRowsUtils.toRecords({
+ rows: batch?.['rows'],
+ keepEmptyStrings: keep_empty_strings === true,
+ });
+ const columns = insertRowsUtils.collectColumns({ records });
+ const target = mssqlCommon.quoteTable(table);
+
+ const pool = await mssqlCommon.connect({ auth: context.auth });
+ pool.on('error', () => undefined);
+ try {
+ const output =
+ return_rows === true
+ ? `OUTPUT ${cursorUtils.exactProjection({
+ columns: (await mssqlCommon.getTableMeta({ pool, table })).columns,
+ prefix: 'INSERTED',
+ })}`
+ : '';
+ const statements = insertRowsUtils.buildStatements({
+ quotedTable: target,
+ columns,
+ records,
+ output,
+ });
+ const results = await runInTransaction({ pool, statements, target });
+ const rowCount = results.reduce(
+ (total, result) => total + sumOf({ counts: result.rowsAffected }),
+ 0
+ );
+ return return_rows === true
+ ? { rows: results.flatMap((result) => result.recordset ?? []), row_count: rowCount }
+ : { row_count: rowCount };
+ } finally {
+ await pool.close().catch(() => undefined);
+ }
+ },
+});
+
+async function runInTransaction({
+ pool,
+ statements,
+ target,
+}: {
+ pool: sql.ConnectionPool;
+ statements: InsertStatement[];
+ target: string;
+}): Promise<sql.IResult<Record<string, unknown>>[]> {
+ const transaction = new sql.Transaction(pool);
+ await transaction.begin();
+ try {
+ const results: sql.IResult<Record<string, unknown>>[] = [];
+ for (const statement of statements) {
+ const request = transaction.request();
+ statement.values.forEach((value, index) =>
+ request.input(`p${index}`, value)
+ );
+ results.push(await request.query<Record<string, unknown>>(statement.text));
+ }
+ await transaction.commit();
+ return results;
+ } catch (error) {
+ await transaction.rollback().catch(() => undefined);
+ if (mssqlCommon.isOutputBlockedByTrigger(error)) {
+ throw new Error(
+ `${target} has enabled triggers, so SQL Server refuses to return the inserted rows. Turn off Return Inserted Rows and the batch will insert normally.`
+ );
+ }
+ const reason = error instanceof Error ? error.message : String(error);
+ throw new Error(
+ `Insert failed and the whole batch was rolled back: ${reason}`
+ );
+ }
+}
+
+function sumOf({ counts }: { counts: number[] | undefined }): number {
+ return (counts ?? []).reduce((total, count) => total + count, 0);
+}
+
+function isRequiredColumn({ column }: { column: MssqlColumnDetail }): boolean {
+ const isFilledByDatabase =
+ column.hasDefault ||
+ column.isIdentity ||
+ column.isComputed ||
+ column.typeName === 'timestamp';
+ return !column.isNullable && !isFilledByDatabase;
+}
+
+function describeColumn({ column }: { column: MssqlColumnDetail }): string {
+ return isRequiredColumn({ column })
+ ? `${column.typeName} · required`
+ : column.typeName;
+}
+
+function toRecords({
+ rows,
+ keepEmptyStrings,
+}: {
+ rows: unknown;
+ keepEmptyStrings: boolean;
+}): Record<string, unknown>[] {
+ if (!Array.isArray(rows) || rows.length === 0) {
+ throw new Error('Rows must be a non-empty array of objects.');
+ }
+ return rows.map((row, index) => {
+ if (typeof row !== 'object' || row === null || Array.isArray(row)) {
+ throw new Error(
+ `Row ${index + 1} must be an object of column names mapped to values.`
+ );
+ }
+ return Object.fromEntries(
+ Object.entries(row).filter(
+ ([, value]) => value !== undefined && (keepEmptyStrings || value !== '')
+ )
+ );
+ });
+}
+
+function collectColumns({
+ records,
+}: {
+ records: Record<string, unknown>[];
+}): string[] {
+ const columns = [
+ ...new Set(records.flatMap((record) => Object.keys(record))),
+ ];
+ if (columns.length === 0) {
+ throw new Error('Rows must contain at least one column.');
+ }
+ if (columns.length > MAX_PARAMETERS) {
+ throw new Error(
+ `Rows use ${columns.length} columns, but SQL Server allows at most ${MAX_PARAMETERS} parameters per statement.`
+ );
+ }
+ return columns;
+}
+
+function toBindValue(value: unknown): unknown {
+ const isStructured =
+ typeof value === 'object' &&
+ value !== null &&
+ !(value instanceof Date) &&
+ !Buffer.isBuffer(value);
+ return isStructured ? JSON.stringify(value) : value;
+}
+
+function buildStatements({
+ quotedTable,
+ columns,
+ records,
+ output,
+}: {
+ quotedTable: string;
+ columns: string[];
+ records: Record<string, unknown>[];
+ output: string;
+}): InsertStatement[] {
+ const rowsPerStatement = Math.min(
+ MAX_ROWS_PER_STATEMENT,
+ Math.floor(MAX_PARAMETERS / columns.length)
+ );
+ const quotedColumns = columns
+ .map((column) => mssqlCommon.quoteId(column))
+ .join(', ');
+
+ return splitIntoBatches({ records, maxRows: rowsPerStatement }).map((batch) => {
+ const values: unknown[] = [];
+ const tuples = batch.map((record) => {
+ const placeholders = columns.map((column) => {
+ const value = record[column];
+ if (value === undefined) {
+ return 'DEFAULT';
+ }
+ values.push(toBindValue(value) ?? null);
+ return `@p${values.length - 1}`;
+ });
+ return `(${placeholders.join(', ')})`;
+ });
+ return {
+ text: `INSERT INTO ${quotedTable} (${quotedColumns}) ${output} VALUES ${tuples.join(', ')}`,
+ values,
+ };
+ });
+}
+
+function splitIntoBatches({
+ records,
+ maxRows,
+}: {
+ records: Record<string, unknown>[];
+ maxRows: number;
+}): Record<string, unknown>[][] {
+ const batches: Record<string, unknown>[][] = [];
+ let current: Record<string, unknown>[] = [];
+ let currentBytes = 0;
+ for (const record of records) {
+ const bytes = estimateBytes({ record });
+ const isFull =
+ current.length >= maxRows || currentBytes + bytes > MAX_STATEMENT_BYTES;
+ if (current.length > 0 && isFull) {
+ batches.push(current);
+ current = [];
+ currentBytes = 0;
+ }
+ current.push(record);
+ currentBytes += bytes;
+ }
+ return current.length > 0 ? [...batches, current] : batches;
+}
+
+function estimateBytes({ record }: { record: Record<string, unknown> }): number {
+ return Object.values(record).reduce<number>(
+ (total, value) => total + sizeOf({ value }),
+ 0
+ );
+}
+
+function sizeOf({ value }: { value: unknown }): number {
+ if (typeof value === 'string') {
+ return Buffer.byteLength(value);
+ }
+ if (Buffer.isBuffer(value)) {
+ return value.length;
+ }
+ if (typeof value === 'object' && value !== null && !(value instanceof Date)) {
+ return Buffer.byteLength(JSON.stringify(value));
+ }
+ return 16;
+}
+
+const MAX_PARAMETERS = 2098;
+const MAX_ROWS_PER_STATEMENT = 1000;
+const MAX_STATEMENT_BYTES = 1024 * 1024;
+
+export const insertRowsUtils = {
+ toRecords,
+ collectColumns,
+ buildStatements,
+};
+
+export type InsertStatement = {
+ text: string;
+ values: unknown[];
+};
diff --git a/packages/pieces/community/microsoft-sql-server/src/lib/common/index.ts b/packages/pieces/community/microsoft-sql-server/src/lib/common/index.ts
index 2fad7f1ce3c..7b940048a30 100644
--- a/packages/pieces/community/microsoft-sql-server/src/lib/common/index.ts
+++ b/packages/pieces/community/microsoft-sql-server/src/lib/common/index.ts
@@ -276,6 +276,35 @@ async function getTableMeta({
};
}
+async function getColumnDetails({
+ pool,
+ table,
+}: {
+ pool: sql.ConnectionPool;
+ table: MssqlTable;
+}): Promise<MssqlColumnDetail[]> {
+ const result = await pool
+ .request()
+ .input('schema', table.table_schema)
+ .input('name', table.table_name)
+ .query<Record<string, unknown>>(
+ `SELECT c.name, TYPE_NAME(c.system_type_id) AS type_name, c.is_nullable,
+ c.is_identity, c.is_computed,
+ CASE WHEN c.default_object_id <> 0 THEN 1 ELSE 0 END AS has_default
+ FROM sys.columns c
+ WHERE c.object_id = OBJECT_ID(QUOTENAME(@schema) + '.' + QUOTENAME(@name))
+ ORDER BY c.column_id`
+ );
+ return result.recordset.map((row) => ({
+ name: String(row['name']),
+ typeName: String(row['type_name']).toLowerCase(),
+ isNullable: Boolean(row['is_nullable']),
+ isIdentity: Boolean(row['is_identity']),
+ isComputed: Boolean(row['is_computed']),
+ hasDefault: Boolean(row['has_default']),
+ }));
+}
+
function bindParameters({
request,
parameters,
@@ -320,6 +349,7 @@ export const mssqlCommon = {
connect,
getTables,
getTableMeta,
+ getColumnDetails,
bindParameters,
writeReturningRows,
};
@@ -346,3 +376,12 @@ export type MssqlTableMeta = {
identity?: string;
keyColumns: string[];
};
+
+export type MssqlColumnDetail = {
+ name: string;
+ typeName: string;
+ isNullable: boolean;
+ isIdentity: boolean;
+ isComputed: boolean;
+ hasDefault: boolean;
+};
diff --git a/packages/pieces/community/microsoft-sql-server/src/lib/common/props.ts b/packages/pieces/community/microsoft-sql-server/src/lib/common/props.ts
index 895317cf743..3750e952e9f 100644
--- a/packages/pieces/community/microsoft-sql-server/src/lib/common/props.ts
+++ b/packages/pieces/community/microsoft-sql-server/src/lib/common/props.ts
@@ -111,4 +111,5 @@ export const warningMarkdown = Property.MarkDown({
export const mssqlProps = {
table,
column,
+ asTable,
};
diff --git a/packages/pieces/community/microsoft-sql-server/src/lib/output-schemas.ts b/packages/pieces/community/microsoft-sql-server/src/lib/output-schemas.ts
index f76d50ae8e2..d26f2d2b8b9 100644
--- a/packages/pieces/community/microsoft-sql-server/src/lib/output-schemas.ts
+++ b/packages/pieces/community/microsoft-sql-server/src/lib/output-schemas.ts
@@ -52,6 +52,18 @@ export const insertRowActionOutputSchema: OutputSchema = {
],
};
+export const insertRowsActionOutputSchema: OutputSchema = {
+ fields: [
+ {
+ key: 'rows',
+ label: 'Inserted Rows',
+ description:
+ 'The inserted rows as stored by the database, including IDENTITY ids and defaults; each row\'s fields are the columns of the target table. Only present when Return Inserted Rows is enabled.',
+ },
+ { key: 'row_count', label: 'Inserted Row Count', format: 'number' },
+ ],
+};
+
export const newOrUpdatedRowTriggerOutputSchema: OutputSchema = {
fields: [
{
diff --git a/packages/pieces/community/mysql/package.json b/packages/pieces/community/mysql/package.json
index 06a2431c388..bf14ab4ea89 100644
--- a/packages/pieces/community/mysql/package.json
+++ b/packages/pieces/community/mysql/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/piece-mysql",
- "version": "0.2.7",
+ "version": "0.3.0",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"dependencies": {
diff --git a/packages/pieces/community/mysql/src/lib/actions/index.ts b/packages/pieces/community/mysql/src/lib/actions/index.ts
index eef26ac9bbc..e467b8df0fc 100644
--- a/packages/pieces/community/mysql/src/lib/actions/index.ts
+++ b/packages/pieces/community/mysql/src/lib/actions/index.ts
@@ -1,6 +1,7 @@
import executeQuery from './execute-query';
import getTables from './get-tables';
import insertRow from './insert-row';
+import { insertRows } from './insert-rows';
import updateRows from './update-row';
import deleteRows from './delete-row';
import selectRows from './find-rows';
@@ -8,6 +9,7 @@ import selectRows from './find-rows';
export default [
selectRows,
insertRow,
+ insertRows,
updateRows,
deleteRows,
getTables,
diff --git a/packages/pieces/community/mysql/src/lib/actions/insert-rows.ts b/packages/pieces/community/mysql/src/lib/actions/insert-rows.ts
new file mode 100644
index 00000000000..65d52dae96a
--- /dev/null
+++ b/packages/pieces/community/mysql/src/lib/actions/insert-rows.ts
@@ -0,0 +1,242 @@
+import { createAction, InputPropertyMap, Property } from '@activepieces/pieces-framework';
+import { Connection } from 'promise-mysql';
+import { mysqlAuth } from '../..';
+import { MysqlColumn, mysqlCommon, mysqlConnect, mysqlGetColumns, sanitizeColumnName } from '../common';
+import { insertRowsOutputSchema } from '../output-schemas';
+
+export const insertRows = createAction({
+ auth: mysqlAuth,
+ name: 'insert_rows',
+ classification: 'WRITE',
+ displayName: 'Insert Multiple Rows',
+ description: 'Inserts many rows into a table in a single transaction',
+ audience: 'both',
+ outputSchema: insertRowsOutputSchema,
+ aiMetadata: {
+ description: 'Inserts an array of rows into a MySQL table, each row a map of column names to values, using multi-row INSERT statements inside one transaction: if any row fails, none are inserted (InnoDB tables only). Columns a row omits get the column default. Returns the inserted row count and the auto-increment id of the first inserted row. Use instead of repeated single inserts for bulk loads. Not idempotent: each call appends new rows, so repeating it adds duplicates or errors on a unique-key collision.',
+ idempotent: false,
+ },
+ props: {
+ timezone: mysqlCommon.timezone,
+ table: mysqlCommon.table(),
+ batch: Property.DynamicProperties({
+ auth: mysqlAuth,
+ displayName: 'Rows',
+ required: true,
+ refreshers: ['table'],
+ props: async ({ auth, table }): Promise<InputPropertyMap> => {
+ if (!auth || typeof table !== 'string' || table === '') {
+ return {};
+ }
+ const conn = await mysqlConnect(auth, {});
+ try {
+ const columns = await mysqlGetColumns({ conn, table });
+ return {
+ rows: Property.Array({
+ displayName: 'Rows',
+ description: 'One item per row. Leave a field empty to use the column default.',
+ required: true,
+ properties: Object.fromEntries(
+ columns.map((column) => [
+ column.column_name,
+ Property.ShortText({
+ displayName: column.column_name,
+ description: describeColumn({ column }),
+ required: isRequiredColumn({ column }),
+ }),
+ ]),
+ ),
+ }),
+ };
+ } finally {
+ await conn.end();
+ }
+ },
+ }),
+ keep_empty_strings: Property.Checkbox({
+ displayName: 'Keep Empty Strings',
+ description: 'Insert empty fields as empty strings instead of the column default.',
+ required: false,
+ defaultValue: false,
+ }),
+ },
+ async run(context) {
+ const { table, batch, keep_empty_strings } = context.propsValue;
+ const records = insertRowsUtils.toRecords({ rows: batch?.['rows'], keepEmptyStrings: keep_empty_strings === true });
+ const columns = insertRowsUtils.collectColumns({ records });
+ const statements = insertRowsUtils.buildStatements({
+ quotedTable: sanitizeColumnName(table),
+ columns,
+ records,
+ });
+
+ const conn = await mysqlConnect(context.auth, context.propsValue);
+ conn.on('error', () => undefined);
+ try {
+ await assertTransactionalTable({ conn, table });
+ return await insertInTransaction({ conn, statements });
+ } finally {
+ await conn.end().catch(() => conn.destroy());
+ }
+ },
+});
+
+async function assertTransactionalTable({ conn, table }: { conn: Connection; table: string | undefined }): Promise<void> {
+ const rows: Record<string, unknown>[] = await conn.query(
+ 'SELECT t.engine AS engine, e.transactions AS transactions FROM information_schema.tables t JOIN information_schema.engines e ON e.engine = t.engine WHERE t.table_schema = DATABASE() AND t.table_name = ?',
+ [table]
+ );
+ const [row] = rows;
+ if (row !== undefined && String(row['transactions']).toUpperCase() === 'NO') {
+ throw new Error(`Table "${table}" uses the ${String(row['engine'])} storage engine, which does not support transactions, so a failed batch could leave some rows inserted. Use an InnoDB table.`);
+ }
+}
+
+async function insertInTransaction({ conn, statements }: { conn: Connection; statements: InsertStatement[] }) {
+ try {
+ await conn.beginTransaction();
+ const results: MysqlWriteResult[] = [];
+ for (const statement of statements) {
+ results.push(await conn.query(statement.text, statement.values));
+ }
+ await conn.commit();
+ return {
+ affectedRows: results.reduce((total, result) => total + result.affectedRows, 0),
+ firstInsertId: results[0]?.insertId ?? 0,
+ };
+ } catch (error) {
+ await conn.rollback().catch(() => undefined);
+ const reason = error instanceof Error ? error.message : String(error);
+ throw new Error(`Insert failed and the whole batch was rolled back: ${reason}`);
+ }
+}
+
+function isRequiredColumn({ column }: { column: MysqlColumn }): boolean {
+ const isFilledByDatabase =
+ column.column_default !== null ||
+ column.extra.includes('auto_increment') ||
+ /(VIRTUAL|STORED) GENERATED/.test(column.extra);
+ return column.is_nullable === 'NO' && !isFilledByDatabase;
+}
+
+function describeColumn({ column }: { column: MysqlColumn }): string {
+ return isRequiredColumn({ column }) ? `${column.data_type} · required` : column.data_type;
+}
+
+function toRecords({ rows, keepEmptyStrings }: { rows: unknown; keepEmptyStrings: boolean }): Record<string, unknown>[] {
+ if (!Array.isArray(rows) || rows.length === 0) {
+ throw new Error('Rows must be a non-empty array of objects.');
+ }
+ return rows.map((row, index) => {
+ if (typeof row !== 'object' || row === null || Array.isArray(row)) {
+ throw new Error(`Row ${index + 1} must be an object of column names mapped to values.`);
+ }
+ return Object.fromEntries(Object.entries(row).filter(([, value]) => value !== undefined && (keepEmptyStrings || value !== '')));
+ });
+}
+
+function collectColumns({ records }: { records: Record<string, unknown>[] }): string[] {
+ const columns = [...new Set(records.flatMap((record) => Object.keys(record)))];
+ if (columns.length === 0) {
+ throw new Error('Rows must contain at least one column.');
+ }
+ return columns;
+}
+
+function toBindValue(value: unknown): unknown {
+ const isStructured = typeof value === 'object' && value !== null && !(value instanceof Date) && !Buffer.isBuffer(value);
+ return isStructured ? JSON.stringify(value) : value;
+}
+
+function buildStatements({ quotedTable, columns, records }: {
+ quotedTable: string;
+ columns: string[];
+ records: Record<string, unknown>[];
+}): InsertStatement[] {
+ const rowsPerStatement = Math.max(1, Math.min(MAX_ROWS_PER_STATEMENT, Math.floor(MAX_PARAMETERS / columns.length)));
+ const quotedColumns = columns.map((column) => sanitizeColumnName(column)).join(', ');
+
+ return splitIntoBatches({ records, maxRows: rowsPerStatement }).map((batch) => {
+ const values: unknown[] = [];
+ const tuples = batch.map((record) => {
+ const placeholders = columns.map((column) => {
+ const value = record[column];
+ if (value === undefined) {
+ return 'DEFAULT';
+ }
+ values.push(toBindValue(value));
+ return '?';
+ });
+ return `(${placeholders.join(', ')})`;
+ });
+ return {
+ text: `INSERT INTO ${quotedTable} (${quotedColumns}) VALUES ${tuples.join(', ')}`,
+ values,
+ };
+ });
+}
+
+function splitIntoBatches({
+ records,
+ maxRows,
+}: {
+ records: Record<string, unknown>[];
+ maxRows: number;
+}): Record<string, unknown>[][] {
+ const batches: Record<string, unknown>[][] = [];
+ let current: Record<string, unknown>[] = [];
+ let currentBytes = 0;
+ for (const record of records) {
+ const bytes = estimateBytes({ record });
+ const isFull =
+ current.length >= maxRows || currentBytes + bytes > MAX_STATEMENT_BYTES;
+ if (current.length > 0 && isFull) {
+ batches.push(current);
+ current = [];
+ currentBytes = 0;
+ }
+ current.push(record);
+ currentBytes += bytes;
+ }
+ return current.length > 0 ? [...batches, current] : batches;
+}
+
+function estimateBytes({ record }: { record: Record<string, unknown> }): number {
+ return Object.values(record).reduce<number>(
+ (total, value) => total + sizeOf({ value }),
+ 0
+ );
+}
+
+function sizeOf({ value }: { value: unknown }): number {
+ if (typeof value === 'string') {
+ return Buffer.byteLength(value);
+ }
+ if (Buffer.isBuffer(value)) {
+ return value.length;
+ }
+ if (typeof value === 'object' && value !== null && !(value instanceof Date)) {
+ return Buffer.byteLength(JSON.stringify(value));
+ }
+ return 16;
+}
+
+const MAX_PARAMETERS = 65535;
+const MAX_ROWS_PER_STATEMENT = 1000;
+const MAX_STATEMENT_BYTES = 1024 * 1024;
+
+export const insertRowsUtils = {
+ toRecords,
+ collectColumns,
+ buildStatements,
+};
+
+export type InsertStatement = {
+ text: string;
+ values: unknown[];
+};
+
+type MysqlWriteResult = {
+ affectedRows: number;
+ insertId: number;
+};
diff --git a/packages/pieces/community/mysql/src/lib/common/index.ts b/packages/pieces/community/mysql/src/lib/common/index.ts
index 1b05be93100..42e0259d584 100644
--- a/packages/pieces/community/mysql/src/lib/common/index.ts
+++ b/packages/pieces/community/mysql/src/lib/common/index.ts
@@ -35,6 +35,20 @@ export async function mysqlGetTableNames(conn: Connection): Promise<string[]> {
return result.map((row: Record<string, string>) => row[Object.keys(row)[0]]);
}
+export async function mysqlGetColumns({ conn, table }: { conn: Connection; table: string }): Promise<MysqlColumn[]> {
+ const rows: Record<string, unknown>[] = await conn.query(
+ 'SELECT column_name AS column_name, data_type AS data_type, is_nullable AS is_nullable, column_default AS column_default, extra AS extra FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = ? ORDER BY ordinal_position',
+ [table]
+ );
+ return rows.map((row) => ({
+ column_name: String(row['column_name']),
+ data_type: String(row['data_type']),
+ is_nullable: String(row['is_nullable']),
+ column_default: row['column_default'] === null ? null : String(row['column_default']),
+ extra: String(row['extra']),
+ }));
+}
+
export const mysqlCommon = {
timezone: Property.ShortText({
displayName: 'Timezone',
@@ -81,3 +95,11 @@ export function sanitizeColumnName(name: string | undefined): string {
}
return sqlstring.escapeId(name);
}
+
+export type MysqlColumn = {
+ column_name: string;
+ data_type: string;
+ is_nullable: string;
+ column_default: string | null;
+ extra: string;
+};
diff --git a/packages/pieces/community/mysql/src/lib/output-schemas.ts b/packages/pieces/community/mysql/src/lib/output-schemas.ts
index 232ff3f6003..f4f6e1423ae 100644
--- a/packages/pieces/community/mysql/src/lib/output-schemas.ts
+++ b/packages/pieces/community/mysql/src/lib/output-schemas.ts
@@ -14,6 +14,18 @@ export const insertRowOutputSchema: OutputSchema = {
],
};
+export const insertRowsOutputSchema: OutputSchema = {
+ fields: [
+ { key: 'affectedRows', label: 'Inserted Rows', format: 'number' },
+ {
+ key: 'firstInsertId',
+ label: 'First Insert ID',
+ format: 'number',
+ description: 'Auto-increment id of the first inserted row (0 when the table has no auto-increment column). MySQL reports only this one id; the other rows follow it in sequence.',
+ },
+ ],
+};
+
export const updateRowOutputSchema: OutputSchema = {
fields: [
{
diff --git a/packages/pieces/community/postgres/package.json b/packages/pieces/community/postgres/package.json
index 94a03a3a0a2..48a5a7d229c 100644
--- a/packages/pieces/community/postgres/package.json
+++ b/packages/pieces/community/postgres/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/piece-postgres",
- "version": "0.3.1",
+ "version": "0.4.0",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"dependencies": {
diff --git a/packages/pieces/community/postgres/src/index.ts b/packages/pieces/community/postgres/src/index.ts
index 0f7d26adc0f..79672aede89 100644
--- a/packages/pieces/community/postgres/src/index.ts
+++ b/packages/pieces/community/postgres/src/index.ts
@@ -7,6 +7,7 @@ import { AppConnectionType, PieceCategory } from '@activepieces/pieces-framework
import { runQuery } from './lib/actions/run-query';
import { findRows } from './lib/actions/find-rows';
import { insertRow } from './lib/actions/insert-row';
+import { insertRows } from './lib/actions/insert-rows';
import { updateRow } from './lib/actions/update-row';
import { deleteRow } from './lib/actions/delete-row';
import { getTables } from './lib/actions/get-tables';
@@ -96,6 +97,6 @@ export const postgres = createPiece({
logoUrl: 'https://cdn.activepieces.com/pieces/postgres.png',
authors: ["AbdullahBitar", "Willianwg", "dentych", "kishanprmr", "AbdulTheActivePiecer", "khaledmashaly", "abuaboud"],
auth: postgresAuth,
- actions: [findRows, insertRow, updateRow, deleteRow, getTables, runQuery],
+ actions: [findRows, insertRow, insertRows, updateRow, deleteRow, getTables, runQuery],
triggers: [newRow],
});
diff --git a/packages/pieces/community/postgres/src/lib/actions/insert-rows.ts b/packages/pieces/community/postgres/src/lib/actions/insert-rows.ts
new file mode 100644
index 00000000000..1cb8f3fb96d
--- /dev/null
+++ b/packages/pieces/community/postgres/src/lib/actions/insert-rows.ts
@@ -0,0 +1,244 @@
+import { createAction, InputPropertyMap, Property } from '@activepieces/pieces-framework';
+import { QueryResult } from 'pg';
+import { postgresAuth } from '../..';
+import { pgClient, PostgresColumn, postgresCommon, postgresUtils } from '../common';
+import { insertRowsOutputSchema } from '../output-schemas';
+
+export const insertRows = createAction({
+ auth: postgresAuth,
+ name: 'insert-rows',
+ classification: 'WRITE',
+ displayName: 'Insert Multiple Rows',
+ description: 'Inserts many rows into a table in a single transaction',
+ audience: 'both',
+ outputSchema: insertRowsOutputSchema,
+ aiMetadata: {
+ description: 'Inserts an array of rows into a PostgreSQL table, each row a map of column names to values, using multi-row INSERT statements inside one transaction: if any row fails, none are inserted. Columns a row omits get the column default. Returns the inserted count and, when Return Inserted Rows is on, the stored rows including generated ids. Use instead of repeated single inserts for bulk loads. Not idempotent: each call appends new rows, so repeating it adds duplicates or errors on a unique-key collision.',
+ idempotent: false,
+ },
+ props: {
+ table: postgresCommon.table,
+ batch: Property.DynamicProperties({
+ auth: postgresAuth,
+ displayName: 'Rows',
+ required: true,
+ refreshers: ['table'],
+ props: async ({ auth, table }): Promise<InputPropertyMap> => {
+ if (!auth || !postgresUtils.isPostgresTable(table)) {
+ return {};
+ }
+ const client = await pgClient(auth);
+ try {
+ const columns = await postgresUtils.listColumnDetails({ client, table });
+ return {
+ rows: Property.Array({
+ displayName: 'Rows',
+ description: 'One item per row. Leave a field empty to use the column default.',
+ required: true,
+ properties: Object.fromEntries(
+ columns.map((column) => [
+ column.column_name,
+ Property.ShortText({
+ displayName: column.column_name,
+ description: describeColumn({ column }),
+ required: isRequiredColumn({ column }),
+ }),
+ ]),
+ ),
+ }),
+ };
+ } finally {
+ await client.end();
+ }
+ },
+ }),
+ keep_empty_strings: Property.Checkbox({
+ displayName: 'Keep Empty Strings',
+ description: 'Insert empty fields as empty strings instead of the column default.',
+ required: false,
+ defaultValue: false,
+ }),
+ return_rows: Property.Checkbox({
+ displayName: 'Return Inserted Rows',
+ description: 'Return the inserted rows, including generated ids. Needs SELECT permission on the table.',
+ required: false,
+ defaultValue: false,
+ }),
+ },
+ async run(context) {
+ const { table, batch, keep_empty_strings, return_rows } = context.propsValue;
+ const submittedRecords = insertRowsUtils.toRecords({ rows: batch?.['rows'], keepEmptyStrings: keep_empty_strings === true });
+ insertRowsUtils.collectColumns({ records: submittedRecords });
+
+ const client = await pgClient(context.auth);
+ client.on('error', () => undefined);
+ try {
+ const tableColumns = await postgresUtils.listColumnDetails({ client, table });
+ const records = insertRowsUtils.parseArrayColumns({ records: submittedRecords, tableColumns });
+ const statements = insertRowsUtils.buildStatements({
+ qualifiedTable: postgresUtils.qualifiedName(table),
+ columns: insertRowsUtils.collectColumns({ records }),
+ records,
+ returnRows: return_rows === true,
+ });
+ await client.query('BEGIN');
+ const results: QueryResult[] = [];
+ for (const statement of statements) {
+ results.push(await client.query(statement.text, statement.values));
+ }
+ await client.query('COMMIT');
+ const rowCount = results.reduce((total, result) => total + (result.rowCount ?? 0), 0);
+ return return_rows === true
+ ? { rows: results.flatMap((result) => result.rows), rowCount }
+ : { rowCount };
+ } catch (error) {
+ await client.query('ROLLBACK').catch(() => undefined);
+ const reason = error instanceof Error ? error.message : String(error);
+ throw new Error(`Insert failed and the whole batch was rolled back: ${reason}`);
+ } finally {
+ await client.end();
+ }
+ },
+});
+
+function isRequiredColumn({ column }: { column: PostgresColumn }): boolean {
+ const isFilledByDatabase = column.column_default !== null || column.is_identity === 'YES' || column.is_generated === 'ALWAYS';
+ return column.is_nullable === 'NO' && !isFilledByDatabase;
+}
+
+function describeColumn({ column }: { column: PostgresColumn }): string {
+ return isRequiredColumn({ column }) ? `${column.data_type} · required` : column.data_type;
+}
+
+function toRecords({ rows, keepEmptyStrings }: { rows: unknown; keepEmptyStrings: boolean }): Record<string, unknown>[] {
+ if (!Array.isArray(rows) || rows.length === 0) {
+ throw new Error('Rows must be a non-empty array of objects.');
+ }
+ return rows.map((row, index) => {
+ if (typeof row !== 'object' || row === null || Array.isArray(row)) {
+ throw new Error(`Row ${index + 1} must be an object of column names mapped to values.`);
+ }
+ return Object.fromEntries(Object.entries(row).filter(([, value]) => value !== undefined && (keepEmptyStrings || value !== '')));
+ });
+}
+
+function parseArrayColumns({ records, tableColumns }: { records: Record<string, unknown>[]; tableColumns: PostgresColumn[] }): Record<string, unknown>[] {
+ const arrayColumns = new Set(tableColumns.filter((column) => column.data_type === 'ARRAY').map((column) => column.column_name));
+ if (arrayColumns.size === 0) {
+ return records;
+ }
+ return records.map((record) => Object.fromEntries(
+ Object.entries(record).map(([column, value]) => [column, arrayColumns.has(column) ? parseJsonArray({ value }) : value]),
+ ));
+}
+
+function parseJsonArray({ value }: { value: unknown }): unknown {
+ if (typeof value !== 'string' || !value.trimStart().startsWith('[')) {
+ return value;
+ }
+ try {
+ const parsed: unknown = JSON.parse(value);
+ return Array.isArray(parsed) ? parsed : value;
+ } catch {
+ return value;
+ }
+}
+
+function collectColumns({ records }: { records: Record<string, unknown>[] }): string[] {
+ const columns = [...new Set(records.flatMap((record) => Object.keys(record)))];
+ if (columns.length === 0) {
+ throw new Error('Rows must contain at least one column.');
+ }
+ return columns;
+}
+
+function buildStatements({ qualifiedTable, columns, records, returnRows }: {
+ qualifiedTable: string;
+ columns: string[];
+ records: Record<string, unknown>[];
+ returnRows: boolean;
+}): InsertStatement[] {
+ const rowsPerStatement = Math.max(1, Math.min(MAX_ROWS_PER_STATEMENT, Math.floor(MAX_PARAMETERS / columns.length)));
+ const quotedColumns = columns.map((column) => postgresUtils.quoteIdentifier(column)).join(', ');
+ const suffix = returnRows ? ' RETURNING *' : '';
+
+ return splitIntoBatches({ records, maxRows: rowsPerStatement }).map((batch) => {
+ const values: unknown[] = [];
+ const tuples = batch.map((record) => {
+ const placeholders = columns.map((column) => {
+ const value = record[column];
+ if (value === undefined) {
+ return 'DEFAULT';
+ }
+ values.push(value);
+ return `$${values.length}`;
+ });
+ return `(${placeholders.join(', ')})`;
+ });
+ return {
+ text: `INSERT INTO ${qualifiedTable} (${quotedColumns}) VALUES ${tuples.join(', ')}${suffix}`,
+ values,
+ };
+ });
+}
+
+function splitIntoBatches({
+ records,
+ maxRows,
+}: {
+ records: Record<string, unknown>[];
+ maxRows: number;
+}): Record<string, unknown>[][] {
+ const batches: Record<string, unknown>[][] = [];
+ let current: Record<string, unknown>[] = [];
+ let currentBytes = 0;
+ for (const record of records) {
+ const bytes = estimateBytes({ record });
+ const isFull =
+ current.length >= maxRows || currentBytes + bytes > MAX_STATEMENT_BYTES;
+ if (current.length > 0 && isFull) {
+ batches.push(current);
+ current = [];
+ currentBytes = 0;
+ }
+ current.push(record);
+ currentBytes += bytes;
+ }
+ return current.length > 0 ? [...batches, current] : batches;
+}
+
+function estimateBytes({ record }: { record: Record<string, unknown> }): number {
+ return Object.values(record).reduce<number>(
+ (total, value) => total + sizeOf({ value }),
+ 0
+ );
+}
+
+function sizeOf({ value }: { value: unknown }): number {
+ if (typeof value === 'string') {
+ return Buffer.byteLength(value);
+ }
+ if (Buffer.isBuffer(value)) {
+ return value.length;
+ }
+ if (typeof value === 'object' && value !== null && !(value instanceof Date)) {
+ return Buffer.byteLength(JSON.stringify(value));
+ }
+ return 16;
+}
+
+const MAX_PARAMETERS = 65535;
+const MAX_ROWS_PER_STATEMENT = 1000;
+const MAX_STATEMENT_BYTES = 1024 * 1024;
+
+export const insertRowsUtils = {
+ toRecords,
+ collectColumns,
+ parseArrayColumns,
+ buildStatements,
+};
+
+export type InsertStatement = {
+ text: string;
+ values: unknown[];
+};
diff --git a/packages/pieces/community/postgres/src/lib/common.ts b/packages/pieces/community/postgres/src/lib/common.ts
index 0d204888774..fe517ad2246 100644
--- a/packages/pieces/community/postgres/src/lib/common.ts
+++ b/packages/pieces/community/postgres/src/lib/common.ts
@@ -51,6 +51,14 @@ const listColumns = async ({ client, table }: { client: Client, table: PostgresT
return result.rows.map((row) => row.column_name);
}
+const listColumnDetails = async ({ client, table }: { client: Client, table: PostgresTable }): Promise<PostgresColumn[]> => {
+ const result = await client.query<PostgresColumn>(
+ `SELECT column_name, data_type, is_nullable, column_default, is_identity, is_generated FROM information_schema.columns WHERE table_schema = $1 AND table_name = $2 ORDER BY ordinal_position`,
+ [table.table_schema, table.table_name]
+ );
+ return result.rows;
+}
+
const isPostgresTable = (value: unknown): value is PostgresTable => {
if (typeof value !== 'object' || value === null) {
return false;
@@ -180,6 +188,7 @@ export const postgresCommon = {
export const postgresUtils = {
listTables,
listColumns,
+ listColumnDetails,
isPostgresTable,
qualifiedName,
quoteIdentifier,
@@ -189,3 +198,12 @@ export type PostgresTable = {
table_schema: string;
table_name: string;
};
+
+export type PostgresColumn = {
+ column_name: string;
+ data_type: string;
+ is_nullable: string;
+ column_default: string | null;
+ is_identity: string;
+ is_generated: string;
+};
diff --git a/packages/pieces/community/postgres/src/lib/output-schemas.ts b/packages/pieces/community/postgres/src/lib/output-schemas.ts
index 2b0d20a6361..580f46257be 100644
--- a/packages/pieces/community/postgres/src/lib/output-schemas.ts
+++ b/packages/pieces/community/postgres/src/lib/output-schemas.ts
@@ -26,6 +26,17 @@ export const insertRowOutputSchema: OutputSchema = {
],
};
+export const insertRowsOutputSchema: OutputSchema = {
+ fields: [
+ {
+ key: 'rows',
+ label: 'Inserted Rows',
+ description: 'The inserted rows as stored by the database, including defaults and generated ids; each row\'s fields are the columns of the target table. Only present when Return Inserted Rows is enabled.',
+ },
+ { key: 'rowCount', label: 'Inserted Row Count', format: 'number' },
+ ],
+};
+
export const updateRowOutputSchema: OutputSchema = {
fields: [
{