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

sand-abstract-model

v0.0.0

Published

Abstract model layer

Downloads

2

Readme

Abstract Model

Abstract Asynchronous Data Model API

Rationale

In REST APIs, I wanted to load nested API objects in a more organized fashion. The primary complexity of doing this was making asynchronous database calls in a synchronous syntax. Using the syntactic sugar of GeneratorFunctions, this module provides a simple way to asynchronously load properties onto a JavaScript model class instance.

The object below exemplifies the type of nested object I'm referring to.

var user = {
  id: 1,
  name: 'My Name',
  image: {
    path: '/user/1.png',
    url: 'https://example.com/user/1.png'
  }
};

Usage

This is an SQL-ish example of how the model works. It may be adapted to many other types of asynchronous methods.

"use strict";

const co = require('co');
const AbstractModel = require('AbstractModel').AbstractModel;
const db = require('./db');

class ImageModel extends AbstractModel {
  // by default, simply calling `ImageModel.fromRow(row)`, where 
  // row is an instance of Array or Object, will create 
  // an instance of `ImageModel` for each of the given object(s) 
  // with all the original properties attached.
  
  /**
   * @return Promise
   */
  static findByUserId(userId) {
    return db.query('select * from images where user_id = ? limit 1', [userId]);
  }
}


const AbstractModel = require('AbstractModel').AbstractModel;
const ImageModel = require('./ImageModel');

class UserModel extends AbstractModel {
  *load(row, opts) {
    // this will ensure that the row object gets wrapped onto this instance
  	 yield super.load(row, opts);
  	 
  	 // this.id is an (example) Integer user id that got applied
  	 // in the super.load()
  	 this.image = yield ImageModel.findByUserId(this.id);
  	 
  	 return this;
  }
}


co(function *() {
  let row = yield db.query('select * from users where user_id = ? limit 1', [userId]);
  let user = yield UserModel.fromRow(row);
  console.log(user);
  
  let rows = yield db.query('select * from users limit 10');
  let users = yield UserModel.fromRow(rows);
  console.log(users);
});

More Examples

See SandJS MySQL Model for a full example.