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

model-thin

v0.4.2

Published

A super-slim model layer.

Downloads

9

Readme

model-thin

A super-skinny model layer, with simple object property syntax and type validation.

Build Status Coverage Status

Supported types:

  • String
  • Number
  • Boolean
  • Object
  • Array
  • Date
  • Model subclasses
  • null (indicates a deleted property)

Unless a property is marked required, all types are nullable.

Still in early development.

installation

install with npm install model-thin, or add to your project dependencies:

dependencies: {
  "model-thin": "^0.4.1"
}

usage

var Model = require('model');

creating model classes

/*
 * Model.create(kind, properties[, parent]);
 */
var Person = Model.create('Person', {
  name: String,
  age: Number,
  greet: function () {
    return "Hi, my name is " + this.name + " and I'm " + this.age + " years old.";
  }
});

kind is the string name of the class to create, usually capitalized.

properties is a map of property names to types (constructors). properties can also define methods, declared as a function that isn't one of the supported type constructors. Methods are exposed on the prototype of the new model class.

The optional parent param will be ignored if it isn't a subclass of Model.

To provide meta property information use the extended property specification syntax:

// Inherits `age` property & `greet` method by passing Person as parent
var NamedPerson = Model.create('NamedPerson', {
  name: { type: String, required: true, indexed: true }
}, Person);

Properties can also be defined after the class is constructed - this method is required for any self-referential model types:

/**
 * Model.defineProperty(name, type);
 */
Person.defineProperty('parent', Person);

using models

var father = new Person();
father.name = 'Arthur';
father.age = 42;

var son = new Person();
son.name = 'Ron';
son.age = 17;
son.parent = father;

// Setting values of an invalid type fails with a console warning
son.name = 24;
son.age = 'Charlie';

console.log(son.greet()); // prints "Hi, my name is Ron and I'm 17 years old."

// Setting to null 'deletes' the property
son.name = son.age = null;

// Properties can also be set at construction time
var daughter = new Person({
  name: 'Ginny',
  age: 15,
  parent: father
});

storing models

Interacting with storage is abstracted into a common-sense set of async methods on every Model instance which proxy to an underlying storage adapter:

  • model.get(callback)
  • model.put([callback])
  • model.del([callback])

Callbacks will be invoked with the server response after a successful or failed operation. Callback params should follow the idiomatic (err, response) schema.

Persistence is configured via the extensible storage adapter system and can be configured globally:

// Sets the adapter for all model types
Model.useAdapter(adapter);

or at the level of individual subclasses:

// Sets the adapter for all models of type Person
Person.useAdapter(adapter);

storage adapters

A storage adapter is any object that fulfills the Adapter interface, and should handle:

  • configuration
  • connecting and disconnecting
  • keying models
  • converting models from responses
  • CRUD operations
  • querying by kind

Adapters can be used anonymously by passing the adapter itself to useAdapter(), or they can be registered for use by name:

Model.adapters('adapter-name', adapter);

Custom storage adapters should ideally be published as a separate NPM module with the naming schema model-thin-<db>, so they can be managed as package-level dependencies.

The in-memory adapter is the only built-in storage provided, and is selected by default.

Available adapters:

querying models

Querying is provided through a single static method, available for kinded querying on all model classes:

Model.find(queryOpts, callback);

This method will handle building model classes out of any result set returned (unless the query contains a property select), by calling the adapter's toModel() method. All other heavy lifting is expected to be accomplished by the selected storage adapter, which receives the queryOpts object with the Model kind added. The callback should expect any error as the first param, and a list of results as the second. If the adapter supports query cursors, they will be passed through as the third parameter.

There is a suggested Query interface for the queryOpts that is lightweight but richly configurable.

A sample implementation can be found in the gcloud-datastore adapter, while samples of its usage are in the example code.

why?

Because it's seemingly impossible to find a simple, unopinionated, framework-and-storage-agnostic model layer for Node that also offers object property syntax.

Because you like querying declaratively, not imperatively.

Because it's models, for javascript™ (lol). If you're looking for ActiveRecord on Node, try Waterline.

in progress/todo:

  • collections
  • transactions
  • adapters: redis, mongodb, postgresql
  • streaming interface
  • schema management?