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

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
NameDescription
pubsub PubSub
idOrName string

Properties

id

id: string;
Property Value
TypeDescription
string

name_

name_?: string;
Property Value
TypeDescription
string

pubsub

pubsub: PubSub;
Property Value
TypeDescription
PubSub

Methods

create(type, definition, gaxOpts)

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

Create a schema.

Parameters
NameDescription
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
TypeDescription
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
NameDescription
gaxOpts CallOptions
Returns
TypeDescription
Promise<void>

{Promise

formatName_(projectId, nameOrId)

static formatName_(projectId: string, nameOrId: string): string;
Parameters
NameDescription
projectId string
nameOrId string
Returns
TypeDescription
string

get(view, gaxOpts)

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

Get full information about the schema from the service.

Parameters
NameDescription
view SchemaView

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

gaxOpts CallOptions
Returns
TypeDescription
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
TypeDescription
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
NameDescription
attributes Attributes
Returns
TypeDescription
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
NameDescription
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
TypeDescription
Promise<void>

{Promise