 
   _getEventTypes: (events) -> events.map (event) -> event.typeName()
 
+  _getLastCommit: (sourceId) ->
+    @commits.findOne(
+      { sourceId: sourceId.toString() }, # selector
+      { sort: [['version', 'desc']], fields: { version: 1 } } # options
+    )
+
   _logMsg: (message) ->
     "#{@configuration.appId}: #{this}: #{message}"

      try
        commitId = @commits.insert commit
      catch error
        throw new Error "While inserting:\n
          #{JSON.stringify(commit)}\n
          error:#{error.message}\n
          stack:#{error.stack}"

      @commitPublisher.publishCommit
        _id: commitId,

    if !changes.commands? then changes.commands = []

    # fetch last inserted batch to get the current version
    lastCommit = @_getLastCommit(sourceId)
    if lastCommit?
      # take version of last existing commit
      currentVersion = lastCommit.version
