Skip to content

Commit

Permalink
refactor(ts): use es module imports (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Nov 12, 2018
1 parent 34d6b75 commit 43381cf
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 572 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"system-test": "mocha build/system-test --timeout 600000",
"fix": "eslint 'samples/*.js' 'samples/**/*.js' --fix",
"clean": "gts clean",
"compile": "tsc -p . && cp -r src/v1/ build/src/v1/ && cp -r protos build/",
"compile": "tsc -p . && cp -r src/v1/ build/src/v1/ && cp -r protos build/ && cp test/*.js build/test",
"prepare": "npm run compile",
"pretest": "npm run compile"
},
Expand All @@ -53,7 +53,6 @@
"google-auth-library": "^2.0.0",
"google-gax": "^0.20.0",
"is": "^3.2.1",
"lodash.flatten": "^4.4.0",
"lodash.merge": "^4.6.1",
"split-array-stream": "^2.0.0",
"stream-events": "^1.0.4",
Expand Down
7 changes: 4 additions & 3 deletions src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
* limitations under the License.
*/

'use strict';

import * as arrify from 'arrify';
import * as extend from 'extend';
import * as is from 'is';

const entity = module.exports;
// tslint:disable-next-line no-any
const entity: any = {};

class InvalidKeyError extends Error {
constructor(opts) {
Expand Down Expand Up @@ -875,3 +874,5 @@ function queryToQueryProto(query) {
}

entity.queryToQueryProto = queryToQueryProto;

export {entity};
35 changes: 21 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,17 @@
* @namespace google.type
*/

'use strict';

import * as arrify from 'arrify';
import * as extend from 'extend';
const gax = require('google-gax');
const {grpc} = new gax.GrpcClient();
import {GrpcClient, GrpcClientOptions} from 'google-gax';
import {GoogleAuth} from 'google-auth-library';
import * as is from 'is';

const DatastoreRequest = require('./request.js');
const entity = require('./entity.js');
const Query = require('./query.js');
const Transaction = require('./transaction.js');
import {DatastoreRequest} from './request';
import {entity} from './entity';
import {Query} from './query';
import {Transaction} from './transaction';

const {grpc} = new GrpcClient({} as GrpcClientOptions);

// Import the clients for each version supported by this package.
const gapic = Object.freeze({
Expand Down Expand Up @@ -380,7 +378,16 @@ const gapic = Object.freeze({
* });
*/
class Datastore extends DatastoreRequest {
constructor(options) {
clients_;
namespace;
projectId: string;
defaultBaseUrl_: string;
options;
baseUrl_?: string;
port_?: number;
customEndpoint_?: boolean;
auth: GoogleAuth;
constructor(options?) {
super();
options = options || {};
this.clients_ = new Map();
Expand All @@ -405,7 +412,7 @@ class Datastore extends DatastoreRequest {
this.defaultBaseUrl_ = 'datastore.googleapis.com';
this.determineBaseUrl_(options.apiEndpoint);

this.options = extend(
this.options = Object.assign(
{
libName: 'gccl',
libVersion: require('../../package.json').version,
Expand Down Expand Up @@ -631,7 +638,7 @@ class Datastore extends DatastoreRequest {
* const datastore = new Datastore();
* const query = datastore.createQuery('Company');
*/
createQuery(namespace, kind) {
createQuery(namespace: string, kind?: string) {
if (arguments.length < 2) {
kind = namespace;
namespace = this.namespace;
Expand Down Expand Up @@ -730,7 +737,7 @@ class Datastore extends DatastoreRequest {
* const datastore = new Datastore();
* const transaction = datastore.transaction();
*/
transaction(options) {
transaction(options?) {
return new Transaction(this, options);
};

Expand Down Expand Up @@ -758,7 +765,7 @@ class Datastore extends DatastoreRequest {
}

if (port.test(baseUrl)) {
this.port_ = Number(baseUrl.match(port)[1]);
this.port_ = Number(baseUrl.match(port)![1]);
}

this.baseUrl_ = baseUrl
Expand Down
14 changes: 6 additions & 8 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

'use strict';

import * as arrify from 'arrify';

/**
Expand Down Expand Up @@ -49,7 +47,7 @@ class Query {
endVal;
limitVal;
offsetVal;
constructor(scope, namespace, kinds) {
constructor(scope?, namespace?, kinds?) {
if (!kinds) {
kinds = namespace;
namespace = null;
Expand Down Expand Up @@ -154,7 +152,7 @@ class Query {
* const key = datastore.key(['Company', 'Google']);
* const keyQuery = query.filter('__key__', key);
*/
filter(property, operator, value) {
filter(property: string, operator: string, value?) {
if (arguments.length === 2) {
value = operator;
operator = '=';
Expand Down Expand Up @@ -212,7 +210,7 @@ class Query {
* descending: true
* });
*/
order(property, options) {
order(property: string, options?) {
const sign = options && options.descending ? '-' : '+';
this.orders.push({name: property, sign: sign});
return this;
Expand Down Expand Up @@ -404,9 +402,9 @@ class Query {
* const entities = data[0];
* });
*/
run() {
run(...argy) {
const query = this;
const args = [query].concat([].slice.call(arguments));
const args = [query].concat(argy);
return this.scope.runQuery.apply(this.scope, args);
}

Expand Down Expand Up @@ -455,4 +453,4 @@ class Query {
* @name module:@google-cloud/datastore.Query
* @see Query
*/
module.exports = Query;
export {Query};
23 changes: 11 additions & 12 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

'use strict';

import * as arrify from 'arrify';
import {replaceProjectIdToken} from '@google-cloud/projectify';
import {promisifyAll} from '@google-cloud/promisify';
Expand All @@ -31,8 +29,9 @@ const gapic = Object.freeze({
v1: require('./v1'),
});

const entity = require('./entity');
const Query = require('./query');
import {entity} from './entity';
import {Query} from './query';
import { Datastore } from '.';

/**
* A map of read consistency values to proto codes.
Expand All @@ -59,7 +58,7 @@ class DatastoreRequest {
id;
requests_;
requestCallbacks_;
datastore;
datastore!: Datastore;

/**
* Format a user's input to mutation methods. This will create a deep clone of
Expand Down Expand Up @@ -221,7 +220,7 @@ class DatastoreRequest {
* // All entities retrieved.
* });
*/
createReadStream(keys, options) {
createReadStream(keys, options?) {
options = options || {};
keys = arrify(keys).map(entity.keyToKeyProto);
if (keys.length === 0) {
Expand Down Expand Up @@ -331,7 +330,7 @@ class DatastoreRequest {
* const apiResponse = data[0];
* });
*/
delete(keys, gaxOptions, callback) {
delete(keys, gaxOptions, callback?) {
if (is.fn(gaxOptions)) {
callback = gaxOptions;
gaxOptions = {};
Expand Down Expand Up @@ -448,7 +447,7 @@ class DatastoreRequest {
* const entities = data[0];
* });
*/
get(keys, options, callback) {
get(keys, options, callback?) {
if (is.fn(options)) {
callback = options;
options = {};
Expand Down Expand Up @@ -581,7 +580,7 @@ class DatastoreRequest {
* const entities = data[0];
* });
*/
runQuery(query, options, callback) {
runQuery(query, options, callback?) {
if (is.fn(options)) {
callback = options;
options = {};
Expand Down Expand Up @@ -634,7 +633,7 @@ class DatastoreRequest {
* this.end();
* });
*/
runQueryStream(query, options) {
runQueryStream(query, options?) {
options = options || {};
query = extend(true, new Query(), query);

Expand Down Expand Up @@ -1157,7 +1156,7 @@ class DatastoreRequest {
);
}
const gaxClient = datastore.clients_.get(clientName);
reqOpts = replaceProjectIdToken(reqOpts, projectId);
reqOpts = replaceProjectIdToken(reqOpts, projectId!);
const gaxOpts = extend(true, {}, config.gaxOpts, {
headers: {
'google-cloud-resource-prefix': `projects/${projectId}`,
Expand All @@ -1180,4 +1179,4 @@ promisifyAll(DatastoreRequest);
* @name module:@google-cloud/datastore.DatastoreRequest
* @see DatastoreRequest
*/
module.exports = DatastoreRequest;
export {DatastoreRequest};
27 changes: 15 additions & 12 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@
* limitations under the License.
*/

'use strict';

import * as arrify from 'arrify';
import {promisifyAll} from '@google-cloud/promisify';
const flatten = require('lodash.flatten');
import * as is from 'is';

const entity = require('./entity');
const Request = require('./request');
import {entity} from './entity';
import {DatastoreRequest} from './request';

/**
* A transaction is a set of Datastore operations on one or more entities. Each
Expand All @@ -42,7 +39,13 @@ const Request = require('./request');
* const datastore = new Datastore();
* const transaction = datastore.transaction();
*/
class Transaction extends Request {
class Transaction extends DatastoreRequest {
projectId: string;
namespace: string;
readOnly: boolean;
request;
modifiedEntities_;
skipCommit?: boolean;
constructor(datastore, options) {
super();
/**
Expand Down Expand Up @@ -120,7 +123,7 @@ class Transaction extends Request {
* const apiResponse = data[0];
* });
*/
commit(gaxOptions, callback) {
commit(gaxOptions, callback?) {
if (is.fn(gaxOptions)) {
callback = gaxOptions;
gaxOptions = {};
Expand Down Expand Up @@ -193,13 +196,13 @@ class Transaction extends Request {
.forEach(modifiedEntity => {
const method = modifiedEntity.method;
const args = modifiedEntity.args.reverse();
Request.prototype[method].call(this, args, () => {});
DatastoreRequest.prototype[method].call(this, args, () => {});
});

// Take the `req` array built previously, and merge them into one request to
// send as the final transactional commit.
const reqOpts = {
mutations: flatten(this.requests_.map(x => x.mutations)),
mutations: this.requests_.map(x => x.mutations).reduce((a, b) => a.concat(b), []),
};

this.request_(
Expand Down Expand Up @@ -270,7 +273,7 @@ class Transaction extends Request {
* });
* });
*/
createQuery() {
createQuery(namespace: string, kind?: string) {
return this.datastore.createQuery.apply(this, arguments);
}

Expand Down Expand Up @@ -422,7 +425,7 @@ class Transaction extends Request {
* const apiResponse = data[1];
* });
*/
run(options, callback) {
run(options, callback?) {
if (is.fn(options)) {
callback = options;
options = {};
Expand Down Expand Up @@ -624,4 +627,4 @@ promisifyAll(Transaction, {
* @name module:@google-cloud/datastore.Transaction
* @see Transaction
*/
module.exports = Transaction;
export {Transaction};
6 changes: 0 additions & 6 deletions system-test/.eslintrc.yml

This file was deleted.

6 changes: 2 additions & 4 deletions system-test/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
* limitations under the License.
*/

'use strict';

import * as assert from 'assert';
import * as async from 'async';
const {Datastore} = require('../src');
const entity = require('../src/entity.js');
import {Datastore} from '../src';
import {entity} from '../src/entity';

describe('Datastore', () => {
const testKinds: {}[] = [];
Expand Down
7 changes: 2 additions & 5 deletions test/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@
* limitations under the License.
*/

'use strict';

import * as assert from 'assert';
import * as extend from 'extend';

const Datastore = require('../src').Datastore;
import {Datastore} from '../src';

describe('entity', function() {
let entity;

beforeEach(function() {
delete require.cache[require.resolve('../src/entity.js')];
entity = require('../src/entity.js');
entity = require('../src/entity.js').entity;
});

describe('KEY_SYMBOL', function() {
Expand Down
Loading

0 comments on commit 43381cf

Please sign in to comment.