Giter VIP home page Giter VIP logo

etcd-node-client's Introduction

etcd-node-client

A gRPC based etcd client which supports etcd V3 api, written in node.js

Prerequisites:

  • etcd version supported: > 3.0.0
  • node.js version supported: > 0.12 (grpc only works on node 0.12 or above)

About this client:

  • Currently it supports some but not all v3.0 APIs (Adding). Please check out this for more about etcd official API.
  • Proto files are directly copied from etcd repo, if there is something wrong, please check create an issue here and follow the etcd official proto files.

How to install

npm install etcd-node-client

etcd V3.0 APIs supported (Updating):

  • KV service:

    • put
    • range
    • deleteRange
    • txn
    • compact
  • Lease service:

    • leaseGrant
    • leaseRevoke
  • Watch service: Watch service is a streaming api, please check this out

  • Maintenance:

    • status (same as official api)
    • getLeaderId

How to use (Take the KV service as an example):

  • Create a client:
var etcd = require('etcd-node-client');

var endpoints = ['127.0.0.1:2379', '127.0.0.1:22379', '127.0.0.1:32379'];
var lbAlgorithm = 'RoundRobin';

var client = new etcd.Client(endpoints, lbAlgorithm);
  • Put a kv value:
client.kv.put({
  key: new Buffer('name'),
  value: new Buffer('mario'),
  lease: 0
},function (err, res) {
  if (err) {
    console.log(err);
  }
});
  • get a kv value:
client.kv.range({
  key: new Buffer('name'),
  limit: 1,
  revision: 0,
  sort_order: 'NONE',
  sort_target: 'KEY',
  serializable: true,
  keys_only: false,
  count_only: false
}, function (err, res) {
  console.log(res.kvs[0].value.toString()); //This will print 'mario'
});
  • Delete a kv value:
client.kv.deleteRange({
  key: new Buffer('name')
}, function (err, res) {
  if (err) {
    console.dir(err);
  }
});
  • Transaction ops:
client.kv.txn({
  compare: [
    {
      result: 'EQUAL',
      target: 'CREATE',
      key: new Buffer('name'),
      version: 1
    }
    ],
  success: [
    {
      request_put: {
        key: new Buffer('id'),
        value: new Buffer('123'),
        lease: 0}
    },
    {
      request_put: {
        key: new Buffer('uid'),
        value: new Buffer('yoyoyo'),
        lease: 0
      }
    }
    ],
    failure: []
}, function (err, res) {
  if (err) {
    console.dir(err);
  }
});

Watch:

Watch is a bit different from the official v3 api, this is how we use watch:

  • Create a watcher on a key:
client.watcher.create({create_request: new Buffer('uid')});
  • A watcher created:
client.watcher.on('created', function (request, id) {
  console.log(request.key + ''); //request is the watch reqeust body, it contains watched key and other info.
  console.log(id); //id is the watch_id when a key watcher created successfully.
});
  • Cancel/delete a watcher:
client.watcher.cancel('0'); //'0' is the watcher id which associated to the watched key.
  • A watcher canceled/deleted:
client.watcher.on('canceled', function (id) {
  console.log(id); //id is the watcher id.
});
  • Events happened on a watcher:
client.watcher.on('events', function (res) {
  console.dir(res);
  /***********
  res has two fields: 'id' and 'events'
  id is the watcher id
  events is an array which contains all events happened:
  {id: '1'
   events: [ { type: 'PUT',
    kv:
     { key: [Object],
       create_revision: '449',
       mod_revision: '449',
       version: '1',
       value: [Object],
       lease: '0' },
    prev_kv: null } ]}
  **************/
});
  • Close the watch stream:
client.watcher.close(message); //message is optional

Maintenance:

  • status
client.maintenance.status(function(err, res) {
  console.dir(res); //res is the response object
});
  • getLeaderId
client.maintenance.getLeaderId(function (err, res) {
  console.log(res); //res should be the leader id, like '10501334649042878790'
});

License

  • MIT.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.