Skip to content

Commit

Permalink
Added clients creation logic with elastic settings (+ tests).
Browse files Browse the repository at this point in the history
Added tables listing, creation and removal (automated tests will be in done next).
  • Loading branch information
gluwer committed Jan 20, 2014
1 parent 984879b commit 8a8aec3
Show file tree
Hide file tree
Showing 10 changed files with 667 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -3,4 +3,5 @@
*.pid
npm-debug.log
node_modules
.idea
.idea
hydepark
119 changes: 117 additions & 2 deletions README.md
Expand Up @@ -3,7 +3,122 @@ azure-table-node

Simplified Azure Table Storage client library for NodeJS.

*This is a work in progress. Not usable yet!*

Usage
=====
==============

## Setting the default client connection info and other settings

Default client uses environment variable to set up the access key and storage URL if possible. It looks for the `CLOUD_STORAGE_ACCOUNT` setting with three elements (it is the default format used by Azure Storage):

```
TableEndpoint=http://accountName.table.core.windows.net/;AccountName=accountName;AccountKey=theKey
```

No error is returned if this doesn't exists, is incomplete or malformed.

*Current version does not support quotes and AccountKey must be the last one to be parsed correctly. This will be fixed in the future.*

If the environment variable is not set, the default connection info have to be set using below command to be usable:

```javascript
var azureTable = require('azure-table-node');
azureTable.setDefaultClient({
accountUrl: 'http://accountName.table.core.windows.net/',
accountName: 'accountName',
accountKey: 'theKey'
});
```

The same method allows to set other default client settings (see *Client settings*).

## Using default client

```javascript
var azureTable = require('azure-table-node');
var defaultClient = azureTable.getDefaultClient();

// use the client to create the table
defaultClient.createTable('tableName', true, cb);

defaultClient.insert('table', entity, options, cb);
```

## Creating customized client

It is possible to create additional clients that are based on other client (or on default settings), but customized and independent. This allows to for example use several table storage accounts but still have one default for convenience.

```javascript
var azureTable = require('azure-table-node');
var tableClient = azureTable.createClient({
// predefined settings
}, [baseClient]);

```

Base client is the client on which the new one will be based. If not provided, it is based on the default one.

Client settings
===============

Account related:

* `accountUrl` (string) - URL of the service's endpoint (no default value)
* `accountName` (string) - name of the used account (no default value)
* `accountKey` (string) - base64 encoded account key (no default value)

Underlying HTTP request related (passed without changes to request module):

* `timeout` (int) - request timeout in miliseconds (default: 10000)
* `proxy` (string) - proxy URL
* `forever` (bool) - use true to turn advanced socket reuse
* `agentOptions` (object) - used to set maxSockets for forever or standard agent
* `pool` (false|object) - use false to turn off socket reuse

Azure Table Storage related:

* `metadata` (string) - default metadata level, available values: `no`, `minimal`, `full` (default: `no`)
* `returnInserts` (bool) - set to true to get back inserted content (usable if etag is needed)

API
===

If not explained differently, `cb` in API is a functions in format function cb(err, data). For queries there may be additional third argument passed if there is a continuation token.

## Module level

### getDefaultClient()

Returns default `Client` object. If `setDefaultClient()` was not used earlier the client only have default module settings and environment variable applied.

### setDefaultClient(settings)

Sets up and returns default `Client` object with provided `settings`. It is using default settings as a base.

### createClient(settings, [base])

Returns new `Client` object using new settings and `base` client settings as a fallback. If `base` is not provided, uses default client settings.

## Client object level

### create(settings)

Returns new `Client` object using only provided settings object. Shouldn't be used directly unless you want to provide all options. Use `createClient` from main module if possible.

### getSettings()

Returns sealed settings object used by this client.

### createTable(table, [options], cb)

Creates new table. The `table` is table name. The `options` is optional, but if exists and `ignoreIfExists` key equals `true`, the error 'table already exists' is ignored. The `cb` is a standard callback function.

### deleteTable(table, cb)

Removes existing table. The `table` is table name. The `cb` is a standard callback function.

### listTables([options], cb)

Returns array with table names (as strings). The `options` is optional, but if exists and `nextTableName` key is provided, the retrieval will start from last continuation token. The `cb` is a standard callback function, but if continuation is required, the third argument will be passed with value for `nextTableName` key.

It is a work in progress. Not yet usable in any way.
68 changes: 68 additions & 0 deletions index.js
@@ -1 +1,69 @@
'use strict';

var _ = require('lodash');
var utils = require('./lib/utils');
var Client = require('./lib/client').Client;

var _defaultClientSetting = {
timeout: 10000,
metadata: 'no'
};

// default client is created lazily on first get or set request
var _defaultClient = null;

// initializes default client using default settings and environment variable CLOUD_STORAGE_ACCOUNT
function _initDefaultConnection() {
if ('CLOUD_STORAGE_ACCOUNT' in process.env) {
var accountSettings = utils.parseAccountString(process.env.CLOUD_STORAGE_ACCOUNT);
if (accountSettings !== null) {
_defaultClientSetting = _.defaults(_defaultClientSetting, accountSettings);
}
}
}

function getDefaultClient() {
if (_defaultClient === null) {
_defaultClient = Client.create(_defaultClientSetting);
}
return _defaultClient;
}

function setDefaultClient(settings) {
_defaultClient = createClient(settings);
return _defaultClient;
}

function createClient(settings, base) {
var baseSettings;
if (base) {
baseSettings = base.getSettings();
} else if (_defaultClient !== null) {
baseSettings = _defaultClient.getSettings();
} else {
baseSettings = _defaultClientSetting;
}

var finalSettings = _.clone(baseSettings);
if (settings) {
finalSettings = _.merge(finalSettings, settings, function(a, b) {
return _.isArray(a) ? a.concat(b) : undefined;
});
}

return Client.create(finalSettings);
}


var azureTable = {
// () -> Client object
getDefaultClient: getDefaultClient,
// (options{object}) -> Client object
setDefaultClient: setDefaultClient,
// (options{object}, [base{object}]) -> Client object
createClient: createClient
};

_initDefaultConnection();

module.exports = azureTable;

0 comments on commit 8a8aec3

Please sign in to comment.