 pg = require 'pg'
 pgurl = dbconf.postgres
 
-DEBUG_updateRowsLimit = 10 #null  # set for debugging
+DEBUG_updateRowsLimit =  null  # set for debugging
 OMIT_NULL_VALUE_FIELDS_IN_COMMITMENTS_JSON = true
  
 PATH = "data/imports/"
 TEMP_DIR = "temp"
 AIDDATA_TEMP_FILE = PATH + TEMP_DIR + "/aiddata.json"
 LOCATIONS_TEMP_FILE = PATH + TEMP_DIR + "/locations.json"
+IMPORT_COMMITMENT_DATES_AS_DATES = true
 
 USE_MONGOIMPORT = true
 

    os.mkdir TEMP_DIR
    fd = fs.openSync(AIDDATA_TEMP_FILE, 'w')

    epochMillis = (field, q) -> "EXTRACT(EPOCH FROM #{q})*1000 AS #{field}"

    dateFields = [
      epochMillis("date", "COALESCE(commitment_date, start_date, to_timestamp(to_char(year, '9999'), 'YYYY'))"),
      epochMillis("start_date", "start_date"),
      epochMillis("commitment_date", "commitment_date"),
      epochMillis("end_date", "end_date"),
    ].join(',')
    
    query = pgclient.query "
        SELECT
          *, #{dateFields}
        FROM 
          aiddata2 
        #{if DEBUG_updateRowsLimit? then 'LIMIT '+DEBUG_updateRowsLimit}

      row.origin = locationNameToCode[row.donor]
      row.dest = locationNameToCode[row.recipient]

      if IMPORT_COMMITMENT_DATES_AS_DATES
        for f,q of dateFields 
          if row[f]?
            row[f] = { "$date" : row[f] }

      delete row.donor
      delete row.donorcode
