@versesdev/genius-core-sdk

0.0.2 • Public • Published

Genius Core Typescript SDK

Before you start

You will need the following information to connect to any genius core instance:

  1. The address of your Genius Core instance. Examples here will use [::1]:50052 as the address.
  2. The client ID, client secret, and authentication domain for your OAuth provider.
    • For Auth0, this would be the client ID, client secret, and "auth0.com"

Creating the client

Create the client using its constructor:

import { CollisionStrategy } from '@versesdev/proto-kortex/hstp/v1'
import { Client, ClientCredentials } from '../src/'

const client_id = process.env.CLIENT_ID
const client_secret = process.env.CLIENT_SECRET
const oauth_domain = process.env.OAUTH_DOMAIN

// Server address
const addr = 'http://127.0.0.1:50052'
let credentials: ClientCredentials | null = null

// If you do not have credentials, set them to None. This is only valid if the server accepts
// "localhost" or unauthenticated requests.
if (client_id && client_secret && oauth_domain) {
  credentials = new ClientCredentials(client_id, client_secret, oauth_domain)
}

// Create the client
const client = new Client({
  addr: addr,
  clientCredentials: credentials,
})

console.debug('Client is now created and connected');

Simple Queries

Send any HSML query with the .query() method.

const entity = JSON.stringify(`{ "swid": "${Client.makeSwid()}", "schema": ["entity"] }`);

client.query("#entity where name = 'Apple'");
client.upsert({
  entity,
  collisionStrategy: CollisionStrategy.MERGE
});
client.query(`%archive [${entity}]`)

Listen to Changes

You can listen to changes on a given entity, set of entities, link, or set of links by:

  1. Opening a listening port
  2. Adding the items to listen to, of the appropriate type
  3. Asynchronously iterating over the listener

If you would later like to keep the listen socket open but stop listening to certain changes, call the corresponding remove function. Listeners are automatically destroyed when no longer referenced.

const listener = client.listen();
// Add a single
listener.add_entity_listeners('swid:entity:r11C0fOzt0nBoCkr1lI4l')
// Add a list (repetition has no effect)
listener.add_entity_listeners(
  'swid:entity:95KAC7PRbNbwKEC9RaLFs',
  'swid:entity:r11C0fOzt0nBoCkr1lI4l'
)
// Add a link or links
listener.add_link_listeners('swid:link:pQ7ftgk9jZLfh5NfJxbLW')
// Remove an entity (will remove all listeners)
listener.remove_entity_listeners('swid:entity:r11C0fOzt0nBoCkr1lI4l')

// Iterate over the result
for await (const new_entity_or_link of listener.iterator()) {
  console.log(new_entity_or_link)
}

Readme

Keywords

Package Sidebar

Install

npm i @versesdev/genius-core-sdk

Weekly Downloads

1

Version

0.0.2

License

MIT

Unpacked Size

16.2 kB

Total Files

8

Last publish

Collaborators

  • yauhen-baichuk
  • raquinoverse
  • kfvs
  • dmixsmi-verses
  • jamd1977
  • scottboats
  • liorsaar-verses
  • marina_p
  • coopwilliams-verses
  • jodie
  • szehtabiverses
  • rpettyverses
  • timb-verses
  • sylvain_pronovost_verses
  • javed.verses
  • ozancatalverses
  • tverbele
  • joseph_lee97
  • tom-mason-verses
  • qkmadsonvs
  • verses-benfu
  • alessandro_muzzi
  • dennis.hahn
  • ryan-verses
  • moverses
  • aidinism