Syncano Server-side Library
This library supposed to be used in Syncano Sockets (inside scripts) to communicate with Syncano Core Services. Syncano provides various Core Services:
- Database (db) - NoSQL database to store your application data
- Users Management (users) - service to store and manage users and groups of your application
- Event Loop (events) - service to emit events which can be caught by any Socket
- Realtime Channels (channels) - implement publish/subscribe model for realtime communication
Library initialization
To initialize library simply type:
Library initiated that way will grab necessary information from the context of you Socket Script - it means that you don't need to provide additional information such as Instance name or authentication key (token) to your Instance.
If you want to force the library to connect to specified instance type:
const data events = token: '9-12jdiasdnfo23nrokms' instanceName: 'example-instance-name'
Examples
Using Database (data)
In this example tags
is a name of a class (data model) configured for that instance.
// Create new object in tags classdatatags ; // Get list of 140 tags used more than 100 timesdatatags // Get list of post where author is reference to other class // and author email is john@example.comdataposts // Get list of post - author column will be expanded with data from target classdataposts // Delete tags with with given array of idsdatatags // Delete single tagdatatags
Managing users (users)
// Get first user with given mailusers // Get first user with given mail, throws error if user was not foundusers
Using Events (events)
event
Publishing to channels
channel
Socket connection
const latestTags = await socketconst createdTag = await socket
HTTP Responses
// Simple text/plain response// response(content, status, contentType, headers) // Respond with custom headerresponse 'Check headers' // Respond with json stringresponse response
Logging
For debug purposes you can use logger
:
Example:
// Listen for all events logger // Create custom logger levels - optionally // Defaults are: error, warn, debug, infologger // Initialize logger with scope "User Socket"const log = // Specific level loggersloglogloglog
Check documentation to learn more.