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

sand-abstract-model

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sand-abstract-model

Abstract model layer

  • 0.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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.

Keywords

FAQs

Package last updated on 14 Jun 2016

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