UNPKG

11.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Routine = void 0;
4/*!
5 * Copyright 2020 Google LLC
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19const common_1 = require("@google-cloud/common");
20const promisify_1 = require("@google-cloud/promisify");
21const extend = require("extend");
22/**
23 * Routine objects are returned by methods such as
24 * {@link Dataset#routine}, {@link Dataset#createRoutine}, and
25 * {@link Dataset#getRoutines}.
26 *
27 * @class
28 * @param {Dataset} dataset {@link Dataset} instance.
29 * @param {string} id The ID of the routine.
30 *
31 * @example
32 * ```
33 * const {BigQuery} = require('@google-cloud/bigquery');
34 * const bigquery = new BigQuery();
35 * const dataset = bigquery.dataset('my-dataset');
36 *
37 * const routine = dataset.routine('my_routine');
38 * ```
39 */
40class Routine extends common_1.ServiceObject {
41 constructor(dataset, id) {
42 const methods = {
43 /**
44 * Create a routine.
45 *
46 * See {@link https://cloud.google.com/bigquery/docs/reference/rest/v2/routines/insert| Routines: insert API Documentation}
47 *
48 * @method Routine#create
49 * @param {object} config A [routine resource]{@link https://cloud.google.com/bigquery/docs/reference/rest/v2/routines#Routine}.
50 * @param {CreateRoutineCallback} [callback] The callback function.
51 * @returns {Promise<CreateRoutineResponse>}
52 *
53 * @example
54 * ```
55 * const {BigQuery} = require('@google-cloud/bigquery');
56 * const bigquery = new BigQuery();
57 * const dataset = bigquery.dataset('my-dataset');
58 * const routine = dataset.routine('my_routine');
59 *
60 * const config = {
61 * arguments: [{
62 * name: 'x',
63 * dataType: {
64 * typeKind: 'INT64'
65 * }
66 * }],
67 * definitionBody: 'x * 3',
68 * routineType: 'SCALAR_FUNCTION',
69 * returnType: {
70 * typeKind: 'INT64'
71 * }
72 * };
73 *
74 * routine.create(config, (err, routine, apiResponse) => {
75 * if (!err) {
76 * // The routine was created successfully.
77 * }
78 * });
79 *
80 * ```
81 * @example If the callback is omitted a Promise will be returned
82 * ```
83 * const [routine, apiResponse] = await routine.create(config);
84 * ```
85 */
86 create: true,
87 /**
88 * @callback DeleteRoutineCallback
89 * @param {?Error} err Request error, if any.
90 * @param {object} apiResponse The full API response.
91 */
92 /**
93 * @typedef {array} DeleteRoutineResponse
94 * @property {object} 0 The full API response.
95 */
96 /**
97 * Deletes a routine.
98 *
99 * See {@link https://cloud.google.com/bigquery/docs/reference/rest/v2/routines/delete| Routines: delete API Documentation}
100 *
101 * @method Routine#delete
102 * @param {DeleteRoutineCallback} [callback] The callback function.
103 * @returns {Promise<DeleteRoutineResponse>}
104 *
105 * @example
106 * ```
107 * const {BigQuery} = require('@google-cloud/bigquery');
108 * const bigquery = new BigQuery();
109 * const dataset = bigquery.dataset('my-dataset');
110 * const routine = dataset.routine('my_routine');
111 *
112 * routine.delete((err, apiResponse) => {
113 * if (!err) {
114 * // The routine was deleted successfully.
115 * }
116 * });
117 *
118 * ```
119 * @example If the callback is omitted a Promise will be returned
120 * ```
121 * const [apiResponse] = await routine.delete();
122 * ```
123 */
124 delete: true,
125 /**
126 * @callback RoutineExistsCallback
127 * @param {?Error} err Request error, if any.
128 * @param {boolean} exists Indicates if the routine exists.
129 */
130 /**
131 * @typedef {array} RoutineExistsResponse
132 * @property {boolean} 0 Indicates if the routine exists.
133 */
134 /**
135 * Check if the routine exists.
136 *
137 * @method Routine#exists
138 * @param {RoutineExistsCallback} [callback] The callback function.
139 * @returns {Promise<RoutineExistsResponse>}
140 *
141 * @example
142 * ```
143 * const {BigQuery} = require('@google-cloud/bigquery');
144 * const bigquery = new BigQuery();
145 * const dataset = bigquery.dataset('my-dataset');
146 * const routine = dataset.routine('my_routine');
147 *
148 * routine.exists((err, exists) => {});
149 *
150 * ```
151 * @example If the callback is omitted a Promise will be returned
152 * ```
153 * const [exists] = await routine.exists();
154 * ```
155 */
156 exists: true,
157 /**
158 * @callback GetRoutineCallback
159 * @param {?Error} err Request error, if any.
160 * @param {Routine} routine The routine.
161 * @param {object} apiResponse The full API response body.
162 */
163 /**
164 * @typedef {array} GetRoutineResponse
165 * @property {Routine} 0 The routine.
166 * @property {object} 1 The full API response body.
167 */
168 /**
169 * Get a routine if it exists.
170 *
171 * See {@link https://cloud.google.com/bigquery/docs/reference/rest/v2/routines/get| Routines: get API Documentation}
172 *
173 * @method Routine#get
174 * @param {GetRoutineCallback} [callback] The callback function.
175 * @returns {Promise<GetRoutineResponse>}
176 *
177 * @example
178 * ```
179 * const {BigQuery} = require('@google-cloud/bigquery');
180 * const bigquery = new BigQuery();
181 * const dataset = bigquery.dataset('my-dataset');
182 * const routine = dataset.routine('my_routine');
183 *
184 * routine.get((err, routine) => {});
185 *
186 * ```
187 * @example If the callback is omitted a Promise will be returned
188 * ```
189 * const [routine2] = await routine.get();
190 * ```
191 */
192 get: true,
193 /**
194 * @callback GetRoutineMetadataCallback
195 * @param {?Error} err Request error, if any.
196 * @param {object} metadata The routine metadata.
197 * @param {object} apiResponse The full API response.
198 */
199 /**
200 * @typedef {array} GetRoutineMetadataResponse
201 * @property {object} 0 The routine metadata.
202 * @property {object} 1 The full API response.
203 */
204 /**
205 * Get the metadata associated with a routine.
206 *
207 * See {@link https://cloud.google.com/bigquery/docs/reference/rest/v2/routines/get| Routines: get API Documentation}
208 *
209 * @method Routine#getMetadata
210 * @param {GetRoutineMetadataCallback} [callback] The callback function.
211 * @returns {Promise<GetRoutineMetadataResponse>}
212 *
213 * @example
214 * ```
215 * const {BigQuery} = require('@google-cloud/bigquery');
216 * const bigquery = new BigQuery();
217 * const dataset = bigquery.dataset('my-dataset');
218 * const routine = dataset.routine('my_routine');
219 *
220 * routine.getMetadata((err, metadata, apiResponse) => {});
221 *
222 * ```
223 * @example If the callback is omitted a Promise will be returned
224 * ```
225 * const [metadata, apiResponse] = await routine.getMetadata();
226 * ```
227 */
228 getMetadata: true,
229 /**
230 * @callback SetRoutineMetadataCallback
231 * @param {?Error} err Request error, if any.
232 * @param {object} metadata The routine metadata.
233 * @param {object} apiResponse The full API response.
234 */
235 /**
236 * @typedef {array} SetRoutineMetadataResponse
237 * @property {object} 0 The routine metadata.
238 * @property {object} 1 The full API response.
239 */
240 /**
241 * Update a routine.
242 *
243 * See {@link https://cloud.google.com/bigquery/docs/reference/rest/v2/routines/update| Routines: update API Documentation}
244 *
245 * @method Routine#setMetadata
246 * @param {object} metadata A [routine resource object]{@link https://cloud.google.com/bigquery/docs/reference/rest/v2/routines#Routine}.
247 * @param {SetRoutineMetadataCallback} [callback] The callback function.
248 * @returns {Promise<SetRoutineMetadataResponse>}
249 *
250 * @example
251 * ```
252 * const {BigQuery} = require('@google-cloud/bigquery');
253 * const bigquery = new BigQuery();
254 * const dataset = bigquery.dataset('my-dataset');
255 * const routine = dataset.routine('my_routine');
256 *
257 * const updates = {
258 * description: 'The perfect description!'
259 * };
260 *
261 * routine.setMetadata(updates, (err, metadata, apiResponse) => {});
262 *
263 * ```
264 * @example If the callback is omitted a Promise will be returned
265 * ```
266 * const [metadata, apiResponse] = await routine.setMetadata(updates);
267 * ```
268 */
269 setMetadata: {
270 reqOpts: {
271 method: 'PUT',
272 },
273 },
274 };
275 super({
276 parent: dataset,
277 baseUrl: '/routines',
278 id,
279 methods,
280 createMethod: dataset.createRoutine.bind(dataset),
281 });
282 }
283 setMetadata(metadata, callback) {
284 // per the python client, it would appear that in order to update a routine
285 // you need to send the routine in its entirety, not just the updated fields
286 this.getMetadata((err, fullMetadata) => {
287 if (err) {
288 callback(err);
289 return;
290 }
291 const updatedMetadata = extend(true, {}, fullMetadata, metadata);
292 super.setMetadata(updatedMetadata, callback);
293 });
294 }
295}
296exports.Routine = Routine;
297/*! Developer Documentation
298 *
299 * All async methods (except for streams) will return a Promise in the event
300 * that a callback is omitted.
301 */
302(0, promisify_1.promisifyAll)(Routine);
303//# sourceMappingURL=routine.js.map
\No newline at end of file