packages/mongo/lib/utils.js
async function connect (mongoose, connectionName, connectionOpts) {
  const isDefault = connectionName === 'default'

  // Normalize and destructure connection options
  if (typeof connectionOpts === 'string') {
    connectionOpts = { uri: connectionOpts }
  }
  let { uri, options, forceReconnect } = connectionOpts

  // Apply default options
  // https://mongoosejs.com/docs/connections.html#options
  options = {
    useNewUrlParser: true,
    useCreateIndex: true,
    ...options
  }

  // Manualy create connection
  let conn
  if (isDefault) {
    conn = mongoose.connection
  } else {
    conn = new mongoose.Connection(mongoose)
    mongoose.connections.push(conn)
  }

  // $logger helper
  conn.$logger = isDefault
    ? consola
    : consola.withTag(connectionName)

  // Log connection events
  logConnectionEvents(conn)

  // $connect helper
  conn.$connect = () => {
    return new Promise((resolve, reject) => {
      conn.openUri(uri, options, (error) => {
        if (error) {
          reject(error)
        } else {
          resolve(conn)
        }
      })
    })
  }

  // Make accessable via mongoose.$connectionName
  mongoose['$' + connectionName] = conn

  // Setup force reconnect
  if (forceReconnect) {
    const timeout = forceReconnect === true ? 1000 : forceReconnect
    conn.on('error', () => {
      conn.close()
    })
    conn.on('disconnected', () => {
      setTimeout(() => { conn.$connect() }, timeout)
    })
  }

  // Connect
  await conn.$connect()
  await conn.$initialConnection
}
async function connect (mongoose, connectionName, connectionOpts) {
  const isDefault = connectionName === 'default'

  // Normalize and destructure connection options
  if (typeof connectionOpts === 'string') {
    connectionOpts = { uri: connectionOpts }
  }
  let { uri, options, forceReconnect } = connectionOpts

  // Apply default options
  // https://mongoosejs.com/docs/connections.html#options
  options = {
    useNewUrlParser: true,
    useCreateIndex: true,
    ...options
  }

  // Manualy create connection
  let conn
  if (isDefault) {
    conn = mongoose.connection
  } else {
    conn = new mongoose.Connection(mongoose)
    mongoose.connections.push(conn)
  }

  // $logger helper
  conn.$logger = isDefault
    ? consola
    : consola.withTag(connectionName)

  // Log connection events
  logConnectionEvents(conn)

  // $connect helper
  conn.$connect = () => {
    return new Promise((resolve, reject) => {
      conn.openUri(uri, options, (error) => {
        if (error) {
          reject(error)
        } else {
          resolve(conn)
        }
      })
    })
  }

  // Make accessable via mongoose.$connectionName
  mongoose['$' + connectionName] = conn

  // Setup force reconnect
  if (forceReconnect) {
    const timeout = forceReconnect === true ? 1000 : forceReconnect
    conn.on('error', () => {
      conn.close()
    })
    conn.on('disconnected', () => {
      setTimeout(() => { conn.$connect() }, timeout)
    })
  }

  // Connect
  await conn.$connect()
  await conn.$initialConnection
}
async function connect ( mongoose , connectionName , connectionOpts ) { const isDefault = connectionName === 'default' // Normalize and destructure connection options if ( typeof connectionOpts === 'string' ) { connectionOpts = { uri : connectionOpts } } let { uri , options , forceReconnect } = connectionOpts // Apply default options // https://mongoosejs.com/docs/connections.html#options options = { useNewUrlParser : true , useCreateIndex : true , ... options } // Manualy create connection let conn if ( isDefault ) { conn = mongoose . connection } else { conn = new mongoose . Connection ( mongoose ) mongoose . connections . push ( conn ) } // $logger helper conn . $logger = isDefault ? consola : consola . withTag ( connectionName ) // Log connection events logConnectionEvents ( conn ) // $connect helper conn . $connect = ( ) => { return new Promise ( ( resolve , reject ) => { conn . openUri ( uri , options , ( error ) => { if ( error ) { reject ( error ) } else { resolve ( conn ) } } ) } ) } // Make accessable via mongoose.$connectionName mongoose [ '$' + connectionName ] = conn // Setup force reconnect if ( forceReconnect ) { const timeout = forceReconnect === true ? 1000 : forceReconnect conn . on ( 'error' , ( ) => { conn . close ( ) } ) conn . on ( 'disconnected' , ( ) => { setTimeout ( ( ) => { conn . $connect ( ) } , timeout ) } ) } // Connect await conn . $connect ( ) await conn . $initialConnection }
Utility function to setup a connection
Utility function to setup a connection