Is there a way to bulk import simple data to Supab...
# help
e
Is there a way to bulk import simple data to Supabase?
n
Hello @edgaras! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
🆕 How to bulk import data?
m
If you mean the database, then you can do so with CSV or simply by pasting the data in csv format. You do that when creating a table in the Dashboard. Supabase automatically picks up your schema and you can later modify that too.
n
How to bulk import data?
l
hi how can we upload via csv after the table was created?
e
@Muezz that one I know, it's advertised well, but I am missing data import AFTER the table is created.
m
@edgaras @lanbau I dont think it is possible to add data AFTER the table has been created. Maybe someone more experienced can elaborate. I'd suggest that you delete the existing table and then create it again with the CSV data. and IF you want to keep the data that is already there, export it in csv, add the bulk of data that you needed to add before and then create the table with this CSV. IF you want to add data in bulk in the form of CSV from your front end, then I think the only option is converting that CSV to JSON.
e
Thanks! And how do you them import JSON data?
m
I use Flutter as my front end so I will just give you the example of my code from my current project. This is the method that sends the "transaction" i.e. data to Supabase.
Copy code
dart
Future<Result> addTransaction(TransactionModel transaction) async {
    var response = await _transactionsDB.insert(transaction.toMap()).execute();
    if (response.error != null) {
      return Result.error(message: response.error!.message);
    } else {
      return Result.success(message: 'Success');
    }
  }
This is my data class's method that converts the class instance to a map (a map and json is the same thing in dart)
Copy code
dart
Map<String, dynamic> toMap() {
    return {
      't_date': tDate!.toIso8601String(),
      'category': category,
      'amount': amount,
      'deb_acc': debAcc,
      'cred_acc': credAcc,
      'item_name': itemName,
      'prod_price': prodPrice,
      'prod_quantity': prodQuantity,
    };
  }
Keep in mind that the "keys" in this json/map have the be the exact and correct names of your table's columns. If one is incorrect, that value would not be added. As you can see, I am converting my data class instance to a map, you have to figure out a way to convert a CSV to a map/json. I dont think that would be too hard. As for actually inserting the data in bulk, I dont think that the
insert
method supports multiple insertions in one call. I may be wrong but if that is the case, you can have a
for
loop for each line of the CSV converted to json and add that.