Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit aab28a2

Browse files
committed
Improve documentation for search paging
1 parent 9303d4f commit aab28a2

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

docs/client.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ containing the following fields:
204204
|attrsOnly |boolean on whether you want the server to only return the names of the attributes, and not their values. Borderline useless. Defaults to false.|
205205
|sizeLimit |the maximum number of entries to return. Defaults to 0 (unlimited).|
206206
|timeLimit |the maximum amount of time the server should take in responding, in seconds. Defaults to 10. Lots of servers will ignore this.|
207+
|paging |enable and/or configure automatic result paging|
207208

208209
Responses from the `search` method are an `EventEmitter` where you will get a
209210
notification for each `searchEntry` that comes back from the server. You will
@@ -277,6 +278,80 @@ The `not` character is represented as a `!`, the `or` as a single pipe `|`.
277278
It gets a little bit complicated, but it's actually quite powerful, and lets you
278279
find almost anything you're looking for.
279280

281+
## Paging
282+
Many LDAP server enforce size limits upon the returned result set (commonly
283+
1000). In order to retrieve results beyond this limit, a `PagedResultControl`
284+
is passed between the client and server to iterate through the entire dataset.
285+
While callers could choose to do this manually via the `controls` parameter to
286+
`search()`, ldapjs has internal mechanisms to easily automate the process. The
287+
most simple way to use the paging automation is to set the `paging` option to
288+
true when performing a search:
289+
290+
var opts = {
291+
filter: '(objectclass=commonobject)',
292+
scope: 'sub',
293+
paging: true,
294+
sizeLimit: 200
295+
};
296+
client.search('o=largedir', opts, function(err, res) {
297+
assert.ifError(err);
298+
res.on('searchEntry', function(entry) {
299+
// do per-entry processing
300+
});
301+
res.on('page', function(result) {
302+
console.log('page end');
303+
});
304+
res.on('error', function(resErr) {
305+
assert.ifError(resErr);
306+
});
307+
res.on('end', function(result) {
308+
console.log('done ');
309+
});
310+
});
311+
312+
This will enable paging with a default page size of 199 (`sizeLimit` - 1) and
313+
will output all of the resulting objects via the `searchEntry` event. At the
314+
end of each result during the operation, a `page` event will be emitted as
315+
well (which includes the intermediate `searchResult` object).
316+
317+
For those wanting more precise control over the process, an object with several
318+
parameters can be provided for the `paged` option. The `pageSize` parameter
319+
sets the size of result pages requested from the server. If no value is
320+
specified, it will fall back to the default (100 or `sizeLimit` - 1, to obey
321+
the RFC). The `pagePause` parameter allows back-pressure to be exerted on the
322+
paged search operation by pausing at the end of each page. When enabled, a
323+
callback function is passed as an additional parameter to `page` events. The
324+
client will wait to request the next page until that callback is executed.
325+
326+
Here is an example where both of those parameters are used:
327+
328+
var queue = new MyWorkQueue(someSlowWorkFunction);
329+
var opts = {
330+
filter: '(objectclass=commonobject)',
331+
scope: 'sub',
332+
paging: {
333+
pageSize: 250,
334+
pagePause: true
335+
},
336+
};
337+
client.search('o=largerdir', opts, function(err, res) {
338+
assert.ifError(err);
339+
res.on('searchEntry', function(entry) {
340+
// Submit incoming objects to queue
341+
queue.push(entry);
342+
});
343+
res.on('page', function(result, cb) {
344+
// Allow the queue to flush before fetching next page
345+
queue.cbWhenFlushed(cb);
346+
});
347+
res.on('error', function(resErr) {
348+
assert.ifError(resErr);
349+
});
350+
res.on('end', function(result) {
351+
console.log('done');
352+
});
353+
});
354+
280355
# starttls
281356
`starttls(options, controls, callback)`
282357

0 commit comments

Comments
 (0)