var config = require("./config");
var documentDbClient = require("documentdb").DocumentClient;

console.log(config.primaryKey);
var client =  new documentDbClient(config.uri, { "masterKey" : config.primaryKey});

var databaseUrl = `dbs/${config.database.id}`;
var collectionUrl = `${databaseUrl}/colls/${config.collection.id}`;
var HttpStatusCodes = { NOTFOUND: 404};

console.log("databaseUrl " + databaseUrl);
console.log("collectionUrl " + collectionUrl);

createCollectionIfnotExists();

function createDatabaseIfNotExists(){
  console.log("databaseUrl" + databaseUrl);

  client.readDatabase(databaseUrl, (err, result) => {
    if(err){
      if (err.code == HttpStatusCodes.NOTFOUND) {
        client.createDatabase(config.database, (err, created ) => {
          if (err) {
            console.log(JSON.stringify(err));
          }else {
            console.log("database created", JSON.stringify(created));
          }
        });
      }else {
        console.log("database created" , JSON.stringify(result));
      }
    }
  });
}

function createCollectionIfnotExists(){
  console.log("Getting collection: " + config.collection.id);

  client.readCollection(collectionUrl, (err, result) => {
    if (err) {
      if (err.code == HttpStatusCodes.NOTFOUND) {
        client.createCollection(databaseUrl, config.collection, {offerThroughput: 1000}, (e, c) => {
          if (err) {
            console.log("Error :" + JSON.stringify(e) );
          }else {
            console.log("Created : " + JSON.stringify(c) );
          }
        });
      }
    }else {
      console.log("result collection:" + JSON.stringify(result) );
    }
  });

}

@@ -457,40 +457,8 @@
 );%0A%0A
-createCollectionIfnotExists();%0A%0A
 func
@@ -1612,8 +1612,859 @@
  %7D);%0A%0A%7D%0A
+%0A(function createDocumentIfNotExists(documents)%7B%0A%0A  for (let i = 0; i %3C documents.length; i++) %7B%0A    let documentUrl = collectionUrl + %22/docs/%22 + documents%5Bi%5D.id;%0A%0A    console.log(%22documentUrl%22 + documentUrl);%0A%0A    client.readDocument(documentUrl, null, (err, result) =%3E %7B%0A      if (err) %7B%0A        if (err = HttpStatusCodes.NOTFOUND) %7B%0A%0A          client.createDocument(collectionUrl, documents%5Bi%5D, (err, created) =%3E %7B%0A            if (err) %7B%0A              console.log('Error on creation' + JSON.stringify(err));%0A            %7Delse %7B%0A              console.log('Document created' + JSON.stringify(created));%0A            %7D%0A          %7D);%0A%0A%0A        %7Delse %7B%0A          console.log('Error except NOTFOUND' + JSON.stringify(err));%0A        %7D%0A      %7Delse %7B%0A        console.log('result is' + JSON.stringify(result)+%22%5Cn%22);%0A      %7D%0A    %7D);%0A  %7D%0A%0A%7D(config.documents));%0A

Add function CreateDocument