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/pubsubExamples
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
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.
type
definition
string
The text describing the schema in terms of the type.
gaxOpts
CallOptions
Promise
<void>
{Promise
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.
gaxOpts
CallOptions
Promise
<void>
{Promise
formatName_(projectId, nameOrId)
static
formatName_
(
projectId
:
string
,
nameOrId
:
string
)
:
string
;
projectId
string
nameOrId
string
string
get(view, gaxOpts)
get
(
view
?:
SchemaView
,
gaxOpts
?:
CallOptions
)
:
Promise<ISchema>
;
Get full information about the schema from the service.
view
SchemaView
The type of schema object requested, which should be an enum value from SchemaViews . Defaults to Full.
gaxOpts
CallOptions
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
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.
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 .
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
Promise
<void>
{Promise