# api-orm

> API Builder ORM

Latest version **0.2.7** (published 2015-02-06) · Apache-2 license · 0 weekly downloads

## Install

```sh
npm install api-orm
pnpm add api-orm
yarn add api-orm
bun add api-orm
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.7 |
| Published | 2015-02-06 |
| First published | 2014-11-11 |
| Weekly downloads | 0 |
| License | Apache-2 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| Author | Jeff Haynie |
| Maintainers | jhaynie, appcelerator, tonylukasavage, dawsontoth, cb1kenobi |

## Links

- npm: https://www.npmjs.com/package/api-orm
- Repository: https://github.com/appcelerator/api-orm
- Issues: https://github.com/appcelerator/api-orm/issues
- npm.io page: https://npm.io/package/api-orm

## Dependencies (6)

- [async](https://npm.io/package/async.md) ^0.9.0
- [chalk](https://npm.io/package/chalk.md) ^0.5.1
- [debug](https://npm.io/package/debug.md) ^2.1.1
- [mingo](https://npm.io/package/mingo.md) ^0.3.1
- [lodash](https://npm.io/package/lodash.md) ^2.4.1
- [pluralize](https://npm.io/package/pluralize.md) ^1.0.3

## Recent versions

- 0.2.7 (latest) — 2015-02-06
- 0.2.6 — 2015-02-06
- 0.2.5 — 2015-02-06
- 0.2.4 — 2015-02-06
- 0.2.3 — 2015-02-06
- 0.2.2 — 2015-02-04
- 0.2.1 — 2015-02-04
- 0.2.0 — 2015-02-01
- 0.0.48 — 2015-01-26
- 0.0.47 — 2015-01-20
- 0.0.46 — 2015-01-20
- 0.0.45 — 2015-01-18
- 0.0.44 — 2015-01-16
- 0.0.43 — 2015-01-16
- 0.0.42 — 2015-01-16
- … 41 more at https://npm.io/package/api-orm/versions

## README

# API Builder ORM [![Build Status](https://magnum.travis-ci.com/appcelerator/api-orm.svg?token=xjwxUDk3aUJaLhguTqyB&branch=master)](https://magnum.travis-ci.com/appcelerator/api-orm)

Object relational mapping (ORM) framework for [API Builder](https://github.com/appcelerator/apibuilder).

> 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](https://github.com/appcelerator/api-orm/blob/master/CHANGELOG.md) 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.

```javascript
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.

```javascript
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.

```javascript
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.

```javascript
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:

```javascript
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:

```javascript
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.

---
_Source: https://npm.io/package/api-orm · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
