diff --git a/package.json b/package.json index d426f859f..73c26bf4b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/request.ts b/src/request.ts index 42a8a971c..fe4bf7ab7 100644 --- a/src/request.ts +++ b/src/request.ts @@ -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. * @@ -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]; @@ -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 = {}; @@ -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) => { @@ -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; + get(keys, options?, callback?): void|Promise { if (is.fn(options)) { callback = options; options = {}; @@ -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>>; + runQuery(query, options?, callback?): void|Promise>> { if (is.fn(options)) { callback = options; options = {}; @@ -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)) { @@ -1036,7 +1042,7 @@ class DatastoreRequest { client: 'DatastoreClient', method: 'commit', reqOpts, - gaxOpts: gaxOptions, + gaxOpts: gaxOptions || {}, }, onCommit); } diff --git a/src/transaction.ts b/src/transaction.ts index 9dab1b610..c38419c49 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -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 = {}; @@ -215,7 +215,7 @@ class Transaction extends DatastoreRequest { client: 'DatastoreClient', method: 'commit', reqOpts, - gaxOpts: gaxOptions, + gaxOpts: gaxOptions || {}, }, (err, resp) => { if (err) { @@ -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 = {}; diff --git a/system-test/datastore.ts b/system-test/datastore.ts index b54029533..5dd666a3e 100644 --- a/system-test/datastore.ts +++ b/system-test/datastore.ts @@ -15,7 +15,6 @@ */ import * as assert from 'assert'; -import * as async from 'async'; import {Datastore} from '../src'; import {entity} from '../src/entity'; @@ -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 => { @@ -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); @@ -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 => {