-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Updated the version of spanner library #334
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4bbad15
Updated the version of spanner library and changed the samples to wor…
vkedia 43d28a4
Fixed some lint errors
vkedia 026b2cb
Converted callbacks to promises wherever possible
vkedia b93f54c
Merge branch 'master' into txn-retry
vkedia cbf5f43
Fixed error handling
vkedia b698735
Used arrow functions
vkedia 9541f13
Merge branch 'master' into txn-retry
vkedia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,46 +33,43 @@ function readOnlyTransaction (instanceId, databaseId) { | |
|
||
// Gets a transaction object that captures the database state | ||
// at a specific point in time | ||
database.runTransaction({readOnly: true}) | ||
.then((results) => { | ||
const transaction = results[0]; | ||
|
||
const queryOne = 'SELECT SingerId, AlbumId, AlbumTitle FROM Albums'; | ||
database.runTransaction({readOnly: true}, (err, transaction) => { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
const queryOne = 'SELECT SingerId, AlbumId, AlbumTitle FROM Albums'; | ||
|
||
// Read #1, using SQL | ||
transaction.run(queryOne) | ||
.then((results) => { | ||
const rows = results[0]; | ||
|
||
rows.forEach((row) => { | ||
const json = row.toJSON(); | ||
console.log(`SingerId: ${json.SingerId.value}, AlbumId: ${json.AlbumId.value}, AlbumTitle: ${json.AlbumTitle}`); | ||
}); | ||
transaction.run(queryOne) | ||
.then((results) => { | ||
const rows = results[0]; | ||
rows.forEach((row) => { | ||
const json = row.toJSON(); | ||
console.log(`SingerId: ${json.SingerId.value}, AlbumId: ${json.AlbumId.value}, AlbumTitle: ${json.AlbumTitle}`); | ||
}); | ||
|
||
const queryTwo = { | ||
columns: ['SingerId', 'AlbumId', 'AlbumTitle'], | ||
keySet: { | ||
all: true | ||
} | ||
}; | ||
const queryTwo = { | ||
columns: ['SingerId', 'AlbumId', 'AlbumTitle'], | ||
keySet: { | ||
all: true | ||
} | ||
}; | ||
|
||
// Read #2, using the `read` method. Even if changes occur | ||
// in-between the reads, the transaction ensures that both | ||
// return the same data. | ||
transaction.read('Albums', queryTwo) | ||
.then((results) => { | ||
const rows = results[0]; | ||
|
||
rows.forEach((row) => { | ||
const json = row.toJSON(); | ||
console.log(`SingerId: ${json.SingerId.value}, AlbumId: ${json.AlbumId.value}, AlbumTitle: ${json.AlbumTitle}`); | ||
}); | ||
return transaction.read('Albums', queryTwo); | ||
}) | ||
.then((results) => { | ||
const rows = results[0]; | ||
rows.forEach((row) => { | ||
const json = row.toJSON(); | ||
console.log(`SingerId: ${json.SingerId.value}, AlbumId: ${json.AlbumId.value}, AlbumTitle: ${json.AlbumTitle}`); | ||
}); | ||
}) | ||
.then(() => { | ||
console.log('Successfully executed read-only transaction.'); | ||
}); | ||
console.log('Successfully executed read-only transaction.'); | ||
transaction.end(); | ||
}); | ||
}); | ||
// [END read_only_transaction] | ||
} | ||
|
||
|
@@ -96,53 +93,50 @@ function readWriteTransaction (instanceId, databaseId) { | |
const instance = spanner.instance(instanceId); | ||
const database = instance.database(databaseId); | ||
|
||
// Gets a transaction object that captures the database state | ||
// at a specific point in time | ||
let transaction, firstBudget, secondBudget; | ||
const transferAmount = 200000; | ||
const minimumAmountToTransfer = 300000; | ||
|
||
database.runTransaction() | ||
.then((results) => { | ||
transaction = results[0]; | ||
|
||
const queryOne = { | ||
columns: [`MarketingBudget`], | ||
keys: [2, 2] // SingerId: 2, AlbumId: 2 | ||
}; | ||
|
||
const queryTwo = { | ||
columns: ['MarketingBudget'], | ||
keys: [1, 1] // SingerId: 1, AlbumId: 1 | ||
}; | ||
|
||
return Promise.all([ | ||
// Reads the second album's budget | ||
transaction.read('Albums', queryOne).then((results) => { | ||
// Gets second album's budget | ||
// Note: MarketingBudget is an INT64, which comes from Cloud Spanner | ||
// as a string - so we convert it to a number with parseInt() | ||
const rows = results[0].map((row) => row.toJSON()); | ||
secondBudget = parseInt(rows[0].MarketingBudget.value); | ||
console.log(`The second album's marketing budget: ${secondBudget}`); | ||
|
||
// Makes sure the second album's budget is sufficient | ||
if (secondBudget < minimumAmountToTransfer) { | ||
throw new Error(`The second album's budget (${secondBudget}) is less than the minimum required amount to transfer.`); | ||
} | ||
}), | ||
|
||
// Reads the first album's budget | ||
transaction.read('Albums', queryTwo).then((results) => { | ||
// Gets first album's budget | ||
// As above, MarketingBudget is an INT64 and comes as a string | ||
const rows = results[0].map((row) => row.toJSON()); | ||
firstBudget = parseInt(rows[0].MarketingBudget.value); | ||
console.log(`The first album's marketing budget: ${firstBudget}`); | ||
}) | ||
]); | ||
}) | ||
.then(() => { | ||
database.runTransaction(function (err, transaction) { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
let firstBudget, secondBudget; | ||
const queryOne = { | ||
columns: [`MarketingBudget`], | ||
keys: [[2, 2]] // SingerId: 2, AlbumId: 2 | ||
}; | ||
|
||
const queryTwo = { | ||
columns: ['MarketingBudget'], | ||
keys: [[1, 1]] // SingerId: 1, AlbumId: 1 | ||
}; | ||
|
||
Promise.all([ | ||
// Reads the second album's budget | ||
transaction.read('Albums', queryOne).then((results) => { | ||
// Gets second album's budget | ||
// Note: MarketingBudget is an INT64, which comes from Cloud Spanner | ||
// as a string - so we convert it to a number with parseInt() | ||
const rows = results[0].map((row) => row.toJSON()); | ||
secondBudget = parseInt(rows[0].MarketingBudget.value); | ||
console.log(`The second album's marketing budget: ${secondBudget}`); | ||
|
||
// Makes sure the second album's budget is sufficient | ||
if (secondBudget < minimumAmountToTransfer) { | ||
throw new Error(`The second album's budget (${secondBudget}) is less than the minimum required amount to transfer.`); | ||
} | ||
}), | ||
|
||
// Reads the first album's budget | ||
transaction.read('Albums', queryTwo).then((results) => { | ||
// Gets first album's budget | ||
// As above, MarketingBudget is an INT64 and comes as a string | ||
const rows = results[0].map((row) => row.toJSON()); | ||
firstBudget = parseInt(rows[0].MarketingBudget.value); | ||
console.log(`The first album's marketing budget: ${firstBudget}`); | ||
}) | ||
]).then(() => { | ||
// Transfer the budgets between the albums | ||
console.log(firstBudget, secondBudget); | ||
firstBudget += transferAmount; | ||
|
@@ -159,12 +153,15 @@ function readWriteTransaction (instanceId, databaseId) { | |
]); | ||
}) | ||
// Commits the transaction and send the changes to the database | ||
.then(() => transaction.commit()) | ||
.then(() => { | ||
// Logs success | ||
console.log(`Successfully executed read-write transaction to transfer ${transferAmount} from Album 2 to Album 1.`); | ||
}); | ||
// [END read_write_transaction] | ||
.then(() => transaction.commit(function (err) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: use arrow functions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
if (err) { | ||
console.error(err); | ||
} else { | ||
console.log(`Successfully executed read-write transaction to transfer ${transferAmount} from Album 2 to Album 1.`); | ||
} | ||
})); | ||
}); | ||
// [END read_write_transaction] | ||
} | ||
|
||
const cli = require(`yargs`) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: use arrow functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done