Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 144 additions & 10 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,158 @@ app.listen(3000);
> - see [express docs](http://expressjs.com/api.html) for details
> - supports [express / connect middleware](http://expressjs.com/api.html#middleware)

#### app.model(Model)
#### app.boot([options])

Expose a `Model` to remote clients.
Initialize an application from an options object or a set of JSON and JavaScript files.

**What happens during an app _boot_?**

1. **DataSources** are created from an `options.dataSources` object or `datasources.json` in the current directory
2. **Models** are created from an `options.models` object or `models.json` in the current directory
3. Any JavaScript files in the `./models` directory are loaded with `require()`.

**Options**

- `cwd` - _optional_ - the directory to use when loading JSON and JavaScript files
- `models` - _optional_ - an object containing `Model` definitions
- `dataSources` - _optional_ - an object containing `DataSource` definitions

> **NOTE:** mixing `app.boot()` and `app.model(name, config)` in multiple files may result
> in models being **undefined** due to race conditions. To avoid this when using `app.boot()`
> make sure all models are passed as part of the `models` definition.

<a name="model-definition"></a>
**Model Definitions**

The following is an example of an object containing two `Model` definitions: "location" and "inventory".

```js
// create a testing data source
var memory = loopback.memory();
var Color = memory.createModel('color', {name: String});
{
"dealership": {
// a reference, by name, to a dataSource definition
"dataSource": "my-db",
// the options passed to Model.extend(name, properties, options)
"options": {
"relationships": {
"cars": {
"type": "hasMany",
"model": "Car",
"foreignKey": "dealerId"
}
},
"remoteMethods": {
"nearby": {
"description": "Find nearby locations around the geo point",
"accepts": [
{"arg": "here", "type": "GeoPoint", "required": true, "description": "geo location (lat & lng)"}
],
"returns": {"arg": "locations", "root": true}
}
}
},
// the properties passed to Model.extend(name, properties, options)
"properties": {
"id": {"id": true},
"name": "String",
"zip": "Number",
"address": "String"
}
},
"car": {
"dataSource": "my-db"
"properties": {
"id": {
"type": "String",
"required": true,
"id": true
},
"make": {
"type": "String",
"required": true
},
"model": {
"type": "String",
"required": true
}
}
}
}
```

app.model(Color);
app.use(loopback.rest());
**Model Definition Properties**

- `dataSource` - **required** - a string containing the name of the data source definition to attach the `Model` to
- `options` - _optional_ - an object containing `Model` options
- `properties` _optional_ - an object defining the `Model` properties in [LoopBack Definition Language](http://docs.strongloop.com/loopback-datasource-juggler/#loopback-definition-language)

**DataSource Definition Properties**

- `connector` - **required** - the name of the [connector](#working-with-data-sources-and-connectors)

#### app.model(name, definition)

Define a `Model` and export it for use by remote clients.

```js
// declare a DataSource
app.boot({
dataSources: {
db: {
connector: 'mongodb',
url: 'mongodb://localhost:27015/my-database-name'
}
}
});

// describe a model
var modelDefinition = {dataSource: 'db'};

// create the model
var Product = app.model('product', modelDefinition);

// use the model api
Product.create({name: 'pencil', price: 0.99}, console.log);
```

> **Note** - this will expose all [shared methods](#shared-methods) on the model.


You may also export an existing `Model` by calling `app.model(Model)` like the example below.

#### app.models.MyModel

All models are avaialbe from the `loopback.models` object. In the following
example the `Product` and `CustomerReceipt` models are accessed using
the `models` object.

> **NOTE:** you must call `app.boot()` in order to build the app.models object.

```js
var loopback = require('loopback');
var app = loopback();
app.boot({
dataSources: {
db: {connector: 'memory'}
}
});
app.model('product', {dataSource: 'db'});
app.model('customer-receipt', {dataSource: 'db'});

// available based on the given name
var Product = app.models.Product;

// also available as camelCase
var product = app.models.product;

// multi-word models are avaiable as pascal cased
var CustomerReceipt = app.models.CustomerReceipt;

// also available as camelCase
var customerReceipt = app.models.customerReceipt;
```

#### app.models()

Get the app's exposed models.
Get the app's exported models. Only models defined using `app.model()` will show up in this list.

```js
var models = app.models();
Expand All @@ -47,7 +181,7 @@ models.forEach(function (Model) {
console.log(Model.modelName); // color
});
```

#### app.docs(options)

Enable swagger REST api documentation.
Expand Down
Loading