Genius Core Typescript SDK
Before you start
You will need the following information to connect to any genius core instance:
- The address of your Genius Core instance. Examples here will use
[::1]:50052
as the address. - 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:
- Opening a listening port
- Adding the items to listen to, of the appropriate type
- 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)
}