new DynamicView(collection, name, optionsopt)
DynamicView class is a versatile 'live' view class which can have filters and sorts applied. Collection.addDynamicView(name) instantiates this DynamicView object and notifies it whenever documents are add/updated/removed so it can remain up-to-date. (chainable)
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
collection |
Collection | A reference to the collection to work against |
|||||||||||||||||||||
name |
string | The name of this dynamic view |
|||||||||||||||||||||
options |
object |
<optional> |
(Optional) Pass in object with 'persistent' and/or 'sortPriority' options. Properties
|
- Implements:
- Source:
- See:
-
- Collection#addDynamicView to construct instances of DynamicView
Example
var mydv = mycollection.addDynamicView('test'); // default is non-persistent
mydv.applyFind({ 'doors' : 4 });
mydv.applyWhere(function(obj) { return obj.name === 'Toyota'; });
var results = mydv.data();
Methods
applyFilter(filter) → {DynamicView}
applyFilter() - Adds or updates a filter in the DynamicView filter pipeline
Parameters:
Name | Type | Description |
---|---|---|
filter |
object | A filter object to add to the pipeline. The object is in the format { 'type': filter_type, 'val', filter_param, 'uid', optional_filter_id } |
Returns:
this DynamicView object, for further chain ops.
- Type
- DynamicView
applyFind(query, uidopt) → {DynamicView}
applyFind() - Adds or updates a mongo-style query option in the DynamicView filter pipeline
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
query |
object | A mongo-style query object to apply to pipeline |
|
uid |
string | number |
<optional> |
Optional: The unique ID of this filter, to reference it in the future. |
Returns:
this DynamicView object, for further chain ops.
- Type
- DynamicView
applySimpleSort(propname, options) → {DynamicView}
applySimpleSort() - Used to specify a property used for view translation.
Parameters:
Name | Type | Description | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
propname |
string | Name of property by which to sort. |
|||||||||||||||||||||||||
options |
object | boolean | boolean for sort descending or options object Properties
|
Returns:
this DynamicView object, for further chain ops.
- Type
- DynamicView
Example
dv.applySimpleSort("name");
applySort(comparefun) → {DynamicView}
applySort() - Used to apply a sort to the dynamic view
Parameters:
Name | Type | Description |
---|---|---|
comparefun |
function | a javascript compare function used for sorting |
Returns:
this DynamicView object, for further chain ops.
- Type
- DynamicView
Example
dv.applySort(function(obj1, obj2) {
if (obj1.name === obj2.name) return 0;
if (obj1.name > obj2.name) return 1;
if (obj1.name < obj2.name) return -1;
});
applySortCriteria(properties) → {DynamicView}
applySortCriteria() - Allows sorting a resultset based on multiple columns.
Parameters:
Name | Type | Description |
---|---|---|
properties |
array | array of property names or subarray of [propertyname, isdesc] used evaluate sort order |
Returns:
Reference to this DynamicView, sorted, for future chain operations.
- Type
- DynamicView
Example
// to sort by age and then name (both ascending)
dv.applySortCriteria(['age', 'name']);
// to sort by age (ascending) and then by name (descending)
dv.applySortCriteria(['age', ['name', true]);
// to sort by age (descending) and then by name (descending)
dv.applySortCriteria(['age', true], ['name', true]);
applyWhere(fun, uidopt) → {DynamicView}
applyWhere() - Adds or updates a javascript filter function in the DynamicView filter pipeline
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fun |
function | A javascript filter function to apply to pipeline |
|
uid |
string | number |
<optional> |
Optional: The unique ID of this filter, to reference it in the future. |
Returns:
this DynamicView object, for further chain ops.
- Type
- DynamicView
branchResultset(transform, parametersopt) → {Resultset}
branchResultset() - Makes a copy of the internal resultset for branched queries. Unlike this dynamic view, the branched resultset will not be 'live' updated, so your branched query should be immediately resolved and not held for future evaluation.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
transform |
string | array | Optional name of collection transform, or an array of transform steps |
|
parameters |
object |
<optional> |
optional parameters (if optional transform requires them) |
Returns:
A copy of the internal resultset for branched queries.
- Type
- Resultset
Example
var db = new loki('test');
var coll = db.addCollection('mydocs');
var dv = coll.addDynamicView('myview');
var tx = [
{
type: 'offset',
value: '[%lktxp]pageStart'
},
{
type: 'limit',
value: '[%lktxp]pageSize'
}
];
coll.addTransform('viewPaging', tx);
// add some records
var results = dv.branchResultset('viewPaging', { pageStart: 10, pageSize: 10 }).data();
count() → {number}
count() - returns the number of documents representing the current DynamicView contents.
Returns:
The number of documents representing the current DynamicView contents.
- Type
- number
data(optionsopt) → {array}
data() - resolves and pending filtering and sorting, then returns document array as result.
Parameters:
Name | Type | Attributes | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
<optional> |
optional parameters to pass to resultset.data() if non-persistent Properties
|
Returns:
An array of documents representing the current DynamicView contents.
- Type
- array
mapReduce(mapFunction, reduceFunction)
mapReduce() - data transformation via user supplied functions
Parameters:
Name | Type | Description |
---|---|---|
mapFunction |
function | this function accepts a single document for you to transform and return |
reduceFunction |
function | this function accepts many (array of map outputs) and returns single value |
Returns:
The output of your reduceFunction
rematerialize(optionsopt) → {DynamicView}
rematerialize() - internally used immediately after deserialization (loading) This will clear out and reapply filterPipeline ops, recreating the view. Since where filters do not persist correctly, this method allows restoring the view to state where user can re-apply those where filters.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options |
Object |
<optional> |
(Optional) allows specification of 'removeWhereFilters' option |
Fires:
- DynamicView.event:rebuild
Returns:
This dynamic view for further chained ops.
- Type
- DynamicView
removeFilter(uid) → {DynamicView}
removeFilter() - Remove the specified filter from the DynamicView filter pipeline
Parameters:
Name | Type | Description |
---|---|---|
uid |
string | number | The unique ID of the filter to be removed. |
Returns:
this DynamicView object, for further chain ops.
- Type
- DynamicView
removeFilters(optionsopt)
removeFilters() - Used to clear pipeline and reset dynamic view to initial state. Existing options should be retained.
Parameters:
Name | Type | Attributes | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
<optional> |
configure removeFilter behavior Properties
|