Add ProjectionsManager
This commit is contained in:
@ -51,10 +51,12 @@ module.exports.PersistentSubscriptionSettings = require('./persistentSubscriptio
|
||||
module.exports.SystemConsumerStrategies = require('./systemConsumerStrategies');
|
||||
module.exports.GossipSeed = require('./gossipSeed');
|
||||
module.exports.EventStoreConnection = require('./eventStoreConnection');
|
||||
module.exports.ProjectionsManager = require('./projections/projectionsManager');
|
||||
// Expose errors
|
||||
module.exports.WrongExpectedVersionError = require('./errors/wrongExpectedVersionError');
|
||||
module.exports.StreamDeletedError = require('./errors/streamDeletedError');
|
||||
module.exports.AccessDeniedError = require('./errors/accessDeniedError');
|
||||
module.exports.ProjectionCommandFailedError = require('./errors/projectionCommandFailedError');
|
||||
// Expose enums/constants
|
||||
module.exports.expectedVersion = expectedVersion;
|
||||
module.exports.positions = positions;
|
||||
|
10
src/errors/projectionCommandFailedError.js
Normal file
10
src/errors/projectionCommandFailedError.js
Normal file
@ -0,0 +1,10 @@
|
||||
const util = require('util');
|
||||
|
||||
function ProjectionCommandFailedError(httpStatusCode, message) {
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
this.httpStatusCode = httpStatusCode;
|
||||
this.message = message;
|
||||
}
|
||||
util.inherits(ProjectionCommandFailedError, Error);
|
||||
|
||||
module.exports = ProjectionCommandFailedError;
|
56
src/projections/projectionDetails.js
Normal file
56
src/projections/projectionDetails.js
Normal file
@ -0,0 +1,56 @@
|
||||
function ProjectionDetails(
|
||||
coreProcessingTime,
|
||||
version,
|
||||
epoch,
|
||||
effectiveName,
|
||||
writesInProgress,
|
||||
readsInProgress,
|
||||
partitionsCached,
|
||||
status,
|
||||
stateReason,
|
||||
name,
|
||||
mode,
|
||||
position,
|
||||
progress,
|
||||
lastCheckpoint,
|
||||
eventsProcessedAfterRestart,
|
||||
statusUrl,
|
||||
stateUrl,
|
||||
resultUrl,
|
||||
queryUrl,
|
||||
enableCommandUrl,
|
||||
disableCommandUrl,
|
||||
checkpointStatus,
|
||||
bufferedEvents,
|
||||
writePendingEventsBeforeCheckpoint,
|
||||
writePendingEventsAfterCheckpoint
|
||||
) {
|
||||
this.coreProcessingTime = coreProcessingTime;
|
||||
this.version = version;
|
||||
this.epoch = epoch;
|
||||
this.effectiveName = effectiveName;
|
||||
this.writesInProgress = writesInProgress;
|
||||
this.readsInProgress = readsInProgress;
|
||||
this.partitionsCached = partitionsCached;
|
||||
this.status = status;
|
||||
this.stateReason = stateReason;
|
||||
this.name = name;
|
||||
this.mode = mode;
|
||||
this.position = position;
|
||||
this.progress = progress;
|
||||
this.lastCheckpoint = lastCheckpoint;
|
||||
this.eventsProcessedAfterRestart = eventsProcessedAfterRestart;
|
||||
this.statusUrl = statusUrl;
|
||||
this.stateUrl = stateUrl;
|
||||
this.resultUrl = resultUrl;
|
||||
this.queryUrl = queryUrl;
|
||||
this.enableCommandUrl = enableCommandUrl;
|
||||
this.disableCommandUrl = disableCommandUrl;
|
||||
this.checkpointStatus = checkpointStatus;
|
||||
this.bufferedEvents = bufferedEvents;
|
||||
this.writePendingEventsBeforeCheckpoint = writePendingEventsBeforeCheckpoint;
|
||||
this.writePendingEventsAfterCheckpoint = writePendingEventsAfterCheckpoint;
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
module.exports = ProjectionDetails;
|
170
src/projections/projectionsClient.js
Normal file
170
src/projections/projectionsClient.js
Normal file
@ -0,0 +1,170 @@
|
||||
const http = require('http');
|
||||
const url = require('url');
|
||||
const util = require('util');
|
||||
const ProjectionCommandFailedError = require('../errors/projectionCommandFailedError');
|
||||
|
||||
const HTTP_OK = 200;
|
||||
const HTTP_CREATED = 201;
|
||||
|
||||
function safeParseJson(json) {
|
||||
try {
|
||||
return JSON.parse(json);
|
||||
} catch(e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function ProjectionsClient(log, operationTimeout) {
|
||||
this._log = log;
|
||||
this._operationTimeout = operationTimeout;
|
||||
}
|
||||
|
||||
ProjectionsClient.prototype.enable = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendPost(httpEndPoint + '/projection/' + name + '/command/enable', '', userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.disable = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendPost(httpEndPoint + '/projection/' + name + '/command/disable', '', userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.abort = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendPost(httpEndPoint + '/projection/' + name + '/command/abort', '', userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.createOneTime = function(httpEndPoint, query, userCredentials) {
|
||||
return this.sendPost(httpEndPoint + '/projections/onetime?type=JS', query, userCredentials, HTTP_CREATED);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.createTransient = function(httpEndPoint, name, query, userCredentials) {
|
||||
return this.sendPost(httpEndPoint + '/projections/transient?name=' + name + '&type=JS', query, userCredentials, HTTP_CREATED);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.createContinuous = function(httpEndPoint, name, query, trackEmittedStreams, userCredentials) {
|
||||
return this.sendPost(httpEndPoint + '/projections/continuous?name=' + name + '&type=JS&emit=1&trackEmittedStreams=' + trackEmittedStreams, query, userCredentials, HTTP_CREATED);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.listAll = function(httpEndPoint, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projections/any', userCredentials, HTTP_OK)
|
||||
.then(function (json) {
|
||||
var r = safeParseJson(json);
|
||||
if (r && r.projections) return r.projections;
|
||||
return null;
|
||||
});
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.listOneTime = function(httpEndPoint, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projections/onetime', userCredentials, HTTP_OK)
|
||||
.then(function (json) {
|
||||
var r = safeParseJson(json);
|
||||
if (r && r.projections) return r.projections;
|
||||
return null;
|
||||
});
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.listContinuous = function(httpEndPoint, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projections/continuous', userCredentials, HTTP_OK)
|
||||
.then(function (json) {
|
||||
var r = safeParseJson(json);
|
||||
if (r && r.projections) return r.projections;
|
||||
return null;
|
||||
});
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.getStatus = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projection/' + name, userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.getState = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projection/' + name + '/state', userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.getPartitionState = function(httpEndPoint, name, partitionId, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projection/' + name + '/state?partition=' + partitionId, userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.getResult = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projection/' + name + '/result', userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.getPartitionResult = function(httpEndPoint, name, partitionId, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projection/' + name + '/result?partition=' + partitionId, userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.getStatistics = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projection/' + name + '/statistics', userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.getQuery = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projection/' + name + '/query', userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.updateQuery = function(httpEndPoint, name, query, userCredentials) {
|
||||
return this.sendPut(httpEndPoint + '/projection/' + name + '/query?type=JS', query, userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.delete = function(httpEndPoint, name, deleteEmittedStreams, userCredentials) {
|
||||
return this.sendDelete(httpEndPoint + '/projection/' + name + '?deleteEmittedStreams=' + deleteEmittedStreams, userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.request = function(method, _url, data, userCredentials, expectedCode) {
|
||||
const options = url.parse(_url);
|
||||
options.method = method;
|
||||
if (userCredentials) {
|
||||
options.auth = [userCredentials.username, userCredentials.password].join(':');
|
||||
}
|
||||
var self = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
const timeout = setTimeout(function () {
|
||||
reject(new Error(util.format('Request timed out for %s on %s', method, _url)))
|
||||
}, self._operationTimeout);
|
||||
const req = http.request(options, function (res) {
|
||||
const hasExpectedCode = res.statusCode === expectedCode;
|
||||
var result = '';
|
||||
res.setEncoding('utf8');
|
||||
res.on('data', function (chunk) {
|
||||
result += chunk;
|
||||
});
|
||||
res.on('end', function () {
|
||||
if (hasExpectedCode) {
|
||||
clearTimeout(timeout);
|
||||
resolve(result);
|
||||
} else {
|
||||
clearTimeout(timeout);
|
||||
reject(new ProjectionCommandFailedError(
|
||||
res.statusCode,
|
||||
util.format('Server returned %d (%s) for %s on %s', res.statusCode, res.statusMessage, method, _url)
|
||||
));
|
||||
}
|
||||
});
|
||||
});
|
||||
req.on('error', reject);
|
||||
if (data) {
|
||||
req.setHeader('Content-Type', 'application/json');
|
||||
req.write(data);
|
||||
}
|
||||
req.end();
|
||||
});
|
||||
};
|
||||
|
||||
function voidResult() {}
|
||||
|
||||
ProjectionsClient.prototype.sendGet = function(_url, userCredentials, expectedCode) {
|
||||
return this.request('GET', _url, null, userCredentials, expectedCode);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.sendPost = function(_url, data, userCredentials, expectedCode) {
|
||||
return this.request('POST', _url, data, userCredentials, expectedCode)
|
||||
.then(voidResult);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.sendPut = function(_url, data, userCredentials, expectedCode) {
|
||||
return this.request('PUT', _url, data, userCredentials, expectedCode)
|
||||
.then(voidResult);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.sendDelete = function(_url, data, userCredentials, expectedCode) {
|
||||
return this.request('DELETE', _url, data, userCredentials, expectedCode)
|
||||
.then(voidResult);
|
||||
};
|
||||
|
||||
module.exports = ProjectionsClient;
|
202
src/projections/projectionsManager.js
Normal file
202
src/projections/projectionsManager.js
Normal file
@ -0,0 +1,202 @@
|
||||
const ensure = require('../common/utils/ensure');
|
||||
const ProjectionsClient = require('./projectionsClient');
|
||||
|
||||
/**
|
||||
* Creates a new instance of ProjectionsManager.
|
||||
* @param {Logger} log Instance of Logger to use for logging.
|
||||
* @param {string} httpEndPoint HTTP endpoint of an Event Store server.
|
||||
* @param {number} operationTimeout Operation timeout in milliseconds.
|
||||
* @constructor
|
||||
*/
|
||||
function ProjectionsManager(log, httpEndPoint, operationTimeout) {
|
||||
ensure.notNull(log, "log");
|
||||
ensure.notNull(httpEndPoint, "httpEndPoint");
|
||||
this._client = new ProjectionsClient(log, operationTimeout);
|
||||
this._httpEndPoint = httpEndPoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables a projection.
|
||||
* @param name The name of the projection.
|
||||
* @param userCredentials Credentials for a user with permission to enable a projection.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.enable = function(name, userCredentials) {
|
||||
return this._client.enable(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Aborts and disables a projection without writing a checkpoint.
|
||||
* @param name The name of the projection.
|
||||
* @param userCredentials Credentials for a user with permission to disable a projection.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.disable = function(name, userCredentials) {
|
||||
return this._client.disable(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Disables a projection.
|
||||
* @param name The name of the projection.
|
||||
* @param userCredentials Credentials for a user with permission to disable a projection.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.abort = function(name, userCredentials) {
|
||||
return this._client.abort(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a one-time query.
|
||||
* @param query The JavaScript source code for the query.
|
||||
* @param userCredentials Credentials for a user with permission to create a query.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.createOneTime = function(query, userCredentials) {
|
||||
return this._client.createOneTime(this._httpEndPoint, query, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a one-time query.
|
||||
* @param name A name for the query.
|
||||
* @param query The JavaScript source code for the query.
|
||||
* @param userCredentials Credentials for a user with permission to create a query.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.createTransient = function(name, query, userCredentials) {
|
||||
return this._client.createTransient(this._httpEndPoint, query, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a one-time query.
|
||||
* @param name The name of the projection.
|
||||
* @param query The JavaScript source code for the query.
|
||||
* @param trackEmittedStreams Whether the streams emitted by this projection should be tracked.
|
||||
* @param userCredentials Credentials for a user with permission to create a query.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.createContinuous = function(name, query, trackEmittedStreams, userCredentials) {
|
||||
return this._client.createContinuous(this._httpEndPoint, name, query, trackEmittedStreams, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Lists the status of all projections.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<ProjectionDetails[]>}
|
||||
*/
|
||||
ProjectionsManager.prototype.listAll = function(userCredentials) {
|
||||
return this._client.listAll(this._httpEndPoint, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Lists the status of all one-time projections.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<ProjectionDetails[]>}
|
||||
*/
|
||||
ProjectionsManager.prototype.listOneTime = function(userCredentials) {
|
||||
return this._client.listOneTime(this._httpEndPoint, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Lists the status of all continuous projections.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<ProjectionDetails[]>}
|
||||
*/
|
||||
ProjectionsManager.prototype.listContinuous = function(userCredentials) {
|
||||
return this._client.listContinuous(this._httpEndPoint, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the status of a projection.
|
||||
* @param name The name of the projection.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<string>} String of JSON containing projection status.
|
||||
*/
|
||||
ProjectionsManager.prototype.getStatus = function(name, userCredentials) {
|
||||
return this._client.getStatus(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the state of a projection.
|
||||
* @param name The name of the projection.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<string>} String of JSON containing projection state.
|
||||
*/
|
||||
ProjectionsManager.prototype.getState = function(name, userCredentials) {
|
||||
return this._client.getState(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the state of a projection for a specified partition.
|
||||
* @param name The name of the projection.
|
||||
* @param partitionId The id of the partition.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<string>} String of JSON containing projection state.
|
||||
*/
|
||||
ProjectionsManager.prototype.getPartitionState = function(name, partitionId, userCredentials) {
|
||||
return this._client.getPartitionState(this._httpEndPoint, name, partitionId, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the state of a projection.
|
||||
* @param name The name of the projection.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<string>} String of JSON containing projection state.
|
||||
*/
|
||||
ProjectionsManager.prototype.getResult = function(name, userCredentials) {
|
||||
return this._client.getResult(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the state of a projection for a specified partition.
|
||||
* @param name The name of the projection.
|
||||
* @param partitionId The id of the partition.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<string>} String of JSON containing projection state.
|
||||
*/
|
||||
ProjectionsManager.prototype.getPartitionResult = function(name, partitionId, userCredentials) {
|
||||
return this._client.getPartitionResult(this._httpEndPoint, name, partitionId, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the statistics of a projection.
|
||||
* @param name The name of the projection.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<string>} String of JSON containing projection statistics.
|
||||
*/
|
||||
ProjectionsManager.prototype.getStatistics = function(name, userCredentials) {
|
||||
return this._client.getStatistics(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the status of a query.
|
||||
* @param name The name of the query.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<string>} String of JSON containing query status.
|
||||
*/
|
||||
ProjectionsManager.prototype.getQuery = function(name, userCredentials) {
|
||||
return this._client.getQuery(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Updates the definition of a query.
|
||||
* @param name The name of the query.
|
||||
* @param query The JavaScript source code for the query.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.updateQuery = function(name, query, userCredentials) {
|
||||
return this._client.updateQuery(this._httpEndPoint, name, query, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Updates the definition of a query.
|
||||
* @param name The name of the projection.
|
||||
* @param deleteEmittedStreams Whether to delete the streams that were emitted by this projection.
|
||||
* @param userCredentials Credentials for a user with permission to delete a projection.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.delete = function(name, deleteEmittedStreams, userCredentials) {
|
||||
return this._client.delete(this._httpEndPoint, name, deleteEmittedStreams, userCredentials);
|
||||
};
|
||||
|
||||
module.exports = ProjectionsManager;
|
Reference in New Issue
Block a user