UNPKG

14.8 kBTypeScriptView Raw
1/*!
2 * Copyright 2014 Google Inc. All Rights Reserved.
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 */
16/// <reference types="node" />
17import * as common from '@google-cloud/common';
18import { ResourceStream } from '@google-cloud/paginator';
19import { BigQuery, Job, Dataset, Query, SimpleQueryRowsResponse, SimpleQueryRowsCallback, ResourceCallback, RequestCallback, PagedResponse, PagedCallback, JobRequest, PagedRequest } from '.';
20import { Duplex, Writable } from 'stream';
21import { JobMetadata } from './job';
22import bigquery from './types';
23import { IntegerTypeCastOptions } from './bigquery';
24export interface File {
25 bucket: any;
26 kmsKeyName?: string;
27 userProject?: string;
28 name: string;
29 generation?: number;
30}
31export declare type JobMetadataCallback = RequestCallback<JobMetadata>;
32export declare type JobMetadataResponse = [JobMetadata];
33export declare type RowMetadata = any;
34export declare type InsertRowsOptions = bigquery.ITableDataInsertAllRequest & {
35 createInsertId?: boolean;
36 partialRetries?: number;
37 raw?: boolean;
38 schema?: string | {};
39};
40export declare type InsertRowsResponse = [bigquery.ITableDataInsertAllResponse | bigquery.ITable];
41export declare type InsertRowsCallback = RequestCallback<bigquery.ITableDataInsertAllResponse | bigquery.ITable>;
42export declare type RowsResponse = PagedResponse<RowMetadata, GetRowsOptions, bigquery.ITableDataList | bigquery.ITable>;
43export declare type RowsCallback = PagedCallback<RowMetadata, GetRowsOptions, bigquery.ITableDataList | bigquery.ITable>;
44export interface InsertRow {
45 insertId?: string;
46 json?: bigquery.IJsonObject;
47}
48export declare type TableRow = bigquery.ITableRow;
49export declare type TableRowField = bigquery.ITableCell;
50export declare type TableRowValue = string | TableRow;
51export declare type GetRowsOptions = PagedRequest<bigquery.tabledata.IListParams> & {
52 wrapIntegers?: boolean | IntegerTypeCastOptions;
53};
54export declare type JobLoadMetadata = JobRequest<bigquery.IJobConfigurationLoad> & {
55 format?: string;
56};
57export declare type CreateExtractJobOptions = JobRequest<bigquery.IJobConfigurationExtract> & {
58 format?: 'CSV' | 'JSON' | 'AVRO' | 'PARQUET' | 'ORC';
59 gzip?: boolean;
60};
61export declare type JobResponse = [Job, bigquery.IJob];
62export declare type JobCallback = ResourceCallback<Job, bigquery.IJob>;
63export declare type CreateCopyJobMetadata = CopyTableMetadata;
64export declare type SetTableMetadataOptions = TableMetadata;
65export declare type CopyTableMetadata = JobRequest<bigquery.IJobConfigurationTableCopy>;
66export declare type TableMetadata = bigquery.ITable & {
67 name?: string;
68 schema?: string | TableField[] | TableSchema;
69 partitioning?: string;
70 view?: string | ViewDefinition;
71};
72export declare type ViewDefinition = bigquery.IViewDefinition;
73export declare type FormattedMetadata = bigquery.ITable;
74export declare type TableSchema = bigquery.ITableSchema;
75export declare type TableField = bigquery.ITableFieldSchema;
76export interface PartialInsertFailure {
77 message: string;
78 reason: string;
79 row: RowMetadata;
80}
81export declare type Policy = bigquery.IPolicy;
82export declare type GetPolicyOptions = bigquery.IGetPolicyOptions;
83export declare type SetPolicyOptions = Omit<bigquery.ISetIamPolicyRequest, 'policy'>;
84export declare type PolicyRequest = bigquery.IGetIamPolicyRequest;
85export declare type PolicyResponse = [Policy];
86export declare type PolicyCallback = RequestCallback<PolicyResponse>;
87export declare type PermissionsResponse = [bigquery.ITestIamPermissionsResponse];
88export declare type PermissionsCallback = RequestCallback<PermissionsResponse>;
89export interface TableOptions {
90 location?: string;
91}
92/**
93 * Table objects are returned by methods such as
94 * {@link Dataset#table}, {@link Dataset#createTable}, and
95 * {@link Dataset#getTables}.
96 *
97 * @class
98 * @param {Dataset} dataset {@link Dataset} instance.
99 * @param {string} id The ID of the table.
100 * @param {object} [options] Table options.
101 * @param {string} [options.location] The geographic location of the table, by
102 * default this value is inherited from the dataset. This can be used to
103 * configure the location of all jobs created through a table instance. It
104 * cannot be used to set the actual location of the table. This value will
105 * be superseded by any API responses containing location data for the
106 * table.
107 *
108 * @example
109 * const {BigQuery} = require('@google-cloud/bigquery');
110 * const bigquery = new BigQuery();
111 * const dataset = bigquery.dataset('my-dataset');
112 *
113 * const table = dataset.table('my-table');
114 */
115declare class Table extends common.ServiceObject {
116 dataset: Dataset;
117 bigQuery: BigQuery;
118 location?: string;
119 createReadStream: (options?: GetRowsOptions) => ResourceStream<RowMetadata>;
120 constructor(dataset: Dataset, id: string, options?: TableOptions);
121 /**
122 * Convert a comma-separated name:type string to a table schema object.
123 *
124 * @static
125 * @private
126 *
127 * @param {string} str Comma-separated schema string.
128 * @returns {object} Table schema in the format the API expects.
129 */
130 static createSchemaFromString_(str: string): TableSchema;
131 /**
132 * Convert a row entry from native types to their encoded types that the API
133 * expects.
134 *
135 * @static
136 * @private
137 *
138 * @param {*} value The value to be converted.
139 * @returns {*} The converted value.
140 */
141 static encodeValue_(value?: {} | null): {} | null;
142 /**
143 * @private
144 */
145 static formatMetadata_(options: TableMetadata): FormattedMetadata;
146 copy(destination: Table, metadata?: CopyTableMetadata): Promise<JobMetadataResponse>;
147 copy(destination: Table, metadata: CopyTableMetadata, callback: JobMetadataCallback): void;
148 copy(destination: Table, callback: JobMetadataCallback): void;
149 copyFrom(sourceTables: Table | Table[], metadata?: CopyTableMetadata): Promise<JobMetadataResponse>;
150 copyFrom(sourceTables: Table | Table[], metadata: CopyTableMetadata, callback: JobMetadataCallback): void;
151 copyFrom(sourceTables: Table | Table[], callback: JobMetadataCallback): void;
152 createCopyJob(destination: Table, metadata?: CreateCopyJobMetadata): Promise<JobResponse>;
153 createCopyJob(destination: Table, metadata: CreateCopyJobMetadata, callback: JobCallback): void;
154 createCopyJob(destination: Table, callback: JobCallback): void;
155 createCopyFromJob(source: Table | Table[], metadata?: CopyTableMetadata): Promise<JobResponse>;
156 createCopyFromJob(source: Table | Table[], metadata: CopyTableMetadata, callback: JobCallback): void;
157 createCopyFromJob(source: Table | Table[], callback: JobCallback): void;
158 createExtractJob(destination: File, options?: CreateExtractJobOptions): Promise<JobResponse>;
159 createExtractJob(destination: File, options: CreateExtractJobOptions, callback: JobCallback): void;
160 createExtractJob(destination: File, callback: JobCallback): void;
161 createLoadJob(source: string | File, metadata?: JobLoadMetadata): Promise<JobResponse>;
162 createLoadJob(source: string | File, metadata: JobLoadMetadata, callback: JobCallback): void;
163 createLoadJob(source: string | File, callback: JobCallback): void;
164 /**
165 * @param {string | File | File[]} source
166 * @param {JobLoadMetadata} metadata
167 * @returns {Promise<JobResponse>}
168 * @private
169 */
170 _createLoadJob(source: string | File | File[], metadata: JobLoadMetadata): Promise<JobResponse>;
171 createQueryJob(options: Query): Promise<JobResponse>;
172 createQueryJob(options: Query, callback: JobCallback): void;
173 /**
174 * Run a query scoped to your dataset as a readable object stream.
175 *
176 * See {@link BigQuery#createQueryStream} for full documentation of this
177 * method.
178 *
179 * @param {object} query See {@link BigQuery#createQueryStream} for full
180 * documentation of this method.
181 * @returns {stream} See {@link BigQuery#createQueryStream} for full
182 * documentation of this method.
183 */
184 createQueryStream(query: Query): Duplex;
185 /**
186 * Creates a write stream. Unlike the public version, this will not
187 * automatically poll the underlying job.
188 *
189 * @private
190 *
191 * @param {string|object} [metadata] Metadata to set with the load operation.
192 * The metadata object should be in the format of the
193 * [`configuration.load`](https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#JobConfigurationLoad)
194 * property of a Jobs resource. If a string is given, it will be used
195 * as the filetype.
196 * @param {string} [metadata.jobId] Custom job id.
197 * @param {string} [metadata.jobPrefix] Prefix to apply to the job id.
198 * @returns {WritableStream}
199 */
200 createWriteStream_(metadata: JobLoadMetadata | string): Writable;
201 /**
202 * Load data into your table from a readable stream of AVRO, CSV, JSON, ORC,
203 * or PARQUET data.
204 *
205 * @see [Jobs: insert API Documentation]{@link https://cloud.google.com/bigquery/docs/reference/v2/jobs/insert}
206 *
207 * @param {string|object} [metadata] Metadata to set with the load operation.
208 * The metadata object should be in the format of the
209 * [`configuration.load`](https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#JobConfigurationLoad)
210 * property of a Jobs resource. If a string is given,
211 * it will be used as the filetype.
212 * @param {string} [metadata.jobId] Custom job id.
213 * @param {string} [metadata.jobPrefix] Prefix to apply to the job id.
214 * @returns {WritableStream}
215 *
216 * @throws {Error} If source format isn't recognized.
217 *
218 * @example
219 * const {BigQuery} = require('@google-cloud/bigquery');
220 * const bigquery = new BigQuery();
221 * const dataset = bigquery.dataset('my-dataset');
222 * const table = dataset.table('my-table');
223 *
224 * //-
225 * // Load data from a CSV file.
226 * //-
227 * const request = require('request');
228 *
229 * const csvUrl = 'http://goo.gl/kSE7z6';
230 *
231 * const metadata = {
232 * allowJaggedRows: true,
233 * skipLeadingRows: 1
234 * };
235 *
236 * request.get(csvUrl)
237 * .pipe(table.createWriteStream(metadata))
238 * .on('job', (job) => {
239 * // `job` is a Job object that can be used to check the status of the
240 * // request.
241 * })
242 * .on('complete', (job) => {
243 * // The job has completed successfully.
244 * });
245 *
246 * //-
247 * // Load data from a JSON file.
248 * //-
249 * const fs = require('fs');
250 *
251 * fs.createReadStream('./test/testdata/testfile.json')
252 * .pipe(table.createWriteStream('json'))
253 * .on('job', (job) => {
254 * // `job` is a Job object that can be used to check the status of the
255 * // request.
256 * })
257 * .on('complete', (job) => {
258 * // The job has completed successfully.
259 * });
260 */
261 createWriteStream(metadata: JobLoadMetadata | string): Writable;
262 extract(destination: File, options?: CreateExtractJobOptions): Promise<JobMetadataResponse>;
263 extract(destination: File, options: CreateExtractJobOptions, callback?: JobMetadataCallback): void;
264 extract(destination: File, callback?: JobMetadataCallback): void;
265 getRows(options?: GetRowsOptions): Promise<RowsResponse>;
266 getRows(options: GetRowsOptions, callback: RowsCallback): void;
267 getRows(callback: RowsCallback): void;
268 insert(rows: RowMetadata | RowMetadata[], options?: InsertRowsOptions): Promise<InsertRowsResponse>;
269 insert(rows: RowMetadata | RowMetadata[], options: InsertRowsOptions, callback: InsertRowsCallback): void;
270 insert(rows: RowMetadata | RowMetadata[], callback: InsertRowsCallback): void;
271 /**
272 * Insert rows with retries, but will create the table if not exists.
273 *
274 * @param {RowMetadata | RowMetadata[]} rows
275 * @param {InsertRowsOptions} options
276 * @returns {Promise<bigquery.ITableDataInsertAllResponse | bigquery.ITable>}
277 * @private
278 */
279 private _insertAndCreateTable;
280 /**
281 * This method will attempt to insert rows while retrying any partial failures
282 * that occur along the way. Because partial insert failures are returned
283 * differently, we can't depend on our usual retry strategy.
284 *
285 * @private
286 *
287 * @param {RowMetadata|RowMetadata[]} rows The rows to insert.
288 * @param {InsertRowsOptions} options Insert options.
289 * @returns {Promise<bigquery.ITableDataInsertAllResponse>}
290 */
291 private _insertWithRetry;
292 /**
293 * This method does the bulk of the work for processing options and making the
294 * network request.
295 *
296 * @private
297 *
298 * @param {RowMetadata|RowMetadata[]} rows The rows to insert.
299 * @param {InsertRowsOptions} options Insert options.
300 * @returns {Promise<bigquery.ITableDataInsertAllResponse>}
301 */
302 private _insert;
303 load(source: string | File, metadata?: JobLoadMetadata): Promise<JobMetadataResponse>;
304 load(source: string | File, metadata: JobLoadMetadata, callback: JobMetadataCallback): void;
305 load(source: string | File, callback: JobMetadataCallback): void;
306 query(query: Query): Promise<SimpleQueryRowsResponse>;
307 query(query: string): Promise<SimpleQueryRowsResponse>;
308 query(query: Query, callback: SimpleQueryRowsCallback): void;
309 setMetadata(metadata: SetTableMetadataOptions): Promise<common.SetMetadataResponse>;
310 setMetadata(metadata: SetTableMetadataOptions, callback: common.ResponseCallback): void;
311 getIamPolicy(optionsOrCallback?: GetPolicyOptions | PolicyCallback): Promise<PolicyResponse>;
312 getIamPolicy(options: GetPolicyOptions, callback: PolicyCallback): void;
313 setIamPolicy(policy: Policy, options?: SetPolicyOptions): Promise<PolicyResponse>;
314 setIamPolicy(policy: Policy, options: SetPolicyOptions, callback: PolicyCallback): void;
315 setIamPolicy(policy: Policy, callback: PolicyCallback): void;
316 testIamPermissions(permissions: string | string[]): Promise<PermissionsResponse>;
317 testIamPermissions(permissions: string | string[], callback: PermissionsCallback): void;
318}
319/**
320 * Reference to the {@link Table} class.
321 * @name module:@google-cloud/bigquery.Table
322 * @see Table
323 */
324export { Table };