Skip to content

Latest commit

 

History

History
108 lines (85 loc) · 2.23 KB

api.md

File metadata and controls

108 lines (85 loc) · 2.23 KB

#class: Bolty Members

##new Bolty(schema) Bolty main class

Params

  • schema object - The main schemma with a name and fields.

Example

var Bolty = require('bolty');
var schema = {
  name: 'string',
  surname: 'string'
};
var template = new Bolty(schema);

##bolty.decode(buffer) Decode an buffer serialized by Bolty

Params

  • buffer buffer - The buffer to be decoded into an object

Returns: object - The object resulting from the decoding
Example

var user = template.decode(<Buffer 01 0c 48 65 6c 6c 6f 20 77 6f 72 ...>);
console.log(user);

##bolty.encode(obj) Encode an object into a serialized buffer

Params

  • obj object - The object to be serialized

Returns: buffer - the resulting buffer
Example

var buff = template.encode({
  firname: 'Alan',
  surname: 'Hoffmeister'
});

##bolty.plugin(obj) Add custom encoder/decoder to your schema

Params

  • obj object - The plugin object, we need a name, decoder and encoder

Example

// We will create a plugin to handle MongoDB's ObjectID
var ObjectID = require('mongodb').ObjectID
var Bolty = require('bolty');
var template = new Bolty({
  _id: 'objectid'
});

// Now plug the custom encoder/decoder
template.plugin({
  name: 'objectid' // must be the same as the type
  encoder: function(value){
    return new Buffer(value.toString(), 'hex');
  },
  decoder: function(buff){
    return new ObjectID(buff.toString('hex'));
  }
});

##bolty.schema(name, fields) Add an aditionar schema to your main schema, so you can have nested objects.

Params

  • name string - The name of the additional schema
  • fields object - The fields of this aditional schema

Example

template.schema('info', {
  street: 'string',
  telephone: 'varint'
});