Skip to content

Commit 0933148

Browse files
authored
switch to axios (#178)
1 parent e689afe commit 0933148

File tree

5 files changed

+280
-479
lines changed

5 files changed

+280
-479
lines changed

app.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,12 @@ const exportStream = function (ws, opts, callback) {
129129
let headings = []
130130
let lastsize = 0
131131
iam.getToken(process.env.IAM_API_KEY).then((t) => {
132-
const nanoopts = { url: opts.url }
132+
let headers = {}
133133
if (t) {
134-
nanoopts.defaultHeaders = { Authorization: 'Bearer ' + t }
134+
headers = { Authorization: 'Bearer ' + t }
135135
}
136-
const Nano = require('nano')
137-
const nano = Nano(nanoopts)
138136
const ChangesReader = require('changesreader')
139-
const changesReader = new ChangesReader(opts.database, nano.request)
137+
const changesReader = new ChangesReader(opts.database, opts.url, headers)
140138

141139
const changesOpts = {
142140
batchSize: opts.buffer,

includes/writer.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const async = require('async')
22
const debug = require('debug')('couchimport')
33
const iam = require('./iam.js')
4+
const axios = require('axios').default
45

56
// look for IAM_API_KEY
67
const IAM_API_KEY = process.env.IAM_API_KEY ? process.env.IAM_API_KEY : null
@@ -18,12 +19,10 @@ module.exports = function (couchURL, couchDatabase, bufferSize, parallelism, ign
1819
iam.getToken(IAM_API_KEY).then(function (t) {
1920
iamAccessToken = t
2021

21-
const opts = { url: couchURL }
22+
const headers = { }
2223
if (IAM_API_KEY && iamAccessToken) {
23-
opts.defaultHeaders = { Authorization: 'Bearer ' + iamAccessToken }
24+
headers.Authorization = 'Bearer ' + iamAccessToken
2425
}
25-
const cloudant = require('nano')(opts)
26-
const db = cloudant.db.use(couchDatabase)
2726

2827
// process the writes in bulk as a queue
2928
const q = async.queue(async (payload) => {
@@ -48,7 +47,16 @@ module.exports = function (couchURL, couchDatabase, bufferSize, parallelism, ign
4847
keys.push(payload.docs[i]._id)
4948
}
5049
}
51-
const existingData = await db.fetch({ keys: keys })
50+
const req = {
51+
method: 'post',
52+
baseURL: couchURL,
53+
url: couchDatabase + '/_all_docs',
54+
data: { keys: keys },
55+
headers: headers
56+
}
57+
const response = axios(req)
58+
const existingData = response.data
59+
5260
// make lookup table between id-->rev
5361
const lookup = {}
5462
for (i in existingData.rows) {
@@ -73,7 +81,15 @@ module.exports = function (couchURL, couchDatabase, bufferSize, parallelism, ign
7381
// write data
7482
let data = null
7583
try {
76-
data = await db.bulk(payload)
84+
const req = {
85+
method: 'post',
86+
baseURL: couchURL,
87+
url: couchDatabase + '/_bulk_docs',
88+
headers: headers,
89+
data: payload
90+
}
91+
const response = await axios(req)
92+
data = response.data
7793
} catch (e) {
7894
console.log('ERR', e)
7995
writer.emit('writeerror', e)

0 commit comments

Comments
 (0)