3.4.3 • Published 4 years ago

iop-connector v3.4.3

Weekly downloads
14
License
MIT
Repository
-
Last release
4 years ago

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:

MethodParametersDescription
loginuser, passwordAuthentication via user name and password. Returns an object with the fields userId, token and tokenExpires
loginAppappId, keyIdAuthentication via appId and keyId for application-type users
renewTokenRenew authentication token (only needed for regular users)
isLoggedReturns whether the user is logged in or not
findAllcollection, selectorQuery documents from a collection
insertcollection, dataInsert one or multiple documents. data can be an array of documents
updatecollection, dataUpdate one or multiple documents. data can be an array of documents
removecollection, selectorDelete all the matching documents (Use carefully!)
findOnecollection, idGet 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:

MethodParametersDescription
healthCheck the server availability
loginuser, passwordAuthentication via user name and password
loginAppappId, keyIdAuthentication via appId and keyId for application-type users
renewTokenRenew authentication token (only needed for regular users)
meGet logged user ID and profile
getlayer, collection, selectorQuery documents from a collection
addlayer, collection, dataInsert one or multiple documents. data can be an array of documents
modlayer, collection, dataUpdate one or multiple documents. data can be an array of documents
dellayer, collection, selectorDelete all the matching documents
getOnelayer, collection, idGet a single document
modOnelayer, collection, id, dataUpdate a document. data is an object with the changes to be applied
delOnelayer, collection, idDelete a document
rangelayer, collection, dataQuery 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)
nearlayer, collection, dataQuery 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:

  1. Pass your credentials when initializing the connector:
const server = IOPHTTP('https://your.server.com/api/v2', '<appId>', '<keyId>');
  1. 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.