Schema

Schema

A Schema object allows you to interact with a Cloud Pub/Sub schema.

This should only be instantiated by the PubSub class. To obtain an instance for end user usage, call pubsub.schema().

Constructor

new Schema(pubsub, id)

Parameters:
Name Type Description
pubsub PubSub

The PubSub object creating this object.

id id

name or ID of the schema.

Examples

Creating an instance of this class.

const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();

const schema = pubsub.schema('my-schema');

Getting the details of a schema. Note that Schema methods do not provide a callback interface. Use .then() or await.

const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();

const schema = pubsub.schema('my-schema');
schema.get(SchemaViews.Basic).then(console.log);

Members

id

The fully qualified name of this schema. We will qualify this if it's only an ID passed (assuming the parent project). Unfortunately, we might not be able to do that if our pubsub's client hasn't been initialized. In that case, we just set the id and get the name later.

pubsub

The parent PubSub instance of this topic instance.

Methods

(static) metadataFromMessage()

Translates the schema attributes in messages delivered from Pub/Sub. All resulting fields may end up being blank.

(async) create(type, definition, optionsopt) → {Promise.<void>}

Create a schema.

Parameters:
Name Type Attributes Description
type SchemaType

The type of the schema (Protobuf, Avro, etc).

definition string

The text describing the schema in terms of the type.

options object <optional>

Request configuration options, outlined here: https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html.

Returns:
Type Description
Promise.<void>
See:
Throws:
  • if the schema type is incorrect.

    Type
    Error
  • if the definition is invalid.

    Type
    Error
Example

Create a schema.

const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();

const schema = pubsub.schema('messageType');
await schema.create(
  SchemaTypes.Avro,
  '{...avro definition...}'
);

(async) delete(optionsopt) → {Promise.<void>}

Delete the schema from the project.

Parameters:
Name Type Attributes Description
options object <optional>

Request configuration options, outlined here: https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html.

Returns:
Type Description
Promise.<void>
See:

(async) get(viewopt, optionsopt) → {Promise.<ISchema>}

Get full information about the schema from the service.

Parameters:
Name Type Attributes Description
view google.pubsub.v1.SchemaView <optional>

The type of schema object requested, which should be an enum value from SchemaViews. Defaults to Full.

options object <optional>

Request configuration options, outlined here: https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html.

Returns:
Type Description
Promise.<ISchema>
See:

(async) getName() → {Promise.<string>}

Return the fully qualified name of this schema.

Note that we have to verify that we have a projectId before returning this, so we have to check that first.

Returns:
Type Description
Promise.<string>

a Promise that resolves to the full schema name

(async) validateMessage(message, encoding, optionsopt) → {Promise.<void>}

Validate a message against this schema's definition.

If you would like to validate a message against an arbitrary schema, please use the SchemaServiceClient GAPIC class directly, using your PubSub instance's configuration, via PubSub#getClientConfig.

Parameters:
Name Type Attributes Description
message string

The message to validate.

encoding Encoding | "JSON" | "BINARY"

The encoding of the message to validate.

options object <optional>

Request configuration options, outlined here: https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html.

Returns:
Type Description
Promise.<void>
See:
Throws:
  • if the validation fails.

    Type
    Error
  • if other parameters are invalid.

    Type
    Error