3.0.5 • Published 4 years ago
citric-connector v3.0.5
CITRIC Connector
Module to connect to a CITRIC server via DDP or HTTP:
- CITRIC. Extends SimpleDDP and provides methods to interact with a CITRIC server via DDP.
- CITRICHTTP. Provides methods to interact with a CITRIC server via HTTP.
Usage
Install the package running:
npm install citric-connector
DDP connector
For a detailed explanation on how to manage subscriptions and collections, please refer to the SimpleDDP documentation.
DDP example
import { CITRIC } from 'citric-connector';
// or const { CITRIC } = require('citric-connector');
async function start() {
// Initialize the connection
const server = CITRIC('https://your.server.com');
// Login as a regular user
const userLogin = await server.login('<username>', '<password>');
console.log(userLogin); // { userId: "khdfHbDE1aytzFvtf", token: "bnGmbgwgeY8NM2QBC9XRYRsRKhlEy2sHHbE7mlXYZYM", 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)
const appLogin = await server.loginApp('<appId>', '<keyId>');
console.log(appLogin); // { userId: "khdfHbDE1aytzFvtf", token: "bnGmbgwgeY8NM2QBC9XRYRsRKhlEy2sHHbE7mlXYZYM", tokenExpires: "2020-07-28T11:49:06.455Z" }
// Subscribe to the desired data
const subscription = server.subscribe('<layer>_<collection>.all');
await subscription.ready();
// Retrieve the desired data
const allItems = await server.collection('<layer>_<collection>').fetch();
const oneItem = await server.collection('<layer>_<collection>').filter(item => item.id === '<item_id>').fetch();
// Manage the token renovation if needed
await server.renewToken();
}
start();
HTTP connector
Available methods:
Method | Parameters | Description |
---|---|---|
health | Check the server availability | |
login | user, password | Authentication via user (ID, name or email) and password |
loginApp | appId, keyId | Authentication via appId and keyId |
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. Except for login, the response is an object with the fields status (that should be "success") and data.
HTTP example
import { CITRICHTTP } from 'citric-connector';
// or const { CITRICHTTP } = require('citric-connector');
async function start() {
// Initialize the connection
const server = CITRICHTTP('https://citric.server.com/api/v2');
// Login
await server.login();
// Retrieve the desired data
const allItems = await server.get('<layer>_<collection>');
const oneItem = await server.getOne('<layer>_<collection>', '<item_id>');
}
start();
If you are provided with application credentials in the form of appId and keyId, there are two options to manage the authentication:
- Using the loginApp method:
server.loginApp('<appId>', '<keyId>')
Note that it is a synchronous method so doesn't accept a callback nor return a Promise.
- Pass your credentials when initializing the connector:
const server = CITRICHTTP('https://your.server.com/api/v2', '<appId>', '<keyId>');