3.0.5 • Published 4 years ago

citric-connector v3.0.5

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

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:

MethodParametersDescription
healthCheck the server availability
loginuser, passwordAuthentication via user (ID, name or email) and password
loginAppappId, keyIdAuthentication via appId and keyId
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. 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:

  1. 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.

  1. Pass your credentials when initializing the connector:
const server = CITRICHTTP('https://your.server.com/api/v2', '<appId>', '<keyId>');
3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago