Salesforce DX Absolute Beginner ( Part 7 Of 7 )

In this seventh part of the salesforce DX series, I am going to show how to use salesforce DX data commands to migrate the sample data from the scratch org to sandbox for testing or you can create data into the scratch org from the CLI.
Use force:data:soql:query command to Executes a SOQL query as shoe below.This Command will help if you wanted to retrieve data into CLI from scratch org without login. Use -t for tooling API 
sfdx force:data:soql:query -q "Select id , Name ,Passenger_Code__c,Reg_Number__c ,Vehicle_Id__c from Vehicle__c"

sfdx force:data:soql:query -q "SELECT Name FROM ApexTrigger" -t

 

Export Command 
 Exports data from an org into the sObject tree format  as shown below
sfdx force:data:tree:export -q "select Id,Name, Passenger_Code__c, Reg_Number__c, Vehicle_Id__c,(Select Id,Name,Office_Id__c,Violations_Id__c from Violations__r) from Vehicle__c " -x export-demo -d /force-app/data
 "records": [
        {
            "attributes": {
                "type": "Vehicle__c",
                "referenceId": "Vehicle__cRef1"
            },
            "Name": "V1",
            "Violations__r": {
                "records": [
                    {
                        "attributes": {
                            "type": "Violations__c",
                            "referenceId": "Violations__cRef1"
                        },
                        "Name": "VO0",
                        "Office_Id__c": "3278640",
                        "Violations_Id__c": "2387627836120"
                    },
                    {
                        "attributes": {
                            "type": "Violations__c",
                            "referenceId": "Violations__cRef2"
                        },
                        "Name": "V1"
                    }
                ]
            }
        },
......
]

 

Import Command 
 Imports data into an org using the SObject Tree Save API. This data can include master-detail relationships. The following command imports the data into Salesforce objects by using SObject Tree.
sfdx force:data:tree:import -f Vehicle__c-Violations__c.json

Create/Update/Delete Commands 
 You can use CRUD commands to manipulate the data the CLI as shown below.
Below example showing the how to inserts account record from the CLI.
sfdx force:data:record:create -s Account -v "Name='Universal Containers' 
Website=www.example.com"
Below example showing the how to delete account record from the CLI.
sfdx force:data:record:delete -s Account -i 001D000000Kv3dl
Below example showing the how to update account record from the CLI.
sfdx force:data:record:update -s Account -i 001D000000Kv3dl -v "Name=NewAcme"
Bulk Commands 

 You can use Bulk commands to delete or upsert the data into Bulk API Style.

The Following commands Create a job and one or more batches for inserting new rows and updating existing rows by accessing the Bulk API.
sfdx force:data:bulk:upsert -s MyObject__c -f ./path/to/file.csv -i MyField__c

Above Command Inserts or updates records from a CSV file with one or more batch based on the CSV, file size and Returns a job ID and a batch ID. Use these IDs to check job status with data:bulk: status as shown below.

sfdx force:data:bulk:status -i 750xx000000005sAAA

 

Below are some of the useful reference about salesforce DX

  1.  Salesforce DX Trailhead
  2. First Impressions with Salesforce DX by Peter Chittum
  3. Getting Started With Salesforce DX