UNPKG

6.84 kBJavaScriptView Raw
1"use strict";
2/*!
3 * Copyright 2017 Google Inc. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.Snapshot = void 0;
19const promisify_1 = require("@google-cloud/promisify");
20const subscription_1 = require("./subscription");
21/**
22 * A Snapshot object will give you access to your Cloud Pub/Sub snapshot.
23 *
24 * Snapshots are sometimes retrieved when using various methods:
25 *
26 * - {@link PubSub#getSnapshots}
27 * - {@link PubSub#getSnapshotsStream}
28 * - {@link PubSub#snapshot}
29 *
30 * Snapshots may be created with:
31 *
32 * - {@link Subscription#createSnapshot}
33 *
34 * You can use snapshots to seek a subscription to a specific point in time.
35 *
36 * - {@link Subscription#seek}
37 *
38 * @class
39 *
40 * @example
41 * //-
42 * // From {@link PubSub#getSnapshots}:
43 * //-
44 * pubsub.getSnapshots((err, snapshots) => {
45 * // `snapshots` is an array of Snapshot objects.
46 * });
47 *
48 * //-
49 * // From {@link PubSub#getSnapshotsStream}:
50 * //-
51 * pubsub.getSnapshotsStream()
52 * .on('error', console.error)
53 * .on('data', (snapshot) => {
54 * // `snapshot` is a Snapshot object.
55 * });
56 *
57 * //-
58 * // From {@link PubSub#snapshot}:
59 * //-
60 * const snapshot = pubsub.snapshot('my-snapshot');
61 * // snapshot is a Snapshot object.
62 *
63 * //-
64 * // Create a snapshot with {module:pubsub/subscription#createSnapshot}:
65 * //-
66 * const subscription = pubsub.subscription('my-subscription');
67 *
68 * subscription.createSnapshot('my-snapshot', (err, snapshot) => {
69 * if (!err) {
70 * // `snapshot` is a Snapshot object.
71 * }
72 * });
73 *
74 * //-
75 * // Seek to your snapshot:
76 * //-
77 * const subscription = pubsub.subscription('my-subscription');
78 *
79 * subscription.seek('my-snapshot', (err) => {
80 * if (err) {
81 * // Error handling omitted.
82 * }
83 * });
84 */
85class Snapshot {
86 constructor(parent, name) {
87 this.parent = parent;
88 this.name = Snapshot.formatName_(parent.projectId, name);
89 }
90 /**
91 * Delete the snapshot.
92 *
93 * @param {function} [callback] The callback function.
94 * @param {?error} callback.err An error returned while making this
95 * request.
96 * @param {object} callback.apiResponse The full API response from the
97 * service.
98 *
99 * @example
100 * snapshot.delete((err, apiResponse) => {});
101 *
102 * //-
103 * // If the callback is omitted, we'll return a Promise.
104 * //-
105 * snapshot.delete().then((data) => {
106 * const apiResponse = data[0];
107 * });
108 */
109 delete(callback) {
110 const reqOpts = {
111 snapshot: this.name,
112 };
113 this.parent.request({
114 client: 'SubscriberClient',
115 method: 'deleteSnapshot',
116 reqOpts,
117 }, callback);
118 }
119 /*@
120 * Format the name of a snapshot. A snapshot's full name is in the format of
121 * projects/{projectId}/snapshots/{snapshotName}
122 *
123 * @private
124 */
125 static formatName_(projectId, name) {
126 return 'projects/' + projectId + '/snapshots/' + name.split('/').pop();
127 }
128 /**
129 * Create a snapshot with the given name.
130 *
131 * **This is only available if you accessed this object through
132 * {@link Subscription#snapshot}.**
133 *
134 * @method Snapshot#create
135 * @param {string} name Name of the snapshot.
136 * @param {function} [callback] The callback function.
137 * @param {?error} callback.err An error from the API call, may be null.
138 * @param {Snapshot} callback.snapshot The newly created
139 * snapshot.
140 * @param {object} callback.apiResponse The full API response from the
141 * service.
142 *
143 * @example
144 * const subscription = pubsub.subscription('my-subscription');
145 * const snapshot = subscription.snapshot('my-snapshot');
146 *
147 * const callback = (err, snapshot, apiResponse) => {
148 * if (!err) {
149 * // The snapshot was created successfully.
150 * }
151 * };
152 *
153 * snapshot.create('my-snapshot', callback);
154 *
155 * //-
156 * // If the callback is omitted, we'll return a Promise.
157 * //-
158 * snapshot.create('my-snapshot').then((data) => {
159 * const snapshot = data[0];
160 * const apiResponse = data[1];
161 * });
162 */
163 create(optsOrCallback, callback) {
164 if (!(this.parent instanceof subscription_1.Subscription)) {
165 throw new Error('This is only available if you accessed this object through Subscription#snapshot');
166 }
167 const options = typeof optsOrCallback === 'object' ? optsOrCallback : {};
168 callback = typeof optsOrCallback === 'function' ? optsOrCallback : callback;
169 return this.parent.createSnapshot(this.name, options, (err, snapshot, resp) => {
170 if (err) {
171 callback(err, null, resp);
172 return;
173 }
174 Object.assign(this, snapshot);
175 callback(null, this, resp);
176 });
177 }
178 /**
179 * Seeks an existing subscription to the snapshot.
180 *
181 * **This is only available if you accessed this object through
182 * {@link Subscription#snapshot}.**
183 *
184 * @method Snapshot#seek
185 * @param {function} callback The callback function.
186 * @param {?error} callback.err An error from the API call, may be null.
187 * @param {object} callback.apiResponse The full API response from the
188 * service.
189 *
190 * @example
191 * const subscription = pubsub.subscription('my-subscription');
192 * const snapshot = subscription.snapshot('my-snapshot');
193 *
194 * snapshot.seek((err, apiResponse) => {});
195 *
196 * //-
197 * // If the callback is omitted, we'll return a Promise.
198 * //-
199 * snapshot.seek().then((data) => {
200 * const apiResponse = data[0];
201 * });
202 */
203 seek(gaxOpts, callback) {
204 if (!(this.parent instanceof subscription_1.Subscription)) {
205 throw new Error('This is only available if you accessed this object through Subscription#snapshot');
206 }
207 return this.parent.seek(this.name, gaxOpts, callback);
208 }
209}
210exports.Snapshot = Snapshot;
211/*! Developer Documentation
212 *
213 * All async methods (except for streams) will return a Promise in the event
214 * that a callback is omitted.
215 */
216promisify_1.promisifyAll(Snapshot);
217//# sourceMappingURL=snapshot.js.map
\No newline at end of file