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

api-orm

v0.2.7

Published

API Builder ORM

Downloads

17

Readme

API Builder ORM Build Status

Object relational mapping (ORM) framework for API Builder.

This software is pre-release and not yet ready for usage. Please don't use this just yet while we're working through testing and finishing it up. Once it's ready, we'll make an announcement about it.

Changelog

Please see the CHANGELOG for the latest changes.

Main Components

There are 4 main components to the ORM framework:

  • Model - the model that represents data
  • Instance - an instance of a Model object
  • Collection - a collection of zero or more Instances
  • Connector - a connector which is responsible for managing Models

Model

To define a model, you must give a set of fields and a connector.

var User = orm.Model.define('user',{
	fields: {
		name: {
			type: String,
			default: 'Jeff',
		}
	},
	connector: Connector
});

The first argument is the name of the model. The second argument is the definition of the model.

The following are Model field properties:

| Name | Description | |-------------|---------------------------------------------------------------| | type | the column type (such as String, Number, etc) | | required | if true, the field is required |

The model has several instance methods:

| Name | Description | |---------------|------------------------------------------------------------------| | extend | create a new Model class from the current Model | | create | create a new Model instance | | update | update a Model instance | | remove | remove a Model instance | | removeAll | remove all Model instances | | find | find one or more Models | | findOne | find one Model from a primary key | | findAll | find all Model |

A model can have custom functions by defining them in the definition as a property. They will automatically be available on the model instance.

var User = orm.Model.define('user',{
	fields: {
		name: {
			type: String,
			required: true,
			default: 'jeff'
		}
	},
	connector: Connector,

	// implement a function that will be on the Model and
	// available to all instances
	getProperName: function() {
		// this points to the instance when this is invoked
		return this.name.charAt(0).toUpperCase() + this.name.substring(1);
	}
});

User.create(function(err,user){
	console.log(user.getProperName());
});

Instance

One you've defined a model, you can then use it to create an Instance of the Model.

User.create({name:'Nolan'}, function(err,user){
	// you now have a user instance
});

Instances has several methods for dealing with the model.

| Name | Description | |---------------|------------------------------------------------------------------| | get | get the value of a field property | | set | set a value or a set of values (Object) | | isUnsaved | returns true if the instance has pending changes | | isDeleted | returns true if the instance has been deleted and cannot be used | | update | save any pending changes | | remove | remove this instance (delete it) | | getPrimaryKey | return the primary key value set by the Connector |

In addition to get and set, you can also use property accessors to get field values.

console.log('name is',user.name);
user.name = 'Rick';

Collection

If the Connector returns more than one Model instance, it will return it as a Collection, which is a container of Model instances.

A collection is an array and has additional helper functions for manipulating the collection.

You can get the length of the collection with the length property.

Connector

To create a connector, you can either inherit from the Connector class using utils.inherit or extend:

var MyConnector = orm.Connector.extend({
	constructor: function(){
	},
	findOne: function(Model, id, callback) {
	}
});

Once you have created a Connector class, you can create a new instance:

var connector = new MyConnector({
	url: 'http://foobar.com'
});

Licensing

This software is licensed under the Apache 2 Public License. However, usage of the software to access the Appcelerator Platform is governed by the Appcelerator Enterprise Software License Agreement.