Update model docs further.

This commit is contained in:
Michael Schoonmaker 2013-09-04 16:14:50 -07:00
parent e0ff75bc84
commit b17a3c4ea7
2 changed files with 89 additions and 80 deletions

View File

@ -1,135 +1,144 @@
##Concepts ## Concepts
###What is LoopBack? ### Overview
- a component in the StrongLoop Suite Before we go into all the wonderful concepts that make up LoopBack, let's first
- a library of Node.js modules for connecting mobile apps to various data answer a couple of questions:
sources
- a command line tool `slc lb` for generating applications and models
- a set of SDKs for native and web mobile clients
###How it Works > What _is_ LoopBack?
LoopBack apps are made up of three components: mobile clients, data sources, and - A component in the [StrongLoop Suite](www.strongloop.com/strongloop-suite).
models. Clients, such as mobile or web apps, use LoopBack mobile SDKs to - A library of Node.js modules for connecting mobile apps to a variety of data
interact with data sources, such as a database or REST API. Access to these sources.
data sources is provided by models, which control how a data source is exposed - A command line tool, `slc lb`, for generating models and entire applications
to a mobile client. with the LoopBack library.
- A set of SDKs for native and web-based mobile clients.
Any mobile or web app can interact with a LoopBack data source through > How does LoopBack work?
the model API. The model API is available in Node.js, over REST, and as
native mobile SDKs for iOS, Android, and HTML5. Using the API, clients can query LoopBack Applications are made up of three components:
databases, store data, upload files, send emails, create push notifications, [Data Sources](#data-sources-and-connectors) (also referred to as "Connectors"),
register users, and any other behavior provided by data sources. [Models](#models), and the [Mobile Clients](#mobile-clients) that consume them.
Any mobile or web app can interact with a LoopBack Data Source through the Model
API. The Model API is available [locally within Node.js](#model), [remotely over
REST](#rest-api), and as native mobile SDKs for [iOS, Android, and
HTML5](#mobile-clients). Using the API, clients can query databases, store data,
upload files, send emails, create push notifications, register users, and any
other behavior provided by data sources.
### Mobile Clients ### Mobile Clients
**PLACEHOLDER FOR SDK INTRO** **PLACEHOLDER FOR SDK INTRO**
###Models ### Models
**What is a Model?** > What is a Model?
In LoopBack, a **Model** consists of the following. In LoopBack, a Model consists of the following:
- application data - Application data
- business rules - Validation rules
- logic - Business logic
- functions
A mobile client uses APIs provided by **Models** to request any information A mobile client uses the remote API provided by Models to request any
needed to display a useful interface to the user. information needed to display a useful interface to the user.
**A Simple Example** #### A Simple Example
For example, an e-commerce app might have `Product` and `Inventory` models. For example, an e-commerce app might have `Product` and `Inventory` Models.
A mobile client could use the `Product` model API to search all the products in A mobile client could use the `Product` Model API to search through all of the
a database. A client could join the `Product` and `Inventory` data to determine Procuts in a database. A client could join the `Product` and `Inventory` data to
what products are in-stock, or the `Product` model could provide a server-side determine what products are in stock, or the `Product` Model could provide a
function (or [remote method](#remote-methods)) that returns this information. server-side function (or [remote method](#remote-methods)) that aggregates this
information.
```js ```js
var loopback = require('loopback'); // Step 1: Create Models
var Model = loopback.Model; var Model = require('loopback').Model;
var Product = Model.extend('product'); var Product = Model.extend('product');
var Inventory = Model.extend('customer'); var Inventory = Model.extend('customer');
``` ```
> - Models are **schema-less** by default. **NOTE:** Models are _schema-less_ by default, but some Connectors, such as
> - Some data sources, such as relational databases, require schemas. relational databases, _require_ schemas. Additionally, schemas are immensely
> - Adding a schema allows you to sanitize data coming from mobile clients. valuable for validating sanitizing data coming from mobile clients. See
[Sanitizing and Validating Models](#sanitizing-and-validating-models) if your
application needs to connect to an RDBMS, for example.
**Attaching Data Sources** #### Attaching Data Sources
Attaching a model to a data source gives you access to the data source's API. Attaching a Model to a Data Source gives you access to a powerful API mixed into
Using the MongoDB connector, this data source provides a `create` method. This Models by their Sources. The [MongoDB Connector](#), for example, mixes in a
example uses the `create` method to store a new product. `create` method that allows us to store a new Product in the database:
```js ```js
// Step 2: Attach Data Sources
var db = loopback.createDataSource({ var db = loopback.createDataSource({
connector: require('loopback-connector-mongodb') connector: require('loopback-connector-mongodb')
}); });
// enables the model to use // Enables the Model to use the MongoDB API
// the mongodb api
Product.attachTo(db); Product.attachTo(db);
// create a new product in the database // Create a new product in the database
Product.create({name: 'widget', price: 99.99}, function(err, widget) { Product.create({ name: 'widget', price: 99.99 }, function(err, widget) {
console.log(widget.id); // the product's id console.log(widget.id); // The product's id, added by MongoDB
}); });
``` ```
**Exposing to Mobile Clients** #### Exposing to Mobile Clients
Models can be exposed to mobile clients using one of the remoting middlewares. Models can be exposed to mobile clients using one of the remoting middlewares.
This example uses the `app.rest` middleware to expose the `Product` model's API This example uses the `app.rest` middleware to expose the `Product` Model's API
over REST. over REST.
```js ```js
// create a loopback app // Step 3: Create a LoopBack Application
var app = loopback(); var app = loopback();
// use the REST remoting middleware // Use the REST remoting middleware
app.use(loopback.rest()); app.use(loopback.rest());
// expose the `Product` model // Expose the `Product` model
app.model(Product); app.model(Product);
``` ```
**Sanitizing and Validating Models** #### Sanitizing and Validating Models
Once a schema is added to a model, it will validate and sanitize data before Once a schema is added to a Model, it will validate and sanitize data before
giving it to a data source. For example, the `Product` model has a schema that giving it to a Data Source. For example, the `Product` Model has a schema that
will not change. The example below updates the `Product` model with a schema will not change. The example below updates the `Product` Model with a schema
written in **LoopBack Definition Language**, a well documented flavor of JSON. written in **LoopBack Definition Language**, a well-documented flavor of JSON.
```js ```js
// Step 4: Add a Schema
var productSchema = { var productSchema = {
"name": {"type": "string", "required": true}, "name": { "type": "string", "required": true },
"price": "number" "price": "number"
}; };
var Product = Model.extend('product', productSchema); var Product = Model.extend('product', productSchema);
``` ```
If a remote client tries to save a product with extra properties, they will be On one hand, If a remote client tries to save a product with extra properties
removed. The model will only be saved if the product contains the required (e.g. `description`), those properties will be removed before saving the Model.
`name` property. On the other hand, the Model will _only_ be saved if the product contains the
required `name` property.
**Learn more about models** #### More About Models
- Check out the model [REST API](#rest-api) - Check out the Model [REST API](#rest-api).
- Read the - Read the
[LoopBack Definition Language Guide](http://docs.strongloop.com/loopback-datasource-juggler#loopback-definition-language-guide). [LoopBack Definition Language Guide](http://docs.strongloop.com/loopback-datasource-juggler#loopback-definition-language-guide).
- Browse the [Node.js Model API](#model). - Browse the [Node.js Model API](#model).
- Before you build your own, check out the [bundled models](#bundled-models). - Before you build your own, check out the [bundled Models](#bundled-models).
- Expose custom behavior to clients using [remote methods](#remote-methods). - Expose custom behavior to clients using [remote methods](#remote-methods).
- See how to [define relationships](#relationships) between models. - See how to [define relationships](#relationships) between Models.
###Datasources and Connectors ### Data Sources and Connectors
LoopBack allows you to connect to many sources of data and services in the cloud LoopBack allows you to connect to many sources of data and services both in the
and on premise in your data center. These sources of data and services are cloud and on-premise in your data center. These sources of data and services are
called DataSources. DataSources are accessed through a plugin called a Connector called DataSources. DataSources are accessed through a plugin called a Connector
in LoopBack. Plugins are highly customizable and extensible. Unlike other in LoopBack. Plugins are highly customizable and extensible. Unlike other
mobile backend, LoopBack can leverage your existing data and organize them in mobile backend, LoopBack can leverage your existing data and organize them in
@ -146,7 +155,7 @@ using database drivers or other client APIs. In general, connectors are not used
directly by application code. The DataSource class provides APIs to configure directly by application code. The DataSource class provides APIs to configure
the underlying connector and exposes functions via DataSource or model classes. the underlying connector and exposes functions via DataSource or model classes.
**LoopBack Connector Modules** #### LoopBack Connector Modules
| Type | Package Name | | Type | Package Name |
| --------- | -------------------------------------------------------------------------------------- | | --------- | -------------------------------------------------------------------------------------- |
@ -155,13 +164,12 @@ the underlying connector and exposes functions via DataSource or model classes.
| Oracle | [loopback-connector-oracle](https://github.com/strongloop/loopback-connector-oracle) | | Oracle | [loopback-connector-oracle](https://github.com/strongloop/loopback-connector-oracle) |
| REST | [loopback-connector-rest](https://github.com/strongloop/loopback-connector-rest) | | REST | [loopback-connector-rest](https://github.com/strongloop/loopback-connector-rest) |
For more information, please read [LoopBack DataSource and Connector Guide](/loopback-datasource-juggler/#loopback-datasource-and-connector-guide). For more information, please read the [LoopBack DataSource and Connector Guide](/loopback-datasource-juggler/#loopback-datasource-and-connector-guide).
### REST ### REST
Everything defined in LoopBack is available to you as a REST endpoint. For Everything defined in LoopBack (e.g. Models) can be made available as a REST
every model that is created in LoopBack, a REST endpoint is automatically endpoint. You can see and experiment with _your_ REST api using the
created for you. You can see and experiment with your REST api using the
[LoopBack API Explorer](http://localhost:3000/explorer/). [LoopBack API Explorer](http://localhost:3000/explorer/).
LoopBack also supports other protocols for your API as well. Socket.io is LoopBack also supports other protocols for your API as well. Socket.io is

View File

@ -1,14 +1,15 @@
#LoopBack # LoopBack
**v1.0.0** **v1.0.0**
LoopBack is part of the [StrongLoop Suite](www.strongloop.com/strongloop-suite) LoopBack is part of the [StrongLoop Suite](www.strongloop.com/strongloop-suite).
LoopBack is a mobile backend framework that puts you in control. As LoopBack is a mobile backend framework that puts you in control. As
a mobile developer you decide where to run your mobile backend - in a mobile developer you decide where to run your mobile backend - in
the cloud or on permise. the cloud or on-premise.
LoopBack is built on [StrongNode](/strongnode) and open source Node. LoopBack is built on [StrongNode](/strongnode) and open source Node.js
js modules. It leverages the power of Node, the community that modules. It leverages the power of Node, the community that
stands behind Node, and the support that [StrongLoop](www.strongloop.com) offers. stands behind Node, and the support that [StrongLoop](www.strongloop.com)
offers.
LoopBack is here to help you develop mobile applications with rich LoopBack is here to help you develop mobile applications with rich
functionality and data that is in your datacenter and the cloud. functionality and data that is in your datacenter and the cloud.