
Security News
NVD Concedes Inability to Keep Pace with Surging CVE Disclosures in 2025
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
A base class for objects to be recursively mapped to a view-friendly object structure
Inherit from this base class to enable recursive mapping to a view-friendly object structure.
To output Assets from this object model:
Do this:
Asset.findById(req.params.id, function(err, asset){
asset.toViewObject({
// Selectively include non-default properties in output
asset: { owner: { lastSeenAt: true } }
}, function(err, viewObject){
res.send(viewObject);
});
});
Or for collections of Assets:
Asset.findAll(function(err, assets, summary){
ViewContext.create({
assets: assets,
summary: summary
}).template({
assets: { owner: { lastSeenAt: true } },
summary: true
}).toViewObject(function(err, viewObject){
res.send(viewObject);
});
});
See examples:
$ npm install mappable
For each of the classes in your object model, inherit from Base.
function MyClass(){
MyClass.super_.apply(this);
}
util.inherits(MyClass, require('mappable').Base);
(If your class already inherits from something else, wrap instances using ViewContext or consider wrapping it within a class that inherits from Base.)
MyClass.prototype.__defineViewMappings__({
// Simple deep map by specifying the path
'deepValue': 'doc.deeper.value',
// Synchronously return a value
'syncValue': function(){
return new Date(doc.updatedAtMs);
},
// Asynchronously return a value
'asyncValue': function(done) {
this.fetchValue(done);
},
// Or with `self` already declared, for use with deeper callbacks
'asyncValueSelf': function(self, done) {
self.fetchValueAlt(function(err, valueAlt){
if (err) return done(err);
self.fetchWithAlt(valueAlt, done);
});
}
});
MyClass.prototype.__defineViewTemplate__({
deepValue: true,
syncValue: true
});
Mappable#toViewObject(template, callback)
Arguments:
'*'
will include all properties available, recursively (not recommended for production)true
or a sub-template Object, e.g. { updatedAt: true, owner: { updatedAt: true }}
function(err, viewObject){}
Use a ViewContext instance to recursively convert a custom object containing instances.
var ViewContext = require('mappable').ViewContext;
ViewContext.create(obj)
Returns a new ViewContext instance.
Arguments:
ViewContext#map(mappings)
Returns the same ViewContext instance for chaining.
ViewContext#template(template)
Returns the same ViewContext instance for chaining.
ViewContext#toViewObject(template, callback)
See instance#toViewObject.
ViewContext#on(eventName, fn)
Events:
'error'
e.g function(err){}
'complete'
e.g. function(viewObject){}
The default is to run mapping operations in parallel. To limit the number of concurrent mappings:
require('mappable').setConcurrencyLimit(5);
Or, for series:
require('mappable').setConcurrencyLimit(1);
FAQs
A base class for objects to be recursively mapped to a view-friendly object structure
The npm package mappable receives a total of 6 weekly downloads. As such, mappable popularity was classified as not popular.
We found that mappable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
Security Fundamentals
Attackers use obfuscation to hide malware in open source packages. Learn how to spot these techniques across npm, PyPI, Maven, and more.
Security News
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.