Import/export feature for the DataBase

If it is not already available (I couldn;t find it) it would be extremely helpful to imprt data for the DB fields from a CSV file. Might not be a bad idea to off the ability to export the DB to a CSV file.

2 Likes

Hey drosenblatt,
You’re right, it’s unfortunately not yet available. Thanks for the suggestion!

Hay drosenblatt,
having looked online, I have found on stackoverflow the following code which allows to parse a CSV file into a table (an array of arrays).

function parseCsv(csv, reviver) {
    reviver = reviver || function(r, c, v) { return v; };
    var chars = csv.split(''), c = 0, cc = chars.length, start, end, table = [], row;
    while (c < cc) {
        table.push(row = []);
        while (c < cc && '\r' !== chars[c] && '\n' !== chars[c]) {
            start = end = c;
            if ('"' === chars[c]){
                start = end = ++c;
                while (c < cc) {
                    if ('"' === chars[c]) {
                        if ('"' !== chars[c+1]) { break; }
                        else { chars[++c] = ''; } // unescape ""
                    }
                    end = ++c;
                }
                if ('"' === chars[c]) { ++c; }
                while (c < cc && '\r' !== chars[c] && '\n' !== chars[c] && ',' !== chars[c]) { ++c; }
            } else {
                while (c < cc && '\r' !== chars[c] && '\n' !== chars[c] && ',' !== chars[c]) { end = ++c; }
            }
            row.push(reviver(table.length-1, row.length, chars.slice(start, end).join('')));
            if (',' === chars[c]) { ++c; }
        }
        if ('\r' === chars[c]) { ++c; }
        if ('\n' === chars[c]) { ++c; }
    }
    return table;
}

It can be used to input a CSV file into Wix Database using the following usage:

import wixData from "wix-data";

const csv = 
`1,Joe, Springfield
2,Bart, Nashvile
3,Marge, New York`;

let table = parseCsv(csv);
table.forEach(row => {
  let item = {no: Number(row[0]), name: row[1], city: row[2].trim()};
  wixData.insert('my-collection', item);
});

A few notes:

  1. Note the use of the javascript backticks (Javascript template literals).

  2. The parseCsv function parses all values as strings and does not trims extra spaces. For this reason I have used the number conversion for the number field and trim for the city field.

  3. The second parameter of praseCsv is a reviver function, which allows to process values of the csv before they are stored in the result table. The function gets three parameters - the row, column and the value. Whatever it returns is used as the value in the table.
    Home this helps,
    Yoav

Yes! I want this feature. Thanks for showing us a way to get it done. I’m still hoping for a “Wix” way to do it! lol!

Yes!

Have you seen https://www.wix.com/code/home/coming-soon ? :slight_smile:

Yep! It’s on the way.

I want to see Zapier supported by Wix, as a standard offering vs. the current Wix answer of developing Wix API code.