 #log     = log4js.addAppender log4js.fileAppender(logFile), "[MongoDBApi]"
 log     = log4js.getLogger('[MongoDBApi]')
 
+class AccessError extends Error
+  constructor:(@message)->
+
+class KodingError extends Error
+  constructor:(message)->
+    return new KodingError(message) unless @ instanceof KodingError
+    Error.call @
+    @message = message
+    @name = 'KodingError'
+
 class MongoDB
 
   appendKodingUsername = (username, str)->

    dbConf = {dbName,dbUser,dbPass}

    dbConf.dbHost = @mongoHost
    dbConf.dbType = "mongo"/

    dbConf.dbPass = uniqueId()
    dbConf.dbUser = dbConf.dbName


    db = new mongo.Db dbConf.dbName, @server

    dbCount = (username,callback) =>
      @fetchDatabaseList username:username,(err,dbsArray)->
        if err then callback err
        else
          callback null,dbsArray.length
  
    dbCount username,(err,dbNr)=>
      unless err
        if dbNr > 20 # 0..4
          log.error e = "You exceeded your quota, please delete one before adding a new one."
          callback new KodingError e
        else
          db.open (err,db)=>
            if err?
              log.error "[ERROR] can't open database #{dbConf.dbName}: #{err}"
              callback? "[ERROR] can't open database #{dbConf.dbName}: #{err}"
            else
              db.admin().authenticate @mongoUser, @mongoPass,(err,result)=>
                if err?
                  log.error "[ERROR] can't authenticate with admin credentials: #{err}"
                  callback?  "[ERROR] can't authenticate with admin credentials: #{err}"
                else
                  db.addUser dbConf.dbUser,dbConf.dbPass,(err,res)->
                    log.debug err,res
                    db.authenticate dbConf.dbUser,dbConf.dbPass,(err,res)->
                      if err?
                        log.error "[ERROR] can't create user #{dbConf.dbUser} and database #{dbConf.dbName}: #{err}"
                        callback? "[ERROR] can't create user #{dbConf.dbUser} and database #{dbConf.dbName}: #{err}"
                      else
                        log.debug res
                        log.info "[OK] user #{dbConf.dbUser} and database #{dbConf.dbName} has been created"
                        log.info dbConf
                        callback? null,dbConf

  changePassword : (options,callback)->

