Skip to content

Import JSON from body or file#24

Merged
Brayden merged 2 commits intomainfrom
bwilmoth/import-json
Oct 16, 2024
Merged

Import JSON from body or file#24
Brayden merged 2 commits intomainfrom
bwilmoth/import-json

Conversation

@Brayden
Copy link
Member

@Brayden Brayden commented Oct 15, 2024

Purpose

Allow users to import data from either part of the POST body as a JSON object, or from a .json file. The data will attempted to be inserted row by row, reporting any errors back in the response and the failure reason, into the specified table as indicated in the URL.

Tasks

  • Expose an endpoint that supports both FILE and JSON upload
  • Attempt to insert new data into provided table (table name in URL)
  • On failure of insert, add to array to return as final result

Verify

Failed Entry

cURL

curl --location 'https://starbasedb.YOUR-IDENTIFIER.workers.dev/import/json/users' \
--header 'Authorization: Bearer ABC123' \
--header 'Content-Type: application/json' \
--data '{
  "data": [
    { "column1": "value1", "column2": "value2" },
    { "column1": "value3", "column2": "value4" }
  ],
  "columnMapping": {
    "column1": "database_column1",
    "column2": "database_column2"
  }
}'

Response

{
    "result": {
        "message": "Imported 0 out of 2 records successfully. 2 records failed.",
        "failedStatements": [
            {
                "statement": "INSERT INTO users (database_column1, database_column2) VALUES (?, ?)",
                "error": "Unknown error"
            },
            {
                "statement": "INSERT INTO users (database_column1, database_column2) VALUES (?, ?)",
                "error": "Unknown error"
            }
        ]
    }
}

Successful Entry

cURL

curl --location 'https://starbasedb.YOUR-IDENTIFIER.workers.dev/import/json/users' \
--header 'Authorization: Bearer ABC123' \
--header 'Content-Type: application/json' \
--data-raw '{
  "data": [
    { "column1": 9, "column2": "Brayden", "column3": "[email protected]" },
    { "column1": 10, "column2": "Brandon", "column3": "[email protected]" }
  ],
  "columnMapping": {
    "column1": "user_id",
    "column2": "name",
    "column3": "email"
  }
}'

Response:

{
    "result": {
        "message": "Imported 2 out of 2 records successfully. 0 records failed.",
        "failedStatements": []
    }
}

Before

After

@Brayden Brayden added the enhancement New feature or request label Oct 15, 2024
@Brayden Brayden self-assigned this Oct 15, 2024
@Brayden Brayden merged commit a49849e into main Oct 16, 2024
@Brayden Brayden deleted the bwilmoth/import-json branch October 16, 2024 01:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant