2.10.0 • Published 8 years ago

ndata v2.10.0

Weekly downloads
318
License
-
Repository
github
Last release
8 years ago

nData

nData is a lightweight key-value store and message broker. It is written entirely in node.js for maximum portability.

Installation

npm install ndata

Overview

To use it call:

var ndata = require('ndata');

Firstly, launch a new nData server. If you're using the node cluster module, you might want to launch the nData server once from the master process and then interact with it using nData clients.

Server

To launch the server, use:

var dataServer = ndata.createServer({port: 9000, secretKey: 'mySecretKey'})

The secretKey argument is optional; you should use it if you want to restrict access to the server. If you're running a node cluster, you may want to use a random key and distribute it to all the workers so that only your application can interact with the nData server.

Once the server is setup, you should create clients to interact with it.

Make sure that the server is running before creating clients

This ca be done in the following way:

var conf = {port: 9000}
  , server = ndata.createServer(conf);

server.on('ready', function () {

 console.log('Server ready, create client');
 var client = ndata.createClient(conf);
 
 // do client stuff
 
});

After all the server provides a destroy function:

server.destroy()

Client

To create a client use:

var dataClient = ndata.createClient({port: 9000, secretKey: 'mySecretKey'});

The port and secretKey must match those supplied to the createServer function.

Client methods

The client exposes the following methods: (Please see the section on keys to see how you can use keys in nData. Also, note that the callback argument in all of the following cases is optional.)

run

run(code,[data,] callback)

Run a special JavaScript function declaration (code) as a query on the nData server. This function declaration accepts the DataMap as a parameter. This is the most important function in nData, all the other functions are basically utility functions to make things quicker. Using run() offers the most flexibility. The callback is in form:

callback(err, data)

Example:

var queryFn = function (DataMap) {
    // The myMessage variable comes from queryFn.data
    DataMap.set(['main', 'message'], myMessage);
    return DataMap.get(['main']);
};

queryFn.data = {
    myMessage: 'This is an important message'
};

client.run(queryFn, function(err, data) {
    console.log(data); // outputs {message: "This is an important message"}
});

Note

The query functions are not regular functions. Query functions are executed remotely (on the nData server), therefore, you cannot access variables from the outer parent scope while inside them.

To pass data from the current process to use inside your query functions, you need to set them through the data property (see queryFn.data) in example above. Properties of queryFn.data will be available as regular variables inside the query function when it gets executed on the server. All query data is escaped automatically, so it's safe to supply user input. The queryFn.data property is optional.

set

set(keyChain, value, callback)

Set a key-value pair, when the operation has been completed, callback will be executed. The callback is in form:

callback(err)

add

add(keyChain, value, callback)

Append a value at the given keyChain; the object at keyChain will be treated as an array. If a value already exists at that keyChain and is not an array, this existing value will be placed inside an empty array and the specified value argument will be appended to that array. The callback is in form:

callback(err, insertionIndex)

concat

concat(keyChain, value, callback)

Concatenate the array or object at keyChain with the specified array or object (value). The callback is in form:

callback(err)

remove

remove(keyChain,[getValue,] callback)

Remove the value at keyChain. If value is an array, it will remove the entire array. The optional getValue is a boolean which indicates whether or not to return the removed value in the callback. The callback is in form:

callback(err, value)

removeRange

removeRange(keyChain, fromIndex,[ toIndex, getValue,] callback)

Remove a range of values at keyChain between fromIndex and toIndex. This function assumes that the value at keyChain is an object or array. The optional getValue argument specifies whether or not to return the removed section as an argument to the callback. The callback is in form:

callback(err, value)

removeAll

removeAll(callback)

Clear nData completely. The callback is in form:

callback(err)

splice

splice(keyChain,[ options,] callback)

This operation is designed to work on Arrays (the keyChain argument should point to an Array). It is similar to JavaScript's Array.splice() function. It can be used to remove and insert elements within an Array. The options argument is an object which can have the following properties:

  • index // The index at which to start inserting/deleting
  • count // The number of items to delete starting from index
  • items // An Array of items to insert at index

Callback form:

callback(err, value)

pop

pop(keyChain,[getValue,] callback)

Remove the last numerically-indexed entry at keyChain. The optional getValue is a boolean which indicates whether or not to return the removed value in the callback. The callback is in form:

callback(err, value)

get

get(keyChain, callback)

Get the value at keyChain. The callback is in form:

callback(err, value)

getRange

getRange(keyChain, fromIndex,[ toIndex,] callback)

This function assumes that the value at keyChain is an Array or Object. Capture all values starting at fromIndex and finishing at toIndex but not including toIndex. If toIndex is not specified, all values from fromIndex until the end of the Array or Object will be included. The callback is in form:

callback(err, value)

getAll

getAll(callback)

Get all the values in nData. The callback is in form:

callback(err, value)

count

count(keyChain, callback)

Count the number of elements at keyChain. The callback is in form:

callback(err, value)

publish subscribe

nData provides publish and subscribe functionality.

subscribe

subscribe(channel, ackCallback)

Watch a channel on nData. This is the nData equivalent to Redis' subscribe(). When an event happens on any watched channel, you can handle it using

nDataClient.on('message', function (channel, data) {
    // ...
})

unsubscribe

unsubscribe(channel, ackCallback)

Unwatch the specified channel. If channel is not specified, it will unsubscribe from all channels.

on

on(event, listener)

Listen to events on nData, you should listen to the 'message' event to handle messages from subscribed channels. Events are:

  • 'ready': Triggers when nData is initialized and connected. You often don't need to wait for that event though. The nData client will buffer actions until the nData server ready.
  • 'exit' This event carries two arguments to it's listener: code and signal. It gets triggered when the nData server process dies.
  • 'connect_failed' This happens if the nData client fails to connect to the server after the maximum number of retries have been attempted.
  • 'message' Captures data published to a channel which the client is subscribed to.
  • 'subscribe' Triggers whenever a successful subscribe operations occurs.
  • 'subscribefail' Triggers whenever a subscribtion fails.
  • 'unsubscribe' Triggers on a successful unsubscribe operation.
  • 'unsubscribefail' Triggers whenever a unsubscribtion fails.
  • 'error' Triggers whenever a error occurs.

publish

publish(channel, message, callback)

Publish data to a channel - Can be any JSON-compatible JavaScript object.

Example:

After starting the server (server.js):

var ndata = require('ndata')
  , dss   = ndata.createServer({port: 9000})

a first client (client1.js) can subscribe to channel foo and listen to messages:

var ndata   = require('ndata')
  , dc      = ndata.createClient({port: 9000})
  , ch      = 'foo'
  , onMsgFn = function(ch, data){
      console.log('message on channel ' + ch );
      console.log('data:');
      console.log(data);
    }
dc.subscribe(ch, function(err){
  if(!err){
    console.log('client 1 subscribed channel ' + ch  );
  }
})
dc.on('message', onMsgFn )

If a second client (client2.js) publishes a message, the first client will execute the onMsgFn function:

var ndata  = require('ndata')
   , dc    = ndata.createClient({port: 9000})
   , data  = {a:'b'}
   , ch    = 'foo';
dc.publish(ch,data , function(err){
  if(!err){
    console.log('client 2 published data:');
    console.log(data);
  }
})

Keys

nData is very flexible with how you can use keys. It lets you set key chains of any dimension without having to manually create each link in the chain.

A key chain is an array of keys - Each subsequent key in the chain is a child of the previous key. For example, consider the following object:

{'this': {'is': {'a': {'key': 123}}}}

The key chain ['this', 'is', 'a', 'key'] would reference the number 123. The key chain ['this', 'is'] would reference the object {'a': {'key': 123}}, etc.

When you start, nData will be empty, but this code is perfectly valid:

dataClient.set(['this', 'is', 'a', 'deep', 'key'], 'Hello world');

In this case, nData will create the necessary key chain and set the bottom-level 'key' to 'Hello World'. If you were to call:

dataClient.get(['this', 'is', 'a'], function(err, val) {
    console.log(val);
});

The above would output:

{deep:{key:'Hello world'}}

nData generally doesn't restrict you from doing anything you want. Following from the previous example, it is perfectly OK to call this:

dataClient.add(['this', 'is', 'a'], 'foo');

In this case, the key chain ['this', 'is', 'a'] would evaluate to:

{0:'foo', deep:{key:'Hello world'}}

In this case, nData will add the value at the next numeric index in the specified key path (which in this case is 0).

You can access numerically-indexed values like this:

dataClient.get(['this', 'is', 'a', 0], function(err, val) {
    console.log(val);
});

The output here will be 'foo'. You can also add entire JSON-compatible objects as value.

Tests

To run tests, go to the ndata module directory then run:

npm test

If you get an error, make sure that you have mocha installed:

npm install mocha
2.10.0

8 years ago

2.9.7

8 years ago

2.9.6

8 years ago

2.9.5

8 years ago

2.9.4

9 years ago

2.9.3

9 years ago

2.9.2

9 years ago

2.9.1

9 years ago

2.9.0

9 years ago

2.8.5

9 years ago

2.8.4

9 years ago

2.8.3

9 years ago

2.8.2

9 years ago

2.8.1

9 years ago

2.8.0

9 years ago

2.7.4

9 years ago

2.7.3

9 years ago

2.7.2

9 years ago

2.7.1

9 years ago

2.7.0

9 years ago

2.6.6

9 years ago

2.6.5

9 years ago

2.6.4

9 years ago

2.6.3

9 years ago

2.6.2

9 years ago

2.6.1

9 years ago

2.6.0

9 years ago

2.5.3

10 years ago

2.5.2

10 years ago

2.5.1

10 years ago

2.5.0

10 years ago

2.4.2

10 years ago

2.4.1

10 years ago

2.4.0

10 years ago

2.3.0

10 years ago

2.2.2

10 years ago

2.2.1

10 years ago

2.2.0

10 years ago

2.1.1

10 years ago

2.1.0

10 years ago

2.0.0

10 years ago

1.0.0

10 years ago

0.9.37

10 years ago

0.9.36

10 years ago

0.9.35

10 years ago

0.9.34

10 years ago

0.9.33

10 years ago

0.9.32

10 years ago

0.9.31

10 years ago

0.9.30

10 years ago

0.9.29

10 years ago

0.9.27

10 years ago

0.9.26

10 years ago

0.9.25

10 years ago

0.9.24

11 years ago

0.9.23

11 years ago

0.9.22

11 years ago

0.9.21

11 years ago

0.9.20

11 years ago

0.9.19

11 years ago

0.9.18

11 years ago

0.9.17

11 years ago

0.9.16

11 years ago

0.9.15

11 years ago

0.9.13

11 years ago

0.9.11

11 years ago

0.9.10

11 years ago

0.9.9

11 years ago

0.9.8

11 years ago

0.9.7

11 years ago

0.9.6

11 years ago

0.9.5

11 years ago

0.9.4

11 years ago

0.9.3

12 years ago

0.9.2

12 years ago

0.9.1

12 years ago

0.9.0

12 years ago