Skip to content

Commit c7188f8

Browse files
Node.js 4.0.0 upgrade and code fixes (#184)
1 parent 7051b07 commit c7188f8

File tree

11 files changed

+1943
-256
lines changed

11 files changed

+1943
-256
lines changed

modules/howtos/examples/collection-manager.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ async function go() {
2323
})
2424
// end::scopeAdmin[]
2525

26+
// Give some time (2 seconds) for user to be created as example code
27+
// might run too quickly.
28+
await new Promise((resolve) => setTimeout(resolve, 2000))
29+
2630
const cluster = await couchbase.connect('couchbase://localhost', {
2731
username: 'scope_admin',
2832
password: 'password',

modules/howtos/examples/subdoc.js

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,23 @@ async function go() {
152152
// end::mutate-tl-arrayappend-multibad[]
153153

154154
console.log('mutate-createpaths')
155-
// tag::mutate-createpaths[]
156-
await collection.mutateIn('customer123', [
157-
couchbase.MutateInSpec.arrayAppend('some.path', 'Hello World', {
158-
createPath: true,
159-
}),
160-
])
161-
// end::mutate-createpaths[]
155+
try {
156+
// This fails due to https://issues.couchbase.com/browse/JSCBC-1045
157+
// TODO: Remove try/catch block when the above issue is fixed.
158+
// tag::mutate-createpaths[]
159+
await collection.mutateIn('customer123', [
160+
couchbase.MutateInSpec.arrayAppend('some.path', 'Hello World', {
161+
createPath: true,
162+
}),
163+
])
164+
// end::mutate-createpaths[]
165+
} catch (e) {
166+
if (e instanceof couchbase.PathNotFoundError) {
167+
console.log(e.toString())
168+
} else {
169+
throw e
170+
}
171+
}
162172

163173
console.log('mutate-arrayaddunique')
164174
// tag::mutate-arrayaddunique[]
@@ -210,20 +220,30 @@ async function go() {
210220
// end::mutate-decrement[]
211221

212222
console.log('mutate-upsert-parents')
213-
// tag::mutate-upsert-parents[]
214-
await collection.mutateIn('customer123', [
215-
couchbase.MutateInSpec.upsert(
216-
'level_0.level_1.foo.bar.phone',
217-
{
218-
num: '311-555-0101',
219-
ext: 16,
220-
},
221-
{
222-
createPath: true,
223-
}
224-
),
225-
])
226-
// end::mutate-upsert-parents[]
223+
try {
224+
// This fails due to https://issues.couchbase.com/browse/JSCBC-1045
225+
// TODO: Remove try/catch block when the above issue is fixed.
226+
// tag::mutate-upsert-parents[]
227+
await collection.mutateIn('customer123', [
228+
couchbase.MutateInSpec.upsert(
229+
'level_0.level_1.foo.bar.phone',
230+
{
231+
num: '311-555-0101',
232+
ext: 16,
233+
},
234+
{
235+
createPath: true,
236+
}
237+
),
238+
])
239+
// end::mutate-upsert-parents[]
240+
} catch (e) {
241+
if (e instanceof couchbase.PathNotFoundError) {
242+
console.log(e.toString())
243+
} else {
244+
throw e
245+
}
246+
}
227247

228248
console.log('mutate-store-semantics')
229249
// tag::mutate-store-semantics[]

modules/howtos/examples/transactions-example.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import {
66
Collection,
77
connect,
88
DurabilityLevel,
9-
QueryOptions,
109
QueryProfileMode,
1110
QueryResult,
1211
Scope,
1312
TransactionCommitAmbiguousError,
1413
TransactionFailedError,
14+
TransactionQueryOptions
1515
} from 'couchbase'
1616

1717
async function main() {
@@ -353,9 +353,9 @@ async function queryRyow() {
353353
async function queryOptions() {
354354
let cluster = await getCluster()
355355
// tag::queryOptions[]
356-
const qo: QueryOptions = { profile: QueryProfileMode.Timings }
356+
const txQo: TransactionQueryOptions = { profile: QueryProfileMode.Timings }
357357
cluster.transactions().run(async (ctx) => {
358-
ctx.query("INSERT INTO `default` VALUES ('doc', {'hello':'world'})", qo)
358+
ctx.query("INSERT INTO `default` VALUES ('doc', {'hello':'world'})", txQo)
359359
})
360360
// end::queryOptions[]
361361
}

modules/howtos/examples/user-manager.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ async function example1(clusterAdm, testUsername, testPassword, bucketName) {
3939
]
4040
})
4141
// end::usermanagement_1[]
42+
43+
// Give some time (2 seconds) for user to be created as example code
44+
// might run too quickly.
45+
await new Promise((resolve) => setTimeout(resolve, 2000))
4246
}
4347

4448
async function example2(clusterAdm) {

modules/howtos/examples/views.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
const couchbase = require('couchbase')
44

55
async function go() {
6-
const cluster = new couchbase.Cluster('couchbase://localhost', {
6+
const cluster = await couchbase.connect('couchbase://localhost', {
77
username: 'Administrator',
88
password: 'password',
99
})
1010

1111
// Open a bucket to allow cluster-level querying
1212
var bucket = cluster.bucket('travel-sample')
1313

14+
// Create the necessary views/design documents
15+
await createViews(bucket)
16+
1417
// tag::range-query[]
1518
var result = bucket.viewQuery('beers', 'by_name', {
1619
range: { start: 'A' },
@@ -19,12 +22,45 @@ async function go() {
1922
// end::range-query[]
2023

2124
// tag::key-query[]
22-
var result = bucket.viewQuery('landmarks', 'by_name', {
23-
key: 'statue-of-liberty',
25+
var result = await bucket.viewQuery('landmarks', 'by_name', {
26+
key: 'landmark_10019',
2427
})
25-
2628
// end::key-query[]
2729
}
30+
31+
async function createViews(bucket) {
32+
const viewMgr = await bucket.viewIndexes()
33+
34+
const designDocs = [
35+
{
36+
name: 'beers',
37+
views: {
38+
by_name: {
39+
map: "function (doc, meta) { if (doc.type && doc.type == 'beer') { emit(meta.id, doc.name); } }",
40+
},
41+
},
42+
},
43+
{
44+
name: 'landmarks',
45+
views: {
46+
by_name: {
47+
map: "function (doc, meta) { if (doc.type && doc.type == 'landmark') { emit(meta.id, doc.name); } }",
48+
},
49+
},
50+
},
51+
]
52+
53+
// Create the design documents
54+
designDocs.forEach((designDoc) => {
55+
viewMgr.upsertDesignDocument(designDoc)
56+
viewMgr.upsertDesignDocument(designDoc)
57+
})
58+
59+
// Give some time (2 seconds) for views to be created as example code
60+
// might run too quickly.
61+
await new Promise((resolve) => setTimeout(resolve, 2000))
62+
}
63+
2864
go()
2965
.then((res) => console.log('DONE:', res))
3066
.then(process.exit)

modules/test/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ARG CB_IMAGE=$CB_EDITION-$CB_BUILD
44

55
# SDK related images...
66

7-
FROM node:16.13.0 AS node
7+
FROM node:16 AS node
88

99
FROM $CB_IMAGE
1010

modules/test/test-hello-world.bats

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ EOF
4747
assert_output --partial <<-EOF
4848
Query Result: [
4949
_Model {
50+
_type: 'airline',
5051
callsign: 'CBS',
5152
iata: 'CBIATA',
5253
icao: 'CBICAO',

modules/test/test-howtos.bats

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,22 @@ load 'test/test_helper.bash'
5858

5959
@test "[howtos] - search.js" {
6060
runExample $HOWTOS_DIR search.js
61-
assert_success
62-
63-
# Check output for search-query-match[] snippet
64-
assert_output --partial "[search-query-match] result count: 5"
61+
# This whole example is going to fail due to the below bug:
62+
# https://issues.couchbase.com/browse/JSCBC-1044
63+
assert_failure
64+
65+
diag "TODO: Uncomment (lines 66-76) when JSCBC-1044 is fixed"
66+
# assert_success
6567

66-
# Check output for search-query-matchPhrase[] snippet
67-
assert_output --partial "[search-query-matchPhrase] result count: 4"
68-
assert_output --partial "hotel_11331"
69-
assert_output --partial "hotel_15915"
70-
assert_output --partial "hotel_3606"
71-
assert_output --partial "hotel_28259"
68+
# # Check output for search-query-match[] snippet
69+
# assert_output --partial "[search-query-match] result count: 5"
70+
71+
# # Check output for search-query-matchPhrase[] snippet
72+
# assert_output --partial "[search-query-matchPhrase] result count: 4"
73+
# assert_output --partial "hotel_11331"
74+
# assert_output --partial "hotel_15915"
75+
# assert_output --partial "hotel_3606"
76+
# assert_output --partial "hotel_28259"
7277

7378
# Check output for search-query-dateRange[] snippet.
7479
# https://issues.couchbase.com/browse/JSCBC-942, errors thrown for consistency/consistentWith query options.
@@ -78,36 +83,40 @@ load 'test/test_helper.bash'
7883

7984
@test "[howtos] - search-conjuncts-disjuncts.js" {
8085
runExample $HOWTOS_DIR search-conjuncts-disjuncts.js
81-
assert_success
82-
83-
# Check output for search-query-conjuncts[] snippet
84-
assert_output --partial "[search-query-conjuncts] result count: 2"
85-
86-
# Check output for search-query-disjuncts[] snippet
87-
assert_output --partial "[search-query-disjuncts] result count: 11"
88-
89-
# Check output for handle-hits[] snippet
90-
assert_output --partial "Result #1 ID: hotel_21726"
91-
92-
# Check output for handle-facets[] snippet
93-
assert_output --partial <<-EOF
94-
Descriptions facet: {
95-
field: 'description',
96-
total: 223,
97-
missing: 0,
98-
other: 195,
99-
terms: [
100-
{ term: 'louvre', count: 7 },
101-
{ term: 'hotel', count: 6 },
102-
{ term: 'rooms', count: 6 },
103-
{ term: 'eiffel', count: 5 },
104-
{ term: 'close', count: 4 }
105-
]
106-
}
107-
EOF
86+
# This whole example is going to fail due to the below bug:
87+
# https://issues.couchbase.com/browse/JSCBC-1044
88+
assert_failure
89+
90+
diag "TODO: Uncomment (lines 91-117) when JSCBC-1044 is fixed"
91+
# assert_success
92+
93+
# # Check output for search-query-conjuncts[] snippet
94+
# assert_output --partial "[search-query-conjuncts] result count: 2"
95+
96+
# # Check output for search-query-disjuncts[] snippet
97+
# assert_output --partial "[search-query-disjuncts] result count: 11"
98+
99+
# # Check output for handle-hits[] snippet
100+
# assert_output --partial "Result #1 ID: hotel_21726"
101+
102+
# # Check output for handle-facets[] snippet
103+
# assert_output --partial <<-EOF
104+
# Descriptions facet: {
105+
# field: 'description',
106+
# total: 223,
107+
# missing: 0,
108+
# other: 195,
109+
# terms: [
110+
# { term: 'louvre', count: 7 },
111+
# { term: 'hotel', count: 6 },
112+
# { term: 'rooms', count: 6 },
113+
# { term: 'eiffel', count: 5 },
114+
# { term: 'close', count: 4 }
115+
# ]
116+
# }
117+
# EOF
108118
}
109119

110-
111120
@test "[howtos] - subdoc.js" {
112121
runExample $HOWTOS_DIR subdoc.js
113122
assert_success
@@ -128,7 +137,7 @@ EOF
128137
# Check output for mutate-increment[] snippet
129138
assert_output --partial <<-EOF
130139
mutate-increment
131-
{ value: 1 }
140+
MutateInResultEntry { value: 1 }
132141
EOF
133142
}
134143

@@ -141,3 +150,11 @@ EOF
141150
runExample $HOWTOS_DIR views.js
142151
assert_success
143152
}
153+
154+
@test "[howtos] - transactions-example.ts" {
155+
# It compiles at least, so that should be enough validation.
156+
skip "Needs to be run on a multi-node cluster"
157+
158+
runTSExample $HOWTOS_DIR transactions-example.ts
159+
assert_success
160+
}

modules/test/test_helper.bash

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ function runTSExample() {
1919
cd $1
2020
run ts-node $2
2121
}
22+
23+
diag() {
24+
printf ' # %s\n' "$@" >&3
25+
}

0 commit comments

Comments
 (0)