salesforce-to-sqlite
v1.0.2
Published
CLI tool to extract Salesforce data and load into SQLite
Readme
Salesforce to SQLite CLI Tool
A Node.js CLI tool that extracts data from Salesforce and loads it into a SQLite database based on a load plan configuration.
Features
- Extracts data from Salesforce using SF CLI
- Creates SQLite tables automatically based on query results
- Handles Salesforce relationship fields (e.g.,
Parent.NamebecomesParent_Namein SQLite) - Automatic data type inference
- Progress logging with colored output
- Verbose mode for debugging
Prerequisites
- Node.js (v14 or higher)
- Salesforce CLI (
sf) installed and authenticated - Active Salesforce org connection
Sample Load Plans
SQLite Explorer Pro
Use this explorer to explore the create database:
Installation
npm install -g salesforce-to-sqliteUsage
Basic Usage
sf2sqlite -o <salesforce-username> -l load-plan.jsonWith Custom Database Name
sf2sqlite -o [email protected] -l load-plan.json -d my-data.dbVerbose Mode
sf2sqlite -o [email protected] -l load-plan.json -vOptions
-o, --org <username>- Salesforce org username or alias (required)-l, --load-plan <file>- Path to load plan JSON file (required)-d, --database <file>- SQLite database file path (default:salesforce.db)-v, --verbose- Enable verbose logging-h, --help- Display help information-V, --version- Display version number
Load Plan Format
The load plan is a JSON array of object configurations. Each configuration includes:
{
"object": "ObjectName",
"compositeKeys": ["Field1", "Field2"],
"query": "SELECT Field1, Field2, Related.Field FROM ObjectName",
"fieldMappings": {
"Field1": "Field1",
"Field2": {
"lookup": {
"object": "RelatedObject",
"key": "KeyField",
"field": "Related.Field"
}
}
}
}How It Works
- Read Load Plan: Parses the JSON configuration file
- Query Salesforce: Executes each SOQL query using
sf data query - Field Name Sanitization: Converts relationship fields (e.g.,
Account.Name) to SQLite-compatible names (Account_Name) - Table Creation: Creates SQLite tables with inferred data types
- Data Insertion: Inserts queried data into SQLite tables
- Type Conversion: Automatically converts Salesforce data types to SQLite equivalents
Field Name Handling
Salesforce relationship fields like Parent.Name or Account.Owner.Email are automatically converted to SQLite-compatible field names:
Parent.Name→Parent_NameAccount.Owner.Email→Account_Owner_EmailProduct2.StockKeepingUnit→Product2_StockKeepingUnit
Data Type Mapping
| Salesforce Type | SQLite Type | Notes | |----------------|-------------|-------| | String/Text | TEXT | Default for most fields | | Boolean | INTEGER | true=1, false=0 | | Number (Integer) | INTEGER | Whole numbers | | Number (Decimal) | REAL | Decimal numbers | | Date/DateTime | TEXT | ISO format strings | | ID | TEXT | Salesforce IDs |
Examples
Extract Product Catalog Data
sf2sqlite -o production-org -l load-plan.json -d catalog.dbDebug Query Issues
sf2sqlite -o sandbox -l load-plan.json -vOutput
The tool provides colored console output:
- 🔵 Info messages
- ✅ Success messages
- ⚠️ Warning messages
- ❌ Error messages
At the end, a summary shows:
- Number of successfully processed objects
- Number of failed objects
- Location of the SQLite database file
Error Handling
- Invalid load plan JSON: Exits with error message
- Failed Salesforce queries: Logs warning and continues with next object
- Table creation errors: Logs error and skips object
- Record insertion errors: Logs error but continues with other records
Performance Notes
- Large queries use a 50MB buffer for CSV data
- SQLite WAL mode is enabled for better performance
- Batch inserts using transactions for speed
- Progress is logged for each object
Troubleshooting
"Command not found: sf"
Install Salesforce CLI: https://developer.salesforce.com/tools/salesforcecli
"No auth found for org"
Authenticate with: sf org login web -a <alias>
"Query timeout"
Try splitting large objects into smaller queries or increase the buffer size in the code
"Failed to parse CSV"
Check query syntax and ensure fields exist in your org
License
MIT (c) Mohan Chinnappan
