Skip to content

Commit 826ec82

Browse files
committed
mixin cleanup
1 parent 678c3e7 commit 826ec82

File tree

9 files changed

+165
-130
lines changed

9 files changed

+165
-130
lines changed

etc/test-lib.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,17 @@ function makePath(endpoint, params) {
176176

177177
return path;
178178
}
179-
function createManager(adminClient) {
180-
var manager = {
181-
client: adminClient
182-
};
183-
manager.get = manageGet.bind(manager);
184-
manager.post = managePost.bind(manager);
185-
manager.put = managePut.bind(manager);
186-
manager.remove = manageRemove.bind(manager);
187179

188-
return manager;
180+
function Manager(adminClient) {
181+
this.client = adminClient;
182+
}
183+
Manager.prototype.get = manageGet;
184+
Manager.prototype.post = managePost;
185+
Manager.prototype.put = managePut;
186+
Manager.prototype.remove = manageRemove;
187+
188+
function createManager(adminClient) {
189+
return new Manager(adminClient);
189190
};
190191

191192
module.exports = {

lib/documents.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ function writeDocuments() {
491491
requestOptions.path = endpoint;
492492

493493
var requestPartList = [];
494-
documents.forEach(addDocumentParts.bind(requestPartList));
494+
documents.forEach(mlutil.callbackOn(requestPartList, addDocumentParts));
495495

496496
var operation = mlrest.createOperation(
497497
'write documents', this.client, requestOptions, 'multipart', 'single'
@@ -1253,16 +1253,15 @@ function patchDocuments() {
12531253

12541254
function documents(client) {
12551255
this.client = client;
1256-
1257-
this.createReadStream = createReadStream.bind(this);
1258-
this.createWriteStream = createWriteStream.bind(this);
1259-
this.patch = patchDocuments.bind(this);
1260-
this.probe = probeDocument.bind(this);
1261-
this.query = queryDocuments.bind(this);
1262-
this.read = readDocuments.bind(this);
1263-
this.remove = removeDocument.bind(this);
1264-
this.removeAll = removeAllDocuments.bind(this);
1265-
this.write = writeDocuments.bind(this);
12661256
}
1257+
documents.prototype.createReadStream = createReadStream;
1258+
documents.prototype.createWriteStream = createWriteStream;
1259+
documents.prototype.patch = patchDocuments;
1260+
documents.prototype.probe = probeDocument;
1261+
documents.prototype.query = queryDocuments;
1262+
documents.prototype.read = readDocuments;
1263+
documents.prototype.remove = removeDocument;
1264+
documents.prototype.removeAll = removeAllDocuments;
1265+
documents.prototype.write = writeDocuments;
12671266

12681267
module.exports = documents;

lib/extlibs.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,10 @@ function listExtensionLibraries(directory) {
157157
/** @ignore */
158158
function extlibs(client) {
159159
this.client = client;
160-
161-
this.read = readExtensionLibrary.bind(this);
162-
this.write = writeExtensionLibrary.bind(this);
163-
this.remove = removeExtensionLibrary.bind(this);
164-
this.list = listExtensionLibraries.bind(this);
165160
}
161+
extlibs.prototype.list = listExtensionLibraries;
162+
extlibs.prototype.read = readExtensionLibrary;
163+
extlibs.prototype.remove = removeExtensionLibrary;
164+
extlibs.prototype.write = writeExtensionLibrary;
166165

167166
module.exports = extlibs;

lib/marklogic.js

Lines changed: 109 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,111 @@ var patchBuilder = require('./patch-builder.js');
4646
* @borrows documents#write as DatabaseClient#write
4747
*/
4848

49+
/**
50+
* Creates a database client to make database requests such as writes, reads,
51+
* and queries. The constructor takes a configuration object with the following
52+
* named parameters.
53+
* @function module:marklogic.createDatabaseClient
54+
* @param {string} [host=localhost] - the host with the REST server for the database
55+
* @param {number} [port=8000] - the port with the REST server for the database
56+
* @param {string} user - the user with permission to access the database
57+
* @param {string} password - the password for the user with permission to access
58+
* the database
59+
* @param {enum} [authType=digest] - the authentication type of digest or basic
60+
* @param {boolean} [ssl=false] - whether the REST server uses SSL; when true,
61+
* the connection parameters can include the
62+
* {@link http://nodejs.org/api/https.html#https_https_request_options_callback|supplemental
63+
* HTTPS options} specifiable for the node.js tls.connect() function.
64+
* @param {object} [agent] - defaults to a connection pooling agent
65+
* @returns {DatabaseClient} a client for accessing the database
66+
* as the user
67+
*/
68+
function MarkLogicClient(connectionParams) {
69+
mlrest.initClient(this, connectionParams);
70+
71+
/**
72+
* Provides functions that write, read, query, or perform other operations
73+
* on documents in the database. As a convenience, the same functions are
74+
* provided on the database client.
75+
* @name documents
76+
* @memberof! DatabaseClient#
77+
* @type {documents}
78+
*/
79+
this.documents = new documents(this);
80+
/**
81+
* Provides functions that open, commit, or rollback multi-statement
82+
* transactions.
83+
* @name transactions
84+
* @memberof! DatabaseClient#
85+
* @type {transactions}
86+
*/
87+
this.transactions = new transactions(this);
88+
89+
/**
90+
* Provides access to namespaces that configure the REST server for the client.
91+
* The client must have been created for a user with the rest-admin role.
92+
* @name config
93+
* @memberof! DatabaseClient#
94+
* @property {config.extlibs} extlibs - provides functions that
95+
* maintain the extension libraries on the REST server
96+
* @property {config.serverprops} serverprops - provides functions that
97+
* modify the properties of the REST server
98+
* @property {config.transforms} transforms - provides functions that
99+
* maintain the transform extensions on the REST server
100+
*/
101+
this.config = {
102+
extlibs: new extlibs(this),
103+
serverprops: new restServerProperties(this),
104+
transforms: new transforms(this)
105+
};
106+
107+
// to inspect
108+
// setClientLogger.call(this, {level:'debug'});
109+
}
110+
111+
// operation shortcuts
112+
function createReadStreamClient() {
113+
return this.documents.createReadStream.apply(this.documents, arguments);
114+
}
115+
MarkLogicClient.prototype.createReadStream = createReadStreamClient;
116+
function createWriteStreamClient() {
117+
return this.documents.createWriteStream.apply(this.documents, arguments);
118+
}
119+
MarkLogicClient.prototype.createWriteStream = createWriteStreamClient;
120+
function patchClient() {
121+
return this.documents.patch.apply(this.documents, arguments);
122+
}
123+
MarkLogicClient.prototype.patch = patchClient;
124+
function probeClient() {
125+
return this.documents.probe.apply(this.documents, arguments);
126+
}
127+
MarkLogicClient.prototype.probe = probeClient;
128+
function queryClient() {
129+
return this.documents.query.apply(this.documents, arguments);
130+
}
131+
MarkLogicClient.prototype.query = queryClient;
132+
function readClient() {
133+
return this.documents.read.apply(this.documents, arguments);
134+
}
135+
MarkLogicClient.prototype.read = readClient;
136+
function removeClient() {
137+
return this.documents.remove.apply(this.documents, arguments);
138+
}
139+
MarkLogicClient.prototype.remove = removeClient;
140+
function removeAllClient() {
141+
return this.documents.removeAll.apply(this.documents, arguments);
142+
}
143+
MarkLogicClient.prototype.removeAll = removeAllClient;
144+
function writeClient() {
145+
return this.documents.write.apply(this.documents, arguments);
146+
}
147+
MarkLogicClient.prototype.write = writeClient;
148+
149+
function releaseMarkLogicClient() {
150+
mlrest.releaseClient.apply(this, arguments);
151+
}
152+
MarkLogicClient.prototype.release = releaseMarkLogicClient;
153+
49154
/**
50155
* Specifies logging for database interactions; takes a configuration
51156
* object with the following named parameters.
@@ -97,6 +202,8 @@ function setClientLogger(params) {
97202
mlrest.setLoggerLevel(logger, level);
98203
}
99204
}
205+
MarkLogicClient.prototype.setLogger = setClientLogger;
206+
100207
/** @ignore */
101208
function getClientLogger() {
102209
var logger = this.logger;
@@ -108,6 +215,8 @@ function getClientLogger() {
108215

109216
return logger;
110217
}
218+
MarkLogicClient.prototype.getLogger = getClientLogger;
219+
111220
/** @ignore */
112221
function createConsoleTransport(use) {
113222
var console = new winston.transports.Console({timestamp: true});
@@ -129,83 +238,6 @@ function configClientLogger(client, transports) {
129238
return logger;
130239
}
131240

132-
/**
133-
* Creates a database client to make database requests such as writes, reads,
134-
* and queries. The constructor takes a configuration object with the following
135-
* named parameters.
136-
* @function module:marklogic.createDatabaseClient
137-
* @param {string} [host=localhost] - the host with the REST server for the database
138-
* @param {number} [port=8000] - the port with the REST server for the database
139-
* @param {string} user - the user with permission to access the database
140-
* @param {string} password - the password for the user with permission to access
141-
* the database
142-
* @param {enum} [authType=digest] - the authentication type of digest or basic
143-
* @param {boolean} [ssl=false] - whether the REST server uses SSL; when true,
144-
* the connection parameters can include the
145-
* {@link http://nodejs.org/api/https.html#https_https_request_options_callback|supplemental
146-
* HTTPS options} specifiable for the node.js tls.connect() function.
147-
* @param {object} [agent] - defaults to a connection pooling agent
148-
* @returns {DatabaseClient} a client for accessing the database
149-
* as the user
150-
*/
151-
function MarkLogicClient(connectionParams) {
152-
mlrest.initClient(this, connectionParams);
153-
154-
this.release = mlrest.releaseClient.bind(this);
155-
156-
/**
157-
* Provides functions that write, read, query, or perform other operations
158-
* on documents in the database. As a convenience, the same functions are
159-
* provided on the database client.
160-
* @name documents
161-
* @memberof! DatabaseClient#
162-
* @type {documents}
163-
*/
164-
this.documents = new documents(this);
165-
/**
166-
* Provides functions that open, commit, or rollback multi-statement
167-
* transactions.
168-
* @name transactions
169-
* @memberof! DatabaseClient#
170-
* @type {transactions}
171-
*/
172-
this.transactions = new transactions(this);
173-
174-
/**
175-
* Provides access to namespaces that configure the REST server for the client.
176-
* The client must have been created for a user with the rest-admin role.
177-
* @name config
178-
* @memberof! DatabaseClient#
179-
* @property {config.extlibs} extlibs - provides functions that
180-
* maintain the extension libraries on the REST server
181-
* @property {config.serverprops} serverprops - provides functions that
182-
* modify the properties of the REST server
183-
* @property {config.transforms} transforms - provides functions that
184-
* maintain the transform extensions on the REST server
185-
*/
186-
this.config = {
187-
extlibs: new extlibs(this),
188-
serverprops: new restServerProperties(this),
189-
transforms: new transforms(this)
190-
};
191-
192-
// operation shortcuts
193-
this.createReadStream = this.documents.createReadStream;
194-
this.createWriteStream = this.documents.createWriteStream;
195-
this.patch = this.documents.patch;
196-
this.probe = this.documents.probe;
197-
this.query = this.documents.query;
198-
this.read = this.documents.read;
199-
this.remove = this.documents.remove;
200-
this.removeAll = this.documents.removeAll;
201-
this.write = this.documents.write;
202-
203-
// to inspect
204-
// setClientLogger.call(this, {level:'debug'});
205-
}
206-
MarkLogicClient.prototype.setLogger = setClientLogger;
207-
MarkLogicClient.prototype.getLogger = getClientLogger;
208-
209241
/** @ignore */
210242
function MarkLogicClientFactory(connectionParams) {
211243
if (arguments.length === 0)

lib/mlrest.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ function authenticatedRequest(operation, authenticator, requester, responder) {
330330
var isRead = valcheck.isNullOrUndefined(requester);
331331
var options = operation.options;
332332
operation.logger.debug('authenticated request for %s', options.path);
333-
var request = operation.client.request(options, responder.bind(operation));
333+
var request = operation.client.request(
334+
options, mlutil.callbackOn(operation, responder)
335+
);
334336
if (!valcheck.isNullOrUndefined(authenticator)) {
335337
request.setHeader('authorization',
336338
authenticator.authorize(options.method, options.path)
@@ -474,7 +476,7 @@ function singleResponder(response) {
474476

475477
var bodyReader = concatStream(
476478
{encoding: (isString ? 'string' : 'buffer')},
477-
operation.dispatchBody
479+
mlutil.callbackOn(operation, dispatchBody)
478480
);
479481
bodyReader.on('error', operation.errorListener);
480482
response.pipe(bodyReader);
@@ -607,18 +609,17 @@ function Operation(name, client, options, requestType, responseType) {
607609
this.partTransform = null;
608610
this.errorTransform = null;
609611
// listeners and other callbacks usable out of context
610-
this.errorListener = operationErrorListener.bind(this);
611-
this.resultPromise = operationResultPromise.bind(this);
612+
this.errorListener = mlutil.callbackOn(this, dispatchEndError);
613+
this.resultPromise = mlutil.callbackOn(this, operationResultPromise);
612614
this.resultStream = null;
613615
this.outputStream = null;
614-
this.dispatchBody = dispatchBody.bind(this);
615616
}
616617

617618
function createOperation(name, client, options, requestType, responseType) {
618619
return new Operation(name, client, options, requestType, responseType);
619620
}
620621
function createSelector(operation) {
621-
var resultStream = operationResultStream.bind(operation);
622+
var resultStream = mlutil.callbackOn(operation, operationResultStream);
622623
operation.resultStream = resultStream;
623624
return {
624625
result: operation.resultPromise,
@@ -639,8 +640,8 @@ function initPartParser(parser) {
639640
operation.nextPart = 0;
640641
operation.hasFinished = false;
641642

642-
parser.on('part', partListener.bind(operation));
643-
parser.on('finish', partFinishListener.bind(operation));
643+
parser.on('part', mlutil.callbackOn(operation, partListener));
644+
parser.on('finish', mlutil.callbackOn(operation, partFinishListener));
644645
parser.on('error', operation.errorListener);
645646
}
646647
Operation.prototype.initPartParser = initPartParser;
@@ -679,8 +680,8 @@ function PartResponder(operation, part) {
679680
this.format = null;
680681
this.isMetadata = false;
681682
this.isUtf8 = false;
682-
this.headersListener = partHeadersListener.bind(this);
683-
this.contentListener = partContentListener.bind(this);
683+
this.headersListener = mlutil.callbackOn(this, partHeadersListener);
684+
this.contentListener = mlutil.callbackOn(this, partContentListener);
684685
}
685686
function partHeadersListener(headers) {
686687
var operation = this.operation;
@@ -848,6 +849,7 @@ function dispatchBody(data) {
848849
operation.dispatchData(value);
849850
operation.dispatchEnd();
850851
}
852+
Operation.prototype.dispatchBody = dispatchBody;
851853
function dispatchMetadata(metadata) {
852854
var operation = this;
853855

@@ -883,11 +885,6 @@ function dispatchData(data) {
883885
}
884886
Operation.prototype.dispatchData = dispatchData;
885887

886-
function operationErrorListener(error) {
887-
var operation = this;
888-
889-
operation.dispatchEndError(error);
890-
}
891888
function dispatchEndError(error) {
892889
var operation = this;
893890

lib/mlutil.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,17 @@ function MarkLogicError() {
103103
}
104104
util.inherits(MarkLogicError, Error);
105105

106+
function callbackOn(object, method) {
107+
var self = object;
108+
var func = method;
109+
return function() {
110+
return func.apply(self, arguments);
111+
};
112+
}
113+
106114
module.exports = {
107115
asArray: asArray,
116+
callbackOn: callbackOn,
108117
copyProperties: copyProperties,
109118
first: first,
110119
isBoolean: isBoolean,

0 commit comments

Comments
 (0)