Skip to content

Commit e8f009f

Browse files
committed
Merge pull request #80 from kuzzleio/kuz-463-es-autorefresh
KUZ-463 ElaticSearch (Auto)Refresh functions
2 parents 7bb4794 + d77e61f commit e8f009f

File tree

6 files changed

+511
-29
lines changed

6 files changed

+511
-29
lines changed

dist/kuzzle.js

Lines changed: 208 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,40 @@ Kuzzle.prototype.whoAmI = function (callback) {
961961
return self;
962962
};
963963

964+
965+
/**
966+
* Update current user in Kuzzle.
967+
*
968+
* @param {object} content - a plain javascript object representing the user's modification
969+
* @param {object} [options] - (optional) arguments
970+
* @param {responseCallback} [cb] - (optional) Handles the query response
971+
*/
972+
Kuzzle.prototype.updateSelf = function (content, options, cb) {
973+
var
974+
self = this,
975+
data = {},
976+
queryArgs = {controller: 'auth', action: 'updateSelf'};
977+
978+
if (!cb && typeof options === 'function') {
979+
cb = options;
980+
options = null;
981+
}
982+
983+
data.body = content;
984+
985+
if (cb) {
986+
self.query(queryArgs, data, options, function (err, res) {
987+
if (err) {
988+
return cb(err);
989+
}
990+
991+
cb(null, res.result);
992+
});
993+
} else {
994+
self.query(queryArgs, data, options);
995+
}
996+
};
997+
964998
/**
965999
* Clean up the queue, ensuring the queryTTL and queryMaxSize properties are respected
9661000
*/
@@ -1376,6 +1410,136 @@ Kuzzle.prototype.getServerInfo = function (options, cb) {
13761410
return this;
13771411
};
13781412

1413+
/**
1414+
* Forces an index refresh
1415+
*
1416+
* @param {string} index - The index to refresh. Defaults to Kuzzle.defaultIndex
1417+
* @param {object} options - Optional arguments
1418+
* @param {responseCallback} cb - Handles the query response
1419+
* @returns {Kuzzle}
1420+
*/
1421+
Kuzzle.prototype.refreshIndex = function () {
1422+
var
1423+
index,
1424+
options,
1425+
cb;
1426+
1427+
Array.prototype.slice.call(arguments).forEach(function(arg) {
1428+
switch (typeof arg) {
1429+
case 'string':
1430+
index = arg;
1431+
break;
1432+
case 'object':
1433+
options = arg;
1434+
break;
1435+
case 'function':
1436+
cb = arg;
1437+
break;
1438+
}
1439+
});
1440+
1441+
if (!index) {
1442+
if (!this.defaultIndex) {
1443+
throw new Error('Kuzzle.refreshIndex: index required');
1444+
}
1445+
index = this.defaultIndex;
1446+
}
1447+
1448+
this.query({ index: index, controller: 'admin', action: 'refreshIndex'}, {}, options, cb);
1449+
1450+
return this;
1451+
};
1452+
1453+
/**
1454+
* Returns de current autoRefresh status for the given index
1455+
*
1456+
* @param {string} index - The index to get the status from. Defaults to Kuzzle.defaultIndex
1457+
* @param {object} options - Optinal arguments
1458+
* @param {responseCallback} cb - Handles the query response
1459+
* @returns {object} this
1460+
*/
1461+
Kuzzle.prototype.getAutoRefresh = function () {
1462+
var
1463+
index,
1464+
options,
1465+
cb;
1466+
1467+
Array.prototype.slice.call(arguments).forEach(function (arg) {
1468+
switch (typeof arg) {
1469+
case 'string':
1470+
index = arg;
1471+
break;
1472+
case 'object':
1473+
options = arg;
1474+
break;
1475+
case 'function':
1476+
cb = arg;
1477+
break;
1478+
}
1479+
});
1480+
1481+
if (!index) {
1482+
if (!this.defaultIndex) {
1483+
throw new Error('Kuzzle.getAutoRefresh: index required');
1484+
}
1485+
index = this.defaultIndex;
1486+
}
1487+
1488+
this.callbackRequired('Kuzzle.getAutoRefresh', cb);
1489+
this.query({ index: index, controller: 'admin', action: 'getAutoRefresh'}, {}, options, cb);
1490+
1491+
return this;
1492+
};
1493+
1494+
/**
1495+
* (Un)Sets the autoRefresh flag on the given index
1496+
*
1497+
* @param {string} index - the index to modify. Defaults to Kuzzle.defaultIndex
1498+
* @param {boolean} autoRefresh - The autoRefresh value to set
1499+
* @param {object} options - Optional arguments
1500+
* @param {responseCallback} cb - Handles the query result
1501+
* @returns {object} this
1502+
*/
1503+
Kuzzle.prototype.setAutoRefresh = function () {
1504+
var
1505+
index,
1506+
autoRefresh,
1507+
options,
1508+
cb;
1509+
1510+
Array.prototype.slice.call(arguments).forEach(function (arg) {
1511+
switch (typeof arg) {
1512+
case 'string':
1513+
index = arg;
1514+
break;
1515+
case 'boolean':
1516+
autoRefresh = arg;
1517+
break;
1518+
case 'object':
1519+
options = arg;
1520+
break;
1521+
case 'function':
1522+
cb = arg;
1523+
break;
1524+
}
1525+
});
1526+
1527+
if (!index) {
1528+
if (!this.defaultIndex) {
1529+
throw new Error('Kuzzle.setAutoRefresh: index required');
1530+
}
1531+
index = this.defaultIndex;
1532+
}
1533+
1534+
if (autoRefresh === undefined) {
1535+
throw new Error('Kuzzle.setAutoRefresh: autoRefresh value is required');
1536+
}
1537+
1538+
this.query({ index: index, controller: 'admin', action: 'setAutoRefresh'}, { body: { autoRefresh: autoRefresh }}, options, cb);
1539+
1540+
return this;
1541+
};
1542+
13791543
/**
13801544
* Return the current Kuzzle's UTC Epoch time, in milliseconds
13811545
* @param {object} [options] - Optional parameters
@@ -1873,27 +2037,6 @@ KuzzleDataCollection.prototype.createDocument = function (id, document, options,
18732037
return this;
18742038
};
18752039

1876-
/**
1877-
* Delete this data collection and all documents in it.
1878-
*
1879-
* @param {object} [options] - Optional parameters
1880-
* @param {responseCallback} [cb] - returns Kuzzle's response
1881-
* @returns {*} this
1882-
*/
1883-
KuzzleDataCollection.prototype.delete = function (options, cb) {
1884-
var data = {};
1885-
1886-
if (!cb && typeof options === 'function') {
1887-
cb = options;
1888-
options = null;
1889-
}
1890-
1891-
data = this.kuzzle.addHeaders(data, this.headers);
1892-
this.kuzzle.query(this.buildQueryArgs('admin', 'deleteCollection'), data, options, cb);
1893-
1894-
return this;
1895-
};
1896-
18972040
/**
18982041
* Delete persistent documents.
18992042
*
@@ -2730,6 +2873,36 @@ KuzzleDocument.prototype.setHeaders = function (content, replace) {
27302873
module.exports = KuzzleDocument;
27312874

27322875
},{}],7:[function(require,module,exports){
2876+
/**
2877+
* This is a global callback pattern, called by all asynchronous functions of the Kuzzle object.
2878+
*
2879+
* @callback responseCallback
2880+
* @param {Object} err - Error object, NULL if the query is successful
2881+
* @param {Object} data - The content of the query response
2882+
*/
2883+
2884+
2885+
/**
2886+
* Kuzzle's memory storage is a separate data store from the database layer.
2887+
* It is internaly based on Redis. You can access most of Redis functions (all
2888+
* lowercased), excepting:
2889+
* * all cluster based functions
2890+
* * all script based functions
2891+
* * all cursors functions
2892+
*
2893+
* For instance:
2894+
* kuzzle.memoryStorage
2895+
* .set('myKey', 'myValue')
2896+
* .get('myKey', function (err, response) {
2897+
* console.log(response.result);
2898+
*
2899+
* // { _id: 'foo', body: { value: 'myValue' }}
2900+
* });
2901+
*
2902+
*
2903+
* @param {object} kuzzle - Kuzzle instance to inherit from
2904+
* @constructor
2905+
*/
27332906
function KuzzleMemoryStorage(kuzzle) {
27342907
Object.defineProperties(this, {
27352908
// read-only properties
@@ -2762,6 +2935,9 @@ function KuzzleMemoryStorage(kuzzle) {
27622935
}
27632936

27642937

2938+
/**
2939+
* constructs the memoryStorage functions.
2940+
*/
27652941
(function() {
27662942

27672943
var
@@ -3064,7 +3240,7 @@ KuzzleRoom.prototype.count = function (cb) {
30643240

30653241
data = this.kuzzle.addHeaders({body: {roomId: this.roomId}}, this.headers);
30663242

3067-
if (this.subscribing) {
3243+
if (!isReady.call(this)) {
30683244
this.queue.push({action: 'count', args: [cb]});
30693245
return this;
30703246
}
@@ -3185,7 +3361,7 @@ KuzzleRoom.prototype.unsubscribe = function () {
31853361
room = self.roomId,
31863362
interval;
31873363

3188-
if (self.subscribing) {
3364+
if (!isReady.call(this)) {
31893365
self.queue.push({action: 'unsubscribe', args: []});
31903366
return self;
31913367
}
@@ -3273,6 +3449,13 @@ function dequeue () {
32733449
}
32743450
}
32753451

3452+
function isReady() {
3453+
if (this.kuzzle.state !== 'connected' || this.subscribing) {
3454+
return false;
3455+
}
3456+
return true;
3457+
}
3458+
32763459
module.exports = KuzzleRoom;
32773460

32783461
},{"node-uuid":2}],9:[function(require,module,exports){
@@ -4218,10 +4401,10 @@ KuzzleSecurity.prototype.updateUser = function (id, content, options, cb) {
42184401
return cb(err);
42194402
}
42204403

4221-
cb(null, res.result._id);
4404+
cb(null, new KuzzleUser(self, res.result._id, res.result._source));
42224405
});
42234406
} else {
4224-
self.kuzzle.query(this.buildQueryArgs(action), data);
4407+
self.kuzzle.query(this.buildQueryArgs(action), data, options);
42254408
}
42264409
};
42274410

dist/kuzzle.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/kuzzle.min.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"mocha": "2.4.5",
4242
"proxyquire": "^1.7.3",
4343
"rewire": "^2.5.0",
44-
"should": "8.2.2"
44+
"should": "8.2.2",
45+
"sinon": "^1.17.4"
4546
}
4647
}

0 commit comments

Comments
 (0)