package
A library to intereact with the Syncano API on a server side
npm install syncano-serverGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
This library supposed to be used in Syncano Sockets (inside scripts) to communicate with Syncano Core Services. Syncano provides various Core Services:
To initialize library simply type:
import { data, users, socket, response, event, logger } from 'syncano-server'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:
import Server from 'syncano-server'
const { data, events } = new Server({
token: '9-12jdiasdnfo23nrokms',
instanceName: 'example-instance-name'
})In this example tags is a name of a class (data model) configured for that instance.
// Create new object in tags class
data.tags
.create({
name: 'javascript',
usage_count: 0
})
.then(tag => {});
// Get list of 140 tags used more than 100 times
data.tags
.where('usage_count', 'gt', 100)
.take(140)
.list()
.then(tags => {})
// Get list of post where author is reference to other class
// and author email is john@example.com
data.posts
.where('author.email', 'john@example.com')
// Get first user with given mail
users
.where('email', 'john.doe@example.com')
.first()
.then(user => {
// user variable is null if not found
// so no need for catch method
})
// Get first user with given mail, throws error if user was not found
users
.where('email', 'john.doe@example.com')
.firstOrFail()
.then(user => {})
.catch(err => {
// error is thrown if user was not found
})event.emit('my_signal', {dummyKey: 'dummy_value'})
.then(event => {})
.catch(err => {
// error is thrown if emit was unsuccessful
})channel.publish('my_channel', {dummyKey: 'dummy_value'})
.then(res => {})
.catch(err => {})const latestTags = await socket.get('tags/list', { sort: 'latest' })
const createdTag = await socket.post('tags/create', { name: 'nature' })// Simple text/plain response
// response(content, status, contentType, headers)
response('Hello world')
response('Hello world', 200, 'text/plain', {
'X-RATE-LIMIT': 50
})
// Respond with custom header
response
.header('X-RATE-LIMIT', 50)
.header('X-USAGE', 35)
('Check headers')
// Respond with json string
response.json({
title: "Post title",
content: "Lorem ipsum dolor sit amet."
})
response
For debug purposes you can use logger:
Example:
import {logger} from 'syncano-server'
// Listen for all events
logger.listen(event => {
// Handle event - save to db or send email
})
// Create custom logger levels - optionally
// Defaults are: error, warn, debug, info
logger.levels(['error', 'notice', 'fatal'])
// Initialize logger with scope "User Socket"
const log = logger('User Socket')
// Specific level loggers
log.error('This is error message!')
log.warn('This is warning message!')
log.info('This is info message!', {hello: "world"})
Check documentation to learn more.
syncano-devops
devops@syncano.com
Estimated unpacked size across 21 packages.
724.0 kB
Includes dependencies.
5 direct · 1 outdated
^2.1.4
4.0.6
^4.4.2
4.4.2
^4.3.2
4.3.2
2.0.0-alpha.3
3.3.2
^4.6.0
4.6.2