Skip to content

Commit b930294

Browse files
vkediaAce Nassri
authored andcommitted
Updated the version of spanner library (#334)
* Updated the version of spanner library and changed the samples to work with that. * Fixed some lint errors * Converted callbacks to promises wherever possible * Fixed error handling * Used arrow functions
1 parent 266250c commit b930294

File tree

3 files changed

+93
-97
lines changed

3 files changed

+93
-97
lines changed

spanner/crud.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,26 @@ function insertData (instanceId, databaseId) {
6666
const singersTable = database.table('Singers');
6767
const albumsTable = database.table('Albums');
6868

69-
Promise.all([
70-
// Inserts rows into the Singers table
71-
// Note: Cloud Spanner interprets Node.js numbers as FLOAT64s, so
72-
// they must be converted to strings before being inserted as INT64s
73-
singersTable.insert([
74-
{ SingerId: '1', FirstName: 'Marc', LastName: 'Richards' },
75-
{ SingerId: '2', FirstName: 'Catalina', LastName: 'Smith' },
76-
{ SingerId: '3', FirstName: 'Alice', LastName: 'Trentor' },
77-
{ SingerId: '4', FirstName: 'Lea', LastName: 'Martin' },
78-
{ SingerId: '5', FirstName: 'David', LastName: 'Lomond' }
79-
]),
80-
69+
// Inserts rows into the Singers table
70+
// Note: Cloud Spanner interprets Node.js numbers as FLOAT64s, so
71+
// they must be converted to strings before being inserted as INT64s
72+
singersTable.insert([
73+
{ SingerId: '1', FirstName: 'Marc', LastName: 'Richards' },
74+
{ SingerId: '2', FirstName: 'Catalina', LastName: 'Smith' },
75+
{ SingerId: '3', FirstName: 'Alice', LastName: 'Trentor' },
76+
{ SingerId: '4', FirstName: 'Lea', LastName: 'Martin' },
77+
{ SingerId: '5', FirstName: 'David', LastName: 'Lomond' }
78+
])
79+
.then(() => {
8180
// Inserts rows into the Albums table
8281
albumsTable.insert([
8382
{ SingerId: '1', AlbumId: '1', AlbumTitle: 'Go, Go, Go' },
8483
{ SingerId: '1', AlbumId: '2', AlbumTitle: 'Total Junk' },
8584
{ SingerId: '2', AlbumId: '1', AlbumTitle: 'Green' },
8685
{ SingerId: '2', AlbumId: '2', AlbumTitle: 'Forever Hold your Peace' },
8786
{ SingerId: '2', AlbumId: '3', AlbumTitle: 'Terrified' }
88-
])
89-
])
87+
]);
88+
})
9089
.then(() => {
9190
console.log('Inserted data.');
9291
});

spanner/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"test": "cd ..; npm run st -- --verbose spanner/system-test/*.test.js"
99
},
1010
"dependencies": {
11-
"@google-cloud/spanner": "0.1.0",
11+
"@google-cloud/spanner": "0.3.0",
1212
"yargs": "6.6.0"
1313
},
1414
"engines": {

spanner/transaction.js

Lines changed: 79 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -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

170167
const cli = require(`yargs`)

0 commit comments

Comments
 (0)