Moved eventStoreConnection in it's onw file

This commit is contained in:
Nicolas Dextraze
2016-03-09 15:37:54 -08:00
parent b5ed21755d
commit 3bf4b92ff3
5 changed files with 77 additions and 70 deletions

50
src/client.js Normal file
View File

@ -0,0 +1,50 @@
var EventData = require('./eventData');
var results = require('./results');
const expectedVersion = {
any: -2,
noStream: -1,
emptyStream: -1
};
const positions = {
start: new results.Position(0, 0),
end: new results.Position(-1, -1)
};
/**
* @param {string} eventId
* @param {object} data
* @param {object} [metadata]
* @param {string} [type]
* @returns {EventData}
*/
function jsonEventDataFactory(eventId, data, metadata, type) {
if (!data || typeof data !== 'object') throw new TypeError("data must be an object.");
var d = new Buffer(JSON.stringify(data));
var m = metadata ? new Buffer(JSON.stringify(metadata)) : null;
return new EventData(eventId, type || data.constructor.name, true, d, m);
}
/**
* @param {string} eventId
* @param {string} type
* @param {boolean} isJson
* @param {Buffer} data
* @param {Buffer} [metadata]
* @returns {EventData}
*/
function eventDataFactory(eventId, type, isJson, data, metadata) {
return new EventData(eventId, type, isJson, data, metadata);
}
module.exports.EventStoreConnection = require('./eventStoreConnection');
module.exports.UserCredentials = require('./systemData/userCredentials');
module.exports.EventData = EventData;
module.exports.expectedVersion = expectedVersion;
module.exports.positions = positions;
// Helper functions
module.exports.createConnection = module.exports.EventStoreConnection.create;
module.exports.createEventData = eventDataFactory;
module.exports.createJsonEventData = jsonEventDataFactory;

View File

@ -1,10 +1,6 @@
var uuid = require('uuid');
var EventStoreNodeConnection = require('./eventStoreNodeConnection');
var StaticEndpointDiscoverer = require('./core/staticEndpointDiscoverer');
var NoopLogger = require('./common/log/noopLogger');
var EventData = require('./eventData');
var results = require('./results');
var UserCredentials = require('./systemData/userCredentials');
var defaultConnectionSettings = {
log: new NoopLogger(),
@ -32,42 +28,6 @@ var defaultConnectionSettings = {
clientConnectionTimeout: 1000
};
const expectedVersion = {
any: -2,
noStream: -1,
emptyStream: -1
};
const positions = {
start: new results.Position(0, 0),
end: new results.Position(-1, -1)
};
/**
* @param {string} eventId
* @param {object} data
* @param {object} [metadata]
* @param {string} [type]
* @returns {EventData}
*/
function jsonEventDataFactory(eventId, data, metadata, type) {
if (!data || typeof data !== 'object') throw new TypeError("data must be an object.");
var d = new Buffer(JSON.stringify(data));
var m = metadata ? new Buffer(JSON.stringify(metadata)) : null;
return new EventData(eventId, type || data.constructor.name, true, d, m);
}
/**
* @param {string} eventId
* @param {string} type
* @param {boolean} isJson
* @param {Buffer} data
* @param {Buffer} [metadata]
* @returns {EventData}
*/
function eventDataFactory(eventId, type, isJson, data, metadata) {
return new EventData(eventId, type, isJson, data, metadata);
}
function merge(a,b) {
var c = {};
@ -82,28 +42,14 @@ function merge(a,b) {
/**
* Create an EventStore connection
* @param {object} tcpEndpoint
* @param {object} settings
* @param {object} tcpEndPoint
* @param {string} [connectionName]
* @returns {EventStoreNodeConnection}
*/
function connectionFactory(tcpEndpoint, settings) {
var mergedSettings = merge(defaultConnectionSettings, settings);
var endpointDiscoverer = new StaticEndpointDiscoverer(tcpEndpoint, settings.useSslConnection);
var connectionName = null;
return new EventStoreNodeConnection(mergedSettings, endpointDiscoverer, connectionName);
}
module.exports = connectionFactory;
module.exports.expectedVersion = expectedVersion;
module.exports.createEventData = eventDataFactory;
module.exports.createJsonEventData = jsonEventDataFactory;
module.exports.positions = positions;
/**
* @param {string} username
* @param {string} password
* @returns {UserCredentials}
*/
module.exports.createUserCredentials = function(username, password) {
return new UserCredentials(username, password);
module.exports.create = function(settings, tcpEndPoint, connectionName) {
//TODO: cluster connection
var mergedSettings = merge(defaultConnectionSettings, settings || {});
var endpointDiscoverer = new StaticEndpointDiscoverer(tcpEndPoint, settings.useSslConnection);
return new EventStoreNodeConnection(mergedSettings, endpointDiscoverer, connectionName || null);
};

View File

@ -1,9 +1,20 @@
function UserCredentials(username, password) {
if (!username || username === '') throw new TypeError("username must be a non-empty string.");
if (!password || password === '') throw new TypeError("password must be a non-empty string.");
var ensure = require('../common/utils/ensure');
this.username = username;
this.password = password;
/**
* @param {string} username
* @param {string} password
* @constructor
* @property {string} username
* @property {string} password
*/
function UserCredentials(username, password) {
ensure.notNullOrEmpty(username, 'username');
ensure.notNullOrEmpty(password, 'password');
Object.defineProperties(this, {
username: {enumerable: true, value: username},
password: {enumerable: true, value: password}
});
}
module.exports = UserCredentials;