LoopBack REST connector allows Node.js application to interact with HTTP REST APIs using a template driven approach. It supports two different styles of API invocations:
If the REST APIs supports CRUD operations for resources, such as users or orders, you can simply bind the model to a REST endpoint that follows REST conventions.
The following methods are mixed into your model class:
Below is a simple example:
var ds = loopback.createDataSource({
connector: require("loopback-connector-rest"),
debug: false,
baseURL: 'http://localhost:3000'
});
var User = ds.createModel('user', {
name: String,
bio: String,
approved: Boolean,
joinedAt: Date,
age: Number
});
User.create(new User({name: 'Mary'}), function (err, user) {
console.log(user);
});
User.find(function (err, user) {
console.log(user);
});
User.findById(1, function (err, user) {
console.log(err, user);
});
User.update(new User({id: 1, name: 'Raymond'}), function (err, user) {
console.log(err, user);
});
Imagine that you use browser or REST client to test drive a REST API, you will specify the following HTTP request properties:
LoopBack REST connector allows you to define the API invocation as a json template. For example,
template: {
"method": "GET",
"url": "http://maps.googleapis.com/maps/api/geocode/{format=json}",
"headers": {
"accepts": "application/json",
"content-type": "application/json"
},
"query": {
"address": "{street},{city},{zipcode}",
"sensor": "{sensor=false}"
},
"responsePath": "$.results[0].geometry.location"
}
The template variable syntax is as follows:
{name=defaultValue:type}
The variable is required if the name has a prefix of ! or ^
For example:
'{x=100:number}'
'{x:number}'
'{x}'
'{x=100}ABC{y}123'
'{!x}'
'{x=100}ABC{^y}123'
To use custom methods, you can configure the REST connector with the operations
property, which is an array of
objects that contain template
and functions
. The template
property defines the API structure while the functions
property defines JavaScript methods that takes the list of parameter names.
var loopback = require("loopback");
var ds = loopback.createDataSource({
connector: require("loopback-connector-rest"),
debug: false,
operations: [
{
template: {
"method": "GET",
"url": "http://maps.googleapis.com/maps/api/geocode/{format=json}",
"headers": {
"accepts": "application/json",
"content-type": "application/json"
},
"query": {
"address": "{street},{city},{zipcode}",
"sensor": "{sensor=false}"
},
"responsePath": "$.results[0].geometry.location"
},
functions: {
"geocode": ["street", "city", "zipcode"]
}
}
]});
Now you can invoke the geocode API as follows:
Model.geocode('107 S B St', 'San Mateo', '94401', processResponse);
By default, LoopBack REST connector also provides an 'invoke' method to call the REST API with an object of parameters, for example:
Model.invoke({street: '107 S B St', city: 'San Mateo', zipcode: '94401'}, processResponse);
Export the initialize method to loopback-datasource-juggler
Name | Type | Description |
---|---|---|
dataSource |
DataSource
|
The loopback data source instance |
[callback] |
function
|
The callback function |
The RestConnector constructor
Name | Type | Description |
---|---|---|
baseURL |
string
|
The base URL |
debug |
boolean
|
The debug flag |
Hook for defining a model by the data source
Name | Type | Description |
---|---|---|
descr |
object
|
The model description |
Install the post processor
Name | Type | Description |
---|---|---|
descr |
object
|
The model description |
Pre-process the request data
Name | Type | Description |
---|---|---|
data |
|
The request data |
Name | Type | Description |
---|---|---|
result |
[object Object]
|
Post-process the response data
Name | Type | Description |
---|---|---|
model |
string
|
The model name |
data |
|
The response data |
many |
boolean
|
Is it an array |
Get a REST resource client for the given model
Name | Type | Description |
---|---|---|
model |
string
|
The model name |
Name | Type | Description |
---|---|---|
result |
|
Create an instance of the model with the given data
Name | Type | Description |
---|---|---|
model |
string
|
The model name |
data |
object
|
The model instance data |
[callback] |
function
|
The callback function |
Update or create an instance of the model
Name | Type | Description |
---|---|---|
model |
string
|
The model name |
data |
object
|
The model instance data |
[callback] |
function
|
The callback function |
A factory to build callback function for a response
Name | Type | Description |
---|---|---|
model |
string
|
The model name |
[callback] |
function
|
The callback function |
Name | Type | Description |
---|---|---|
result |
function
|
Save an instance of a given model
Name | Type | Description |
---|---|---|
model |
string
|
The model name |
data |
object
|
The model instance data |
[callback] |
function
|
The callback function |
Check the existence of a given model/id
Name | Type | Description |
---|---|---|
model |
string
|
The model name |
id |
|
The id value |
[callback] |
function
|
The callback function |
Find an instance of a given model/id
Name | Type | Description |
---|---|---|
model |
string
|
The model name |
id |
|
The id value |
[callback] |
function
|
The callback function |
Delete an instance for a given model/id
Name | Type | Description |
---|---|---|
model |
string
|
The model name |
id |
|
The id value |
[callback] |
function
|
The callback function |
Query all instances for a given model based on the filter
Name | Type | Description |
---|---|---|
model |
string
|
The model name |
filter |
object
|
The filter object |
[callback] |
function
|
The callback function |
Delete all instances for a given model
Name | Type | Description |
---|---|---|
model |
string
|
The model name |
[callback] |
function
|
The callback function |
Count cannot not be supported efficiently.
Name | Type | Description |
---|---|---|
model |
string
|
The model name |
[callback] |
function
|
The callback function |
where |
object
|
The where object |
Update attributes for a given model/id
Name | Type | Description |
---|---|---|
model |
string
|
The model name |
id |
|
The id value |
data |
object
|
The model instance data |
[callback] |
function
|
The callback function |
Build a REST resource client for CRUD operations
Name | Type | Description |
---|---|---|
modelCtor |
function
|
The model constructor |
baseUrl |
string
|
The base URL |
Name | Type | Description |
---|---|---|
result |
RestResource
|
Enable/disable debug
Name | Type | Description |
---|---|---|
enabled |
boolean
|
Wrap the callback so that it takes (err, result, response)
Name | Type | Description |
---|---|---|
cb |
function
|
The callback function |
Name | Type | Description |
---|---|---|
result |
|
Map the create operation to HTTP POST /{model}
Name | Type | Description |
---|---|---|
obj |
object
|
The HTTP body |
[cb] |
function
|
The callback function |
Map the update operation to POST /{model}/{id}
Name | Type | Description |
---|---|---|
id |
|
The id value |
obj |
object
|
The HTTP body |
[cb] |
function
|
The callback function |
Map the delete operation to POST /{model}/{id}
Name | Type | Description |
---|---|---|
id |
|
The id value |
[cb] |
function
|
The callback function |
Map the delete operation to POST /{model}
Name | Type | Description |
---|---|---|
id |
|
The id value |
[cb] |
function
|
The callback function |
Map the find operation to GET /{model}/{id}
Name | Type | Description |
---|---|---|
id |
|
The id value |
[cb] |
function
|
The callback function |
Map the all/query operation to GET /{model}
Name | Type | Description |
---|---|---|
q |
object
|
query string |
[cb] |
function
|
callback with (err, results) |