Solved uploading lots of items from csv

Hey All,
I had someone write some Java script that can upload into your database more than 1000 items, you will have to modify it to your needs but I’m sure it can help someone. I was able to upload 2957 items

Here it is

var request = new XMLHttpRequest();
request.open('GET', 'https:', true);
request.send(null);
request.onreadystatechange = function () {
    if (request.readyState === 4 && request.status === 200) {
        var type = request.getResponseHeader('Content-Type');
        if (type.indexOf("text") !== 1) {
                           var csvString = request.responseText;
            var fieldEndMarker  = /([,\015\012] *)/g; /* Comma is assumed as field separator */
            var qFieldEndMarker = /("")*"([,\015\012] *)/g; /* Double quotes are assumed as the quote character */
            var startIndex = 0;
            var records = [], currentRecord = [];
            do {
                // If the to-be-matched substring starts with a double-quote, use the qFieldMarker regex, otherwise use fieldMarker.
                var endMarkerRE = (csvString.charAt (startIndex) == '"')  ? qFieldEndMarker : fieldEndMarker;
                endMarkerRE.lastIndex = startIndex;
                var matchArray = endMarkerRE.exec (csvString);
                if (!matchArray || !matchArray.length) {
                    break;
                }
                var endIndex = endMarkerRE.lastIndex - matchArray[matchArray.length-1].length;
                var match = csvString.substring (startIndex, endIndex);
                if (match.charAt(0) == '"') { // The matching field starts with a quoting character, so remove the quotes
                    match = match.substring (1, match.length-1).replace (/""/g, '"');
                }
                currentRecord.push (match);
                var marker = matchArray[0];
                if (marker.indexOf (',') < 0) { // Field ends with newline, not comma
                    records.push (currentRecord);
                    currentRecord = [];
                }
                startIndex = endMarkerRE.lastIndex;
            } while (true);
            if (startIndex < csvString.length) { // Maybe something left over?
                var remaining = csvString.substring (startIndex).trim();
                if (remaining) currentRecord.push (remaining);
            }
            if (currentRecord.length > 0) { // Account for the last record
                records.push (currentRecord);
            }


            var fields;
            var objects = [];
            records.forEach(function (t, number) {
                if (t.length !== 1) {
                    if (number === 0) {
                        fields = t;
                    }
                    else {
                        var obj = {};
                        // var l = [];
                        t.forEach(function (t2, number2) {
                            // if (number2 < fields.length - 1)
                            obj[fields[number2]] = t2;
                            // else
                            //     l.push(t2.replace('["', '').replace('"]', '').replace('"', '').replace('"', ''));
                        });
                        // obj[fields[fields.length - 1]] = l;
                        objects.push(obj);
                    }
                }
            });


            objects.forEach(function (t, n) {
                var str = JSON.stringify(t);
                var xmlHttpRequest = new XMLHttpRequest();
                xmlHttpRequest.open('POST', 'https://plumbingpos.bubbleapps.io/version-test/api/1.1/obj/test_import', true);
                xmlHttpRequest.setRequestHeader('Content-Type', 'application/json');
                xmlHttpRequest.setRequestHeader('Authorization', 'Bearer 5c5d6fd4fe0f1112c572e0616c5714cd');
                xmlHttpRequest.send(str);
                console.log(n);
                console.log(t);
            });
            alert("Uploaded " + objects.length + " entries.");
            }
        }
    };
2 Likes

Hi Chad, Thank you for this! I’m not quite sure how to implement it though.

Would you please provide the context around how you would use this? Is this embedded in the csv itself somewhere or in a custom HTML somewhere within the app? I’m still learning some of the functionality in Bubble but this definitely looks useful.

2 Likes

Hey I think this has been made obsolete by the new create and csv parser plug-ins

Does anybody have anything to share about these new plugins?