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

cantina-models

v4.1.3

Published

Extensible models for cantina applications.

Downloads

10

Readme

cantina-models

Models for Cantina applications.

Provides

  • app.collections - A hash of collections, keyed by collection 'name'.
  • app.createCollectionFactory (factoryName, store, defaults) - Create a store-specifc collection factory, optionally including default options.
    • factoryName - used to name the factory, e.g., the factoryName "redis" (case-insensitive) will create app.createRedisCollection
    • store - a function that implements a modeler-compatible API.
    • defaults - default options to be passed to the store

Factory

  • app.create[Factoryname]Collection (name, options) - Create a collection for use in your application.
    • name - used to name the collection, e.g., the name "userComments" will create app.collections.userComments
    • options - passed directly to the store. So, if it's a modeler store, you can pass all the usual modeler options. Other stores may implement additional options. By default it sets up modeler CRUD hooks that emit/run app-level events/hooks, as well as an init event that is triggered once on collection:init. (See more below).

Factory CRUD methods

Every collection created by the factory will implement the following modeler-compatible CRUD methods:

create (attributes, [callback])

Creates a model using the provided attributes. If not provided, ensures that id, rev, and created are defined.

The created model is returned unless a callback is provided. If a callback is provided, the model will be saved and the callback invoked with the results of the save.

load (id, [options], [callback])

Loads a model by id. Any options are passed to the store.

save (model, [options], [callback])

Saves model. Any options are passed to the store.

destroy (id|model, [options], [callback])

Destroys (deletes) model (or the model identified by id). Any options are passed to the store.

list ([options], callback)

Lists all models in the collection. Any options (e.g., offset, limit) are passed to the store.

Model properties

Every model will have the following properties, at a minimum:

id

The model's unique identifier.

rev

The model's revision number (incremented on each save).

created

The Date.now() when the model was created.

updated

If the model has been saved, the Date.now() when the model was most recently saved.

Events

  • collection:init (collection) - A collection was created.
  • collection:init:name (collection) - A collection of type name was created.
  • model:create (model) - A model was created.
  • model:create:name (model) - A model of type name was created.

Hooks

  • model:save (model, cb) - A model is about to be saved.
  • model:save:name (model, cb) - A model of type name is about to be saved.
  • model:afterSave (model, cb) - A model was successfully saved.
  • model:afterSave:name (model, cb) - A model of type name was successfully saved.
  • model:load (model, cb) - A model was loaded.
  • model:load:name (model, cb) - A model of type name was loaded.
  • model:destroy (model, cb) - A model is about to be destroyed.
  • model:destroy:name (model, cb) - A model of type name is about to be destroyed.
  • model:afterDestroy (model, cb) - A model was successfully destroyed.
  • model:afterDestroy:name (model, cb) - A model of type name was successfully destroyed.

Usage

During app start-up, you'll need to create some collection factories. For example, to use cantina-redis with modeler-redis:

var app = require('cantina').createApp();

app.boot(function (err) {
  if (err) throw err;

  app.require('cantina-redis');
  app.require('cantina-models');

  app.start(function (err) {
    if (err) throw err;

    // Use a redis modeler store.
    var modeler = require('modeler-redis');

    // These default options will get passed to modeler.
    var options = {
      client: app.redis,
      prefix: app.redisKey('models') + ':'
    };

    // We can now create collection factories.
    app.createCollectionFactory('redis', modeler, options);
    // Creates app.createRedisCollection

    var peopleOptions = {
      // each collection can still have collection-specific options
    };

    // Ready to start persisting models to redis ...
    app.createRedisCollection('people', peopleOptions);

    // Bind to app events or hooks, for example:
    app.hook('model:save:people', function (person, next) {
      if (!person.first) return next(new Error('People must have a first name'));
      next();
    });

    // Create a person.
    app.collections.people.create({last: 'Doe'}, function (err, model) {
      // We will get a 'validation' error here because of the hook.
    });

  });

});

Developed by Terra Eclipse

Terra Eclipse, Inc. is a nationally recognized political technology and strategy firm located in Santa Cruz, CA and Washington, D.C.