Skip to content
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

refactor: remove async module usage #272

Merged
merged 8 commits into from
Dec 21, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"@types/proxyquire": "^1.3.28",
"@types/sinon": "7.0.0",
"@types/through2": "^2.0.34",
"async": "^2.6.1",
"codecov": "^3.0.2",
"eslint": "^5.0.0",
"eslint-config-prettier": "^3.0.0",
Expand Down
28 changes: 17 additions & 11 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import {entity} from './entity';
import {Query} from './query';
import {Datastore} from '.';

export interface EntityDataObj {
[key: string]: string;
}

/**
* A map of read consistency values to proto codes.
*
Expand Down Expand Up @@ -161,7 +165,7 @@ class DatastoreRequest {
* datastore.allocateIds(incompleteKey, 100, callback);
*
* //-
* // If the callback is omitted, we'll return a Promise.
* // Returns a Promise if callback is omitted.
* //-
* datastore.allocateIds(incompleteKey, 100).then((data) => {
* const keys = data[0];
Expand Down Expand Up @@ -326,13 +330,13 @@ class DatastoreRequest {
* ], (err, apiResponse) => {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* // Returns a Promise if callback is omitted.
* //-
* datastore.delete().then((data) => {
* const apiResponse = data[0];
* });
*/
delete(keys, gaxOptions, callback?) {
delete(keys, gaxOptions?, callback?) {
if (is.fn(gaxOptions)) {
callback = gaxOptions;
gaxOptions = {};
Expand Down Expand Up @@ -425,7 +429,7 @@ class DatastoreRequest {
* datastore.get(keys, (err, entities) => {});
*
* //-
* // Here's how you would update the value of an entity with the help of the
* // Below is how to update the value of an entity with the help of the
* // `save` method.
* //-
* datastore.get(key, (err, entity) => {
Expand All @@ -442,13 +446,14 @@ class DatastoreRequest {
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* // Returns a Promise if callback is omitted.
* //-
* datastore.get(keys).then((data) => {
* const entities = data[0];
* });
*/
get(keys, options, callback?) {
get(keys, options?): Promise<EntityDataObj[]>;
get(keys, options?, callback?): void|Promise<EntityDataObj[]> {
if (is.fn(options)) {
callback = options;
options = {};
Expand Down Expand Up @@ -574,13 +579,14 @@ class DatastoreRequest {
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* // Returns a Promise if callback is omitted.
* //-
* datastore.runQuery(query).then((data) => {
* const entities = data[0];
* });
*/
runQuery(query, options, callback?) {
runQuery(query, options?): Promise<Array<Array<{}>>>;
runQuery(query, options?, callback?): void|Promise<Array<Array<{}>>> {
if (is.fn(options)) {
callback = options;
options = {};
Expand Down Expand Up @@ -922,13 +928,13 @@ class DatastoreRequest {
* datastore.save(entity, (err, apiResponse) => {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* // Returns a Promise if callback is omitted.
* //-
* datastore.save(entity).then((data) => {
* const apiResponse = data[0];
* });
*/
save(entities, gaxOptions, callback?) {
save(entities, gaxOptions?, callback?) {
entities = arrify(entities);

if (is.fn(gaxOptions)) {
Expand Down Expand Up @@ -1036,7 +1042,7 @@ class DatastoreRequest {
client: 'DatastoreClient',
method: 'commit',
reqOpts,
gaxOpts: gaxOptions,
gaxOpts: gaxOptions || {},
},
onCommit);
}
Expand Down
6 changes: 3 additions & 3 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Transaction extends DatastoreRequest {
* const apiResponse = data[0];
* });
*/
commit(gaxOptions, callback?) {
commit(gaxOptions?, callback?) {
if (is.fn(gaxOptions)) {
callback = gaxOptions;
gaxOptions = {};
Expand Down Expand Up @@ -215,7 +215,7 @@ class Transaction extends DatastoreRequest {
client: 'DatastoreClient',
method: 'commit',
reqOpts,
gaxOpts: gaxOptions,
gaxOpts: gaxOptions || {},
},
(err, resp) => {
if (err) {
Expand Down Expand Up @@ -429,7 +429,7 @@ class Transaction extends DatastoreRequest {
* const apiResponse = data[1];
* });
*/
run(options, callback?) {
run(options?, callback?) {
if (is.fn(options)) {
callback = options;
options = {};
Expand Down
106 changes: 38 additions & 68 deletions system-test/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import * as assert from 'assert';
import * as async from 'async';
import {Datastore} from '../src';
import {entity} from '../src/entity';

Expand All @@ -31,25 +30,19 @@ describe('Datastore', () => {
return keyObject;
};

after(done => {
function deleteEntities(kind, callback) {
after(async () => {
async function deleteEntities(kind) {
const query = datastore.createQuery(kind).select('__key__');

datastore.runQuery(query, (err, entities) => {
if (err) {
callback(err);
return;
}

const keys = entities.map(entity => {
return entity[datastore.KEY];
});

datastore.delete(keys, callback);
const [entities] = await datastore.runQuery(query);
const keys = entities.map(entity => {
return entity[datastore.KEY];
});

await datastore.delete(keys);
}

async.each(testKinds, deleteEntities, done);
await Promise.all(testKinds.map(kind => deleteEntities(kind)));
});

it('should allocate IDs', done => {
Expand Down Expand Up @@ -287,7 +280,7 @@ describe('Datastore', () => {
data: post,
},
err => {
assert.notStrictEqual(err, null); // should fail insert
assert.notStrictEqual(err, null); // should fail insert.
datastore.get(postKey, (err, entity) => {
assert.ifError(err);
assert.deepStrictEqual(entity, post);
Expand Down Expand Up @@ -801,67 +794,44 @@ describe('Datastore', () => {
});
});

it('should commit all saves and deletes at the end', done => {
it('should commit all saves and deletes at the end', async () => {
const deleteKey = datastore.key(['Company', 'Subway']);
const key = datastore.key(['Company', 'Google']);
const incompleteKey = datastore.key('Company');

datastore.save(
{
key: deleteKey,
data: {},
},
err => {
assert.ifError(err);

const transaction = datastore.transaction();
await datastore.save({
key: deleteKey,
data: {},
});
const transaction = datastore.transaction();

transaction.run(err => {
assert.ifError(err);
await transaction.run();
transaction.delete(deleteKey);

transaction.delete(deleteKey);
await transaction.save([
{
key,
data: {rating: 10},
},
{
key: incompleteKey,
data: {rating: 100},
},
]);

transaction.save([
{
key,
data: {rating: 10},
},
{
key: incompleteKey,
data: {rating: 100},
},
]);
await transaction.commit();

transaction.commit(err => {
assert.ifError(err);
// Incomplete key should have been given an ID.
assert.strictEqual(incompleteKey.path.length, 2);

// Incomplete key should have been given an ID.
assert.strictEqual(incompleteKey.path.length, 2);

async.parallel(
[
// The key queued for deletion should have been deleted.
callback => {
datastore.get(deleteKey, (err, entity) => {
assert.ifError(err);
assert.strictEqual(typeof entity, 'undefined');
callback();
});
},

// Data should have been updated on the key.
callback => {
datastore.get(key, (err, entity) => {
assert.ifError(err);
assert.strictEqual(entity.rating, 10);
callback();
});
},
],
done);
});
});
});
const [[deletedEntity], [fetchedEntity]] = await Promise.all([
// Deletes the key that is in the deletion queue.
datastore.get(deleteKey),
// Updates data on the key.
datastore.get(key)
]);
assert.strictEqual(typeof deletedEntity, 'undefined');
assert.strictEqual(fetchedEntity.rating, 10);
});

it('should use the last modification to a key', done => {
Expand Down