0.8.5 • Published 6 years ago

syncano-server v0.8.5

Weekly downloads
8
License
MIT
Repository
github
Last release
6 years ago

XO code style CircleCI codecov

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:

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'
})

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 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')
  .list()
  .then(posts => {})

// Get list of post - author column will be expanded with data from target class
data.posts
  .with('author')
  .list()
  .then(posts => {})

// Delete tags with with given array of ids
data.tags.delete([8735, 8733])

// Delete single tag
data.tags.delete(7652)

Managing users (users)

// 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
  })

Using Events (events)

event.emit('my_signal', {dummyKey: 'dummy_value'})
  .then(event => {})
  .catch(err => {
    // error is thrown if emit was unsuccessful
  })

Publishing to channels

channel.publish('my_channel', {dummyKey: 'dummy_value'})
  .then(res => {})
  .catch(err => {})

Socket connection

const latestTags = await socket.get('tags/list', { sort: 'latest' })
const createdTag = await socket.post('tags/create', { name: 'nature' })

HTTP Responses

// 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
  .header('X-RATE-LIMIT', 50)
  .json({
    title: "Post title",
    content: "Lorem ipsum dolor sit amet."
  })

Logging

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"})
log.debug('This is debug message!')

Check documentation to learn more.

1.0.1-14

6 years ago

1.0.1-13

7 years ago

1.0.1-12

7 years ago

1.0.1-11

7 years ago

1.0.1-9

7 years ago

1.0.1-8

7 years ago

1.0.1-7

7 years ago

1.0.1-6

7 years ago

1.0.1-5

7 years ago

1.0.1-4

7 years ago

1.0.1-3

7 years ago

1.0.1-2

7 years ago

1.0.1-1

7 years ago

1.0.1-0

7 years ago

0.8.7-1

7 years ago

0.8.7-0

7 years ago

0.8.5

7 years ago

0.8.5-0

7 years ago

0.8.2-5

7 years ago

0.8.3-3

7 years ago

0.8.2-4

7 years ago

0.8.2-3

7 years ago

0.8.3-2

7 years ago

0.8.3-1

7 years ago

0.8.2-2

7 years ago

0.8.2-1

7 years ago

0.8.2

7 years ago

0.8.2-0

7 years ago

0.8.1-10

7 years ago

0.8.1-9

7 years ago

0.8.1-8

7 years ago

0.8.1-7

7 years ago

0.8.1-6

7 years ago

0.8.1-5

7 years ago

0.8.1-4

7 years ago

0.8.1-3

7 years ago

0.8.1-2

7 years ago

0.8.1-1

7 years ago

0.8.1

7 years ago

0.8.1-0

7 years ago

0.7.2-44

7 years ago

0.7.2-43

7 years ago

0.7.2-42

7 years ago

0.7.2-41

7 years ago

0.7.2-40

7 years ago

0.7.2-39

7 years ago

0.7.2-38

7 years ago

0.7.2-37

7 years ago

0.7.2-36

7 years ago

0.7.2-35

7 years ago

0.7.2-34

7 years ago

0.7.2-33

7 years ago

0.7.2-32

7 years ago

0.7.2-31

7 years ago

0.7.2-30

7 years ago

0.7.2-29

7 years ago

0.7.2-28

7 years ago

0.7.2-27

7 years ago

0.7.2-26

7 years ago

0.7.2-25

7 years ago

0.7.2-24

7 years ago

0.7.2-23

7 years ago

0.7.2-22

7 years ago

0.7.2-21

7 years ago

0.7.2-20

7 years ago

0.7.2-19

7 years ago

0.7.2-18

7 years ago

0.7.2-17

7 years ago

0.7.2-16

7 years ago

0.7.2-15

7 years ago

0.7.2-14

7 years ago

0.7.2-13

7 years ago

0.7.2-12

7 years ago

0.7.2-11

7 years ago

0.7.2-10

7 years ago

0.7.2-9

7 years ago

0.7.2-8

7 years ago

0.7.2-7

7 years ago

0.7.2-6

7 years ago

0.7.2-5

7 years ago

0.7.2-4

7 years ago

0.7.2-3

7 years ago

0.7.2-2

7 years ago

0.7.2-1

7 years ago

0.7.2-0

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.0

7 years ago

0.5.5

7 years ago

0.5.4

7 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.1

7 years ago

0.2.0

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago