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

datastore.js

v2.0.1

Published

This project is a basic yet fully functional embedded data storage library.

Downloads

7

Readme

datastore.js

This project is a basic yet fully functional embedded data storage library.

It features:

  • automatic persistence to disk, in JSON : don't worry if your program crashes
  • a notification system : get notified if something changes in the datastore
  • fast 'by key' lookups, done in memory : decent performances for small to medium sized stores
  • MongoDB like query filters : convenient and easy to use
  • a fully synchronous api
  • fully written in modern JavaScript : no dependency on XXX library

It acts more like a key-value store than a full-fledged database, as indexation is made on only one field : id. As an extra feature, we provide a MongoDB like search interface, that performs linear scan, so this is not optimized for heavy sets of data.

This store is optimized for read operations. Write operations are not extremely fast as they imply a full store rewrite to disk for the moment. This may change in the near future.

Note: we refer to values stored into the store as documents, like in MongoDB.

Basic example

var DataStore = require('datastore.js')

// will save to ./datastore/todo.json, can be changed through extra
// opts usage
var db = new DataStore()
var col = db.getCollection('todo')

// It supports notifications
col.on('updated', () => {
  console.log('store changed!')
})

// id is the `key` (as in key-value) and it can be not specified :
// we'll generate a crypto secure one for you!
var myFirstTodo = col.upsert({
  id: 123,
  done: false,
  what: 'learn how to use datastore.js'
})

// auto-timestamps generation
console.log(myFirstTodo.meta.created)
console.log(myFirstTodo.meta.updated)

// look-up by id (the `key`) - this is FAST
var myFirstTodo2 = col.findOne(123)

// perform a query against the store - this is not optimized as of now
var allUndoneTodos = col.find({ done: false })

// non-existing document, unknownTodo === undefined
var unknownTodo = col.findOne('3233')

DataStore API

DataStore([opts]) : create a new instance of DataStore.

  • opts is an optional setting object which supports the following fields:
    • path : set the directory where to store data files

getCollection(name) : get or create a DataCollection given the collection name.

DataCollection API

Document related

findOne(id) : return a document, searched by id. If not found, returns undefined. This is the path of code optimized, so accessing elements with this function is fast.

find([filter]) : find elements matching filter. If filter is omitted, it returns all documents. filter is a MongoDB compliant query filter. This is a convenience function, and is not optimized as of now. Large data sets (10k docs+) can be problematic. It will very soon.

count() : return the number of documents in this store.

clear() : erase all the content of the store.

delete(id) : removes a document from the store. Return true on success, false if the document is not found.

upsert(doc) : insert or update a document in store.

  • If id is not provided in doc, a crypto secure id will be generated.
  • If id is provided and exists in store, it will perform an update operation. It's an update operation that does not erase fields, ie, it merges new fields with old ones, and overrides existing. It's an additive update.
  • If id is provided and does not exist in store, then it will use your user provided id and store your document.

In each case, it returns the inserted document, with id filled properly, and with added meta data.

Metadata related

Extra data can be saved by the end-user in the datastore. It is usually used to store extra data like a schema version number. It can be used to manage migration of data inside the store. You can store arbitrary objects in the metadata, but keep in mind this data is not intended to be large. It's more like companion data (few kBs).

meta([key]) : return a previously saved metadata property

metaSet(obj) : save new metadata properties, or update existing if field already exists in metadata. It works the way upsert work, ie, it only perform additive update. It won't delete any existing properties. We may provide an API for this though.

To be documented

  • per-doc meta-data....
  • event emitter

Future plans (in no particular order!)

  • more possible policies for save to disk
  • auto indexation on all fields, 'a la' elastic search
  • ...