   catch err
     lookup["slug"] = id
 
-  db.collection "pages", (err, collection) ->
-    collection.findOne lookup, (err, item) ->
-      res.send error: "A DB error has occurred"  if err
-      if item
-        res.send item
-      else
-        res.send 404
+  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 "pages", (err, collection) ->
-    collection.find().toArray (err, items) ->
-      if err
-        res.send error: "A DB error has occurred"
-      if items
-        res.send items
-      else
-        res.send 404
+  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

    showHero: false
  ]

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


#
mongo = require "mongoskin"
BSON = mongo.BSONPure
collectionName = "pages"

if process.env.MONGOHQ_URL
  connectionString = process.env.MONGOHQ_URL
else
  connectionString = "mongodb://localhost:27017/nodelove"

db = mongo.db(connectionString + "?auto_reconnect",
  safe: false
)

db.open (err, db) ->
  unless err
    console.log "Connected to 'nodelove' database"
    db.collection(collectionName).find().toArray (err, items) ->
      unless items.length
        populateDB()

#
