1.0.2 • Published 7 years ago
@connecthing.io/connecthing-api v1.0.2
connecthing-api
connecthing-api offers two module. objectstore to create, read, update and delete with the connecthing objectstore and request for making Rest calls between microservices
objectstore
The objectstore
object allows you to perform CRUD operations on the connecthing objectstore.
With each call a javascript promise will be returned.
This module shares the same function api across node microservices and javascript widget.
The support operations are:
- read: get a collection or document from a collection (
address
) - add: insert a new document into a collection (
address
,js_object
), - replace: replace a document in a collection (
address
,js_object
), - remove: remove a document in a collection (
address
), - drop: delete a collection(
address
)
The
address
can be aString
orobjectStore_Object
(as return from an objectstore promise) Thejs_object
should be a javacriptobject
Examples
microservices - setup for exampels
// get objectstore helper
const { objectstore } = require("connecthing-api")
// some data to save
const myMobileSensor = { device:123, name:"mobile sensor" }
widget - setup for exampels
<script src="https://cdn.jsdelivr.net/npm/@connecthing.io/connecthing-api@latest/objectstore.min.js"></script>
<script>
const myMobileSensor = { device:123, name:"mobile sensor" }
</script>
Using objectstore:
add an object to the store
objectstore.add("sensors",myMobileSensor)
.then( objectStore_MyMobileSensor => {
console.log("your sensor has been added to the objectstore")
})
get all values from a collection
objectstore.read("sensors")
.then( objectStore_Sensors => {
console.log("You have " objectStore_Sensors.length " sensors in the objectstore")
})
get a value from a collection
// if you know the objectstore id
objectstore.read("sensors/5a6f0d713174acbdfd07eabe")
.then( objectStore_Sensor => {
console.log("Your sensor:"+JSON.stringify(objectStore_Sensor))
})
to update the value in a collection
objectstore.read("sensors/5a6f0d713174acbdfd07eabe")
.then( objectStore_Sensor => {
objectStore_Sensor.owner = "John"
return objectStore_Sensor.update()
})
.then(objectStore_Sensor=>{
console.log("sensor updated")
})
to remove value in a collection
objectstore.read("sensors")
.then( objectStore_Sensors => {
// find your device
const johnsSensor = objectStore_Sensor.find(sensor => sensor.owner = "John")
// remove your device
return objectstore.remove(johnsSensor)
})
.then(()=>{
console.log("sensor remove")
})
to remove a collection
objectstore.drop("sensors")
.then(()=>{
console.log("sensors deleted!")
})
request
The "request" module is for making Rest calls between microservices.
const { request } = require("connecthing-api")
request({
url: "/api/v1/devices"
}, function(err, resp, body){
if(err){
throw new Error("Error making request to connecthing api: " + err.stack);
}
console.log("Call to devices api: " + resp.statusCode);
console.dir(body);
});