Skip to content

Commit 954c291

Browse files
author
Glynn Bird
committed
make the library ESM-only. Exchange all "require"s for "import"s.
1 parent 8dcfdd7 commit 954c291

File tree

70 files changed

+336
-341
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+336
-341
lines changed

README.md

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Offical [Apache CouchDB](https://couchdb.apache.org/) library for [Node.js](http
77
> Note: Nano >=11.0.0 is a **breaking change for Node.js versions 16 and older**. Nano 11 uses Node.js's built-in "fetch" HTTP client but this is only available in Node.js versions 18 or later. If you are using Node 16 or older then continue using Nano 10.
88
> See our [migration guide](migration_guide_v10_to_v11.md) for moving from Nano 10 to Nano 11.
99
10+
> Note: Nano >=12.0.0 is ESM-only, meaning that the `import` syntax is used to load the module instead of `require`.
11+
1012
Features:
1113

1214
* **Minimalistic** - There is only a minimum of abstraction between you and
@@ -26,7 +28,7 @@ or save `nano` as a dependency of your project with
2628

2729
npm install --save nano
2830

29-
Note the minimum required version of Node.js is 10.
31+
Note the minimum required version of Node.js is 20.
3032

3133
## Table of contents
3234

@@ -108,21 +110,22 @@ Note the minimum required version of Node.js is 10.
108110
To use `nano` you need to connect it to your CouchDB install, to do that:
109111

110112
```js
111-
const nano = require('nano')('http://127.0.0.1:5984');
113+
import Nano from 'nano'
114+
const nano = Nano('http://127.0.0.1:5984')
112115
```
113116

114117
> Note: Supplying authentication credentials in the URL e.g. `http://admin:mypassword@localhost:5984` is deprecated. Use `nano.auth` instead.
115118
116119
To create a new database:
117120

118121
```js
119-
nano.db.create('alice');
122+
nano.db.create('alice')
120123
```
121124

122125
and to use an existing database:
123126

124127
```js
125-
const alice = nano.db.use('alice');
128+
const alice = nano.db.use('alice')
126129
```
127130

128131
Under-the-hood, calls like `nano.db.create` are making HTTP API calls to the CouchDB service. Such operations are *asynchronous*. There are two ways to receive the asynchronous data back from the library
@@ -184,35 +187,38 @@ You can also see your document in futon (http://127.0.0.1:5984/_utils).
184187
Configuring nano to use your database server is as simple as:
185188

186189
```js
187-
const nano = require('nano')('http://127.0.0.1:5984')
188-
const db = nano.use('foo');
190+
import Nano from 'nano'
191+
const nano = Nano('http://127.0.0.1:5984')
192+
const db = nano.use('foo')
189193
```
190194

191195
If you don't need to instrument database objects you can simply:
192196

193197
```js
194198
// nano parses the URL and knows this is a database
195-
const db = require('nano')('http://127.0.0.1:5984/foo');
199+
const db = Nano('http://127.0.0.1:5984/foo')
196200
```
197201

198202
You can tell nano to not parse the URL (maybe the server is behind a proxy, is accessed through a rewrite rule or other):
199203

200204
```js
201205
// nano does not parse the URL and return the server api
202206
// "http://127.0.0.1:5984/prefix" is the CouchDB server root
203-
const couch = require('nano')(
207+
const couch = Nano(
204208
{
205209
url : "http://127.0.0.1:5984/prefix"
206210
parseUrl : false
207-
});
208-
const db = couch.use('foo');
211+
})
212+
const db = couch.use('foo')
209213
```
210214

211215
### Pool size and open sockets
212216

213217
To specify the number of connections, timeouts and pool size, supply an `agentOptions` object when starting up Nano.
214218

215219
```js
220+
import Nano from 'nano'
221+
import undici from 'undici'
216222
const agentOptions = {
217223
bodyTimeout: 30000,
218224
headersTimeout: 30000,
@@ -229,7 +235,6 @@ const agentOptions = {
229235
connections: null,
230236
maxRedirections: 0
231237
}
232-
const undici = require('undici')
233238
const undiciOptions = new undici.Agent(agentOptions)
234239
const nano = Nano({ url: 'http://127.0.0.1:5984', undiciOptions })
235240
```
@@ -251,13 +256,13 @@ To supply customer headers with each request, supply a headers object when start
251256

252257

253258
```js
254-
const couch = require('nano')(
259+
const couch = Nano(
255260
{
256261
url : "http://127.0.0.1:5984/prefix"
257262
headers: {
258263
mycustomheader: '42'
259264
}
260-
});
265+
})
261266
```
262267

263268
## TypeScript
@@ -344,7 +349,7 @@ Lists all the CouchDB databases as a stream:
344349
```js
345350
nano.db.listAsStream()
346351
.on('error', (e) => console.error('error', e))
347-
.pipe(process.stdout);
352+
.pipe(process.stdout)
348353
```
349354

350355
### nano.db.compact(name, [designname])
@@ -396,7 +401,7 @@ by the call to `replication.enable`:
396401
const r = await nano.db.replication.enable('alice',
397402
'http://admin:password@otherhost.com:5984/alice',
398403
{ create_target:true })
399-
await nano.db.replication.disable(r.id);
404+
await nano.db.replication.disable(r.id)
400405
```
401406

402407
### nano.db.changes(name, [params])
@@ -413,7 +418,7 @@ const c = await nano.db.changes('alice')
413418
Same as `nano.db.changes` but returns a stream.
414419

415420
```js
416-
nano.db.changes('alice').pipe(process.stdout);
421+
nano.db.changes('alice').pipe(process.stdout)
417422
```
418423

419424
### nano.db.info()
@@ -429,7 +434,7 @@ const info = await nano.db.info()
429434
Returns a database object that allows you to perform operations against that database:
430435

431436
```js
432-
const alice = nano.use('alice');
437+
const alice = nano.use('alice')
433438
await alice.insert({ happy: true }, 'rabbit')
434439
```
435440

@@ -505,7 +510,7 @@ The response is an object with [CouchDB cluster information](https://docs.couchd
505510
Inserts `doc` in the database with optional `params`. If params is a string, it's assumed it is the intended document `_id`. If params is an object, it's passed as query string parameters and `docName` is checked for defining the document `_id`:
506511

507512
```js
508-
const alice = nano.use('alice');
513+
const alice = nano.use('alice')
509514
const response = await alice.insert({ happy: true }, 'rabbit')
510515
```
511516

@@ -570,7 +575,7 @@ Bulk operations(update/delete/insert) on the database, refer to the
570575
const documents = [
571576
{ a:1, b:2 },
572577
{ _id: 'tiger', striped: true}
573-
];
578+
]
574579
const response = await alice.bulk({ docs: documents })
575580
```
576581

@@ -581,9 +586,9 @@ List all the docs in the database .
581586
```js
582587
const doclist = await alice.list().then((body)=>{
583588
body.rows.forEach((doc) => {
584-
console.log(doc);
589+
console.log(doc)
585590
})
586-
});
591+
})
587592
```
588593

589594
or with optional query string additions `params`:
@@ -610,7 +615,7 @@ additional query string `params` can be specified, `include_docs` is always set
610615
to `true`.
611616

612617
```js
613-
const keys = ['tiger', 'zebra', 'donkey'];
618+
const keys = ['tiger', 'zebra', 'donkey']
614619
const datat = await alice.fetch({keys: keys})
615620
```
616621

@@ -632,7 +637,7 @@ Create index on database fields, as specified in
632637
const indexDef = {
633638
index: { fields: ['foo'] },
634639
name: 'fooindex'
635-
};
640+
}
636641
const response = await alice.createIndex(indexDef)
637642
```
638643

@@ -656,11 +661,11 @@ const db = nano.db.use('mydb')
656661
db.changesReader.start()
657662
.on('change', (change) => { console.log(change) })
658663
.on('batch', (b) => {
659-
console.log('a batch of', b.length, 'changes has arrived');
664+
console.log('a batch of', b.length, 'changes has arrived')
660665
}).on('seq', (s) => {
661-
console.log('sequence token', s);
666+
console.log('sequence token', s)
662667
}).on('error', (e) => {
663-
console.error('error', e);
668+
console.error('error', e)
664669
})
665670
```
666671

@@ -671,16 +676,16 @@ If you want `changesReader` to hold off making the next `_changes` API call unti
671676
```js
672677
db.changesReader.get({wait: true})
673678
.on('batch', (b) => {
674-
console.log('a batch of', b.length, 'changes has arrived');
679+
console.log('a batch of', b.length, 'changes has arrived')
675680
// do some asynchronous work here and call "changesReader.resume()"
676681
// when you're ready for the next API call to be dispatched.
677682
// In this case, wait 5s before the next changes feed request.
678683
setTimeout( () => {
679684
db.changesReader.resume()
680685
}, 5000)
681686
}).on('end', () => {
682-
console.log('changes feed monitoring has stopped');
683-
});
687+
console.log('changes feed monitoring has stopped')
688+
})
684689
```
685690

686691
You may supply a number of options when you start to listen to the changes feed:
@@ -947,13 +952,13 @@ Inserts a `doc` together with `attachments` and `params`. If params is a string,
947952
The `attachments` parameter must be an array of objects with `name`, `data` and `content_type` properties.
948953

949954
```js
950-
const fs = require('fs');
955+
import fs from 'node:fs'
951956

952957
fs.readFile('rabbit.png', (err, data) => {
953958
if (!err) {
954959
await alice.multipart.insert({ foo: 'bar' }, [{name: 'rabbit.png', data: data, content_type: 'image/png'}], 'mydoc')
955960
}
956-
});
961+
})
957962
```
958963

959964
### db.multipart.get(docname, [params])
@@ -973,7 +978,7 @@ Inserts an attachment `attname` to `docname`, in most cases
973978
[CouchDB doc](https://docs.couchdb.org/en/latest/api/document/attachments.html#db-doc-attachment) for more details.
974979

975980
```js
976-
const fs = require('fs');
981+
import fs from 'node:fs'
977982

978983
fs.readFile('rabbit.png', (err, data) => {
979984
if (!err) {
@@ -983,7 +988,7 @@ fs.readFile('rabbit.png', (err, data) => {
983988
'image/png',
984989
{ rev: '12-150985a725ec88be471921a54ce91452' })
985990
}
986-
});
991+
})
987992
```
988993

989994
### db.attachment.insertAsStream(docname, attname, att, contenttype, [params])
@@ -997,7 +1002,7 @@ Get `docname`'s attachment `attname` with optional query string additions
9971002
`params`.
9981003

9991004
```js
1000-
const fs = require('fs');
1005+
import fs from 'node:fs'
10011006

10021007
const body = await alice.attachment.get('rabbit', 'rabbit.png')
10031008
fs.writeFile('rabbit.png', body)
@@ -1006,10 +1011,10 @@ fs.writeFile('rabbit.png', body)
10061011
### db.attachment.getAsStream(docname, attname, [params])
10071012

10081013
```js
1009-
const fs = require('fs');
1014+
import fs from 'node:fs'
10101015
alice.attachment.getAsStream('rabbit', 'rabbit.png')
10111016
.on('error', e => console.error)
1012-
.pipe(fs.createWriteStream('rabbit.png'));
1017+
.pipe(fs.createWriteStream('rabbit.png'))
10131018
```
10141019

10151020
### db.attachment.destroy(docname, attname, [params])
@@ -1059,7 +1064,7 @@ Same as `db.view` but returns a stream:
10591064
```js
10601065
alice.viewAsStream('characters', 'happy_ones', {reduce: false})
10611066
.on('error', (e) => console.error('error', e))
1062-
.pipe(process.stdout);
1067+
.pipe(process.stdout)
10631068
```
10641069

10651070
### db.viewWithList(designname, viewname, listname, [params])
@@ -1077,7 +1082,7 @@ Calls a list function fed by the given view from the specified design document a
10771082
```js
10781083
alice.viewWithListAsStream('characters', 'happy_ones', 'my_list')
10791084
.on('error', (e) => console.error('error', e))
1080-
.pipe(process.stdout);
1085+
.pipe(process.stdout)
10811086
```
10821087

10831088
### db.show(designname, showname, doc_id, [params])
@@ -1138,7 +1143,7 @@ Check out the tests for a fully functioning example.
11381143
Calls a view of the specified design with optional query string additions `params`. Returns stream.
11391144

11401145
```js
1141-
alice.search('characters', 'happy_ones', { q: 'cat' }).pipe(process.stdout);
1146+
alice.search('characters', 'happy_ones', { q: 'cat' }).pipe(process.stdout)
11421147
```
11431148

11441149
### db.find(selector)
@@ -1154,7 +1159,7 @@ const q = {
11541159
},
11551160
fields: [ "name", "age", "tags", "url" ],
11561161
limit:50
1157-
};
1162+
}
11581163
const response = await alice.find(q)
11591164
```
11601165

@@ -1171,18 +1176,19 @@ const q = {
11711176
},
11721177
fields: [ "name", "age", "tags", "url" ],
11731178
limit:50
1174-
};
1179+
}
11751180
alice.findAsStream(q)
11761181
.on('error', (e) => console.error('error', e))
1177-
.pipe(process.stdout);
1182+
.pipe(process.stdout)
11781183
```
11791184

11801185
## using cookie authentication
11811186

11821187
Nano supports making requests using CouchDB's [cookie authentication](http://guide.couchdb.org/editions/1/en/security.html#cookies) functionality. If you initialise *Nano* so that it is cookie-aware, you may call `nano.auth` first to get a session cookie. Nano will behave like a web browser, remembering your session cookie and refreshing it if a new one is received in a future HTTP response.
11831188

11841189
```js
1185-
const nano = require('nano')({
1190+
import Nano from 'nano'
1191+
const nano = Nano({
11861192
url: 'http://127.0.0.1:5984'
11871193
})
11881194
const username = 'user'
@@ -1237,25 +1243,26 @@ function getrabbitrev(rev) {
12371243
doc: 'rabbit',
12381244
method: 'get',
12391245
params: { rev: rev }
1240-
});
1246+
})
12411247
}
12421248

12431249
getrabbitrev('4-2e6cdc4c7e26b745c2881a24e0eeece2').then((body) => {
1244-
console.log(body);
1245-
});
1250+
console.log(body)
1251+
})
12461252
```
12471253

12481254
### Pipes
12491255

12501256
You can pipe the return values of certain nano functions like other stream. For example if our `rabbit` document has an attachment with name `picture.png` you can pipe it to a `writable stream`:
12511257

12521258
```js
1253-
const fs = require('fs');
1254-
const nano = require('nano')('http://127.0.0.1:5984/');
1255-
const alice = nano.use('alice');
1259+
import fs from 'node:fs'
1260+
import Nano from 'nano'
1261+
const nano = Nano('http://127.0.0.1:5984/')
1262+
const alice = nano.use('alice')
12561263
alice.attachment.getAsStream('rabbit', 'picture.png')
12571264
.on('error', (e) => console.error('error', e))
1258-
.pipe(fs.createWriteStream('/tmp/rabbit.png'));
1265+
.pipe(fs.createWriteStream('/tmp/rabbit.png'))
12591266
```
12601267

12611268
then open `/tmp/rabbit.png` and you will see the rabbit picture.
@@ -1285,13 +1292,12 @@ const nano = Nano({ url: process.env.COUCH_URL, log: console.log })
12851292
You may supply your own logging function to format the data before output:
12861293

12871294
```js
1288-
const url = require('url')
12891295
const logger = (data) => {
12901296
// only output logging if there is an environment variable set
12911297
if (process.env.LOG === 'nano') {
12921298
// if this is a request
12931299
if (typeof data.err === 'undefined') {
1294-
const u = new url.URL(data.uri)
1300+
const u = new URL(data.uri)
12951301
console.log(data.method, u.pathname, data.qs)
12961302
} else {
12971303
// this is a response

0 commit comments

Comments
 (0)