git clone git@github.com:strongloop/loopback-example-office-supplies.git
cd loopback-example-office-supplies
npm install
Make sure you are in the server directory!
node app
To install the latest version of slc
:
npm install strong-cli -g
To check your version of slc
:
slc version
Should print something similar to:
slc v2.1.1 (node v0.10.24)
mkdir loopback-example-office-supplies
cd loopback-example-office-supplies
slc lb project server
cd server
slc lb model product
slc lb model category
Edit the models.json
file, and replace the following lines:
"product": {
"properties": {},
"public": true,
"dataSource": "db"
},
...
with the following:
"product": {
"options": {
"relations": {
"category": {
"model": "category",
"type": "belongsTo",
"foreignKey": "categoryId"
},
"owner": {
"model": "category",
"type": "belongsTo",
"foreignKey": "ownerId"
}
}
},
"properties": {},
"public": true,
"dataSource": "db"
},
Add the following code in /models/products.js
:
var app = require('../app');
var product = app.models.product;
var category = app.models.category;
category.create({name: 'stationery'}, function(err, stationery) {
product.create({
name: 'pencil',
price: 0.99,
categoryId: stationery.id,
ownerId: stationery.id
}, function() {
product.findOne({where: {categoryId: stationery.id}}, function(err, pencil) {
console.log(pencil);
});
});
});
Now run the app as follows. Make sure you are in the server
directory:
$ slc run
View your application's APIs with the LoopBack API Explorer at http://0.0.0.0:3000/explorer.
In addition to the standard endpoints for users, accessTokens, applications, push, installations, and notifications, you will see endpoints for the products and categories models you created.
Click on /products to see the REST endpoints for the products model.
You can see the "product" data you added programmatically, click:
- /products
- GET / Find all instances of the model matched by filter from the data source.
- Try it out!
In the Response Body you'll see the operation returns the data you added:
[
{
"id": 1,
"categoryId": 1,
"ownerId": 1,
"name": "pencil",
"price": 0.99
}
]
If you want to convince yourself that the write APIs also work, try this:
- Click PUT /products Update an existing model instance or insert a new one into the data source.
- Copy the data returned from the above GET operation (as shown above), and paste it in the Value field under Parameters, but edit it so it has a unique
id
value and a different product name, as shown for example below. - Click Try it Out! to perform the PUT operation.
- Repeat the GET operation you performed previously to confirm that the data was saved and is returned by the query.
[
{
"id": 2,
"categoryId": 1,
"ownerId": 1,
"name": "pen",
"price": 0.99
}
]
This will prevent any unauthorized access to the REST API.
slc lb acl --all-models --deny --everyone
slc lb acl --model product --allow --owner --all