Question

I want my docpad website to use a mongoDb rather than the file system for the storage of documents (Heroku deployment). Is there a docpad plugin for this? I can't seem to find a straight forward way of getting docpad to use mongo. I've already looked at Need explanation for Docpad persistence - but that doesn't give the actual technical details of achieving the connection

Was it helpful?

Solution

I am not aware of any mongodb plugin. The easiest starting point might be to take the tubmlr plugin and adapt it to read form mongodb instead of from the tumblr rss/atom feeds.

The code for that plugin is not terribly complex. First, you'd want to replace the fetchTumblrData function with one that gets data from MongoDB instead and then you'd want to adapt the code in populateCollections to turn that data into virtual documents.

OTHER TIPS

I have been working on some code that can now read from Mongodb and return an object that can be rendered into docs. I've also tried to write some code to provide the backend for basic editing of the database but the regeneration after update is not yet working (although it may be by the time you read this!).

The key section (with credit to BALupton for helping with the callback complexity) is below.

extendTemplateData: (opts,next) ->
    config = @getConfig()

    mongoose.connect(config.uristring)          
    db = mongoose.connection

    db.on 'error', (err) ->
        docpad.error(err)  # you may want to change this to `return next(err)`

    db.once 'open', ->
        queries = config.queries
        queryCount = 0
        totalQueries = Object.keys(queries).length

        for index, query of queries
            ((indexClosure) ->
                Dbdata.find query.predicate, (err, data) ->
                    opts.templateData[indexClosure] = data

                    if (++queryCount == totalQueries)
                        mongoose.connection.close()
                        return next(err) if err
                        return next(null, data)
            )(index)
    # Chain
    @

See https://github.com/simonh1000/docpad-plugin-mongo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top