Skip to content

Commit cd3aa37

Browse files
Getting Started tweaks (#196)
* Getting Started tweaks
1 parent 0350afc commit cd3aa37

File tree

11 files changed

+434
-358
lines changed

11 files changed

+434
-358
lines changed
Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,77 @@
1+
// tag::imports[]
12
var couchbase = require('couchbase')
3+
// end::imports[]
24

3-
const clusterConnStr =
4-
'couchbases://cb.abcdefab-cdef-abcd-efab-cdefabcdef.dp.cloud.couchbase.com'
5-
const cloudRootCertificate = '/etc/x509-cert/SSLCA/clientdir/trust.pem'
6-
const username = 'user'
7-
const password = 'password'
8-
const bucketName = 'couchbasecloudbucket'
5+
async function main() {
6+
// tag::connect[]
7+
const clusterConnStr = 'couchbases://cb.<your-endpoint>.cloud.couchbase.com'
8+
const username = 'Administrator'
9+
const password = 'Password123!'
10+
const bucketName = 'travel-sample'
911

10-
async function go() {
1112
const cluster = await couchbase.connect(clusterConnStr, {
1213
username: username,
1314
password: password,
14-
trustStorePath: cloudRootCertificate,
15+
timeouts: {
16+
kvTimeout: 10000, // milliseconds
17+
},
1518
})
19+
// end::connect[]
1620

21+
// tag::bucket[]
1722
const bucket = cluster.bucket(bucketName)
18-
const collection = bucket.defaultCollection()
23+
// end::bucket[]
1924

20-
// Create a N1QL Primary Index (but ignore if it exists)
21-
await cluster
22-
.queryIndexes()
23-
.createPrimaryIndex(bucketName, { ignoreExists: true })
25+
// tag::default-collection[]
26+
// Get a reference to the default collection, required only for older Couchbase server versions
27+
const defaultCollection = bucket.defaultCollection()
28+
// end::default-collection[]
2429

30+
// tag::collection[]
31+
const collection = bucket.scope('tenant_agent_00').collection('users')
32+
// end::collection[]
33+
34+
// tag::test-doc[]
35+
const user = {
36+
type: 'user',
37+
name: 'Michael',
38+
email: 'michael123@test.com',
39+
interests: ['Swimming', 'Rowing'],
40+
}
41+
// end::test-doc[]
42+
43+
// tag::upsert[]
2544
// Create and store a document
26-
await collection.upsert('user:king_arthur', {
27-
name: 'Arthur',
28-
email: 'kingarthur@couchbase.com',
29-
interests: ['Holy Grail', 'African Swallows'],
30-
})
45+
await collection.upsert('michael123', user)
46+
// end::upsert[]
3147

48+
// tag::get[]
3249
// Load the Document and print it
3350
// Prints Content and Metadata of the stored Document
34-
let getResult = await collection.get('user:king_arthur')
35-
console.log('got: ', getResult)
51+
let getResult = await collection.get('michael123')
52+
console.log('Get Result: ', getResult)
53+
// end::get[]
3654

55+
// tag::query[]
3756
// Perform a N1QL Query
38-
let queryResult = await cluster.query(
39-
'SELECT name FROM ' + bucketName + ' WHERE $1 in interests LIMIT 1',
40-
{ parameters: ['African Swallows'] }
41-
)
57+
const queryResult = await bucket
58+
.scope('tenant_agent_00')
59+
.query('SELECT name FROM `users` WHERE $1 in interests', {
60+
parameters: ['Swimming'],
61+
})
62+
console.log('Query Results:')
4263
queryResult.rows.forEach((row) => {
43-
console.log('query row: ', row)
64+
console.log(row)
4465
})
66+
// end::query[]
4567
}
46-
go()
68+
69+
// tag::run-main[]
70+
// Run the main function
71+
main()
72+
.catch((err) => {
73+
console.log('ERR:', err)
74+
process.exit(1)
75+
})
76+
.then(process.exit)
77+
// end::run-main[]
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict'
2+
3+
// tag::ts-imports[]
4+
import {
5+
Bucket,
6+
Cluster,
7+
Collection,
8+
connect,
9+
GetResult,
10+
QueryResult,
11+
} from 'couchbase'
12+
// end::ts-imports[]
13+
14+
async function main() {
15+
// tag::ts-connect[]
16+
const clusterConnStr: string =
17+
'couchbases://cb.<your-endpoint>.cloud.couchbase.com'
18+
const username: string = 'Administrator'
19+
const password: string = 'Password123!'
20+
const bucketName: string = 'travel-sample'
21+
22+
const cluster: Cluster = await connect(clusterConnStr, {
23+
username: username,
24+
password: password,
25+
timeouts: {
26+
kvTimeout: 10000, // milliseconds
27+
},
28+
})
29+
// end::ts-connect[]
30+
31+
// tag::ts-bucket[]
32+
const bucket: Bucket = cluster.bucket(bucketName)
33+
// end::ts-bucket[]
34+
35+
// tag::ts-collection[]
36+
const collection: Collection = bucket
37+
.scope('tenant_agent_00')
38+
.collection('users')
39+
// end::ts-collection[]
40+
41+
// tag::ts-default-collection[]
42+
// Get a reference to the default collection, required only for older Couchbase server versions
43+
const collection_default: Collection = bucket.defaultCollection()
44+
// end::ts-default-collection[]
45+
46+
// tag::ts-test-doc[]
47+
interface User {
48+
type: string
49+
name: string
50+
email: string
51+
interests: string[]
52+
}
53+
54+
const user: User = {
55+
type: 'user',
56+
name: 'Michael',
57+
email: 'michael123@test.com',
58+
interests: ['Swimming', 'Rowing'],
59+
}
60+
// end::ts-test-doc[]
61+
62+
// tag::ts-upsert[]
63+
await collection.upsert('michael123', user)
64+
// end::ts-upsert[]
65+
66+
// tag::ts-get[]
67+
// Load the Document and print it
68+
// Prints Content and Metadata of the stored document
69+
const getResult: GetResult = await collection.get('michael123')
70+
console.log('Get Result:', getResult)
71+
// end::ts-get[]
72+
73+
// tag::ts-query[]
74+
// Perform a N1QL Query
75+
const queryResult: QueryResult = await bucket
76+
.scope('tenant_agent_00')
77+
.query('SELECT name FROM `users` WHERE $1 in interests', {
78+
parameters: ['Swimming'],
79+
})
80+
console.log('Query Results:')
81+
queryResult.rows.forEach((row) => {
82+
console.log(row)
83+
})
84+
// end::ts-query[]
85+
}
86+
87+
// tag::ts-run-main[]
88+
// Run the main function
89+
main()
90+
.catch((err) => {
91+
console.log('ERR:', err)
92+
process.exit(1)
93+
})
94+
.then(() => process.exit(0))
95+
// end::ts-run-main[]
Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,65 @@
1-
'use strict'
2-
3-
// tag::connect[]
4-
const couchbase = require('couchbase')
1+
var couchbase = require('couchbase')
52

63
async function main() {
7-
const cluster = await couchbase.connect('couchbase://localhost', {
8-
username: 'Administrator',
9-
password: 'password',
4+
// tag::connect[]
5+
// For a secure cluster connection, use `couchbases://<your-cluster-ip>` instead.
6+
const clusterConnStr = 'couchbase://localhost'
7+
// const cloudRootCertificate = '/etc/x509-cert/SSLCA/clientdir/trust.pem'
8+
const username = 'Administrator'
9+
const password = 'password'
10+
const bucketName = 'travel-sample'
11+
12+
const cluster = await couchbase.connect(clusterConnStr, {
13+
username: username,
14+
password: password,
15+
// Uncomment if you require a secure cluster connection (TSL/SSL).
16+
// This is strongly recommended for production use.
17+
// trustStorePath: cloudRootCertificate,
1018
})
1119
// end::connect[]
1220

13-
// tag::bucket[]
14-
// get a reference to our bucket
15-
const bucket = cluster.bucket('travel-sample')
16-
// end::bucket[]
17-
18-
// tag::collection[]
19-
// get a reference to a collection
20-
const collection = bucket.scope('inventory').collection('airline')
21-
// end::collection[]
21+
const bucket = cluster.bucket(bucketName)
2222

23-
// tag::default-collection[]
24-
// get a reference to the default collection, required for older Couchbase server versions
23+
// Get a reference to the default collection, required only for older Couchbase server versions
2524
const collection_default = bucket.defaultCollection()
26-
// end::default-collection[]
2725

28-
// tag::test-doc[]
29-
const airline = {
30-
type: 'airline',
31-
id: 8091,
32-
callsign: 'CBS',
33-
iata: 'IATA',
34-
icao: 'ICAO',
35-
name: 'Couchbase Airways',
36-
}
37-
// end::test-doc[]
26+
const collection = bucket.scope('tenant_agent_00').collection('users')
3827

39-
// tag::upsert-func[]
40-
const upsertDocument = async (doc) => {
41-
try {
42-
// key will equal: "airline_8091"
43-
const key = `${doc.type}_${doc.id}`
44-
const result = await collection.upsert(key, doc)
45-
console.log('Upsert Result: ')
46-
console.log(result)
47-
} catch (error) {
48-
console.error(error)
49-
}
50-
}
51-
// end::upsert-func[]
28+
// Create and store a document
29+
await collection.upsert('michael123', {
30+
type: 'user',
31+
name: 'Michael',
32+
email: 'michael123@test.com',
33+
interests: ['Swimming', 'Rowing'],
34+
})
5235

53-
// tag::upsert-invoke[]
54-
await upsertDocument(airline)
55-
// end::upsert-invoke[]
36+
// Load the Document and print it
37+
// Prints Content and Metadata of the stored Document
38+
const getResult = await collection.get('michael123')
39+
console.log('Get Result: ', getResult)
5640

57-
// tag::get-func[]
58-
const getAirlineByKey = async (key) => {
59-
try {
60-
const result = await collection.get(key)
61-
console.log('Get Result: ')
62-
console.log(result)
63-
} catch (error) {
64-
console.error(error)
65-
}
66-
}
67-
// end::get-func[]
41+
// Create a scoped primary index so we can query data
42+
await cluster.queryIndexes().createPrimaryIndex('travel-sample', {
43+
scopeName: 'tenant_agent_00',
44+
collectionName: 'users',
45+
ignoreIfExists: true,
46+
})
6847

69-
// tag::get-invoke[]
70-
await getAirlineByKey('airline_8091')
48+
// Perform a N1QL Query
49+
const queryResult = await bucket
50+
.scope('tenant_agent_00')
51+
.query('SELECT name FROM `users` WHERE $1 in interests', {
52+
parameters: ['Swimming'],
53+
})
54+
console.log('Query Results:')
55+
queryResult.rows.forEach((row) => {
56+
console.log(row)
57+
})
7158
}
72-
// end::get-invoke[]
7359

74-
// tag::run-main[]
75-
// Run the main function
7660
main()
7761
.catch((err) => {
7862
console.log('ERR:', err)
7963
process.exit(1)
8064
})
8165
.then(process.exit)
82-
// end::run-main[]

0 commit comments

Comments
 (0)