Module to connect to an ETRA Interoperability Platform via DDP or HTTP
npm install iop-connectorModule 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.
Install the package running:
```
npm install iop-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.
`javascript
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('
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('
// Subscribe to the desired data. Creating the corresponding subscriptions is mandatory in order to obtain the data
const subscription = server.subscribe('
await subscription.ready();
// Retrieve the desired data
const allItems = await server.findAll('
const oneItem = await server.findOne('
// Remove one element
await server.remove('
// Manage the token renovation when needed
await server.renewToken();
}
start();
`
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.
`javascript
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('
// Retrieve the desired data
const allItems = await server.get('
const oneItem = await server.getOne('
// 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:
`javascript`
const server = IOPHTTP('https://your.server.com/api/v2', '
2. Use the loginApp method:
`javascript``
server.loginApp('
> Note that it is a synchronous method so doesn't accept a callback nor return a Promise.