Skip to content

Instantly share code, notes, and snippets.

@bryanvine
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanvine/4b7e8d885c51b7b9e81a to your computer and use it in GitHub Desktop.
Save bryanvine/4b7e8d885c51b7b9e81a to your computer and use it in GitHub Desktop.
Powershell Script - Get-Parse & Publish-Parse - upload and download data from Parse.com Core DB
#Requires -Version 3.0
Function Get-Parse{
<#
.SYNOPSIS
Gets data from Parse.com's core DB.
.DESCRIPTION
Limitation: 10,000 entries per call. If your class is bigger, you'll need to break up your calls by objectIds.
.PARAMETER Class
Also known as Table, this is the data division in your Parse core.
.PARAMETER AppID
Your Parse Application ID (from your settings page).
.PARAMETER APIKey
Your Parse API key (from your settings page).
.PARAMETER objectID
The parse generated IDs for each entry in the class that you wish to get. Can also be an array of IDs. If no ID is specified, the whole class is returned.
.EXAMPLE
Get-Parse -Class "Widgets" -AppID "My-App-ID" -APIKey "secret" | Export-Csv -NoTypeInformation '.\widget_download.csv'
This returns the whole class "widgets" and saves it to a CSV file.
.EXAMPLE
Get-Parse -Class "Widgets" -AppID "My-App-ID" -APIKey "secret" -objectID "26AS234"
This returns only the single record as an object.
.LINK
http://www.bryanvine.com/2015/06/powershell-scripts-get-parse-publish.html
.LINK
Invoke-RestMethod
.NOTES
Author: Bryan Vine
Last updated: 6/29/2015
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,valuefrompipeline=$false,Position=0)]
[ValidateNotNullOrEmpty()][string]
$Class,
[Parameter(Mandatory=$true,valuefrompipeline=$false,Position=1)]
[ValidateNotNullOrEmpty()][string]
$AppID,
[Parameter(Mandatory=$true,valuefrompipeline=$false,Position=2)]
[ValidateNotNullOrEmpty()][string]
$APIKey,
[Parameter(Mandatory=$false,valuefrompipeline=$false,Position=3)]
[ValidateNotNullOrEmpty()][string[]]
$objectID
)
BEGIN{
$ParseAPI = "https://api.parse.com/1/classes/$Class"
$ParseHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$ParseHeaders.Add("X-Parse-Application-Id", $AppID)
$ParseHeaders.Add("X-Parse-REST-API-Key", $APIKey)
$ParseHeaders.Add("Content-Type", 'application/json')
}
PROCESS{
if(!$objectID){
Write-Verbose "Fetching the whole class '$Class'..."
Invoke-RestMethod -Method Get -Uri "$ParseAPI" -Headers $ParseHeaders | select -ExpandProperty results
}
else{
$objectID | ForEach-Object{
Write-Verbose "Fetching '$objectID' from '$Class'..."
Invoke-RestMethod -Method Get -Uri "$ParseAPI/$_" -Headers $ParseHeaders
}
}
}
END{}
}
Function Publish-Parse{
<#
.SYNOPSIS
Uploads data to Parse.com account core tables.
.DESCRIPTION
Sequentially loads items, one at a time. Returns createdAt time and objectId.
.PARAMETER Class
Also known as Table, this is the data division in your Parse core. If the class doesn't exist, one will be created with the columns of the data being passed.
.PARAMETER AppID
Your Parse Application ID (from your settings page).
.PARAMETER APIKey
Your Parse API key (from your settings page).
.PARAMETER Data
The Data you wish to upload. Can be any valid object type that parse can accept. Can also be an array of objects.
.PARAMETER WipeFirst
Deletes all objects in class before uploading data. Good for refreshing tables. Note: it doesn't drop the class, only removes all objects. Can be slow if you have many objects
.EXAMPLE
Publish-Parse -Class "Widgets" -AppID "My-App-ID" -APIKey "secret" -Data (Import-CSV '.\my_widget_list.csv') -WipeFirst
This will upload the contents of the CSV to the Widgets class after first wiping the class.
.LINK
http://www.bryanvine.com/2015/06/powershell-scripts-get-parse-publish.html
.LINK
Invoke-RestMethod
.NOTES
Author: Bryan Vine
Last updated: 6/29/2015
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,valuefrompipeline=$false,Position=0)]
[ValidateNotNullOrEmpty()][string]
$Class,
[Parameter(Mandatory=$true,valuefrompipeline=$false,Position=1)]
[ValidateNotNullOrEmpty()][string]
$AppID,
[Parameter(Mandatory=$true,valuefrompipeline=$false,Position=2)]
[ValidateNotNullOrEmpty()][string]
$APIKey,
[Parameter(Mandatory=$true,valuefrompipeline=$false,Position=3)]
[ValidateNotNullOrEmpty()][object[]]
$Data,
[switch]$WipeFirst
)
BEGIN{
$ParseAPI = "https://api.parse.com/1/classes/$Class"
$ParseHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$ParseHeaders.Add("X-Parse-Application-Id", $AppID)
$ParseHeaders.Add("X-Parse-REST-API-Key", $APIKey)
$ParseHeaders.Add("Content-Type", 'application/json')
}
PROCESS{
if($WipeFirst){
Write-Verbose "Wiping '$Class'..."
Invoke-RestMethod -Method Get -Uri "$ParseAPI" -Headers $ParseHeaders |
select -ExpandProperty results |select -ExpandProperty objectId | ForEach-Object{
Invoke-RestMethod -Method Delete -Uri "$ParseAPI/$_" -Headers $ParseHeaders -Body "{}"
} | Out-Null
}
Write-Verbose "Loading '$Class'..."
$Data | %{
Invoke-RestMethod -Method Post -Uri $ParseAPI -Headers $ParseHeaders -Body ($_ | ConvertTo-Json -Depth 100)
}
}
END{}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment