0.2.7 • Published 9 years ago

api-orm v0.2.7

Weekly downloads
3
License
Apache-2
Repository
github
Last release
9 years ago

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.

0.2.7

9 years ago

0.2.6

9 years ago

0.2.5

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.0.48

9 years ago

0.0.47

9 years ago

0.0.46

9 years ago

0.0.45

9 years ago

0.0.44

9 years ago

0.0.43

9 years ago

0.0.42

9 years ago

0.0.41

9 years ago

0.0.40

9 years ago

0.0.39

9 years ago

0.0.38

9 years ago

0.0.37

9 years ago

0.0.36

9 years ago

0.0.35

9 years ago

0.0.34

9 years ago

0.0.33

9 years ago

0.0.32

9 years ago

0.0.31

9 years ago

0.0.30

9 years ago

0.0.29

9 years ago

0.0.28

9 years ago

0.0.27

9 years ago

0.0.26

9 years ago

0.0.25

9 years ago

0.0.24

9 years ago

0.0.23

9 years ago

0.0.22

9 years ago

0.0.21

9 years ago

0.0.20

9 years ago

0.0.19

9 years ago

0.0.18

9 years ago

0.0.17

9 years ago

0.0.16

9 years ago

0.0.15

9 years ago

0.0.14

9 years ago

0.0.13

9 years ago

0.0.12

9 years ago

0.0.11

9 years ago

0.0.10

9 years ago

0.0.9

9 years ago

0.0.8

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.1

10 years ago

0.0.0

10 years ago