@@ -33,46 +33,43 @@ function readOnlyTransaction (instanceId, databaseId) {
3333
3434 // Gets a transaction object that captures the database state
3535 // at a specific point in time
36- database . runTransaction ( { readOnly : true } )
37- . then ( ( results ) => {
38- const transaction = results [ 0 ] ;
39-
40- const queryOne = 'SELECT SingerId, AlbumId, AlbumTitle FROM Albums' ;
36+ database . runTransaction ( { readOnly : true } , ( err , transaction ) => {
37+ if ( err ) {
38+ console . error ( err ) ;
39+ return ;
40+ }
41+ const queryOne = 'SELECT SingerId, AlbumId, AlbumTitle FROM Albums' ;
4142
4243 // Read #1, using SQL
43- transaction . run ( queryOne )
44- . then ( ( results ) => {
45- const rows = results [ 0 ] ;
46-
47- rows . forEach ( ( row ) => {
48- const json = row . toJSON ( ) ;
49- console . log ( `SingerId: ${ json . SingerId . value } , AlbumId: ${ json . AlbumId . value } , AlbumTitle: ${ json . AlbumTitle } ` ) ;
50- } ) ;
44+ transaction . run ( queryOne )
45+ . then ( ( results ) => {
46+ const rows = results [ 0 ] ;
47+ rows . forEach ( ( row ) => {
48+ const json = row . toJSON ( ) ;
49+ console . log ( `SingerId: ${ json . SingerId . value } , AlbumId: ${ json . AlbumId . value } , AlbumTitle: ${ json . AlbumTitle } ` ) ;
5150 } ) ;
52-
53- const queryTwo = {
54- columns : [ 'SingerId' , 'AlbumId' , 'AlbumTitle' ] ,
55- keySet : {
56- all : true
57- }
58- } ;
51+ const queryTwo = {
52+ columns : [ 'SingerId' , 'AlbumId' , 'AlbumTitle' ] ,
53+ keySet : {
54+ all : true
55+ }
56+ } ;
5957
6058 // Read #2, using the `read` method. Even if changes occur
6159 // in-between the reads, the transaction ensures that both
6260 // return the same data.
63- transaction . read ( 'Albums' , queryTwo )
64- . then ( ( results ) => {
65- const rows = results [ 0 ] ;
66-
67- rows . forEach ( ( row ) => {
68- const json = row . toJSON ( ) ;
69- console . log ( `SingerId: ${ json . SingerId . value } , AlbumId: ${ json . AlbumId . value } , AlbumTitle: ${ json . AlbumTitle } ` ) ;
70- } ) ;
61+ return transaction . read ( 'Albums' , queryTwo ) ;
62+ } )
63+ . then ( ( results ) => {
64+ const rows = results [ 0 ] ;
65+ rows . forEach ( ( row ) => {
66+ const json = row . toJSON ( ) ;
67+ console . log ( `SingerId: ${ json . SingerId . value } , AlbumId: ${ json . AlbumId . value } , AlbumTitle: ${ json . AlbumTitle } ` ) ;
7168 } ) ;
72- } )
73- . then ( ( ) => {
74- console . log ( 'Successfully executed read-only transaction.' ) ;
75- } ) ;
69+ console . log ( 'Successfully executed read-only transaction.' ) ;
70+ transaction . end ( ) ;
71+ } ) ;
72+ } ) ;
7673 // [END read_only_transaction]
7774}
7875
@@ -96,53 +93,50 @@ function readWriteTransaction (instanceId, databaseId) {
9693 const instance = spanner . instance ( instanceId ) ;
9794 const database = instance . database ( databaseId ) ;
9895
99- // Gets a transaction object that captures the database state
100- // at a specific point in time
101- let transaction , firstBudget , secondBudget ;
10296 const transferAmount = 200000 ;
10397 const minimumAmountToTransfer = 300000 ;
10498
105- database . runTransaction ( )
106- . then ( ( results ) => {
107- transaction = results [ 0 ] ;
108-
109- const queryOne = {
110- columns : [ `MarketingBudget` ] ,
111- keys : [ 2 , 2 ] // SingerId: 2, AlbumId: 2
112- } ;
113-
114- const queryTwo = {
115- columns : [ 'MarketingBudget' ] ,
116- keys : [ 1 , 1 ] // SingerId: 1, AlbumId: 1
117- } ;
118-
119- return Promise . all ( [
120- // Reads the second album's budget
121- transaction . read ( 'Albums' , queryOne ) . then ( ( results ) => {
122- // Gets second album's budget
123- // Note: MarketingBudget is an INT64, which comes from Cloud Spanner
124- // as a string - so we convert it to a number with parseInt()
125- const rows = results [ 0 ] . map ( ( row ) => row . toJSON ( ) ) ;
126- secondBudget = parseInt ( rows [ 0 ] . MarketingBudget . value ) ;
127- console . log ( `The second album's marketing budget: ${ secondBudget } ` ) ;
128-
129- // Makes sure the second album's budget is sufficient
130- if ( secondBudget < minimumAmountToTransfer ) {
131- throw new Error ( `The second album's budget ( ${ secondBudget } ) is less than the minimum required amount to transfer.` ) ;
132- }
133- } ) ,
134-
135- // Reads the first album's budget
136- transaction . read ( 'Albums' , queryTwo ) . then ( ( results ) => {
137- // Gets first album's budget
138- // As above, MarketingBudget is an INT64 and comes as a string
139- const rows = results [ 0 ] . map ( ( row ) => row . toJSON ( ) ) ;
140- firstBudget = parseInt ( rows [ 0 ] . MarketingBudget . value ) ;
141- console . log ( `The first album's marketing budget: ${ firstBudget } ` ) ;
142- } )
143- ] ) ;
144- } )
145- . then ( ( ) => {
99+ database . runTransaction ( ( err , transaction ) => {
100+ if ( err ) {
101+ console . error ( err ) ;
102+ return ;
103+ }
104+ let firstBudget , secondBudget ;
105+ const queryOne = {
106+ columns : [ `MarketingBudget` ] ,
107+ keys : [ [ 2 , 2 ] ] // SingerId: 2, AlbumId: 2
108+ } ;
109+
110+ const queryTwo = {
111+ columns : [ 'MarketingBudget' ] ,
112+ keys : [ [ 1 , 1 ] ] // SingerId: 1, AlbumId: 1
113+ } ;
114+
115+ Promise . all ( [
116+ // Reads the second album's budget
117+ transaction . read ( 'Albums' , queryOne ) . then ( ( results ) => {
118+ // Gets second album's budget
119+ // Note: MarketingBudget is an INT64, which comes from Cloud Spanner
120+ // as a string - so we convert it to a number with parseInt()
121+ const rows = results [ 0 ] . map ( ( row ) => row . toJSON ( ) ) ;
122+ secondBudget = parseInt ( rows [ 0 ] . MarketingBudget . value ) ;
123+ console . log ( `The second album's marketing budget: ${ secondBudget } ` ) ;
124+
125+ // Makes sure the second album's budget is sufficient
126+ if ( secondBudget < minimumAmountToTransfer ) {
127+ throw new Error ( `The second album's budget ( ${ secondBudget } ) is less than the minimum required amount to transfer.` ) ;
128+ }
129+ } ) ,
130+
131+ // Reads the first album's budget
132+ transaction . read ( 'Albums' , queryTwo ) . then ( ( results ) => {
133+ // Gets first album's budget
134+ // As above, MarketingBudget is an INT64 and comes as a string
135+ const rows = results [ 0 ] . map ( ( row ) => row . toJSON ( ) ) ;
136+ firstBudget = parseInt ( rows [ 0 ] . MarketingBudget . value ) ;
137+ console . log ( `The first album's marketing budget: ${ firstBudget } ` ) ;
138+ } )
139+ ] ) . then ( ( ) => {
146140 // Transfer the budgets between the albums
147141 console . log ( firstBudget , secondBudget ) ;
148142 firstBudget += transferAmount ;
@@ -159,12 +153,15 @@ function readWriteTransaction (instanceId, databaseId) {
159153 ] ) ;
160154 } )
161155 // Commits the transaction and send the changes to the database
162- . then ( ( ) => transaction . commit ( ) )
163- . then ( ( ) => {
164- // Logs success
165- console . log ( `Successfully executed read-write transaction to transfer ${ transferAmount } from Album 2 to Album 1.` ) ;
166- } ) ;
167- // [END read_write_transaction]
156+ . then ( ( ) => transaction . commit ( ( err ) => {
157+ if ( err ) {
158+ console . error ( err ) ;
159+ } else {
160+ console . log ( `Successfully executed read-write transaction to transfer ${ transferAmount } from Album 2 to Album 1.` ) ;
161+ }
162+ } ) ) ;
163+ } ) ;
164+ // [END read_write_transaction]
168165}
169166
170167const cli = require ( `yargs` )
0 commit comments