# Web DB Client
This project is an integrated client for connecting and running operations on multiple database connections.
These connections include 2 different databases, MySQL/DynamoDB and Mongo/DocumentDB.

The web db client is a transition project for unifying all web platform and whitelabel services APIs through abstract models that will directly operate on the databases.

## Requirements

* [NVM / Node.js 14](https://github.com/nvm-sh/nvm)
* [NPM access to @sgorg](https://www.npmjs.com/settings/sgorg/packages)
* [MongoDB Community Edition](https://docs.mongodb.com/manual/installation/)


## Installation

### NVM (Node Version Manager)

You have two options to install NVM:

**cURL**
```bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
```

**wget**
```bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
```

You will have to export some environment variables in your environment file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc) :

```bash
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
```

Verify your installation:
```bash
nvm --help
```

### Node.js 14
Once you have nvm running install Node.js 14
```bash
nvm install
```

This command will search for the [.nvmrc](.nvmrc) file and install the version.

Every time you run this project you can switch to the correct node version by running:
```bash
nvm use
```

### Mongo DB
Make sure you have mongo running:
```bash
mongo --version
```

### NPM [@sgorg](https://www.npmjs.com/settings/sgorg/packages)
You need to create an account in [npm](https://npmjs.com) and request in the [#sbs-support](https://softgames.slack.com/archives/CR12N3L5T) slack channel, that your username is added to the [@sgorg](https://www.npmjs.com/settings/sgorg/packages) organization.

Once your username is added to the [@sgorg](https://www.npmjs.com/settings/sgorg/packages) organization, login:
```bash
npm login
```
You'll be promoted for your username and password.

## Development

### Install dependencies
To start your local server in development mode first install dependencies:
```bash
npm install
```

### Start development watch mode
Then you can start the development watch mode:
```bash
npm start
```
Babel will compile the client in the [./dist](./dist) directory everytime you make changes to any file in the [./src](./src) directory.

Once you compile the client, you can link it to your local node package manger (npm)
```bash
npm link
```
This will create a symlink of the `@sgorg/web-db-client` at the current version stated in the [package.json](./package-lock.json) file.

## Usage
Once you have linked the web db client to your local npm you can now install the client in other projects by installing the package:

```bash
npm install
```

And then to use the local development version:
```bash
npm link @sgorg/web-db-client
```

### Set application secrets
Then you need to copy the [.env.example](.env.example) file and provide the credentials for this project. To get them ask for it in the [#web-games-it](https://softgames.slack.com/archives/C011DV9AAS2) slack channel.

Below there's information about each secret:
* **MONGO_DB_HOST** - This is the host for the mongodb connection uri
* **MONGO_DB_USERNAME** - Mongo database username
* **MONGO_DB_PASSWORD** - Mongo database password
* **MONGO_DB_PORT** - Port in which mongo process is running
* **SGME_ACCESS_KEY_ID** - Access Key Id for SGME AWS account (for cloudfront and s3 operations)
* **SGME_SECRET_ACCESS_KEY** - Secret Access Key for SGME AWS account (for cloudfront and s3 operations)
* **SGWEB_ACCESS_KEY_ID** - Access Key Id for SGWEB AWS account (for cloudfront and s3 operations)
* **SGWEB_SECRET_ACCESS_KEY** - Secret Access Key for SGWEB AWS account (for cloudfront and s3 operations)
* **LOCALE_APP_API_KEY** - API Key for access to import translations from [app.localeapp.com](https://app.localeapp.com)
* **ENCRYPTION_KEY** - Encryption secret key for mongodb field encryption (only some fields in the game model use it)
* **BOT_SERVICE_USERNAME** - Username for access to bot-service for x-promo service integration
* **BOT_SERVICE_PASSWORD** - Password for access to bot-service for x-promo service integration
* **AD_REPORTING_SERVICE_USERNAME** - Username for access to ad-reporting-service for dfp key value pair creation
* **AD_REPORTING_SERVICE_PASSWORD** - Password for access to ad-reporting-service for dfp key value pair creation
* **PLATFORM_CLOUDFRONT_DISTRIBUTION_ID** - Cloudfront distribution for invalidation of PLATFORM
* **GIS_CLOUDFRONT_DISTRIBUTION_ID** - Cloudfront distribution for invalidation of the GIS
* **PS_CLOUDFRONT_DISTRIBUTION_ID** - Cloudfront distribution for invalidation of PS
* **GAM_AUTH_PRIVATE_KEY_ID** - Private Key Id for Google Ad Manager Authentication (available from Saket)
* **GAM_AUTH_PRIVATE_KEY** - Private Key for Google Ad Manager Authentication (available from Saket). Make sure to wrap the value in double quotes (") since it contains /n.
* **GAM_AUTH_CLIENT_EMAIL** - Client Email for Google Ad Manager Authentication (available from Saket)
* **GAM_AUTH_CLIENT_ID** - Client Id for Google Ad Manager Authentication (available from Saket)
* **GAM_AUTH_CLIENT_CERT_URL** - Client Certificate Url for Google Ad Manager Authentication (available from Saket)
* **GAM_AUTH_PROJECT_ID** - Project Id for Google Ad Manager Authentication (available from Saket)
* **ADS_SERVICE_USERNAME** - Username for access to the P2 Ads Service for Ad Unit Creation integration
* **AD_SERVICE_PASSWORD** - Password for access to the P2 Ads Service for Ad Unit Creation integration

### Connecting to the database
```javascript
import { connectDbClient } from '@sgorg/web-db-client';
async function getMongoClient(){
  const connection = await connectDbClient(); // Returns a mongoose connection class
  return connection.client;
}
```

### Using models
```javascript
import { Publisher, Category } from '@sgorg/web-db-client';

function getPublisherByName(name){
  return Publisher.getByName(name);
}

function getCategoryByIdOrSlug(id){
  return Category.getByIdOrSlug(id);
}
```

### Using constants and utilities
```javascript
import { consts, utils } from '@sgorg/web-db-client';
import { postDFPKeyValuePair } from 'lib/apis/ad-reporting'

const {slugify, promise: { allArray, resolveAll } } = utils
const { DFP } = consts;
const { GAME_AD_MANAGER_KEY, PUBLISHER_GAME_AD_MANAGER_KEY, GAME_PAGE_BANNER_AD_GAME } = DFP;

function resolveMultiplePromises(promises = []){
  return allArray(promises)
}

function slugifyText(text){
  return slugify(text);
}

function generateKeyValuePairs(publisherId, gameId){
  return resolveAll([
    postDFPKeyValuePair({
      key: GAME_AD_MANAGER_KEY,
      values: [gameId],
    }),
    postDFPKeyValuePair({
      key: PUBLISHER_GAME_AD_MANAGER_KEY,
      values: [`${publisherId}_${gameId}`],
    }),
  ]);
}
```
