Examples
StrongLoop provides complete example apps for all LoopBack's major features, including connecting to various kinds of data sources, creating model relations, adding access control, using all the SDKs, and so on. For a complete list, see Tutorials and examples.
Create backend APIs through models
Use the LoopBack generator to create the project and two models for office supplies app.
$ lb app office-supplies ? What's the name of your application? (office-supplies) ? Enter name of the directory to contain the project: (office-supplies) ? Which version of LoopBack would you like to use? 3.x (Active Long Term Support) ? What kind of application do you have in mind? empty-server (An empty LoopBack API, without any configured models or datasources) ... $ lb model product $ lb model category
Create a secure REST API
Secure all the model APIs, then allow admins to modify products.
$ lb acl
Easily persist data
LoopBack models have a full set of methods for create, read, update, and delete (CRUD) operations. For example:
category.create({name: 'stationery', id: 1});
product.create({
categoryId: 1,
name: 'Pencil',
price: 0.99
});
Define model relations
Use simple JSON declarative syntax to set up relations between models. For example, the following specifies that a product belongs to one category (identified by categoryId) and has one owner (identified by ownerId).
$ lb relation [?] Select the model to create the relationship from: product [?] Relation type: belongs to [?] Choose a model to create a relationship with: category [?] Enter the property name for the relation: category [?] Optionally enter a custom foreign key: categoryId
This updates the product.json file as follows:
{
"name": "product",
"base": "PersistedModel",
"properties": {
"name": {
"type": "string"
},
"category": {
"type": "number"
}
},
"validations": [],
"relations": {
"category": {
"type": "belongsTo",
"model": "category",
"foreignKey": "categoryId"
},
"owner": {
"type": "belongsTo",
"model": "owner",
"foreignKey": "ownerId"
}
},
"acls": [],
"methods": []
}
Run ad-hoc queries
Call directly into the LoopBack APIs directly from iOS, Android, or client JavaScript.
Node.js
MyProduct.find({
where: {price: {lt: 100}},
order: 'price ASC',
limit: 3
}, function(err, products) {
...
});
iOS Objective-C
[MyProduct createWithDictionary:@{
@"name": @"Pencil",
@"price": @0.99
}]
Android Java
Product product = new Product(
ImmutableMap.of("name", "Pencil"));
product.save(new Model.Callback() {
@Override
public void onSuccess() {
// saved!
}
@Override
public void onError(Throwable t) {
// handle errors
}
});
Angular JS
$scope.products = MyProduct.find({
filter: {
where: {price: {lt: 100}},
order: 'price ASC',
limit: 3
}
});
Isomorphic LoopBack
MyProduct.find({
where: {price: {lt: 100}},
order: 'price ASC',
limit: 3
}, function(err, products) {
console.log(products); // => Array of MyProduct
});
Connect to Oracle Database
Create new datasource:
$ lb datasource ? Enter the data-source name: oracledb ? Select the connector for oracledb: Oracle (supported by StrongLoop) ? Connection String tns [Press Enter] ? host: demo.strongloop.com ? port: 1521 ? user: Sample ? password: demo ? database: secret ? Install loopback-connector-oracle@^3.0 Yes
The CLI will automatically run npm to install the LoopBack Oracle connector. Now your app can connect to the specified Oracle database.
Automatically Discover and Expose Database Tables
Connectors for Oracle, MySQL, PostgreSQL, and SQL Server provide APIs that enable you to "discover" model definition information from existing databases, including schema information such as column names and datatypes and relation information such as primary and foreign keys.
Using IBM API Designer you can accomplish the same thing without any coding!
var ds = require('../data-sources/db.js')('oracle');
// Discover and build models from INVENTORY table
ds.discoverAndBuildModels('INVENTORY',
{visited: {}, owner: 'LOOPBACK', associations: true},
function (err, models) {
models.Inventory.findOne({}, function (err, inv) {
console.log("Inventory: ", inv);
inv.product(function (err, prod) {
console.log("Product: ", prod);
});
});
}
)