 
     db = new mongo.Db dbConf.dbName, @server
 
-    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)->
+    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 create user #{dbConf.dbUser} and database #{dbConf.dbName}: #{err}"
-                  callback? "[ERROR] can't create user #{dbConf.dbUser} and database #{dbConf.dbName}: #{err}"
+                  log.error "[ERROR] can't authenticate with admin credentials: #{err}"
+                  callback?  "[ERROR] can't authenticate with admin credentials: #{err}"
                 else
-                  log.debug res
-                  log.info "[OK] user #{dbConf.dbUser} and database #{dbConf.dbName} has been created"
-                  log.info dbConf
-                  callback? null,dbConf
+                  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)->
 

mongoDB = new MongoDB config
module.exports = mongoDB

# options =
#    username : "gokment"   
#    dbName    : "dlskdlskdlsk"

#mongoDB.createDatabase options,(err,res)->
#  console.log err,res

# mongoDB.fetchDatabaseList options,(res)->
#   console.log res
#dbs =  ['gokment_c219e4c7','gokment_df539bb7']
#for db in dbs
#  do (db)->

    dbConf = {dbName,dbUser,dbPass}

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

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