connecthing-api offers two module. objectstore to create, read, update and delete with the connecthing objectstore and request for making Rest calls between microservices
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
// get objectstore helper
const { objectstore } = require("connecthing-api")
// some data to save
const myMobileSensor = { device:123, name:"mobile sensor" }
<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>
objectstore.add("sensors",myMobileSensor)
.then( objectStore_MyMobileSensor => {
console.log("your sensor has been added to the objectstore")
})
objectstore.read("sensors")
.then( objectStore_Sensors => {
console.log("You have " objectStore_Sensors.length " sensors in the objectstore")
})
// if you know the objectstore id
objectstore.read("sensors/5a6f0d713174acbdfd07eabe")
.then( objectStore_Sensor => {
console.log("Your sensor:"+JSON.stringify(objectStore_Sensor))
})
objectstore.read("sensors/5a6f0d713174acbdfd07eabe")
.then( objectStore_Sensor => {
objectStore_Sensor.owner = "John"
return objectStore_Sensor.update()
})
.then(objectStore_Sensor=>{
console.log("sensor updated")
})
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")
})
objectstore.drop("sensors")
.then(()=>{
console.log("sensors deleted!")
})
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);
});