CSV

The Cloud CMS Bulk Import tool makes it easy to import content from a CSV (comma-separated value) text file into a Cloud CMS content repository.

Suppose you have a CSV data set consisting of the 2014 World Cup Finals matches. It might look something like this:

match_num,round/group,date,time,stadium,where,home,score,away,home_scorers,away_scorers
49,Round of 16,Jun 28,13:00,Estádio Mineirão,Belo Horizonte,Brazil,"3-2 pen. 1-1 (1-1, 1-1)",Chile,David Luiz 18', Alexis Sánchez 32'
50,Round of 16,Jun 28,17:00,Estádio do Maracanã,Rio de Janeiro,Colombia,2-0 (1-0),Uruguay,"James Rodríguez 28', 50'",
51,Round of 16,Jun 29,13:00,Estádio Castelão,Fortaleza,Netherlands,2-1 (0-0),Mexico,Wesley Sneijder 88' Klaas Jan Huntelaar 90' (pen), Giovani dos Santos 48'

Obviously, there are many more matches, but let's assume this to be a representative extract in CSV format. The first row contains header information. All other rows are data.

We can import this content into Cloud CMS by using the cloudcms-packager module like this:

var csv = require("csv");
var fs = require("fs");

// create a packager
var PackagerFactory = require("cloudcms-packager");
PackagerFactory.create(function(err, packager) {

    // parse CSV
    var csvText = fs.readFileSync("./data/2014-world-cup-final-matches.csv").toString();
    csv.parse(csvText, function (err, array) {

        // first row is the header row
        var header = array[0];

        // generate all of the content instances
        for (var i = 1; i < array.length; i++)
        {
            var entry = JSON.parse(JSON.stringify(array[i]));

            var contentInstanceObject = {};
            contentInstanceObject.title = entry[0];

            for (var z = 0; z < header.length; z++)
            {
                contentInstanceObject[header[z]] = entry[z];
            }

            packager.addNode(contentInstanceObject);
        }

        // commit
        packager.package(function(err, archiveInfo) {
            console.log("Wrote archive: " + archiveInfo.filename);
        });
    });
});

For a full example of this code in action, please see:

https://github.com/gitana/cloudcms-packager/tree/master/examples/csv/world-cup-2014-final-matches/example1