   JName               = require './name'
   JKite               = require './kite'
   JStorage            = require './storage'
-  JAppStorage         = require './appstorage'
   JCombinedAppStorage = require './combinedappstorage'
 
   @getFlagRole            = 'content'

      return callback err, storage


  migrateOldAppStorageIfExists: (options, callback) ->

    { appId, version } = options

    unless appId and version
      return callback new KodingError 'version and appId is not set!'

    accountId    = @getId()
    newStorage   = null
    oldStorages  = null
    luckyStorage = null

    queue = [

      =>
        # trying to fetch old app storages, there were more than one
        # appStorages for the same appId and version, probably due to some bug
        # so we are trying to pick the one that contains data in it
        query = { 'data.appId':appId, 'data.version':version }
        @fetchAppStorages query, (err, storages) ->
          return calback err  if err
          # returning storage null bcs there was no old storage to be migrated
          return callback null, null  unless storages.length > 0

          storages.forEach (storage) ->
            # if storage bucket is not empty pick it as the lucky one
            luckyStorage = storage  unless _.isEmpty storage?.bucket

          # if there was no lucky storage, pick the first one
          luckyStorage ?= storages[0]
          oldStorages   = storages
          queue.next()

      =>
        # creating new app storage document
        options.data = luckyStorage?.bucket or {}
        @createAppStorage options, (err, storage) ->
          return callback err  if err
          newStorage = storage
          queue.next()

      ->
        _queue = []
        # removing old storages
        oldStorages.forEach (oldStorage) ->
          _queue.push ->
            oldStorage.remove (err) ->
              return _queue.fin err  if err
              _queue.fin()

        dash _queue, (err) ->
          return callback err  if err
          queue.next()

      ->
        callback null, newStorage

    ]

    daisy queue


  fetchAppStorage$: secure (client, options, callback) ->

    unless client?.connection?.delegate


      # bongo doesn't wait models to be loaded and this causes errors
      # in node.js tests so synchronously requiring them
      JCredential      = require './computeproviders/credential'
      JStackTemplate   = require './computeproviders/stacktemplate'

      return {
        storage       :
          as          : 'storage'
          targetType  : 'JStorage'
