{"id":25180651,"url":"https://github.com/base-repos/base-data","last_synced_at":"2026-01-11T13:30:47.669Z","repository":{"id":57189208,"uuid":"43544591","full_name":"base-repos/base-data","owner":"base-repos","description":"adds a `data` method to base-methods. 100% unit test coverage and browserify-friendly lazy-caching.","archived":false,"fork":false,"pushed_at":"2020-04-01T00:06:45.000Z","size":79,"stargazers_count":19,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-07T06:04:32.837Z","etag":null,"topics":["base","context","data","extend","files","glob","merge","object","objects","plugin","read"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/base-repos.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-02T09:16:54.000Z","updated_at":"2025-01-31T00:36:36.000Z","dependencies_parsed_at":"2022-09-11T15:50:51.136Z","dependency_job_id":null,"html_url":"https://github.com/base-repos/base-data","commit_stats":null,"previous_names":["node-base/base-data","jonschlinkert/base-data","base-repos/base-data"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/base-repos%2Fbase-data","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/base-repos%2Fbase-data/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/base-repos%2Fbase-data/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/base-repos%2Fbase-data/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/base-repos","download_url":"https://codeload.github.com/base-repos/base-data/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252823920,"owners_count":21809713,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["base","context","data","extend","files","glob","merge","object","objects","plugin","read"],"created_at":"2025-02-09T16:19:12.951Z","updated_at":"2026-01-11T13:30:47.661Z","avatar_url":"https://github.com/base-repos.png","language":"JavaScript","readme":"# base-data [![NPM version](https://img.shields.io/npm/v/base-data.svg?style=flat)](https://www.npmjs.com/package/base-data) [![NPM monthly downloads](https://img.shields.io/npm/dm/base-data.svg?style=flat)](https://npmjs.org/package/base-data) [![NPM total downloads](https://img.shields.io/npm/dt/base-data.svg?style=flat)](https://npmjs.org/package/base-data) [![Linux Build Status](https://img.shields.io/travis/node-base/base-data.svg?style=flat\u0026label=Travis)](https://travis-ci.org/node-base/base-data)\n\n\u003e adds a `data` method to base-methods.\n\n- [Install](#install)\n- [Usage](#usage)\n- [API](#api)\n- [Glob patterns](#glob-patterns)\n- [Namespacing](#namespacing)\n- [History](#history)\n- [About](#about)\n\n_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n```sh\n$ npm install --save base-data\n```\n\n## Usage\n\nAdds a `data` method to [base](https://github.com/node-base/base) that can be used for setting, getting and loading data onto a specified object in your application.\n\n```js\nvar Base = require('base');\nvar data = require('base-data');\n\n// instantiate `Base`\nvar base = new Base();\n// add `data` as a plugin\nbase.use(data());\n```\n\n**Examples**\n\nAdd data:\n\n```js\napp.data('a', 'b');\napp.data({c: 'd'});\napp.data('e', ['f']);\nconsole.log(app.cache.data);\n//=\u003e {a: 'b', c: 'd', e: ['f']}\n```\n\n**cache.data**\n\nBy default, all data is loaded onto `app.cache.data`. This can be customized by passing the property to use when the plugin is initialized.\n\nFor example, the following set `app.foo` as object for storing data:\n\n```js\napp.use(data('foo'));\napp.data('a', 'b');\nconsole.log(app.foo);\n//=\u003e {a: 'b'}\n```\n\n## API\n\n### [.dataLoader](index.js#L66)\n\nRegister a data loader for loading data onto `app.cache.data`.\n\n**Params**\n\n* `ext` **{String}**: The file extension for to match to the loader\n* `fn` **{Function}**: The loader function.\n\n**Example**\n\n```js\nvar yaml = require('js-yaml');\n\napp.dataLoader('yml', function(str, fp) {\n  return yaml.safeLoad(str);\n});\n\napp.data('foo.yml');\n//=\u003e loads and parses `foo.yml` as yaml\n```\n\n### [.data](index.js#L101)\n\nLoad data onto `app.cache.data`\n\n**Params**\n\n* `key` **{String|Object}**: Key of the value to set, or object to extend.\n* `val` **{any}**\n* `returns` **{Object}**: Returns the instance of `Template` for chaining\n\n**Example**\n\n```js\nconsole.log(app.cache.data);\n//=\u003e {};\n\napp.data('a', 'b');\napp.data({c: 'd'});\nconsole.log(app.cache.data);\n//=\u003e {a: 'b', c: 'd'}\n\n// set an array\napp.data('e', ['f']);\n\n// overwrite the array\napp.data('e', ['g']);\n\n// update the array\napp.data('e', ['h'], true);\nconsole.log(app.cache.data.e);\n//=\u003e ['g', 'h']\n```\n\n### [.data.extend](index.js#L252)\n\nShallow extend an object onto `app.cache.data`.\n\n**Params**\n\n* `key` **{String|Object}**: Property name or object to extend onto `app.cache.data`. Dot-notation may be used for extending nested properties.\n* `value` **{Object}**: The object to extend onto `app.cache.data`\n* `returns` **{Object}**: returns the instance for chaining\n\n**Example**\n\n```js\napp.data({a: {b: {c: 'd'}}});\napp.data.extend('a.b', {x: 'y'});\nconsole.log(app.get('a.b'));\n//=\u003e {c: 'd', x: 'y'}\n```\n\n### [.data.merge](index.js#L285)\n\nDeeply merge an object onto `app.cache.data`.\n\n**Params**\n\n* `key` **{String|Object}**: Property name or object to merge onto `app.cache.data`. Dot-notation may be used for merging nested properties.\n* `value` **{Object}**: The object to merge onto `app.cache.data`\n* `returns` **{Object}**: returns the instance for chaining\n\n**Example**\n\n```js\napp.data({a: {b: {c: {d: {e: 'f'}}}}});\napp.data.merge('a.b', {c: {d: {g: 'h'}}});\nconsole.log(app.get('a.b'));\n//=\u003e {c: {d: {e: 'f', g: 'h'}}}\n```\n\n### [.data.union](index.js#L317)\n\nUnion the given value onto a new or existing array value on `app.cache.data`.\n\n**Params**\n\n* `key` **{String}**: Property name. Dot-notation may be used for nested properties.\n* `array` **{Object}**: The array to add or union on `app.cache.data`\n* `returns` **{Object}**: returns the instance for chaining\n\n**Example**\n\n```js\napp.data({a: {b: ['c', 'd']}});\napp.data.union('a.b', ['e', 'f']}});\nconsole.log(app.get('a.b'));\n//=\u003e ['c', 'd', 'e', 'f']\n```\n\n### [.data.set](index.js#L337)\n\nSet the given value onto `app.cache.data`.\n\n**Params**\n\n* `key` **{String|Object}**: Property name or object to merge onto `app.cache.data`. Dot-notation may be used for nested properties.\n* `val` **{any}**: The value to set on `app.cache.data`\n* `returns` **{Object}**: returns the instance for chaining\n\n**Example**\n\n```js\napp.data.set('a.b', ['c', 'd']}});\nconsole.log(app.get('a'));\n//=\u003e {b: ['c', 'd']}\n```\n\n### [.data.get](index.js#L360)\n\nGet the value of `key` from `app.cache.data`. Dot-notation may be used for getting nested properties.\n\n**Params**\n\n* `key` **{String}**: The name of the property to get.\n* `returns` **{any}**: Returns the value of `key`\n\n**Example**\n\n```js\napp.data({a: {b: {c: 'd'}}});\nconsole.log(app.get('a.b'));\n//=\u003e {c: 'd'}\n```\n\n## Glob patterns\n\nGlob patterns may be passed as a string or array. All of these work:\n\n```js\napp.data('foo.json');\napp.data('*.json');\napp.data(['*.json']);\n// pass options to node-glob\napp.data(['*.json'], {dot: true});\n```\n\n## Namespacing\n\nNamespacing allows you to load data onto a specific key, optionally using part of the file path as the key.\n\n**Example**\n\nGiven that `foo.json` contains `{a: 'b'}`:\n\n```js\napp.data('foo.json');\nconsole.log(app.cache.data);\n//=\u003e {a: 'b'}\n\napp.data('foo.json', {namespace: true});\nconsole.log(app.cache.data);\n//=\u003e {foo: {a: 'b'}}\n\napp.data('foo.json', {\n  namespace: function(fp) {\n    return path.basename(fp);\n  }\n});\nconsole.log(app.cache.data);\n//=\u003e {'foo.json': {a: 'b'}}\n```\n\n## History\n\n**v0.6.0**\n\n* removes `renameKey` option for namespacing\n* replaces [is-valid-instance](https://github.com/jonschlinkert/is-valid-instance) and [is-registered](https://github.com/jonschlinkert/is-registered) with [is-valid-app](https://github.com/node-base/is-valid-app)\n\n**v0.5.0**\n\n* uses [is-valid-instance](https://github.com/jonschlinkert/is-valid-instance) and [is-registered](https://github.com/jonschlinkert/is-registered) to ensure the smart plugin is registered on the correct objects.\n\n**v0.4.0**\n\n* _Refactored_\n\n* adds methods to `.data` for getting and setting data.\n\n**v0.3.6**\n\n* adds a basic loader that only calls the `JSON.parse` method, if no other loaders are defined\n* calls `.isRegistered` from [base](https://github.com/node-base/base) to ensure the plugin is only loaded once on an instance\n\n## About\n\n### Related projects\n\n* [base-cli](https://www.npmjs.com/package/base-cli): Plugin for base-methods that maps built-in methods to CLI args (also supports methods from a… [more](https://github.com/node-base/base-cli) | [homepage](https://github.com/node-base/base-cli \"Plugin for base-methods that maps built-in methods to CLI args (also supports methods from a few plugins, like 'base-store', 'base-options' and 'base-data'.\")\n* [base-config](https://www.npmjs.com/package/base-config): base-methods plugin that adds a `config` method for mapping declarative configuration values to other 'base… [more](https://github.com/node-base/base-config) | [homepage](https://github.com/node-base/base-config \"base-methods plugin that adds a `config` method for mapping declarative configuration values to other 'base' methods or custom functions.\")\n* [base-option](https://www.npmjs.com/package/base-option): Adds a few options methods to base, like `option`, `enable` and `disable`. See the readme… [more](https://github.com/node-base/base-option) | [homepage](https://github.com/node-base/base-option \"Adds a few options methods to base, like `option`, `enable` and `disable`. See the readme for the full API.\")\n* [base-pipeline](https://www.npmjs.com/package/base-pipeline): base-methods plugin that adds pipeline and plugin methods for dynamically composing streaming plugin pipelines. | [homepage](https://github.com/node-base/base-pipeline \"base-methods plugin that adds pipeline and plugin methods for dynamically composing streaming plugin pipelines.\")\n* [base-plugins](https://www.npmjs.com/package/base-plugins): Adds 'smart plugin' support to your base application. | [homepage](https://github.com/node-base/base-plugins \"Adds 'smart plugin' support to your base application.\")\n* [base-store](https://www.npmjs.com/package/base-store): Plugin for getting and persisting config values with your base-methods application. Adds a 'store' object… [more](https://github.com/node-base/base-store) | [homepage](https://github.com/node-base/base-store \"Plugin for getting and persisting config values with your base-methods application. Adds a 'store' object that exposes all of the methods from the data-store library. Also now supports sub-stores!\")\n* [base](https://www.npmjs.com/package/base): Framework for rapidly creating high quality node.js applications, using plugins like building blocks | [homepage](https://github.com/node-base/base \"Framework for rapidly creating high quality node.js applications, using plugins like building blocks\")\n\n### Contributing\n\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).\n\n### Contributors\n\n| **Commits** | **Contributor** | \n| --- | --- |\n| 69 | [jonschlinkert](https://github.com/jonschlinkert) |\n| 10 | [doowb](https://github.com/doowb) |\n\n### Building docs\n\n_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_\n\nTo generate the readme, run the following command:\n\n```sh\n$ npm install -g verbose/verb#dev verb-generate-readme \u0026\u0026 verb\n```\n\n### Running tests\n\nRunning and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:\n\n```sh\n$ npm install \u0026\u0026 npm test\n```\n\n### Author\n\n**Jon Schlinkert**\n\n* [github/jonschlinkert](https://github.com/jonschlinkert)\n* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)\n\n### License\n\nCopyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).\nReleased under the [MIT License](LICENSE).\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on July 20, 2017._","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbase-repos%2Fbase-data","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbase-repos%2Fbase-data","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbase-repos%2Fbase-data/lists"}