-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Supabase - Batch Insert Rows action #14652
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
components/supabase/actions/batch-insert-rows/batch-insert-rows.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import supabase from "../../supabase.app.mjs"; | ||
| import fs from "fs"; | ||
| import { parse } from "csv-parse/sync"; | ||
|
|
||
| export default { | ||
| key: "supabase-batch-insert-rows", | ||
| name: "Batch Insert Rows", | ||
| description: "Inserts new rows into a database. [See the documentation](https://supabase.com/docs/reference/javascript/insert)", | ||
| version: "0.0.1", | ||
| type: "action", | ||
| props: { | ||
| supabase, | ||
| table: { | ||
| propDefinition: [ | ||
| supabase, | ||
| "table", | ||
| ], | ||
| description: "Name of the table to insert rows into", | ||
| }, | ||
| source: { | ||
| type: "string", | ||
| label: "Source of data", | ||
| description: "Whether to enter the row data as an array of objects or to import from a CSV file", | ||
| options: [ | ||
| "Array", | ||
| "CSV File", | ||
| ], | ||
| reloadProps: true, | ||
| }, | ||
| }, | ||
| async additionalProps() { | ||
| const props = {}; | ||
| if (this.source === "Array") { | ||
| props.data = { | ||
| type: "string[]", | ||
| label: "Row Data", | ||
| description: "An array of objects, each object representing a row. Enter column names and values as key/value pairs", | ||
| }; | ||
| } | ||
| if (this.source === "CSV File") { | ||
| props.filePath = { | ||
| type: "string", | ||
| label: "File Path", | ||
| description: "The path to a csv file in the `/tmp` directory. [See the documentation on working with files](https://pipedream.com/docs/code/nodejs/working-with-files/#writing-a-file-to-tmp)", | ||
| }; | ||
| } | ||
| return props; | ||
| }, | ||
| methods: { | ||
| parseArray(arr) { | ||
| if (Array.isArray(arr)) { | ||
| return arr.map((item) => { | ||
| return typeof item === "string" | ||
| ? JSON.parse(item) | ||
| : item; | ||
| }); | ||
| } | ||
| if (typeof arr === "string") { | ||
| return JSON.parse(arr); | ||
| } | ||
| }, | ||
| getRowsFromCSV(filePath) { | ||
| const fileContent = fs.readFileSync(filePath.includes("tmp/") | ||
| ? filePath | ||
| : `/tmp/${filePath}`, "utf-8"); | ||
| const rows = parse(fileContent, { | ||
| columns: true, | ||
| skip_empty_lines: true, | ||
| }); | ||
| return rows; | ||
| }, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| async run({ $ }) { | ||
| const data = this.source === "CSV File" | ||
| ? this.getRowsFromCSV(this.filePath) | ||
| : this.parseArray(this.data); | ||
|
|
||
| const response = await this.supabase.insertRow(this.table, data); | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| $.export("$summary", `Successfully inserted rows into table ${this.table}`); | ||
| return response; | ||
| }, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.