 #
 mongo = require "mongoskin"
 BSON = mongo.BSONPure
+collectionName = "pages"
 
 if process.env.MONGOHQ_URL
-  connection_string = process.env.MONGOHQ_URL
+  connectionString = process.env.MONGOHQ_URL
 else
-  connection_string = "mongodb://localhost:27017/nodelove"
+  connectionString = "mongodb://localhost:27017/nodelove"
 
-db = mongo.db(connection_string + "?auto_reconnect",
+db = mongo.db(connectionString + "?auto_reconnect",
   safe: false
 )
 
 db.open (err, db) ->
   unless err
     console.log "Connected to 'nodelove' database"
-    db.collection "pages", (err, collection) ->
-      if err
+    db.collection(collectionName).find().toArray (err, items) ->
+      unless items.length
         populateDB()
 
 #

    showHero: false
  ]

  db.collection "pages", (err, collection) ->
    collection.insert pages,
      safe: true
    , (err, result) ->


  catch err
    lookup["slug"] = id

  db.collection(collectionName).findOne lookup, (err, item) ->
    if err
      res.send error: "A DB error has occurred"
    if item
      res.send item
    else
      res.send 404

# Find all pages
exports.findAll = (req, res) ->
  db.collection(collectionName).find().toArray (err, items) ->
    if err
      res.send error: "A DB error has occurred"
    if items
      res.send items
    else
      res.send 404

#
# Render main layout
