Skip to content

refactor(ts): enable noImplicitAny on test/transaction.ts #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,13 @@ class DatastoreRequest {
* });
*/
allocateIds(key: entity.Key, options: AllocateIdsOptions|number):
Promise<google.datastore.v1.AllocateIdsResponse>;
Promise<AllocateIdsResponse>;
allocateIds(
key: entity.Key, options: AllocateIdsOptions|number,
callback: AllocateIdsCallback): void;
allocateIds(
key: entity.Key, options: AllocateIdsOptions|number,
callback?: AllocateIdsCallback):
void|Promise<google.datastore.v1.AllocateIdsResponse> {
callback?: AllocateIdsCallback): void|Promise<AllocateIdsResponse> {
if (entity.isKeyComplete(key)) {
throw new Error('An incomplete key should be provided.');
}
Expand Down Expand Up @@ -258,7 +257,7 @@ class DatastoreRequest {
reqOpts,
gaxOpts: options.gaxOptions,
},
(err: Error, resp: Entity) => {
(err, resp) => {
if (err) {
stream.destroy(err);
return;
Expand Down Expand Up @@ -339,20 +338,14 @@ class DatastoreRequest {
* const apiResponse = data[0];
* });
*/
delete(keys: Entities):
void|Promise<google.datastore.v1.Datastore.CommitCallback>;
delete(
keys: Entities,
callback: google.datastore.v1.Datastore.CommitCallback): void;
delete(
keys: Entities, gaxOptions: CallOptions,
callback: google.datastore.v1.Datastore.CommitCallback): void;
delete(): Promise<CommitResponse>;
delete(keys: Entities): void;
delete(keys: Entities, callback: CommitCallback): void;
delete(keys: Entities, gaxOptions: CallOptions, callback: CommitCallback):
void;
delete(
keys: Entities,
gaxOptionsOrCallback?: CallOptions|
google.datastore.v1.Datastore.CommitCallback,
cb?: google.datastore.v1.Datastore.CommitCallback):
void|Promise<google.datastore.v1.Datastore.CommitCallback> {
keys?: Entities, gaxOptionsOrCallback?: CallOptions|CommitCallback,
cb?: CommitCallback): void|Promise<CommitResponse> {
const gaxOptions =
typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {};
const callback =
Expand Down Expand Up @@ -501,10 +494,10 @@ class DatastoreRequest {
* @param {?error} callback.err An error returned while making this request
* @param {object} callback.apiResponse The full API response.
*/
insert(entities: Entities): Promise<google.datastore.v1.ICommitResponse>;
insert(entities: Entities): Promise<CommitResponse>;
insert(entities: Entities, callback: CallOptions): void;
insert(entities: Entities, callback?: CallOptions):
void|Promise<google.datastore.v1.ICommitResponse> {
void|Promise<CommitResponse> {
entities =
arrify(entities).map(DatastoreRequest.prepareEntityObject_).map(x => {
x.method = 'insert';
Expand Down Expand Up @@ -693,7 +686,7 @@ class DatastoreRequest {
onResultSet);
};

function onResultSet(err: Error, resp: Entity) {
function onResultSet(err?: Error|null, resp?: Entity) {
if (err) {
stream.destroy(err);
return;
Expand Down Expand Up @@ -955,14 +948,14 @@ class DatastoreRequest {
* const apiResponse = data[0];
* });
*/
save(entities: Entities, gaxOptions?: CallOptions):
Promise<google.datastore.v1.ICommitResponse>;
save(entities: Entities): Promise<CommitResponse>;
save(entities: Entities, gaxOptions?: CallOptions): Promise<CommitResponse>;
save(entities: Entities, gaxOptions: CallOptions, callback: SaveCallback):
void;
save(entities: Entities, callback: SaveCallback): void;
save(
entities: Entities, gaxOptionsOrCallback?: CallOptions|SaveCallback,
cb?: SaveCallback): void|Promise<google.datastore.v1.ICommitResponse> {
cb?: SaveCallback): void|Promise<CommitResponse> {
entities = arrify(entities);
const gaxOptions =
typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {};
Expand Down Expand Up @@ -1040,7 +1033,7 @@ class DatastoreRequest {
mutations,
};

function onCommit(err: Error|null, resp: {mutationResults: Entity;}) {
function onCommit(err?: Error|null, resp?: {mutationResults: Entity;}) {
if (err || !resp) {
callback(err, resp);
return;
Expand Down Expand Up @@ -1090,10 +1083,10 @@ class DatastoreRequest {
* @param {?error} callback.err An error returned while making this request
* @param {object} callback.apiResponse The full API response.
*/
update(entities: Entities): Promise<google.datastore.v1.ICommitResponse>;
update(entities: Entities): Promise<CommitResponse>;
update(entities: Entities, callback: CallOptions): void;
update(entities: Entities, callback?: CallOptions):
void|Promise<google.datastore.v1.ICommitResponse> {
void|Promise<CommitResponse> {
entities =
arrify(entities).map(DatastoreRequest.prepareEntityObject_).map(x => {
x.method = 'update';
Expand All @@ -1117,10 +1110,10 @@ class DatastoreRequest {
* @param {?error} callback.err An error returned while making this request
* @param {object} callback.apiResponse The full API response.
*/
upsert(entities: Entities): Promise<google.datastore.v1.ICommitResponse>;
upsert(entities: Entities): Promise<CommitResponse>;
upsert(entities: Entities, callback: CallOptions): void;
upsert(entities: Entities, callback?: CallOptions):
void|Promise<google.datastore.v1.ICommitResponse> {
void|Promise<CommitResponse> {
entities =
arrify(entities).map(DatastoreRequest.prepareEntityObject_).map(x => {
x.method = 'upsert';
Expand Down Expand Up @@ -1211,6 +1204,7 @@ export interface BooleanObject {
export interface ConsistencyProtoCode {
[key: string]: number;
}
export type CommitResponse = [google.datastore.v1.ICommitResponse];
export type Entities = Entity|Entity[];
export interface EntityProtoObject {
method?: string;
Expand All @@ -1230,6 +1224,7 @@ export interface AllocateIdsRequestResponse {
keys: KeyProto[];
mutationResults?: Entities;
}
export type AllocateIdsResponse = [google.datastore.v1.AllocateIdsResponse];
export interface AllocateIdsCallback {
(a: Error|null, b: entity.Key[]|null, c: AllocateIdsRequestResponse): void;
}
Expand All @@ -1241,6 +1236,9 @@ export interface CreateReadStreamOptions {
consistency?: string;
gaxOptions?: CallOptions;
}
export interface CommitCallback {
(err?: Error|null, resp?: google.datastore.v1.CommitResponse): void;
}
export interface GetCallback {
(...args: Entity[]): void;
}
Expand All @@ -1259,7 +1257,7 @@ export interface PrepareEntityObjectResponse {
}
export type ProjectId = string|null|undefined;
export interface RequestCallback {
(a: Error,
(a?: Error|null,
b?: AllocateIdsRequestResponse&google.datastore.v1.ILookupResponse&
Entities): void;
}
Expand All @@ -1269,13 +1267,16 @@ export interface RequestConfig {
method: string;
prepared?: boolean;
reqOpts?: Entity|RequestOptions;
gaxOptions?: never;
}
export interface RequestOptions {
mutations?: []|Array<{delete: KeyProto;}>|Array<{}>;
keys?: Entity;
readOptions?: {readConsistency?: number
transaction?: string|number;
};
transactionOptions?:
{readOnly?: {}; readWrite?: {previousTransaction?: string;};};
transaction?: string|number;
mode?: string;
projectId?: string;
Expand Down
62 changes: 30 additions & 32 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
import {promisifyAll} from '@google-cloud/promisify';
import * as arrify from 'arrify';
import {CallOptions} from 'google-gax';

import {google} from '../proto/datastore';

import {Datastore, TransactionOptions} from '.';
import {entity, Entity} from './entity';
import {Query} from './query';
import {DatastoreRequest} from './request';

type RollbackCallback = google.datastore.v1.Datastore.RollbackCallback;
type RollbackResponse = google.datastore.v1.RollbackResponse;
import {CommitResponse, DatastoreRequest, RequestOptions} from './request';

/**
* A transaction is a set of Datastore operations on one or more entities. Each
Expand Down Expand Up @@ -130,16 +129,12 @@ class Transaction extends DatastoreRequest {
* const apiResponse = data[0];
* });
*/
commit(gaxOptions?: CallOptions): Promise<google.datastore.v1.CommitResponse>;
commit(callback: google.datastore.v1.Datastore.CommitCallback): void;
commit(
gaxOptions: CallOptions,
callback: google.datastore.v1.Datastore.CommitCallback): void;
commit(gaxOptions?: CallOptions): Promise<CommitResponse>;
commit(callback: CommitCallback): void;
commit(gaxOptions: CallOptions, callback: CommitCallback): void;
commit(
gaxOptionsOrCallback?: CallOptions|
google.datastore.v1.Datastore.CommitCallback,
cb?: google.datastore.v1.Datastore.CommitCallback):
void|Promise<google.datastore.v1.CommitResponse> {
gaxOptionsOrCallback?: CallOptions|CommitCallback,
cb?: CommitCallback): void|Promise<CommitResponse> {
const callback = typeof gaxOptionsOrCallback === 'function' ?
gaxOptionsOrCallback :
typeof cb === 'function' ? cb : (() => {});
Expand Down Expand Up @@ -332,7 +327,9 @@ class Transaction extends DatastoreRequest {
* });
* });
*/
delete(entities: Entity): void {
delete(): Promise<CommitResponse>;
delete(entities: Entities): void;
delete(entities?: Entities): void|Promise<CommitResponse> {
arrify(entities).forEach((ent: Entity) => {
this.modifiedEntities_.push({
entity: {
Expand Down Expand Up @@ -378,12 +375,12 @@ class Transaction extends DatastoreRequest {
* const apiResponse = data[0];
* });
*/
rollback(gaxOptions?: CallOptions): Promise<RollbackResponse>;
rollback(): void;
rollback(callback: RollbackCallback): void;
rollback(gaxOptions: CallOptions): Promise<RollbackResponse>;
rollback(gaxOptions: CallOptions, callback: RollbackCallback): void;
rollback(
gaxOptionsOrCallback?: CallOptions|RollbackCallback,
cb?: RollbackCallback): void|Promise<RollbackResponse> {
rollback(gaxOptionsOrCallback?: CallOptions|RollbackCallback, cb?: Function):
void|Promise<RollbackResponse> {
const gaxOptions =
typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {};
const callback =
Expand Down Expand Up @@ -451,28 +448,26 @@ class Transaction extends DatastoreRequest {
* const apiResponse = data[1];
* });
*/
run(options?: RunOptions):
Promise<google.datastore.v1.BeginTransactionResponse>;
run(options?: RunOptions): Promise<BeginTransactionResponse>;
run(callback?: RunCallback): void;
run(options?: RunOptions, callback?: RunCallback): void;
run(optionsOrCallback?: RunOptions|RunCallback|Entity, cb?: RunCallback):
void|Promise<google.datastore.v1.BeginTransactionResponse> {
run(optionsOrCallback?: RunOptions|RunCallback|Entity,
cb?: RunCallback): void|Promise<BeginTransactionResponse> {
const options =
typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
const callback =
typeof optionsOrCallback === 'function' ? optionsOrCallback : cb!;

// tslint:disable-next-line no-any
const reqOpts: any = {
const reqOpts = {
transactionOptions: {},
};
} as RequestOptions;

if (options.readOnly || this.readOnly) {
reqOpts.transactionOptions.readOnly = {};
reqOpts.transactionOptions!.readOnly = {};
}

if (options.transactionId || this.id) {
reqOpts.transactionOptions.readWrite = {
reqOpts.transactionOptions!.readWrite = {
previousTransaction: options.transactionId || this.id
};
}
Expand Down Expand Up @@ -644,18 +639,21 @@ class Transaction extends DatastoreRequest {
export type Entities = Entity|Entity[];
export type ModifiedEntities =
Array<{entity: {key: Entity}; method: string; args: Entity[];}>;
export interface RunCallback extends
google.datastore.v1.Datastore.BeginTransactionCallback {
(transaction: Transaction|null): void;
export type CommitCallback = google.datastore.v1.Datastore.CommitCallback;
export type BeginTransactionResponse =
[google.datastore.v1.BeginTransactionResponse];
export interface RunCallback {
(error: Error|null, transaction: Transaction|null,
response?: google.datastore.v1.BeginTransactionResponse): void;
}

export type RollbackCallback = google.datastore.v1.Datastore.RollbackCallback;
export type RollbackResponse = [google.datastore.v1.RollbackResponse];
export interface RunOptions {
readOnly?: boolean;
transactionId?: string;
transactionOptions?: TransactionOptions;
gaxOptions?: CallOptions;
}

/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
Expand Down
Loading