diff --git a/src/api.js b/src/api.js index be160f5..874ef9c 100644 --- a/src/api.js +++ b/src/api.js @@ -10,7 +10,7 @@ import createDebug from 'debug'; const debug = createDebug('squirrel'); -const createAPI = store => { +const createAPI = (store, client, options = {cwd: '/'}) => { const getBy = (index, key) => { debug(`getBy: ${index} => ${key}`); return store('indexes').then(getOr(null, [index, key, 'value'])); @@ -28,6 +28,12 @@ const createAPI = store => { return store('node').then(_get(path)); }; + const set = (_path, value) => { + _path = path.join(options.cwd, _path); + debug(`set => ${_path}`); + return _set(_path, value); + }; + const _get = curry((_path, node) => { if (!node) return null; if (path.relative(node.key, _path) === '') return node; @@ -37,10 +43,26 @@ const createAPI = store => { }, node.nodes)); }); + const _set = (_path, value) => { + return new Promise(function(resolve, reject) { + if (!value) { + return resolve(value); + } + client.set(_path, value, (err, res) => { + if (err) { + return reject(err); + } + + return resolve(res); + }); + }); + }; + return { getBy, getAll, - get + get, + set }; }; diff --git a/src/index.js b/src/index.js index 19a32a1..bff63e6 100644 --- a/src/index.js +++ b/src/index.js @@ -46,7 +46,7 @@ const createSquirrel = options => { save(node$), indexBuilder ); - const api = createAPI(store); + const api = createAPI(store, client, options); return { ...api, diff --git a/src/test/api.js b/src/test/api.js index fa07e6a..44fdab1 100644 --- a/src/test/api.js +++ b/src/test/api.js @@ -1,7 +1,7 @@ import test from 'ava'; import createAPI from '../api'; - +import Etcd from './fixtures/node-etcd'; const node = { key: '/', dir: true, @@ -85,13 +85,17 @@ const store = { }; const getStore = key => Promise.resolve(store[key]); -const api = createAPI(getStore); +const client = new Etcd(); +const api = createAPI(getStore, client); +const clientErrored = new Etcd({err: new Error('boom')}); +const apiErrored = createAPI(getStore, clientErrored); test('should create API', t => { t.truthy(api); t.truthy(api.get); t.truthy(api.getBy); t.truthy(api.getAll); + t.truthy(api.set); }); test('should get keys of simple index', async t => { @@ -174,3 +178,24 @@ test('should get null if any node matches this path', async t => { null ); }); + +test('should set nothing if value is null', async t => { + t.deepEqual( + await api.set('/nope', null), + null + ); +}); + +test('should set value if value setted', async t => { + t.deepEqual( + await api.set('/foo', {foo: 'baz'}), + {foo: 'baz'} + ); +}); + +test('should failed if error occured', async t => { + t.throws( + apiErrored.set('/foo', {foo: 'baz'}), + /boom/ + ); +}); diff --git a/src/test/fixtures/node-etcd.js b/src/test/fixtures/node-etcd.js new file mode 100644 index 0000000..43c3a14 --- /dev/null +++ b/src/test/fixtures/node-etcd.js @@ -0,0 +1,14 @@ +'use strict'; + +function Etcd(options = {}) { + this.options = options; +} + +Etcd.prototype.set = function set(path, value, callback) { + if (this.options.err) { + return callback(this.options.err); + } + callback(null, value); +}; + +module.exports = Etcd;