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

mekanika-adapter

v0.2.0

Published

Abstract mekanika adapter class

Downloads

4

Readme

adapter

An abstract adapter class for mekanika adapters.

Alpha release

Overview

adapter is a method for instantiating new Adapter classes, and returning those adapter instances from an internal cache once declared.

Adapters are designed to map Qo - Query objects to a given service (such as a REST API, a database, a filesystem, etc). Typically they will sit on top of a service driver, and do little more than error check the Qo and translate it into a format the driver can process, finally passing (err, res) back to the callback.

Adapters should by definition be extremely lightweight. Their only 'smart' is their translation layer between Qo and driver APIs. In exceptional circumstances where a driver API is significantly simpler than the default set of Qo actions, an adapter may implement Qo actions via the driver API.

In general an adapter will map as much of the Qo specification that is natively supported by the underlying driver.

Usage

New Adapters are instantiated as follows:

  var myadapter = adapter.new( 'myadapter', defaultConfig );
  // adapter('myadapter') instanceof adapter.Adapter
  // -> true

adapter cache (statics)

All adapters can be retrieved using the adapter( name ) format, and are stored in the adapter cache.

adapter has the following static methods:

  • .list() - list all declared adapter keys
  • .has( id ) - boolean existence for an adapter
  • .remove( id ) - removes an adapter from the cache
  • .reset() - flush the cache

Creating new Adapters

Adapters map a query request object to corresponding actions of a particular service. To do this, adapters must implement an .exec( query, cb ) method that:

  • parses the Qo - Query object
  • Maps the query request to some action (ie. do something)
  • then runs the cb callback passing cb( err, res )
    • err: Error object or null
    • res: Results of successful execution (or undefined)

Adapter Class

All Adapters are EventEmitters (based on EventEmitter2)

The initial Adapter class is very simple:

var moo = adapter.new( 'moo', {prop: true} );
// -> { identity: 'moo', config: {prop: true} }

On the Prototype

  • config - Empty {} object (used to store config)
  • exec() - Stub that throws Error('Not implemented')

Implementing .exec( query, cb )

Action: Find

Two paths for finding records, either via:

Identifiers

  • List of ids to 'find' {identifiers: ['xyz']}

Constraints

  • WHERE field OPERATOR $value [{field:'name', operator:'eq', condition:'joe'}]

Action: Update

There are two (independent) update paths, updating via either/or:

Modifiers

  • SET field to VALUE {set: $field, value: $value}
  • INCR field by AMOUNT {inc: $field, value: $value} (can be negative)

Content

  • Content to modify content: [ {vip: true} ]

Where multiple identifiers are set, the content or modifiers should be applied to all ids. If no identifiers are set, the update will be applied to where conditions, eg:

{
  action: 'update',
  resource: 'users',
  modifiers: [{set:'vip', value: true}],
  where: [{field:'points', operator:'gte', constraint:500}]
}
// Sets 'vip' to true on users who have points >= 500

Installation

npm install --production

For development, requires globally installed:

  • mocha
  • browserify
  • jshint
  • istanbul

License

MIT