iop-connector
v3.4.3
Published
Module to connect to an ETRA Interoperability Platform via DDP or HTTP
Downloads
6
Readme
IOP Connector
Module to connect to an ETRA Interoperability Platform via DDP or HTTP:
- IOP. Extends SimpleDDP and provides methods to interact with an ETRA Interoperability Platform via DDP.
- IOPHTTP. Provides methods to interact with an ETRA Interoperability Platform via HTTP.
Usage
Install the package running:
npm install iop-connector
DDP connector
Available methods:
| Method | Parameters | Description | |---|---|---| | login | user, password | Authentication via user name and password. Returns an object with the fields userId, token and tokenExpires | | loginApp | appId, keyId | Authentication via appId and keyId for application-type users | | renewToken | | Renew authentication token (only needed for regular users) | | isLogged | | Returns whether the user is logged in or not | | findAll | collection, selector | Query documents from a collection | | insert | collection, data | Insert one or multiple documents. data can be an array of documents | | update | collection, data | Update one or multiple documents. data can be an array of documents | | remove | collection, selector | Delete all the matching documents (Use carefully!) | | findOne | collection, id | Get a single document |
All methods accept a callback as a last parameter or return a Promise if not provided. Note that the parameter "collection" would be in the form of layer_collection.
DDP example
import { IOP } from 'iop-connector';
// or const { IOP } = require('iop-connector');
async function start() {
// Initialize the connection
const server = IOP('https://your.server.com');
// Login as a regular user
const userLogin = await server.login('<username>', '<password>');
console.log(userLogin.tokenExpires); // "2020-07-28T11:49:06.455Z"
// Or login as an application (in case you are provided with credentials in the form of appId and keyId)
await server.loginApp('<appId>', '<keyId>');
// Subscribe to the desired data. Creating the corresponding subscriptions is mandatory in order to obtain the data
const subscription = server.subscribe('<layer>_<collection>.all');
await subscription.ready();
// Retrieve the desired data
const allItems = await server.findAll('<layer>_<collection>');
const oneItem = await server.findOne('<layer>_<collection>', '<item_id>');
// Remove one element
await server.remove('<layer>_<collection>', { _id: '<item_id>' });
// Manage the token renovation when needed
await server.renewToken();
}
start();
HTTP connector
Available methods:
| Method | Parameters | Description | |---|---|---| | health | | Check the server availability | | login | user, password | Authentication via user name and password | | loginApp | appId, keyId | Authentication via appId and keyId for application-type users | | renewToken | | Renew authentication token (only needed for regular users) | | me | | Get logged user ID and profile | | get | layer, collection, selector | Query documents from a collection | | add | layer, collection, data | Insert one or multiple documents. data can be an array of documents | | mod | layer, collection, data | Update one or multiple documents. data can be an array of documents | | del | layer, collection, selector | Delete all the matching documents | | getOne | layer, collection, id | Get a single document | | modOne | layer, collection, id, data | Update a document. data is an object with the changes to be applied | | delOne | layer, collection, id | Delete a document | | range | layer, collection, data | Query documents on a range of dates. data is an object with the fields from, to and field (if the field in which to compare the dates is different than timestamp) | | near | layer, collection, data | Query documents near a geographic point. data is an object with the fields lat, lon and distance |
All methods accept a callback as a last parameter or return a Promise if not provided. The response is an object with the fields status (that should be "success") and data.
HTTP example
import { IOPHTTP } from 'iop-connector';
// or const { IOPHTTP } = require('iop-connector');
async function start() {
// Initialize the connection
const server = IOPHTTP('https://iop.server.com');
// Login
await server.login('<username>', '<password>');
// Retrieve the desired data
const allItems = await server.get('<layer>', '<collection>');
const oneItem = await server.getOne('<layer>', '<collection>', '<item_id>');
// Manage the token renovation if needed
await server.renewToken();
}
start();
If you are provided with application credentials in the form of appId and keyId, there are two options to manage the authentication:
- Pass your credentials when initializing the connector:
const server = IOPHTTP('https://your.server.com/api/v2', '<appId>', '<keyId>');
- Use the loginApp method:
server.loginApp('<appId>', '<keyId>')
Note that it is a synchronous method so doesn't accept a callback nor return a Promise.