Package Exports
- base-data
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (base-data) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
base-data 
adds a
data
method to base-methods.
Adds a data
method to base-methods that can be used for setting, getting and loading data onto a specified object in your application.
- Install
- Usage
- API
- Glob patterns
- Namespacing
- Related projects
- Running tests
- Contributing
- Author
- License
(Table of contents generated by [verb])
Install
Install with npm
$ npm i base-data --save
Usage
var Base = require('base-methods');
var data = require('base-data');
// instantiate `Base`
var base = new Base();
// add `data` as a plugin
base.use(data());
Examples
Add data:
app.data('a', 'b');
app.data({c: 'd'});
app.data('e', ['f']);
console.log(app.cache.data);
//=> {a: 'b', c: 'd', e: ['f']}
cache.data
By default, all data is loaded onto app.cache.data
. This can be customized by passing the property to use when the plugin is initialized.
For example, the following set app.foo
as object for storing data:
app.use(data('foo'));
app.data('a', 'b');
console.log(app.foo);
//=> {a: 'b'}
API
.dataLoader
Register a data loader for loading data onto app.cache.data
.
Params
ext
{String}: The file extension for to match to the loaderfn
{Function}: The loader function.
Example
var fs = require('fs');
var yaml = require('js-yaml');
app.dataLoader('yml', function(fp) {
var str = fs.readFileSync(fp, 'utf8');
return yaml.safeLoad(str);
});
app.data('foo.yml');
//=> loads and parses `foo.yml` as yaml
.data
Load data onto app.cache.data
Params
key
{String|Object}: Key of the value to set, or object to extend.val
{any}returns
{Object}: Returns the instance ofTemplate
for chaining
Example
console.log(app.cache.data);
//=> {};
app.data('a', 'b');
app.data({c: 'd'});
console.log(app.cache.data);
//=> {a: 'b', c: 'd'}
// set an array
app.data('e', ['f']);
// overwrite the array
app.data('e', ['g']);
// update the array
app.data('e', ['h'], true);
console.log(app.cache.data.e);
//=> ['g', 'h']
Glob patterns
Glob patterns may be passed as a string or array. All of these work:
app.data('foo.json');
app.data('*.json');
app.data(['*.json']);
// pass options to node-glob
app.data(['*.json'], {dot: true});
Namespacing
Namespacing allows you to load data onto a specific key, optionally using part of the file path as the key.
Example
Given that foo.json
contains {a: 'b'}
:
app.data('foo.json');
console.log(app.cache.data);
//=> {a: 'b'}
app.data('foo.json', {namespace: true});
console.log(app.cache.data);
//=> {foo: {a: 'b'}}
app.data('foo.json', {
namespace: function(fp) {
return path.basename(fp);
}
});
console.log(app.cache.data);
//=> {'foo.json': {a: 'b'}}
Related projects
- base-methods: Starter for creating a node.js application with a handful of common methods, like
set
,get
,… more | homepage - base-options: Adds a few options methods to base-methods, like
option
,enable
anddisable
. See the readme… more | homepage - base-plugins: Upgrade's plugin support in base-methods to allow plugins to be called any time after init. | homepage
Running tests
Install dev dependencies:
$ npm i -d && npm test
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Author
Jon Schlinkert
License
Copyright © 2015 Jon Schlinkert Released under the MIT license.
This file was generated by verb-cli on October 19, 2015.