npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hive-model

v0.1.2

Published

A local-memory model/database system for storing and retrieving object based data

Downloads

4

Readme

hive-model

A javascript based database for local memory based storage of data or resources.

Models are lists of data a la backbone collections. They can contain any sort of data.

Models are not designed for any specific backend. They are designed to provide real-time node repositories of records for transient information management.

There is no structural schema for model data or structure, beyond the existence of a unique identifying key identified by the _pk property (that defaults to 'id'). Although the default _make_pk assumes integer based IDs, you can easily override this for a different index basis.

Dataspaces

Models are stored in dataspaces, which ensure/maintain uniqueness of model name within the dataspace. Dataspaces are hive-components that store models by name in their config.

Queries

Many methods of the model have optional callbacks that if omitted result in a query object being returned. Query objects allow you to compound filtering operations such as

var ds = hive_model.Dataspace();

var myModel = hive_model.Model({data: [
		{id: 1, name: 'alpha', weight: 200},
		{id: 2, name: 'beta', weight: 100},
		{id: 3, name: 'delta', weight: 0},
		{id: 4, name: 'gamma': weight: 100},
	]
}, {name: 'foos'}, ds);

var lightweights = myModel.find(function(record){ return record.weight <= 100; });

// note - lightweights is not an array of objects: it is a query object.
// There is no guarantee of ordering of the records returned .. yet.

var lightweight_records = lightweights.records();

// these are the records from the query object.

var sorted_lightweights = lightweights.sort('name');

// sorted_lightweights is another query;

var sorted_lightweight_records = sorted_lightweights.records();

// HERE we have the desired result - all foos that weight up to 100, ordered by name.

// the practical way to use queries is to chain commands and end with a records output.

var slr_faster = myModel.find(function(record){ return record.weight <= 100;}).sort('name').records()

Creating a Model

Models are instances of the hive-component Component system. A hive-component Component is created with the following API:

var mixins = {
	increment: function(){
		var c = this.get_config('value') + 1;
		this.set_config(c);
		return c;
	}
}

var config = {
	value: 0;
}

// you can think of the API as "hive_component({methods}, {properties})" -- usually -- but once in a while
// you will need to pass data in as a mixin, as is the case with model data.
var MyCompFactory = hive_component(mixins, config);

var mycomp = MyComp():

mycomp.get_config('value') // 0;
mycomp.increment();
mycomp.get_config('value') // 1;

The Model function allows for a third argument, to pass the dataspace in seperately, as shown aboe. You can also create a service that buries the dataspace in closure as in:

var ds = hive_model.Dataspace();

var ds_factory = Model.factory(ds);

var _model_bar_mixin = { name: 'bar_model', data: [ {id: 1, name: 'a', weight: 100} , {id: 2, name: 'b', weight: 150} , {id: 3, name: 'c', weight: 200} , {id: 4, name: 'aa', weight: 125} , {id: 5, name: 'ab', weight: 175} , {id: 6, name: 'ba', weight: 50} , {id: 7, name: 'bb', weight: 75} ] }

var bar_model = ds_factory(_model_bar_mixin, {_pk: 'id'});

THE NAME IS REQUIRED in the first parameter. Its how dataspace indeses the model. You don't have to pass data in, though if you have it its a convenient way to start up your model.

The second parameter (config) has no mandataory properties, unless you want to use an object whose _pk (primary key) is different than "id".

Adding Data

You can add data to a model in one of three ways:

  1. initally, as in the example above.
  2. record by record via my_model.put({foo: 1, bar: 2});
  3. in an array of data viar my_model.add([{}, {}]);

Each of these actions will emit('record', record) for each individual record. If you want to hook your data to a real repo, you can do so by listening for this event.

Updating Data

Putting data in that already has a PK value will overwrite / update a record; you can also call update to update a single record based on its ID, or by calling my_model.update(filter_function,