@bampich.ok/data

1.0.1 • Public • Published

About

provides data access for redis and cockroachdb

Install

npm install @bampich.ok/data

Usage

const {
  RunWithClient,
  RedisClient,
  ConnectionPool
} = require('@bampich.ok/data');
const runWithClient = RunWithClient.RunWithClient;
const format = require('pg-format');

//use RedisClient the same way you would use ioredis
//check https://github.com/luin/ioredis
let redisClient = new RedisClient();
let storeUserLocation = new Promise(async (resolve, reject) => {
  redisClient.geoadd(
    "users",//data set
    0.0, //longitude
    0.0, //latitude
    "userId", //user key
    (error, result) => {
      try {
        if (error) {
          return reject(error);
        }
        return resolve("ok");
      } finally {
        redisClient.quit();
      }
    });
})
.then(result=>console.log(result))
.catch(error=> throw error);

//use RunWithClient to execute SQL statements
let readUserProfiles = return new Promise(async (resolve, reject) => {
  runWithClient(async (client) => {
    let command = format.withArray(
      'SELECT * FROM PROFILES WHERE ID = %L LIMIT 10 offset %L',
      [id, optionalOffset || 0]);
    let profiles = await client.query(command);
    await new ConnectionPool().getInstance().end(); // release the connection
    return resolve(profiles);
  }, reject);
})
.then(resultSet=>{
  resultSet.rows.map(profile=>{handleProfile(profile)});
  //release the connection if you are no longer using it
  RedisClient.instance.disconnect();
  RedisClient.instance = null;
  (new ConnectionPool()).getInstance().end();
})
.catch(error=>throw error);

Configure

The following environment variables control the configuration for cockroachDB:

  • ROACH_CERTS_FOLDER: where to find ca.crt,client.roach.key,client.roach.crt.
  • ROACH_USERNAME: (optional) user name for the roach user.
  • ROACH_SERVICE_HOST: IP address for connecting to cockroachDB.
  • ROACH_SERVICE_PORT_GRPC: port number for connecting to cockroachDB.
  • ROACH_DB_NAME: the name of the database to use within cockroachDB.

The following environment variables control the configuration for Redis:

  • STAGE_NAME: use production to provide server host and port. Otherwise localhost and 6379 are used.
  • REDIS_HOST: IP address for connecting with a redis cluster.
  • REDIS_PORT: port to establish a connection.

To run tests or run locally:

# run cockroachdb on minikube
minikube start & \
kubectl port-forward \
  service/cockroachdb-public \
  26257 \
  -n default

# run redis on your workstation
redis-server ./redis.conf

Readme

Keywords

Package Sidebar

Install

npm i @bampich.ok/data

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

10.5 kB

Total Files

10

Last publish

Collaborators

  • hk6an6
  • alexandra.aragon
  • sarah.morales
  • adriana.jovane