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

backbone-blueprint

v0.3.1

Published

Create object hierarchies based on json schema

Downloads

10

Readme

Backbone-blueprint TravisCI

Creates model/collection hierarchies based on JSON schema.

Based on JSON-Schema specification, but it's slightly modified regards handling relations etc.

Example

var addressSchema = exports.addressSchema = {
  id: '/schemas/address',
  title: 'Address',
  type: 'object',
  properties: {
    street: { type: 'string' },
    city: {type: 'string'},
    country: {type: 'string'}
  }
};

var Address = Model.extend({
  type: 'address',
  schema: addressSchema
});

var address = new Address({
  street: '221B Baker Street ',
  city: 'London',
  country: 'England'
});

Features

Relations

Relations are defined as

type: 'relation'
collection/model: ModelName

Thus relations can be either Models or Collections.

Example

var Addresses = exports.Addresses = Collection.extend({
  model: Address
});

var personSchema = exports.personSchema = {
  id: '/schemas/person',
  type: 'object',
  properties: {
   addresses : {
      type: 'relation',
      collection: Addresses
    }
  }
};

var person = new Person({
  addresses: [{street: 'Baker Street', city: 'London', country: 'GB'}]
});

console.log(person.get('addresses').at(0).get('country'));

Relation references

Initing a relation might need information from the main model. This is done by passing a references - options to the property. E.g.

  properties: {
    owner: {
      type: 'relation',
      model: Person,
      references: {id: 'owner_id'}
    }
  }

will read the value of owner_id property from the main model, and init relation automatically with it. So e.g. if a Model has {owner_id: 2}, owner relation will be inited with:

person = new Person({id: 2})

Validation

Validation is done by jsonschema module. Validating Models can be created by extending from ValidatingModel.

Example

var schema = {
  id: '/schemas/foo',
  type: 'object',
  properties: {
    data: {
      type: 'string'
    }
  }
};
var Foo = ValidatingModel.extend({
  type: 'foo',
  schema: schema
});
var f = new Foo({data: 'a'});
var errors = f.validate();

Will give an error, since 'data' had incorrect type.

Custom validation

If you want to make more complex validations, that jsonschema does not support, you can extend the customValidation method, see tests for more info.

Projection

Passing option {recursive: true} to toJSON, will also include relation in the JSON output. Sometimes it's useful to control which attributes are included in the JSON. This can be done with projection settings. E.g.

var projection = {
  owner: ['id', 'title'],
  removeFields: ['addresses']
};
var json = person.toJSON({recursive: true, projection: projection});

will include only 'id' & 'title' fields from the owner relation and will remove the 'addresses' relation completely from the output.

Projection options

onlyFields

onlyFields option will whitelist the given properties, thus includes only the specified properties in the JSON output.

removeFields

removeFields option will blacklist the given properties, thus removes the specified properties in the JSON output.

projection presets

You can define a projection preset in the schema, e.g.

properties: {
  ...
},
projection: {
  mini: {
    onlyFields: ['city']
  }
}

Then you can give projection options as

person.toJSON({recursive: true, projection: 'mini'})

Schema options

convert

A property can define convert function which is called when attribute is set. E.g.:

    properties: {
      id: {
        type: 'integer',
        convert: function(attribute) {
          return Number(attribute);
        }
      }, ...

Info

This project is based on https://github.com/redpie/backbone-schema

License

The MIT License