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

api-orm

Package Overview
Dependencies
Maintainers
5
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

api-orm

API Builder ORM

  • 0.2.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
increased by50%
Maintainers
5
Weekly downloads
 
Created
Source

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:

NameDescription
typethe column type (such as String, Number, etc)
requiredif true, the field is required

The model has several instance methods:

NameDescription
extendcreate a new Model class from the current Model
createcreate a new Model instance
updateupdate a Model instance
removeremove a Model instance
removeAllremove all Model instances
findfind one or more Models
findOnefind one Model from a primary key
findAllfind 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.

NameDescription
getget the value of a field property
setset a value or a set of values (Object)
isUnsavedreturns true if the instance has pending changes
isDeletedreturns true if the instance has been deleted and cannot be used
updatesave any pending changes
removeremove this instance (delete it)
getPrimaryKeyreturn 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.

FAQs

Package last updated on 06 Feb 2015

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