@bygd/sg-web-db-client
v1.15.4
Published
This project is an integrated client for connecting and running operations on multiple database connections.
Downloads
9
Readme
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
Installation
NVM (Node Version Manager)
You have two options to install NVM:
cURL
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
wget
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) :
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:
nvm --help
Node.js 14
Once you have nvm running install Node.js 14
nvm install
This command will search for the .nvmrc file and install the version.
Every time you run this project you can switch to the correct node version by running:
nvm use
Mongo DB
Make sure you have mongo running:
mongo --version
NPM @sgorg
You need to create an account in npm and request in the #sbs-support slack channel, that your username is added to the @sgorg organization.
Once your username is added to the @sgorg organization, login:
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:
npm install
Start development watch mode
Then you can start the development watch mode:
npm start
Babel will compile the client in the ./dist directory everytime you make changes to any file in the ./src directory.
Once you compile the client, you can link it to your local node package manger (npm)
npm link
This will create a symlink of the @sgorg/web-db-client
at the current version stated in the package.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:
npm install
And then to use the local development version:
npm link @sgorg/web-db-client
Set application secrets
Then you need to copy the .env.example file and provide the credentials for this project. To get them ask for it in the #web-games-it 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
- 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
import { connectDbClient } from '@sgorg/web-db-client';
async function getMongoClient(){
const connection = await connectDbClient(); // Returns a mongoose connection class
return connection.client;
}
Using models
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
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}`],
}),
]);
}