Skip to content

Commit da5cb2c

Browse files
committed
Add app.boot()
1 parent 0075303 commit da5cb2c

File tree

8 files changed

+475
-21
lines changed

8 files changed

+475
-21
lines changed

docs/api.md

Lines changed: 146 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,160 @@ app.listen(3000);
2121
> - see [express docs](http://expressjs.com/api.html) for details
2222
> - supports [express / connect middleware](http://expressjs.com/api.html#middleware)
2323
24-
#### app.model(Model)
24+
#### app.boot([options])
2525

26-
Expose a `Model` to remote clients.
26+
Initialize an application from an options object or a set of JSON and JavaScript files.
27+
28+
**What happens during an app _boot_?**
29+
30+
1. **DataSources** are created from an `options.dataSources` object or `datasources.json` in the current directory
31+
2. **Models** are created from an `options.models` object or `models.json` in the current directory
32+
3. Any JavaScript files in the `./models` and `./datasources` directories are required.
33+
34+
**Options**
35+
36+
- `cwd` - _optional_ - the directory to use when loading JSON and JavaScript files
37+
- `models` - _optional_ - an object containing `Model` definitions
38+
- `dataSources` - _optional_ - an object containing `DataSource` definitions
39+
40+
> **NOTE:** mixing `app.boot()` and `app.model(name, config)` in multiple files may result
41+
> in models being **undefined** due to race conditions. To avoid this when using `app.boot()`
42+
> make sure all models are passed as part of the `models` definition.
43+
44+
<a name="model-definition"></a>
45+
**Model Definitions**
46+
47+
The following is an example of an object containing two `Model` definitions: "location" and "inventory".
2748

2849
```js
29-
// create a testing data source
30-
var memory = loopback.memory();
31-
var Color = memory.createModel('color', {name: String});
50+
{
51+
"location": {
52+
// a reference, by name, to a dataSource definition
53+
"dataSource": "my-db",
54+
// the options passed to Model.extend(name, properties, options)
55+
"options": {
56+
"relationships": {
57+
"hasMany": {
58+
"model": "Inventory", "foreignKey": "locationId", "as": "inventory"
59+
}
60+
},
61+
"remoteMethods": {
62+
"nearby": {
63+
"description": "Find nearby locations around the geo point",
64+
"accepts": [
65+
{"arg": "here", "type": "GeoPoint", "required": true, "description": "geo location (lat & lng)"}
66+
],
67+
"returns": {"arg": "locations", "root": true}
68+
}
69+
}
70+
},
71+
// the properties passed to Model.extend(name, properties, options)
72+
"properties": {
73+
"id": {"id": true},
74+
"name": "String",
75+
"zip": "Number",
76+
"address": "String"
77+
}
78+
},
79+
"inventory": {
80+
"dataSource": "my-db"
81+
"options": {
82+
"plural": "inventory"
83+
},
84+
"properties": {
85+
"id": {
86+
"type": "String",
87+
"required": true,
88+
"id": true,
89+
"length": 20
90+
},
91+
"available": {
92+
"type": "Number",
93+
"required": false
94+
},
95+
"total": {
96+
"type": "Number",
97+
"required": false
98+
}
99+
}
100+
}
101+
}
102+
```
32103

33-
app.model(Color);
34-
app.use(loopback.rest());
104+
**Model Definition Properties**
105+
106+
- `dataSource` - **required** - a string containing the name of the data source definition to attach the `Model` to
107+
- `options` - _optional_ - an object containing `Model` options
108+
- `properties` _optional_ - an object defining the `Model` properties in [LoopBack Definition Language](http://docs.strongloop.com/loopback-datasource-juggler/#loopback-definition-language)
109+
110+
**DataSource Definition Properties**
111+
112+
- `connector` - **required** - the name of the [connector](#working-with-data-sources-and-connectors)
113+
114+
#### app.model(name, definition)
115+
116+
Define a `Model` and export it for use by remote clients.
117+
118+
```js
119+
// declare a DataSource
120+
app.boot({
121+
dataSources: {
122+
db: {
123+
connector: 'mongodb',
124+
url: 'mongodb://localhost:27015/my-database-name'
125+
}
126+
}
127+
});
128+
129+
// describe a model
130+
var modelDefinition = {dataSource: 'db'};
131+
132+
// create the model
133+
var Product = app.model('product', modelDefinition);
134+
135+
// use the model api
136+
Product.create({name: 'pencil', price: 0.99}, console.log);
35137
```
36138

37139
> **Note** - this will expose all [shared methods](#shared-methods) on the model.
38-
140+
141+
You may also export an existing `Model` by calling `app.model(Model)` like the example below.
142+
143+
#### app.models.MyModel
144+
145+
All models are avaialbe from the `loopback.models` object. In the following
146+
example the `Product` and `CustomerReceipt` models are accessed using
147+
the `models` object.
148+
149+
> **NOTE:** you must call `app.boot()` in order to build the app.models object.
150+
151+
```js
152+
var loopback = require('loopback');
153+
var app = loopback();
154+
app.boot({
155+
dataSources: {
156+
db: {connector: 'memory'}
157+
}
158+
});
159+
app.model('product', {dataSource: 'db'});
160+
app.model('customer-receipt', {dataSource: 'db'});
161+
162+
// available based on the given name
163+
var Product = app.models.Product;
164+
165+
// also available as camelCase
166+
var product = app.models.product;
167+
168+
// multi-word models are avaiable as pascal cased
169+
var CustomerReceipt = app.models.CustomerReceipt;
170+
171+
// also available as camelCase
172+
var customerReceipt = app.models.customerReceipt;
173+
```
174+
39175
#### app.models()
40176

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

43179
```js
44180
var models = app.models();
@@ -47,7 +183,7 @@ models.forEach(function (Model) {
47183
console.log(Model.modelName); // color
48184
});
49185
```
50-
186+
51187
#### app.docs(options)
52188

53189
Enable swagger REST api documentation.

0 commit comments

Comments
 (0)