Link

API Reference

Table of contents

  1. API Reference
    1. getSchema(namespace, name, version) => Promise<Object>
    2. saveSchema(schema) => Promise<Object>
    3. encode(schema, payload) => Buffer
    4. decode(binaryBuffer) => Promise<Object>

getSchema(namespace, name, version) => Promise<Object>

Get latest schema version for a given namespace and name. It caches versions in memory, so it only fetches from the API the first time any schema is used.

If no version is specified, the latest one is returned.

Parameters

NameTypeDescription
namespacestringSchema’s namespace
namestringSchema’s name
versionstring(optional) Schema’s version

Example

const schemadb = require('@schemadb/sdk');
schemadb.init('1036fae0-3a28-11ea-a5e3-...');

// The latest version is returned if no version is passed
const latestSchemaVersion = await schemadb.getSchema('com.example.store', 'order');

// Or just specify some
const schemaV2 = await schemadb.getSchema('com.example.store', 'order', '2');

saveSchema(schema) => Promise<Object>

Save new Avro schema to platform. Will fail if version already exists.

Parameters

NameTypeDescription
schemaObjectAvro JSON valid type

Example

const schemadb = require('@schemadb/sdk');
schemadb.init('1036fae0-3a28-11ea-a5e3-...');

const orderSchema = {
	"version": 1,
	"definition": {
		"type": "record",
		"namespace": "com.example.store",
		"name": "order",
		"fields": [
          { "name": "orderId", "type": "long" },
          { "name": "storeId", "type": "long" },
          {
          	"name": "timeplaced",
           	"type": "long",
          	"logicalType": "timestamp-millis" 
          },
          { "name": "orderStatus", "type": "string" }
        ]
	}
};

// Save schema to platform
await schemadb.saveSchema(orderSchema);

encode(schema, payload) => Buffer

Encode Object to Avro binary Buffer.

Parameters

NameTypeDescription
schemaObjectAvro JSON valid type
payloadObjectObject to be encoded

Example

const schemadb = require('@schemadb/sdk');
schemadb.init('1036fae0-3a28-11ea-a5e3-...');

// Get latest schema by namespace and name
const schema = await schemadb.getSchema('com.example.store', 'order');

// Encode payload
const avro = await schemadb.encode(schema, {
    orderId: 1234567890,
    storeId: 1234,
    timeplaced: 1586193018930,
    orderStatus: 'A'
});

decode(binaryBuffer) => Promise<Object>

Decode Avro binary Buffer to Object. Returns both the payload and the schema.

Parameters

NameTypeDescription
binaryBufferBufferAvro encoded binary buffer

Example

const schemadb = require('@schemadb/sdk');
schemadb.init('1036fae0-3a28-11ea-a5e3-...');

// Decode binary buffer to JSON object
const { payload, schema } = await schemadb.decode(avroBinaryBuffer);