1.0.4 • Published 4 years ago

zookeeper-util v1.0.4

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

zookeeper-util

A simple utility Boilerplate code for Zookeeper Clients (Promise based)

###Typescript:

import ZookeeperUtil, { PropertyType } from 'zookeeper-util';
try {
  await ZookeeperUtil.init({
          "connectionParam": "<ip>:<port>, <ip>:<port>, <ip>:<port>",
          "basePath": "/orgName/serviceName/",
          "options": {sessionTimeout: 5000, retries: 1}
        });
  // Zookeeper Connected

  const result = await ZookeeperUtil.getProperty('foo', PropertyType.STRING, function(err: Error, data: any) {
    // Optional Watcher
    // Will return Property Data, When updated.
  });
} catch(e) {

}

###Node.js:

const zookeeperUtil  =  require('zookeeper-util');
const PROPERTY_TYPES = {
    NUMBER: 0,
    STRING: 1,
    OBJECT: 2,
    BOOLEAN: 3
};

try {
  await ZookeeperUtil.init({
          "connectionParam": "<ip>:<port>, <ip>:<port>, <ip>:<port>",
          "basePath": "/orgName/serviceName/",
          "options": {sessionTimeout: 5000, retries: 1}
        });
  // Zookeeper Connected

  const result = await ZookeeperUtil.getProperty('foo', PropertyType["STRING"], function(err: Error, data: any) {
    // Optional Watcher
    // Will return Property Data, When updated.
  });
} catch(e) {

}

Installation

$ npm install zookeeper-util

API Reference

Objects

zkInfo

Passed in the init method

  • connectionParam: Comma seperated list of Ip:port (Cluster/Standalone)
  • basePath: Base Path for the Zookeeper Client. All the property get would be referenced with this base path.
  • options: Zookeeper Options, passed as it is. Please refer to node-zookeeper-client for all available options

PropertyType

enum { STRING, NUMBER, BOOLEAN, OBJECT }

Methods

ZookeeperUtil.init(zkInfo);

Used to init the client; opens connections to ZK

####Usage:

ZookeeperUtil.init({
          "connectionParam": "<ip>:<port>, <ip>:<port>, <ip>:<port>",
          "basePath": "/orgName/serviceName/",
          "options": {sessionTimeout: 5000, retries: 1}
        });

Returns a void Promise

ZkUtil.getProperty(propertyKey, propertyType, watcher);

Get the Zookeeper Property. Watcher is optional

####Usage:

// Absolute Path: basePath + propertyKey
const result = await ZookeeperUtil.getProperty('foo', PropertyType.OBJECT, function(error, data) {});

ZookeeperUtil.getProperty('foo/bar/cat', PropertyType.STRING, function(error, data) {});

Returns a Promise

Note: / in the getProperty should be validated with the basePath suffixing with /

ZkUtil.setProperty(propertyKey, data);

Sets the Zookeeper Property. Data Types supported are Property Types mentioned above

####Usage:

ZookeeperUtil.setProperty('foo', 'test');

ZookeeperUtil.setProperty('foo/bar/cat', {'test': 'test'});

Returns a void Promise

Note: / in the setProperty should be validated with the basePath suffixing with //

ZkUtil.registerWatcher(propertyKey, propertyType, watcher);

Sets the Zookeeper Watcher on the Property. Data Types supported are Property Types mentioned above

####Usage:

const watcher = (err, data) => {};
ZookeeperUtil.registerWatcher('foo', PropertyType.STRING, watcher);

Returns void

Please raise any issues/Pull Requests at the github page.

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago