'use strict'

let winston = require('winston')
let Rat = require('../api/models/rat')
let Rescue = require('../api/models/rescue')
let User = require('../api/models/user')
let mongoose = require('mongoose')

let count = 0

mongoose.connect('mongodb://localhost/fuelrats')

winston.info('Beginning Index Model Sync')

winston.info('Syncing Rats')
let ratStream = Rat.synchronize()
ratStream.on('data', function (error, doc) {
  count++
})
ratStream.on('error', function (error) {
  console.error(error)
})
ratStream.on('close', function () {
  console.log('Finished syncing Rats')
})

winston.info('Syncing Rescue')
Rescue.synchronize()
let rescueStream = Rescue.synchronize()
rescueStream.on('data', function (error, doc) {
  count++
})
rescueStream.on('error', function (error) {
  console.error(error)
})
rescueStream.on('close', function () {
  console.log('Finished syncing Rescues')
})

@@ -214,16 +214,38 @@
 ount = 0
+%0Alet streamsClosed = 0
 %0A%0Amongoo
@@ -594,16 +594,95 @@
 g Rats')
+%0A%0A  streamsClosed++%0A%0A  if (streamsClosed === 2) %7B%0A    mongoose.disconnect()%0A  %7D
 %0A%7D)%0A%0Awin
@@ -932,32 +932,32 @@
 , function () %7B%0A
-
   console.log('F
@@ -981,12 +981,91 @@
 escues')
+%0A%0A  streamsClosed++%0A%0A  if (streamsClosed === 2) %7B%0A    mongoose.disconnect()%0A  %7D
 %0A%7D)%0A

Exit syncing script when it's done