Skip to content

Updated insert-row.mjs #17916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 77 additions & 18 deletions components/postgresql/actions/insert-row/insert-row.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,90 @@ export default {
table,
rowValues,
} = this;

if (!schema || !table) {
throw new Error("Schema and table are required");
}

if (!rowValues) {
throw new Error("Row values are required");
}

const results = [];
const parsedRowValues = parseRowValues(rowValues);
let parsedRowValues;

try {
parsedRowValues = parseRowValues(rowValues);
} catch (error) {
throw new Error(`Invalid row values format: ${error.message}`);
}

if (!parsedRowValues) {
throw new Error("Parsed row values cannot be null or undefined");
}

const parsedRowValuesArray = Array.isArray(parsedRowValues)
? parsedRowValues
: [
parsedRowValues,
];
: [parsedRowValues];

if (parsedRowValuesArray.length === 0) {
throw new Error("No valid rows to insert");
}

const errorMsg = "New row(s) not inserted due to an error. ";

for (const row of parsedRowValuesArray) {
const columns = Object.keys(row);
const values = Object.values(row);
const res = await this.postgresql.insertRow(
schema,
table,
columns,
values,
errorMsg,
);
results.push(res);

for (let i = 0; i < parsedRowValuesArray.length; i++) {
const row = parsedRowValuesArray[i];

try {

if (!row || typeof row !== 'object') {
throw new Error(`Row ${i + 1} is not a valid object`);
}

const columns = Object.keys(row);
const values = Object.values(row);

if (columns.length === 0) {
throw new Error(`Row ${i + 1} cannot be empty`);
}

console.log(`Inserting row ${i + 1}:`, {
schema,
table,
columns,
values: values.map(v => typeof v === 'string' && v.length > 100 ? `${v.substring(0, 100)}...` : v)
});

const res = await this.postgresql.insertRow(
schema,
table,
columns,
values,
errorMsg,
);

results.push(res);
console.log(`Successfully inserted row ${i + 1}`);

} catch (error) {
console.error(`Failed to insert row ${i + 1}:`, {
row,
error: error.message,
stack: error.stack
});


throw new Error(`${errorMsg}Row ${i + 1}: ${error.message}`);
}
}
$.export("$summary", `Successfully inserted ${results.length} row${results.length === 1
? ""
: "s"}`);

const summary = `Successfully inserted ${results.length} row${results.length === 1 ? "" : "s"}`;
console.log(summary);
$.export("$summary", summary);

return results;
},
};