Comparing version 1.0.1 to 1.0.2
@@ -1,1 +0,1 @@ | ||
"use strict";function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}function _defineProperty(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}var Store=require("./Store.js"),_require=require("../utils/typeCheck.js"),throwIfNoArgument=_require.throwIfNoArgument,Database=function(){function a(){var b=this,c=0<arguments.length&&void 0!==arguments[0]?arguments[0]:throwIfNoArgument();_classCallCheck(this,a),_defineProperty(this,"_createStore",function(a){return new Store(a)}),_defineProperty(this,"_findStore",function(a){return b.stores.findIndex(function(b){return b.name===a})}),this.name=c,this.stores=[]}return _createClass(a,[{key:"registerObjectStore",value:function registerObjectStore(a){var b=this._findStore(a);if(-1!==b)throw new Error("Store ".concat(a," has already been registered"));var c=this._createStore(a);return this.stores.push(c),c}}]),a}();module.exports=Database; | ||
"use strict";function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}function _defineProperty(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}var Store=require("./Store.js"),_require=require("../utils/typeCheck.js"),throwIfNoArgument=_require.throwIfNoArgument,Database=function(){function a(){var b=this,c=0<arguments.length&&void 0!==arguments[0]?arguments[0]:throwIfNoArgument();_classCallCheck(this,a),_defineProperty(this,"_createStore",function(a){return new Store(a)}),_defineProperty(this,"_findStore",function(a){return b.stores.findIndex(function(b){return b.name===a})}),this.name=c,this.stores=[]}return _createClass(a,[{key:"registerObjectStore",value:function registerObjectStore(a){var b=this._findStore(a);if(-1!==b)throw new Error("Store '".concat(a,"' has already been registered"));var c=this._createStore(a);return this.stores.push(c),c}}]),a}();module.exports=Database; |
@@ -1,1 +0,1 @@ | ||
"use strict";function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}var Emitter=function(){function a(){_classCallCheck(this,a),this.handlers=new Map}return _createClass(a,[{key:"subscribe",value:function subscribe(a,b){this.handlers.set(a,b)}},{key:"unsubscribe",value:function unsubscribe(a){this.handlers["delete"](a)}},{key:"emit",value:function emit(a){var b=this;if(!this.handlers.has(a))throw new Error("".concat(a," event has not been registered"));return function(){b.handlers.get(a).apply(void 0,arguments)}}}]),a}();module.exports=Emitter; | ||
"use strict";function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}var Emitter=function(){function a(){_classCallCheck(this,a),this.handlers=new Map}return _createClass(a,[{key:"subscribe",value:function subscribe(a,b){this.handlers.set(a,b)}},{key:"unsubscribe",value:function unsubscribe(a){this.handlers["delete"](a)}},{key:"emit",value:function emit(a){var b=this;if(!this.handlers.has(a))throw new Error("'on".concat(a.charAt(0).toUpperCase()+a.slice(1),"' event has not been registered"));return function(){b.handlers.get(a).apply(void 0,arguments)}}}]),a}();module.exports=Emitter; |
@@ -1,1 +0,1 @@ | ||
"use strict";function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}var KeyError=require("../errors/KeyError"),_require=require("../utils/typeCheck"),throwIfNotFn=_require.throwIfNotFn,throwIfNoArgument=_require.throwIfNoArgument,throwIfInvalidKey=_require.throwIfInvalidKey,Emitter=require("./Emitter"),Store=function(){function a(){var b=0<arguments.length&&void 0!==arguments[0]?arguments[0]:throwIfNoArgument();_classCallCheck(this,a),this.name=b,this.data={},this.emitter=new Emitter}return _createClass(a,[{key:"onSuccess",value:function onSuccess(a){throwIfNotFn(a),this.emitter.subscribe("success",a)}},{key:"onError",value:function onError(a){throwIfNotFn(a),this.emitter.subscribe("error",a)}},{key:"get",value:function get(a){throwIfInvalidKey(a);var b=this.data[a];b?this.emitter.emit("success")(b):this.emitter.emit("error")(new KeyError(a,this.name))}},{key:"set",value:function set(a,b){throwIfInvalidKey(a),this.data[a]=b}}]),a}();module.exports=Store; | ||
"use strict";function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}var KeyError=require("../errors/KeyError"),_require=require("../utils/generateEventObject"),generateEvent=_require.generateEvent,_require2=require("../utils/typeCheck"),throwIfNotFn=_require2.throwIfNotFn,throwIfNoArgument=_require2.throwIfNoArgument,throwIfInvalidKey=_require2.throwIfInvalidKey,Emitter=require("./Emitter"),Store=function(){function a(){var b=0<arguments.length&&void 0!==arguments[0]?arguments[0]:throwIfNoArgument();_classCallCheck(this,a),this.name=b,this.data={},this.emitter=new Emitter}return _createClass(a,[{key:"onSuccess",value:function onSuccess(a){return throwIfNotFn(a),this.emitter.subscribe("success",a),this}},{key:"onError",value:function onError(a){return throwIfNotFn(a),this.emitter.subscribe("error",a),this}},{key:"get",value:function get(a){throwIfInvalidKey(a);var b=this.data[a];return void 0===b||null===b?this.emitter.emit("error")(function(b){return generateEvent(b,new KeyError(a,b))}(this.name)):this.emitter.emit("success")(generateEvent(this.name,b)),b}},{key:"set",value:function set(a,b){return throwIfInvalidKey(a),this.data[a]=b,this}}]),a}();module.exports=Store; |
@@ -1,1 +0,1 @@ | ||
"use strict";var _require=require("./isType"),isFunction=_require.isFunction,isString=_require.isString,isNumber=_require.isNumber,ValidationError=require("../errors/ValidationError"),throwIfNotFn=function(a){if(!isFunction(a))throw new ValidationError("".concat(a," is not a function"))},throwIfInvalidKey=function(a){if(!isString(a)&&!isNumber(a))throw new ValidationError("'".concat(a,"' is not a valid key. Must be a string or number"))},throwIfNoArgument=function(){throw new ValidationError("Missing required constructor argument")};module.exports={throwIfNotFn:throwIfNotFn,throwIfInvalidKey:throwIfInvalidKey,throwIfNoArgument:throwIfNoArgument}; | ||
"use strict";var _require=require("./isType"),isFunction=_require.isFunction,isString=_require.isString,isNumber=_require.isNumber,ValidationError=require("../errors/ValidationError"),throwIfNotFn=function(a){if(!isFunction(a))throw new ValidationError("'".concat(a,"' is not a function"))},throwIfInvalidKey=function(a){if(!isString(a)&&!isNumber(a))throw new ValidationError("'".concat(a,"' is not a valid key. Must be a string or number"))},throwIfNoArgument=function(){throw new ValidationError("Missing required constructor argument")};module.exports={throwIfNotFn:throwIfNotFn,throwIfInvalidKey:throwIfInvalidKey,throwIfNoArgument:throwIfNoArgument}; |
@@ -10,4 +10,4 @@ { | ||
"license": "MIT", | ||
"version": "1.0.1", | ||
"description": "A lightweight, hyper-flexible Object database", | ||
"version": "1.0.2", | ||
"description": "A lightweight, hyper-flexible object database", | ||
"main": "lib/index.js", | ||
@@ -58,4 +58,4 @@ "files": [ | ||
"rimraf": "^3.0.2", | ||
"semantic-release": "^17.2.4" | ||
"semantic-release": "^17.3.0" | ||
} | ||
} |
167
README.md
@@ -1,2 +0,2 @@ | ||
# atom-db | a no-hassle database for any environment | ||
## atom-db | a flexible object database for any environment | ||
[![Coverage Status](https://coveralls.io/repos/github/MatthewZito/atom-db/badge.svg?branch=master)](https://coveralls.io/github/MatthewZito/atom-db?branch=master) | ||
@@ -12,8 +12,12 @@ [![Build Status](https://travis-ci.org/MatthewZito/atom-db.svg?branch=master)](https://travis-ci.org/MatthewZito/atom-db) | ||
- [Documentation](#docs) | ||
- [Upcoming Features](#upcoming) | ||
## <a name="intro"></a> Introduction | ||
Soon | ||
*Atom-db* is a zero-dependency object / NOSQL data store. A database can have any number of stores, each of which emit `onSuccess` and `onError` events to the store instance. You can use the `.get` and `.set` methods to persist and retrieve data to and from the store, respectively. | ||
### <a name="feat"></a> Features | ||
Soon | ||
- zero dependencies | ||
- fast object retrieval | ||
- create multiple, namespaced databases with little configuration | ||
- catch errors in an isolated per-store context via a single event hook | ||
@@ -23,3 +27,158 @@ ## <a name="usage"></a> Installation and Usage | ||
Instantiate your database by requiring the library's built-in `init` function: | ||
``` | ||
const db = require("atom-db"); | ||
const storeA = db("test").registerObjectStore("mock"); | ||
``` | ||
Subscribe to the success and error handlers: | ||
``` | ||
storeA.onError(e => { | ||
console.log(e.target.value.message); | ||
}); | ||
storeA.onSuccess(e => { | ||
console.log(e.target.value); | ||
}); | ||
``` | ||
Set some data in the store: | ||
``` | ||
store.set("person", { musician: "Cornelius Cardew" }); | ||
``` | ||
Retrieve data: | ||
``` | ||
const musician = store.get("person"); | ||
``` | ||
## <a name="docs"></a> Documentation | ||
Soon | ||
#### Database instance | ||
A database is initialized with a `name` argument. This can be a String, Number, or Symbol. Initialized database instances have attached to them a `registerObjectStore` method that can be used to create new stores. The database instance places the registered store modules in a `stores` array. | ||
You can instantiate any number of separate database instances, each with their own localized stores. | ||
**Example:** | ||
``` | ||
const db = require("atom-db"); | ||
const database = db("mock"); | ||
``` | ||
#### Store Module Registration | ||
A store module is a key-value store bound to the database instance. | ||
To register a new store module, call `registerObjectStore`. | ||
| Method | Return Value | | ||
| --- | --- | | ||
| **registerObjectStore** | returns the newly created store module with a localized event-emitter attached | | ||
**Example:** | ||
``` | ||
const db = require("atom-db"); | ||
const store = db("mock").registerObjectStore("people"); | ||
``` | ||
#### Store Module Events | ||
Each store module has a localized event-emitter bound to its scope. To subscribe to the event-emitter, register a function on the `onSuccess` and `onError` properties. The functions you register will be dispatched and called once per eventing cycle. | ||
Note: `onSuccess` and `onError` return the store instance and are therefore chainable method properties. | ||
Event listeners available on each store instance: | ||
| Method | Emitted Data | | ||
| --- | --- | | ||
| **onError** | an event object with properties `target` and `value`, where `value` is an Error instance | | ||
| **onSuccess** | an event object with properties `target` and `value`, where `value` is the retrieved data | | ||
**Example:** | ||
``` | ||
const db = require("atom-db"); | ||
const store = db("mock").registerObjectStore("people"); | ||
store.onError(e => { | ||
console.log(e.target.value.message); | ||
}); | ||
store.onSuccess(e => { | ||
console.log(e.target.value); | ||
}); | ||
``` | ||
#### Setting Data in a Store Module | ||
You can set a variety of data in a given store module by using that store's `set` method. `set` expects a String or Number key and a corresponding value of any type. | ||
You'll need to subscribe handlers to that store's `onError` and `onSuccess` properties prior to setting data. | ||
| Method | Return Value | | ||
| --- | --- | | ||
| **set** | returns the store instance itself and is therefore chainable | | ||
**Example:** | ||
``` | ||
const db = require("atom-db"); | ||
const store = db("mock").registerObjectStore("people"); | ||
store.onError(e => { | ||
console.log(e.target.value.message); | ||
}); | ||
store.onSuccess(e => { | ||
console.log(e.target.value); | ||
}); | ||
store.set("person", { musician: "Cornelius Cardew" }); | ||
``` | ||
#### Retrieving Data from a Store | ||
You can retrieve previously set data from a given store module by using that store's `get` method. `get` expects a key identifier and will trigger the given store's emitter to emit either the `onSuccess` event (the requested data exists in the store), or the `onError` event (the requested data either does not exist in the store, or the key argument is malformed). | ||
You'll need to subscribe handlers to that store's `onError` and `onSuccess` properties prior to getting data. You'll also need to set data, of course. | ||
| Method | Return Value | | ||
| --- | --- | | ||
| **get** | returns the requested value, or undefined if the provided key has not been set in the store | | ||
**Example:** | ||
``` | ||
const db = require("atom-db"); | ||
const store = db("mock").registerObjectStore("people"); | ||
store.onError(e => { | ||
console.log(e.target.value.message); | ||
}); | ||
store.onSuccess(e => { | ||
console.log(e.target.value); | ||
}); | ||
store.set("person", { musician: "Cornelius Cardew" }); | ||
const p = store.get("person"); | ||
``` | ||
#### Event Properties | ||
##### onSuccess | ||
``` | ||
{ | ||
parent: <name prop of store to which data belongs>, | ||
target: { | ||
value: <retrieved data> | ||
} | ||
} | ||
``` | ||
##### onError | ||
``` | ||
{ | ||
parent: <name prop of store from which the error derives>, | ||
target: { | ||
value: <an extended instance of Error> | ||
} | ||
} | ||
``` | ||
## <a name="upcoming"></a> Upcoming Features | ||
* configurable adapters for local storage, node file system, and JSON | ||
* multiple event registration | ||
* built-in compose and pipe transformers |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18641
12
182