 log = require('printit')
     date: true
     prefix: 'Cozy DB'
-
 
 # Public: the Model constructor
 module.exports.Model = Model = require './model'

        requestName: 'all'
        requestDefinition: defaultRequests.all

    step = (i = 0) ->
        {model, requestName, requestDefinition, optional} = requestsToSave[i]
        log.info "#{model.getDocType()} - #{requestName} request creation..."
        model.defineRequest requestName, requestDefinition, (err) ->
            if err and not optional
                log.raw err
                log.error "A request creation failed, abandon."
                callback err

            else if i + 1 >= requestsToSave.length
                log.info "requests creation completed"
                callback null

            else
                log.info "succeeded"
                step i + 1
    # loop over them asynchroniously
    step 0

module.exports.api = api = require './api'


maybeSetupPouch = (options) ->
    # if we are given a db or dbName options
    # or env variable is set
    # the app is meant to be used standalone
    if process.env.RUN_STANDALONE or options.db or options.dbName
        Pouch = require 'pouchdb'
        PouchModel = require './pouchmodel'
        module.exports.CozyModel = CozyModel = PouchModel
        if options.db
            PouchModel.db = options.db
        else
            options.dbName ?= process.env.POUCHDB_NAME or 'cozy'
            PouchModel.db = new Pouch options.dbName

getRequests = (root) ->
    modelPath = "#{root}/server/models/"
    # get the requests file
    requests = require modelPath + "requests"

    # get all requests from the request file into an array
    requestsToSave = []
