How to Migrate and Export Data from Supabase with Node.js
This guide explains how to use a Node.js script to export data from a Supabase table, add export metadata, and migrate your data for use in other platforms such as ASP.NET.
1. Exporting Data from Supabase
Connecting to your Supabase database and exporting table data to CSV files is a common requirement for backups or migrations. Each row in the exported CSV should ideally include:
export_sequence: A running number for each rowexport_datetime: The date and time the row was exported
Steps to Export
- Configure Environment: Edit your
.envfile with your Supabase credentials and table name. - Install Dependencies: Run
npm installto install required packages like@supabase/supabase-jsandcsv-writer. - Run the Export: Execute your script (e.g.,
node index.js) to start the export. CSV files will be saved in your designated exports directory.
2. Migrating Data to ASP.NET
Once you have your CSV files, you can import them into an ASP.NET application using modern libraries like Entity Framework Core.
Example: Importing CSV in ASP.NET Core
Add NuGet Packages:
CsvHelperfor reading CSV files efficiently.Microsoft.EntityFrameworkCorefor database access.
Sample Code to Read CSV:
using CsvHelper;
using System.Globalization;
// Example method to process the exported CSV
using (var reader = new StreamReader("path_to_supabase_export.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = csv.GetRecords<YourModel>().ToList();
// Save records to your SQL Server or PostgreSQL database via EF Core
}
Map CSV Columns to Your Model:
- Ensure your C# model has properties for all columns, including
export_sequenceandexport_datetimeif you want to retain the migration metadata.
- Ensure your C# model has properties for all columns, including
Insert into Database:
- Use Entity Framework or ADO.NET to batch insert the records into your ASP.NET application's database.
3. Migration Tips
- Verify Schema: Always verify the CSV column names and types match your C# model exactly.
- Automation: You can automate the import process as part of your ASP.NET app's startup or via a standalone migration tool.
- DateTime Parsing: The
export_datetimefield is usually in ISO format and can be parsed directly toDateTimein C#.
[!TIP] Need help with your data migration? Our team at One Day Developers specializes in rapid data migrations and custom integration services. If you need assistance scaling your application or moving away from Supabase to a custom ASP.NET backend, we are here to help.
Get in touch with us on our Contact Page to discuss your project!