Class Schema (4.4.0)

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().

Package

@google-cloud/pubsub

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);

Constructors

(constructor)(pubsub, idOrName)

constructor(pubsub: PubSub, idOrName: string);

Constructs a new instance of the Schema class

Parameters
Name Description
pubsub PubSub
idOrName string

Properties

id

id: string;

name_

name_?: string;

pubsub

pubsub: PubSub;

Methods

create(type, definition, gaxOpts)

create(type: SchemaType, definition: string, gaxOpts?: CallOptions): Promise<void>;

Create a schema.

Parameters
Name Description
type SchemaType

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

definition string

The text describing the schema in terms of the type.

gaxOpts CallOptions
Returns
Type Description
Promise<void>

{Promise

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

delete(gaxOpts)

delete(gaxOpts?: CallOptions): Promise<void>;

Delete the schema from the project.

Parameter
Name Description
gaxOpts CallOptions
Returns
Type Description
Promise<void>

{Promise

formatName_(projectId, nameOrId)

static formatName_(projectId: string, nameOrId: string): string;
Parameters
Name Description
projectId string
nameOrId string
Returns
Type Description
string

get(view, gaxOpts)

get(view?: SchemaView, gaxOpts?: CallOptions): Promise<ISchema>;

Get full information about the schema from the service.

Parameters
Name Description
view SchemaView

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

gaxOpts CallOptions
Returns
Type Description
Promise<ISchema>

{Promise

getName()

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.

{Promise

Returns
Type Description
Promise<string>

metadataFromMessage(attributes)

static metadataFromMessage(attributes: Attributes): SchemaMessageMetadata;

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

Parameter
Name Description
attributes Attributes
Returns
Type Description
SchemaMessageMetadata

validateMessage(message, encoding, gaxOpts)

validateMessage(message: string, encoding: google.pubsub.v1.Encoding | keyof typeof google.pubsub.v1.Encoding, gaxOpts?: CallOptions): 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 GAPIC class directly, using your PubSub instance's configuration, via .

Parameters
Name Description
message string

The message to validate.

encoding google.pubsub.v1.Encoding | keyof typeof google.pubsub.v1.Encoding

The encoding of the message to validate.

gaxOpts CallOptions
Returns
Type Description
Promise<void>

{Promise