lesscloud-sdk
The lesscloud-sdk is a js client side tool for lesscloud, which based on restful.js , more about the restful.js please visit the url https://github.com/marmelab/restful.js
Install
Use the npm to install
npm install lesscloud-sdk
Based on the package.json with npm install, open your package.json file, and add the following to dependenices
"lesscloud-sdk": "^0.0.5"
Settings
-
First of all, import the lesscloud-sdk
import api from lesscloud-sdk
-
Create a index.html file, add the following codes to head tag, and set your app params
<meta name="LESSCLOUD-APP-NAME" content="{{YOUR-APP-NAME}}"/>
<meta name="LESSCLOUD-APP-ID" content="{{YOUR-APP-ID}}"/>
<meta name="LESSCLOUD-APP-VERSION" content="latest"/>
Usage
Reading one or more record
Find one record with method find
api.find('book', '{{The record id}}')
.then((res)=>{
console.log(res.body().data());
})
.catch((res)=>{
console.log(res)
});
Find all records with method findAll
api.findAll('book')
.then((res)=>{
console.log(res.body(false))
})
.catch((res)=>{
console.log(res)
});
Find all records with sort by some one specified field
api.findAll('book', {'$order':{'createdAt':1}}) // sort by asc
...
api.findAll('book', {'$order':{'createdAt':-1}}) // sort by desc
...
Find all records with a limit and skip command
api.findAll('book', {'$limit':1, '$skip': 10})
...
Find all records and response the specify fields
api.findAll('book', {'$fields':'name,price'})
...
Find all records and response the all fields, except the specify fields
api.findAll('book', {'$fields':'-price'}) // This request without field price
...
Adding one record
const data = {...}; // items
const model = api.save('name', data).then((res)=>{
console.log(res);
}).catch((res)=>{
alert('error');
console.log(res)
});
Update one record
const data = {"_id":"...", ...}; // items
const model = api.save('name', data).then((res)=>{
console.log(res);
}).catch((res)=>{
alert('error');
console.log(res)
});
Delete one record
const data = {"_id":"..."};
const model = api.remove('name', data)
.then((res)=>{
console.log(res);
})
.catch((res)=>{
alert('error');
console.log(res)
});
Use the original restful.js
import api from lesscloud-sdk
api.restful // The restful.js instance