Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

atom-db

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

atom-db

A lightweight, hyper-flexible object database

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

atom-db | a flexible object database for any environment

Coverage Status Build Status npm version License: MIT

Table of Contents

Introduction

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.

Features

  • 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

Installation and Usage

Install via NPM with npm i atom-db.

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");

Documentation

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.

MethodReturn Value
registerObjectStorereturns 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:

MethodEmitted Data
onErroran event object with properties target and value, where value is an Error instance
onSuccessan 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.

MethodReturn Value
setreturns 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.

MethodReturn Value
getreturns 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>
    }
}

Upcoming Features

  • configurable adapters for local storage, node file system, and JSON
  • multiple event registration
  • built-in compose and pipe transformers

Keywords

FAQs

Package last updated on 22 Nov 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc