1 | # Couchbase Node.js Client
|
2 |
|
3 | The Node.js SDK library allows you to connect to a Couchbase cluster from
|
4 | Node.js. It is a native Node.js module and uses the very fast libcouchbase
|
5 | library to handle communicating to the cluster over the Couchbase binary
|
6 | protocol.
|
7 |
|
8 | ## Useful Links
|
9 |
|
10 | Source - [https://github.com/couchbase/couchnode](https://github.com/couchbase/couchnode)
|
11 |
|
12 | Bug Tracker - [https://www.couchbase.com/issues/browse/JSCBC](https://www.couchbase.com/issues/browse/JSCBC)
|
13 |
|
14 | Couchbase Developer Portal - [https://docs.couchbase.com/](https://docs.couchbase.com/nodejs-sdk/3.0/hello-world/start-using-sdk.html)
|
15 |
|
16 | Release Notes - [https://docs.couchbase.com/nodejs-sdk/3.0/project-docs/sdk-release-notes.html](https://docs.couchbase.com/nodejs-sdk/3.0/project-docs/sdk-release-notes.html)
|
17 |
|
18 | ## Installing
|
19 |
|
20 | To install the lastest release using npm, run:
|
21 |
|
22 | ```bash
|
23 | npm install couchbase
|
24 | ```
|
25 |
|
26 | To install the development version directly from github, run:
|
27 |
|
28 | ```bash
|
29 | npm install "git+https://github.com/couchbase/couchnode.git#master"
|
30 | ```
|
31 |
|
32 | ## Introduction
|
33 |
|
34 | Connecting to a Couchbase bucket is as simple as creating a new `Cluster`
|
35 | instance to represent the `Cluster` you are using, and then using the
|
36 | `bucket` and `collection` commands against this to open a connection to
|
37 | open your specific bucket and collection. You are able to execute most
|
38 | operations immediately, and they will be queued until the connection is
|
39 | successfully established.
|
40 |
|
41 | Here is a simple example of instantiating a connection, adding a new document
|
42 | into the bucket and then retrieving its contents:
|
43 |
|
44 | **Javascript:**
|
45 | ```javascript
|
46 | const couchbase = require('couchbase')
|
47 |
|
48 | async function main() {
|
49 | const cluster = await couchbase.connect(
|
50 | 'couchbase://127.0.0.1',
|
51 | {
|
52 | username: 'username',
|
53 | password: 'password',
|
54 | })
|
55 |
|
56 | const bucket = cluster.bucket('default')
|
57 | const coll = bucket.defaultCollection()
|
58 | await coll.upsert('testdoc', { foo: 'bar' })
|
59 |
|
60 | const res = await coll.get('testdoc')
|
61 | console.log(res.content)
|
62 | }
|
63 |
|
64 | // Run the main function
|
65 | main()
|
66 | .then((_) => {
|
67 | console.log ('Success!')
|
68 | })
|
69 | .catch((err) => {
|
70 | console.log('ERR:', err)
|
71 | })
|
72 | ```
|
73 |
|
74 | **Typescript:**
|
75 | ```javascript
|
76 | import {
|
77 | Bucket,
|
78 | Cluster,
|
79 | Collection,
|
80 | connect,
|
81 | GetResult,
|
82 | } from 'couchbase'
|
83 |
|
84 | async function main() {
|
85 | const cluster: Cluster = await connect(
|
86 | 'couchbase://127.0.0.1',
|
87 | {
|
88 | username: 'username',
|
89 | password: 'password',
|
90 | })
|
91 |
|
92 | const bucket: Bucket = cluster.bucket('default')
|
93 | const coll: Collection = bucket.defaultCollection()
|
94 | await coll.upsert('testdoc', { foo: 'bar' })
|
95 |
|
96 | const res: GetResult = await coll.get('testdoc')
|
97 | console.log(res.content)
|
98 | }
|
99 |
|
100 | // Run the main function
|
101 | main()
|
102 | .then((_) => {
|
103 | console.log ('Success!')
|
104 | })
|
105 | .catch((err) => {
|
106 | console.log('ERR:', err)
|
107 | })
|
108 | ```
|
109 |
|
110 | ## AWS Lambda
|
111 |
|
112 | Version 4.2.5 of the SDK significantly reduces the size of the prebuilt binary provided with the SDK on supported platforms. The reduction
|
113 | enables the SDK to meet the [minimum size requirements](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html) for an AWS lambda deployment package without extra steps for reducing the size of the package. However, if further size reduction is desired, the SDK provides a script to provide recommendations for size reduction.
|
114 |
|
115 | **Script:**
|
116 | ```bash
|
117 | npm explore couchbase -- npm run help-prune
|
118 | ```
|
119 |
|
120 | **Example output:**
|
121 | ```bash
|
122 | Checking for platform packages in /tmp/couchnode-test/node_modules/@couchbase that do not match the expected platform package (couchbase-linux-x64-openssl1).
|
123 | Found mismatch: Path=/tmp/couchnode-test/node_modules/@couchbase/couchbase-linuxmusl-x64-openssl1
|
124 |
|
125 | Recommendations for pruning:
|
126 |
|
127 | Removing mismatched platform=couchbase-linuxmusl-x64-openssl1 (path=/tmp/couchnode-test/node_modules/@couchbase/couchbase-linuxmusl-x64-openssl1) saves ~13.31 MB on disk.
|
128 | Removing Couchbase deps/ (path=/tmp/couchnode-test/node_modules/couchbase/deps) saves ~45.51 MB on disk.
|
129 | Removing Couchbase src/ (path=/tmp/couchnode-test/node_modules/couchbase/src) saves ~0.61 MB on disk.
|
130 | ```
|
131 |
|
132 | ## Documentation
|
133 |
|
134 | An extensive documentation is available on the Couchbase website - [https://docs.couchbase.com/nodejs-sdk/3.0/hello-world/start-using-sdk.html](https://docs.couchbase.com/nodejs-sdk/3.0/hello-world/start-using-sdk.html) -
|
135 | including numerous examples and code samples.
|
136 |
|
137 | Visit our [Couchbase Node.js SDK forum](https://forums.couchbase.com/c/node-js-sdk) for help.
|
138 | Or get involved in the [Couchbase Community](https://couchbase.com/community) on the [Couchbase](https://couchbase.com) website.
|
139 |
|
140 | ## Source Control
|
141 |
|
142 | The source code is available at
|
143 | [https://github.com/couchbase/couchnode](https://github.com/couchbase/couchnode).
|
144 | Once you have cloned the repository, you may contribute changes through our
|
145 | gerrit server. For more details see
|
146 | [CONTRIBUTING.md](https://github.com/couchbase/couchnode/blob/master/CONTRIBUTING.md).
|
147 |
|
148 | To execute our test suite, run `make test` from the root directory.
|
149 |
|
150 | To execute our code coverage, run `make cover` from the root directory.
|
151 |
|
152 | In addition to the full test suite and full code coverage, you may additionally
|
153 | execute a subset of the tests which excludes slow-running tests for quick
|
154 | verifications. These can be run through `make fasttest` and `make fastcover`
|
155 | respectively.
|
156 |
|
157 | Finally, to build the API reference for the project, run `make docs` from the
|
158 | root directory, and a docs folder will be created with the api reference.
|
159 |
|
160 | # Support & Additional Resources
|
161 |
|
162 | If you found an issue, please file it in our [JIRA](https://issues.couchbase.com/projects/JSCBC/issues/).
|
163 |
|
164 | The Couchbase Discord server is a place where you can collaborate about all things Couchbase. Connect with others from the community, learn tips and tricks, and ask questions. [Join Discord and contribute](https://discord.com/invite/sQ5qbPZuTh).
|
165 |
|
166 | You can ask questions in our [forums](https://forums.couchbase.com/).
|
167 |
|
168 | ## License
|
169 |
|
170 | Copyright 2013 Couchbase Inc.
|
171 |
|
172 | Licensed under the Apache License, Version 2.0.
|
173 |
|
174 | See
|
175 | [LICENSE](https://github.com/couchbase/couchnode/blob/master/LICENSE)
|
176 | for further details.
|