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

chore: use arrow functions #359

Merged
merged 1 commit into from
Oct 4, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ rules:
no-warning-comments: warn
no-var: error
prefer-const: error
prefer-arrow-callback: error
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"test-no-cover": "mocha test/*.js",
"test": "npm run cover",
"ycsb": "node ./benchmark/ycsb.js run -P ./benchmark/workloada -p table=usertable -p cloudspanner.instance=ycsb-instance -p operationcount=100 -p cloudspanner.database=ycsb",
"fix": "eslint --fix '**/*.js' && npm run prettier"
"fix": "eslint --fix '**/*.js'"
},
"dependencies": {
"@google-cloud/common-grpc": "^0.9.0",
Expand Down
9 changes: 4 additions & 5 deletions src/batch-transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,23 @@ class BatchTransaction extends Transaction {
* @param {function} callback Callback function.
*/
createPartitions_(config, callback) {
const self = this;
const query = extend({}, config.reqOpts, {
session: this.session.formattedName_,
transaction: {id: this.id},
});
config.reqOpts = extend({}, query);
delete query.partitionOptions;
this.request(config, function(err, resp) {
this.request(config, (err, resp) => {
if (err) {
callback(err, null, resp);
return;
}
const partitions = resp.partitions.map(function(partition) {
const partitions = resp.partitions.map(partition => {
return extend({}, query, partition);
});
if (resp.transaction) {
self.id = resp.transaction.id;
self.readTimestamp = resp.transaction.readTimestamp;
this.id = resp.transaction.id;
this.readTimestamp = resp.transaction.readTimestamp;
}
callback(null, partitions, resp);
});
Expand Down
12 changes: 6 additions & 6 deletions src/codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Struct.fromArray = function(arr) {
Struct.fromJSON = function(json) {
const struct = new Struct();

Object.keys(json || {}).forEach(function(name) {
Object.keys(json || {}).forEach(name => {
const value = json[name];
struct.push({name, value});
});
Expand Down Expand Up @@ -176,7 +176,7 @@ function generateToJSONFromRow(row) {
options
);

return row.reduce(function(serializedRow, keyVal) {
return row.reduce((serializedRow, keyVal) => {
const name = keyVal.name;
let value = keyVal.value;

Expand Down Expand Up @@ -240,7 +240,7 @@ function decode(value, field) {
break;
}
case 'ARRAY': {
decoded = decoded.map(function(value) {
decoded = decoded.map(value => {
return decodeValue_(value, type.arrayElementType);
});
break;
Expand Down Expand Up @@ -365,7 +365,7 @@ function getType(field) {
}

if (Struct.isStruct(field)) {
const fields = field.map(function(field) {
const fields = field.map(field => {
return {
name: field.name,
type: getType(field.value),
Expand Down Expand Up @@ -492,7 +492,7 @@ function encodeRead(query) {
}

if (query.keys) {
encoded.keySet.keys = arrify(query.keys).map(function(key) {
encoded.keySet.keys = arrify(query.keys).map(key => {
return {
values: arrify(key).map(codec.encode),
};
Expand All @@ -501,7 +501,7 @@ function encodeRead(query) {
}

if (query.ranges) {
encoded.keySet.ranges = arrify(query.ranges).map(function(rawRange) {
encoded.keySet.ranges = arrify(query.ranges).map(rawRange => {
const range = extend({}, rawRange);

for (const bound in range) {
Expand Down
67 changes: 30 additions & 37 deletions src/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,19 +263,18 @@ class Database extends ServiceObject {
* @returns {Promise<CreateTransactionResponse>}
*/
createBatchTransaction(options, callback) {
const self = this;
if (is.fn(options)) {
callback = options;
options = null;
}
this.createSession(function(err, session, resp) {
this.createSession((err, session, resp) => {
if (err) {
callback(err, null, resp);
return;
}
const transaction = self.batchTransaction({session});
const transaction = this.batchTransaction({session});
transaction.options = extend({}, options);
transaction.begin(function(err, resp) {
transaction.begin((err, resp) => {
if (err) {
callback(err, null, resp);
return;
Expand Down Expand Up @@ -341,7 +340,6 @@ class Database extends ServiceObject {
* });
*/
createSession(options, callback) {
const self = this;
if (is.function(options)) {
callback = options;
options = {};
Expand All @@ -357,12 +355,12 @@ class Database extends ServiceObject {
reqOpts: reqOpts,
gaxOpts: options,
},
function(err, resp) {
(err, resp) => {
if (err) {
callback(err, null, resp);
return;
}
const session = self.session(resp.name);
const session = this.session(resp.name);
session.metadata = resp;
callback(null, session, resp);
}
Expand Down Expand Up @@ -436,14 +434,13 @@ class Database extends ServiceObject {
* });
*/
createTable(schema, callback) {
const self = this;
this.updateSchema(schema, function(err, operation, resp) {
this.updateSchema(schema, (err, operation, resp) => {
if (err) {
callback(err, null, null, resp);
return;
}
const tableName = schema.match(/CREATE TABLE `*([^\s`(]+)/)[1];
const table = self.table(tableName);
const table = this.table(tableName);
callback(null, table, operation, resp);
});
}
Expand Down Expand Up @@ -499,12 +496,11 @@ class Database extends ServiceObject {
* });
*/
delete(callback) {
const self = this;
const reqOpts = {
database: this.formattedName_,
};
this.close(function() {
self.request(
this.close(() => {
this.request(
{
client: 'DatabaseAdminClient',
method: 'dropDatabase',
Expand Down Expand Up @@ -549,7 +545,7 @@ class Database extends ServiceObject {
exists(callback) {
const NOT_FOUND = 5;

this.getMetadata(function(err) {
this.getMetadata(err => {
if (err && err.code !== NOT_FOUND) {
callback(err, null);
return;
Expand Down Expand Up @@ -604,30 +600,29 @@ class Database extends ServiceObject {
* });
*/
get(options, callback) {
const self = this;
if (is.fn(options)) {
callback = options;
options = {};
}
this.getMetadata(function(err, metadata) {
this.getMetadata((err, metadata) => {
if (err) {
if (options.autoCreate && err.code === 5) {
self.create(options, function(err, database, operation) {
this.create(options, (err, database, operation) => {
if (err) {
callback(err);
return;
}
operation.on('error', callback).on('complete', function(metadata) {
self.metadata = metadata;
callback(null, self, metadata);
operation.on('error', callback).on('complete', metadata => {
this.metadata = metadata;
callback(null, this, metadata);
});
});
return;
}
callback(err);
return;
}
callback(null, self, metadata);
callback(null, this, metadata);
});
}
/**
Expand Down Expand Up @@ -846,7 +841,7 @@ class Database extends ServiceObject {
},
function(err, sessions) {
if (sessions) {
arguments[1] = sessions.map(function(metadata) {
arguments[1] = sessions.map(metadata => {
const session = self.session(metadata.name);
session.metadata = metadata;
return session;
Expand Down Expand Up @@ -893,32 +888,31 @@ class Database extends ServiceObject {
* });
*/
getTransaction(options, callback) {
const self = this;
if (is.fn(options)) {
callback = options;
options = null;
}
if (!options || !options.readOnly) {
this.pool_.getWriteSession(function(err, session, transaction) {
this.pool_.getWriteSession((err, session, transaction) => {
if (!err) {
transaction = self.decorateTransaction_(transaction, session);
transaction = this.decorateTransaction_(transaction, session);
}
callback(err, transaction);
});
return;
}
this.pool_.getReadSession(function(err, session) {
this.pool_.getReadSession((err, session) => {
if (err) {
callback(err, null);
return;
}
session.beginTransaction(options, function(err, transaction) {
session.beginTransaction(options, (err, transaction) => {
if (err) {
self.pool_.release(session);
this.pool_.release(session);
callback(err, null);
return;
}
transaction = self.decorateTransaction_(transaction, session);
transaction = this.decorateTransaction_(transaction, session);
callback(null, transaction);
});
});
Expand All @@ -932,15 +926,14 @@ class Database extends ServiceObject {
* @param {function} callback Callback function
*/
makePooledRequest_(config, callback) {
const self = this;
const pool = this.pool_;
pool.getReadSession(function(err, session) {
pool.getReadSession((err, session) => {
if (err) {
callback(err, null);
return;
}
config.reqOpts.session = session.formattedName_;
self.request(config, function() {
this.request(config, function() {
pool.release(session);
callback.apply(null, arguments);
});
Expand Down Expand Up @@ -976,8 +969,8 @@ class Database extends ServiceObject {
session = null;
}
}
waitForSessionStream.on('reading', function() {
pool.getReadSession(function(err, session_) {
waitForSessionStream.on('reading', () => {
pool.getReadSession((err, session_) => {
if (err) {
destroyStream(err);
return;
Expand Down Expand Up @@ -1158,10 +1151,10 @@ class Database extends ServiceObject {
}
this.runStream(query, options)
.on('error', callback)
.on('data', function(row) {
.on('data', row => {
rows.push(row);
})
.on('end', function() {
.on('end', () => {
callback(null, rows);
});
}
Expand Down Expand Up @@ -1432,7 +1425,7 @@ class Database extends ServiceObject {
options = null;
}
options = extend({}, options);
this.getTransaction(options, function(err, transaction) {
this.getTransaction(options, (err, transaction) => {
if (err) {
callback(err);
return;
Expand Down
Loading