npm package: @mimik/mongooser
Description: Helper for setting up mongodb using mongoose for mimik microservices
Author: mimik technology inc
Latest version: 4.0.2

<a name="module_mongooser"></a>

## mongooser
**Example**  
```js
import mongodb from '@mimik/mongooser';
```

* [mongooser](#module_mongooser)
    * [module.exports(config)](#exp_module_mongooser--module.exports) ⇒ <code>object</code> ⏏
        * _async_
            * [~validate()](#module_mongooser--module.exports..validate) ⇒ <code>Promise</code>
        * _sync_
            * [~getKMSProviders()](#module_mongooser--module.exports..getKMSProviders) ⇒ <code>object</code>
            * [~initializeSync()](#module_mongooser--module.exports..initializeSync) ⇒ <code>object</code>
            * [~replicaSet()](#module_mongooser--module.exports..replicaSet) ⇒ <code>boolean</code>

<a name="exp_module_mongooser--module.exports"></a>

### module.exports(config) ⇒ <code>object</code> ⏏
Creates a MongoDB connection manager for mimik microservices.

**Kind**: Exported function  
**Returns**: <code>object</code> - Object with methods: initializeSync, validate, replicaSet, getKMSProviders.  

| Param | Type | Description |
| --- | --- | --- |
| config | <code>object</code> | Service configuration object. |
| config.mongoSettings | <code>object</code> | MongoDB connection settings. |
| config.mongoSettings.url | <code>string</code> | MongoDB connection URL. |
| config.mongoSettings.options | <code>object</code> | Mongoose connection options. Must include `heartbeatFrequencyMS`. |
| [config.mongoSettings.password] | <code>string</code> | MongoDB password (masked in production logs). |
| config.mongoSettings.reconnectOffset | <code>number</code> | Reconnect offset in seconds. |
| config.mongoSettings.connectTimeout | <code>number</code> | Connection timeout in seconds. |
| config.mongoSettings.validationCheck | <code>number</code> | Validation check delay in milliseconds. |
| config.nodeEnvironment | <code>string</code> | Current node environment (e.g. 'production'). |
| [config.encryption] | <code>object</code> | Encryption settings for client-side field level encryption. |
| [config.encryption.set] | <code>string</code> | Set to 'on' to enable auto-encryption. |
| [config.encryption.kmsProvider] | <code>string</code> | KMS provider type ('local' or 'aws'). |
| [config.encryption.localMasterKey] | <code>string</code> | Base64-encoded local master key. |
| [config.encryption.aws] | <code>object</code> | AWS KMS provider settings. |
| [config.encryption.database] | <code>string</code> | Database name for key vault. |
| [config.encryption.keyVaultTable] | <code>string</code> | Collection name for key vault. |

<a name="module_mongooser--module.exports..validate"></a>

#### module.exports~validate() ⇒ <code>Promise</code>
Database connection validation.

**Kind**: inner method of [<code>module.exports</code>](#exp_module_mongooser--module.exports)  
**Returns**: <code>Promise</code> - Resolves to null on success.  
**Category**: async  
**Throws**:

- <code>Error</code> Will throw an error if the database connection is not `connected`.

Will exit 1 if the connection request times out and the connection state is not `connected`.

**Requires**: <code>module:@mimik/sumologic-winston-logger</code>  
<a name="module_mongooser--module.exports..getKMSProviders"></a>

#### module.exports~getKMSProviders() ⇒ <code>object</code>
AutoEncryption KMSProvider.

**Kind**: inner method of [<code>module.exports</code>](#exp_module_mongooser--module.exports)  
**Returns**: <code>object</code> - The KMSProviders object.

Will return the kmsProvider settings for the service.
Requires `config.encryption` to be provided to the factory.  
**Category**: sync  
**Throws**:

- <code>TypeError</code> If encryption config was not provided to the factory.

<a name="module_mongooser--module.exports..initializeSync"></a>

#### module.exports~initializeSync() ⇒ <code>object</code>
Database initialization.

**Kind**: inner method of [<code>module.exports</code>](#exp_module_mongooser--module.exports)  
**Returns**: <code>object</code> - The mongoose database object. Connection is established asynchronously in the background.

When encryption is enabled, configures auto-encryption by setting `keyVaultNamespace` and `kmsProviders`
on the mongoose connection options.
Check MongoDB documentation for the full autoEncryption specification: https://docs.mongodb.com/manual/reference/method/Mongo/#clientsidefieldlevelencryptionoptions

Will exit 1 if the connection request generates an error or the connection state is `disconnected` or `disconnecting`.  
**Category**: sync  
**Requires**: <code>module:@mimik/sumologic-winston-logger</code>  
<a name="module_mongooser--module.exports..replicaSet"></a>

#### module.exports~replicaSet() ⇒ <code>boolean</code>
Indicates if the replica is set.

**Kind**: inner method of [<code>module.exports</code>](#exp_module_mongooser--module.exports)  
**Returns**: <code>boolean</code> - `True` if replica is set, `False` if not.  
**Category**: sync