1 | ;
|
2 | // Copyright 2021 Google LLC
|
3 | //
|
4 | // Licensed under the Apache License, Version 2.0 (the "License");
|
5 | // you may not use this file except in compliance with the License.
|
6 | // You may obtain a copy of the License at
|
7 | //
|
8 | // http://www.apache.org/licenses/LICENSE-2.0
|
9 | //
|
10 | // Unless required by applicable law or agreed to in writing, software
|
11 | // distributed under the License is distributed on an "AS IS" BASIS,
|
12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | // See the License for the specific language governing permissions and
|
14 | // limitations under the License.
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | exports.Encodings = exports.SchemaViews = exports.SchemaTypes = exports.Schema = void 0;
|
17 | // Unlike the earlier classes, this one does not do its own gax access.
|
18 | // Rather, it calls back through the schemaClient instance PubSub holds.
|
19 | // This class is a very lightweight syntactic wrapper around the GAPIC client.
|
20 | /**
|
21 | * A Schema object allows you to interact with a Cloud Pub/Sub schema.
|
22 | *
|
23 | * This should only be instantiated by the PubSub class. To obtain an
|
24 | * instance for end user usage, call pubsub.schema().
|
25 | *
|
26 | * @class
|
27 | * @param {PubSub} pubsub The PubSub object creating this object.
|
28 | * @param {id} id name or ID of the schema.
|
29 | *
|
30 | * @example Creating an instance of this class.
|
31 | * ```
|
32 | * const {PubSub} = require('@google-cloud/pubsub');
|
33 | * const pubsub = new PubSub();
|
34 | *
|
35 | * const schema = pubsub.schema('my-schema');
|
36 | *
|
37 | * ```
|
38 | * @example Getting the details of a schema. Note that Schema methods do not provide a callback interface. Use .then() or await.
|
39 | * ```
|
40 | * const {PubSub} = require('@google-cloud/pubsub');
|
41 | * const pubsub = new PubSub();
|
42 | *
|
43 | * const schema = pubsub.schema('my-schema');
|
44 | * schema.get(SchemaViews.Basic).then(console.log);
|
45 | * ```
|
46 | */
|
47 | class Schema {
|
48 | constructor(pubsub, idOrName) {
|
49 | /**
|
50 | * The parent {@link PubSub} instance of this topic instance.
|
51 | * @name Schema#pubsub
|
52 | * @type {PubSub}
|
53 | */
|
54 | this.pubsub = pubsub;
|
55 | /**
|
56 | * The fully qualified name of this schema. We will qualify this if
|
57 | * it's only an ID passed (assuming the parent project). Unfortunately,
|
58 | * we might not be able to do that if our pubsub's client hasn't been
|
59 | * initialized. In that case, we just set the id and get the name later.
|
60 | * @name Schema#id
|
61 | * @type {string}
|
62 | */
|
63 | this.id =
|
64 | idOrName.indexOf('/') >= 0
|
65 | ? idOrName.substr(idOrName.lastIndexOf('/') + 1)
|
66 | : idOrName;
|
67 | }
|
68 | /**
|
69 | * Return the fully qualified name of this schema.
|
70 | *
|
71 | * Note that we have to verify that we have a projectId before returning this,
|
72 | * so we have to check that first.
|
73 | *
|
74 | * @return {Promise<string>} a Promise that resolves to the full schema name
|
75 | */
|
76 | async getName() {
|
77 | if (!this.name_) {
|
78 | if (!this.pubsub.isIdResolved) {
|
79 | await this.pubsub.getClientConfig();
|
80 | }
|
81 | this.name_ = Schema.formatName_(this.pubsub.projectId, this.id);
|
82 | }
|
83 | return this.name_;
|
84 | }
|
85 | /**
|
86 | * Create a schema.
|
87 | *
|
88 | * @see [Schemas: create API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.schemas/create}
|
89 | *
|
90 | * @throws {Error} if the schema type is incorrect.
|
91 | * @throws {Error} if the definition is invalid.
|
92 | *
|
93 | * @param {SchemaType} type The type of the schema (Protobuf, Avro, etc).
|
94 | * @param {string} definition The text describing the schema in terms of the type.
|
95 | * @param {object} [options] Request configuration options, outlined
|
96 | * here: https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html.
|
97 | * @returns {Promise<void>}
|
98 | *
|
99 | * @example Create a schema.
|
100 | * ```
|
101 | * const {PubSub} = require('@google-cloud/pubsub');
|
102 | * const pubsub = new PubSub();
|
103 | *
|
104 | * const schema = pubsub.schema('messageType');
|
105 | * await schema.create(
|
106 | * SchemaTypes.Avro,
|
107 | * '{...avro definition...}'
|
108 | * );
|
109 | * ```
|
110 | */
|
111 | async create(type, definition, gaxOpts) {
|
112 | const name = await this.getName();
|
113 | await this.pubsub.createSchema(name, type, definition, gaxOpts);
|
114 | }
|
115 | /**
|
116 | * Get full information about the schema from the service.
|
117 | *
|
118 | * @see [Schemas: getSchema API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.schemas/get}
|
119 | *
|
120 | * @param {google.pubsub.v1.SchemaView} [view] The type of schema object
|
121 | * requested, which should be an enum value from {@link SchemaViews}. Defaults
|
122 | * to Full.
|
123 | * @param {object} [options] Request configuration options, outlined
|
124 | * here: https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html.
|
125 | * @returns {Promise<ISchema>}
|
126 | */
|
127 | async get(view = exports.SchemaViews.Full, gaxOpts) {
|
128 | const client = await this.pubsub.getSchemaClient_();
|
129 | const name = await this.getName();
|
130 | const [schema] = await client.getSchema({
|
131 | name,
|
132 | view,
|
133 | }, gaxOpts);
|
134 | return schema;
|
135 | }
|
136 | /**
|
137 | * Delete the schema from the project.
|
138 | *
|
139 | * @see [Schemas: deleteSchema API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.schemas/delete}
|
140 | *
|
141 | * @param {object} [options] Request configuration options, outlined
|
142 | * here: https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html.
|
143 | * @returns {Promise<void>}
|
144 | */
|
145 | async delete(gaxOpts) {
|
146 | const client = await this.pubsub.getSchemaClient_();
|
147 | const name = await this.getName();
|
148 | await client.deleteSchema({
|
149 | name,
|
150 | }, gaxOpts);
|
151 | }
|
152 | /**
|
153 | * Validate a message against this schema's definition.
|
154 | *
|
155 | * If you would like to validate a message against an arbitrary schema, please
|
156 | * use the {@link SchemaServiceClient} GAPIC class directly, using your
|
157 | * {@link PubSub} instance's configuration, via {@link PubSub#getClientConfig}.
|
158 | *
|
159 | * @see [Schemas: validateMessage API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.schemas/validateMessage}
|
160 | *
|
161 | * @throws {Error} if the validation fails.
|
162 | * @throws {Error} if other parameters are invalid.
|
163 | *
|
164 | * @param {string} message The message to validate.
|
165 | * @param {Encoding | "JSON" | "BINARY"} encoding The encoding of the message to validate.
|
166 | * @param {object} [options] Request configuration options, outlined
|
167 | * here: https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html.
|
168 | * @returns {Promise<void>}
|
169 | */
|
170 | async validateMessage(message, encoding, gaxOpts) {
|
171 | const client = await this.pubsub.getSchemaClient_();
|
172 | const name = await this.getName();
|
173 | await client.validateMessage({
|
174 | parent: this.pubsub.name,
|
175 | name,
|
176 | message,
|
177 | encoding,
|
178 | }, gaxOpts);
|
179 | }
|
180 | /*!
|
181 | * Format the name of a schema. A schema's full name is in the
|
182 | * format of projects/{projectId}/schemas/{schemaName}.
|
183 | *
|
184 | * The GAPIC client should do this for us, but since we maintain
|
185 | * names rather than IDs, this is simpler.
|
186 | *
|
187 | * @private
|
188 | */
|
189 | static formatName_(projectId, nameOrId) {
|
190 | if (typeof nameOrId !== 'string') {
|
191 | throw new Error('A name is required to identify a schema.');
|
192 | }
|
193 | // Simple check if the name is already formatted.
|
194 | if (nameOrId.indexOf('/') > -1) {
|
195 | return nameOrId;
|
196 | }
|
197 | return `projects/${projectId}/schemas/${nameOrId}`;
|
198 | }
|
199 | /**
|
200 | * Translates the schema attributes in messages delivered from Pub/Sub.
|
201 | * All resulting fields may end up being blank.
|
202 | */
|
203 | static metadataFromMessage(attributes) {
|
204 | return {
|
205 | name: attributes['googclient_schemaname'],
|
206 | encoding: attributes['googclient_schemaencoding'],
|
207 | };
|
208 | }
|
209 | }
|
210 | exports.Schema = Schema;
|
211 | // Also export these for JavaScript compatible usage.
|
212 | exports.SchemaTypes = {
|
213 | ProtocolBuffer: 'PROTOCOL_BUFFER',
|
214 | Avro: 'AVRO',
|
215 | };
|
216 | exports.SchemaViews = {
|
217 | Basic: 'BASIC',
|
218 | Full: 'FULL',
|
219 | };
|
220 | // These are not schema-specific, but this seems to be the
|
221 | // only place that exports methods that need them.
|
222 | exports.Encodings = {
|
223 | Json: 'JSON',
|
224 | Binary: 'BINARY',
|
225 | };
|
226 | //# sourceMappingURL=schema.js.map |
\ | No newline at end of file |