3.0.1 • Published 6 years ago

hemera-avro v3.0.1

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

Hemera-avro package

npm styled with prettier

This is a plugin to use Avro with Hemera.

Apache Avro™ is a data serialization system.

Features

  • No schema for you pattern is required
  • The request and response schema of a server method (add) is validated by Avro™
  • Flexible base schema
  • Easy to start

Example without payload schema

Only the protocol schema will be validated with Avro

const Hemera = require('nats-hemera')
// Use NATS driver >= 0.7.2
const nats = require('nats').connect({ 
  preserveBuffers: true
})
const HemeraAvro = require('hemera-avro')

const hemera = new Hemera(nats, {
  logLevel: 'info'
})

hemera.use(HemeraAvro)

Example with payload schema

hemera.ready(() => {

  let Avro = hemera.avro

  const type = Avro.parse({
  name: 'Person',
  type: 'record',
  fields: [{
      name: 'a',
      type: 'int'
    }]
  })

  hemera.add({
    topic: 'peopleDirectory',
    cmd: 'create',
    avro$: type // We know how to encode the request
  }, (req, cb) => {

    cb(null, { a: 1 })
  })

  hemera.act({
    topic: 'peopleDirectory',
    cmd: 'create',
    name: 'peter',
    avro$: type // We know how to decode the response
  }, function (err, resp) {

    this.log.info('Result', resp)
  })
})

Use Type inference to auto-generate your schema

const type = avro.Type.forValue([1, 4.5, 8])
// We can now encode or any array of floats using this type:
const buf = type.toBuffer([4, 6.1])
const val = type.fromBuffer(buf) // [4, 6.1]
// We can also access the auto-generated schema:
const schema = type.schema()
const JSON = JSON.stringify(schema) // Copy & Paste

Base Schema

The pattern is encoded to JSON byte-array when it's from type object or array. Primitive values (boolean, strings, numbers) are encoded with Avro. The error, delegate, meta and request data are predefined with a fixed schema. Here a list of the specification.

  • delegate: Can be a Map of strings, boolean, number
  • meta: Can be a Map of strings, boolean, number
  • result: You can use your own schema. If you don't define it the message is interpreted as binary (JSON encoded schema-less)
  • error:
    • name: Name of the error
    • message: Message of the error
    • stack: Stack of the error
    • code: Code of the error
    • statusCode: Code of the error when using hemera-web
    • details: Can be a Map of strings, boolean, number to add additional data
    • hops: An array of services which were involved in this request
3.0.1

6 years ago

3.0.0

6 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.16

7 years ago

1.1.15

7 years ago

1.1.14

7 years ago

1.1.13

7 years ago

1.1.12

7 years ago

1.1.11

7 years ago

1.1.10

7 years ago

1.1.9

7 years ago

1.1.8

7 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.0

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago