-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCouchDBStorage.js
680 lines (643 loc) · 24.7 KB
/
CouchDBStorage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
import nano from 'nano'
import uuid from 'uuid'
import PromisE from './PromisE'
import {
isObj,
isStr,
isArr,
arrUnique,
isMap,
isValidNumber,
mapJoin,
fallbackIfFails,
isPositiveInteger,
objClean,
objSort,
isFn
} from './utils'
// globab connection for use with multiple databases
let connection
// all individual connections
const connections = {}
const debugTag = '[CouchDBStorage]'
let defaultUrl = fallbackIfFails(() => process.env.CouchDB_URL)
/**
* @name CouchDBStorage
* @summary a wrapper for `nano` NPM module for reading from and writing to CouchDB
* @description connection is only initialized only when first request is made or `getDB()` method is called.
*
* @param {String|Object|Null} connectionOrUrl Possible values:
* - string: CouchDB connection URL including username and password
* - object: existing connection
* - null: will use global `connection` if available.
* Alternatively, use a specific environment variable for individual database.
* The name of the environement variable must be in the following format:
* `CouchDB_URL_$DBNAME`
* Replace `$DBNAME` with database name. The same to be provide in param `dbName`
* @param {String} dbName database name
* @param {Array} fields (optional) fields to retreive whenever retrieving documents.
* This can be overridden in the `extraProps` argument wherever applicable.
* Default: `[]` (all fields)
* @param {Function} middleware (optional) middleware function invoked whenever any documents are retrieved. This
* can be used to alter documents before saving or retreiving them or simply do other stuff
* before save/fetch operation.
* Arguments:
* - docs Array: documents retrieved or about to be saved
* - save Boolean: true = save operation. False: fetch operation.
* - funcName String: name of the function that invoked the middleware
*
* Returns: docs Array OR undefined/null.
* If Array is returned it will override the docs retrieved or being saved.
*
* Caution: when saving docs make sure to not remove `_rev` & `_id`
* When retreiving documents exclude any sensitive properties that should not be exposed.
* To bypass middleware, use `CouchDBStorage.getDoc()`.
* @param {String} sortKeys if and when sort document properties. Executed whenever middleware is executed.
* Accepted values:
* - "always" (default): every time documents are retrieved and saved.
* - "save": whenever documents are saved (create/update)
* - any other value to prevent sorting
*
* @returns {CouchDBStorage}
*/
export default class CouchDBStorage {
constructor(
connectionOrUrl,
dbName,
fields = [],
middleware,
sortKeys = 'always'
) {
// Global DB name prefix and suffix applied to all databases excluding
const prefix = process.env.CouchDB_DBName_Prefix || ''
const suffix = process.env.CouchDB_DBName_Suffix || ''
dbName = process.env[`CouchDB_DBName_Override_${dbName}`]
|| `${prefix}${dbName}${suffix}`
// Connection/URL/DBName override for individual databases
let url = (process.env[`CouchDB_URL_${dbName}`] || '').trim()
if (!!url && isStr(url)) {
url = new URL(url)
if (url.pathname !== '/') {
// URL includes a DBName override
dbName = url.pathname.replace('/', '')
}
const { host, password, protocol, username } = url
url = `${protocol}//${username}:${password}@${host}`
// re-use existing connection to the same URL if available
url = connections[url] || url
}
this.connectionOrUrl = url || connectionOrUrl
this.db = null
this.dbName = dbName
this.fields = fields
this.middleware = async (
docs = [],
save = false,
funcName,
allowDelete = false
) => {
const isWrite = ['set', 'setAll'].includes(funcName)
// detach object reference to avoid any undesired effects caused by middleware()
if (isFn(middleware)) docs = docs.map(x => {
// prevent unwanted deletion of entries.
// Otherwise, frontend can send entries with "_deleted" and it will delete the entry.
if (isWrite && !allowDelete) delete x._deleted
return { ...x }
})
docs = middleware
&& await fallbackIfFails(
middleware,
[
docs,
save,
funcName,
],
docs // if middleware() throws exception revert back to docs
)
|| docs
switch (this.sortKeys) {
case 'save': // sort properties by name only when saving (create/update)
if (!isWrite) break
case 'always':// always sort properties by name
docs = docs.map(x => objSort(x))
break
}
return docs
}
this.sortKeys = sortKeys // save/always
// whether to use the global connection or database specific
this.useGlobalCon = !this.connectionOrUrl
// Forces the application to immediately attempt to connect.
// This is required because "nano" (CouchDB's official NPM module) does not handle connection error properly
// and the entire application crashes. Neither try-catch nor async-await can catch this freakish error!
// Doing this will make sure database connection error is thrown on application startup and not later.
if (!this.useGlobalCon) this.getDB()
}
/**
* @name createIndexes
* @summary create CouchDB indexes for this collection
*
* @param {*} indexes
* @returns
*/
async createIndexes(indexes = []) {
if (!indexes?.length) return
const db = await this.getDB()
const promises = indexes.map(index => db.createIndex(index))
return await PromisE.all(promises)
}
/**
* @name delete
* @summary delete documents
*
* @param {String|Object|Map|Array} ids Possible values:
* 1. String: document `_id`
* 2. Object: document with `_id` property. Will be ignored if does not include `_id`.
* 3. Map: collection of (2) with `_id` as key
* 4. Array: collection of (1) or (2) or both.
* @returns {*}
*/
async delete(ids = []) {
ids = isArr(ids)
? ids
: !isMap(ids)
? [ids]
// convert documents Map to array
: Array.from(ids)
.map(([_id, doc]) => ({ ...doc, _id }))
let documents = ids.filter(x => isObj(x) && !!x._id && x._rev)
ids = arrUnique(ids.filter(id => !!id && isStr(id)))
// nothing do to!
if (!ids.length && !documents.length) return
documents = [
...documents,
...(ids.length ? await this.getAll(ids, false) : []),
]
// exclude already deleted or not found documents
.filter(x => x && !x._deleted)
// add `_deleted` flag to mark the document for deletion
.map(d => ({ ...d, _deleted: true }))
// nothing to delete
if (!documents.length) return []
// save documents for deletion
return await this.setAll(documents)
}
/**
* @name find
* @summary find the first item matching criteria
*
* @param {Object} selector CouchDB selector for mango query
* @param {Object} extraProps (optional) extra properties to be supplied to mango query. Eg: for sorting.
*
* @returns {Object} document if available otherwise, undefined
*/
async find(selector, extraProps, timeout) {
let docs = await this.search(
selector,
1,
0,
false,
extraProps,
timeout,
)
return docs[0]
}
/**
* @name get
* @summary get document with specified fields. This method cannot retrieve any design documents.
*
* @param {String} id document ID. (the `_id` property)
* @param {Array} fields list of properties to retrieve.
*
* @returns {Object} document if available otherwise, undefined
*/
async get(id, fields = this.fields, timeout) {
return await this.find(
{ _id: id },
isArr(fields)
? { fields }
: undefined,
timeout
)
}
/**
* @name getAll
* @summary get all or specific documents from a database.
*
* @param {Array|Null} ids (optional) If document IDs supplied will retrieve all relevant documents in one go.
* Otherwise, will retrieve paginated documents by using `skip` and `limit`.
* @param {Boolean} asMap whether to return result as an `Array` or `Map`.
* Default: true
* @param {Number} limit (optional) maximum number of items to retrieve in one go.
* Ignored if `ids` supplied.
* Default: 25
* @param {Number} skip (optional) for pagination, number of items to skip.
* Ignored if `ids` supplied.
* Default: 0
* @param {Object} extraProps extra properties to be supplied to `searchRaw()`.
* Can be used for sorting, limiting which fields to retrieve etc.
* Only used when no IDs supplied.
* @param {Number} timeout timeout duration in milliseconds (only when no IDs supplied).
* Default: `15000`
* @returns {Map|Array}
*/
async getAll(
ids = [],
asMap = true,
limit = 25,
skip = 0,
extraProps,
timeout,
) {
const db = await this.getDB()
// if ids supplied only retrieve only those otherwise, retrieve all (paginated)
const paginate = !ids || ids.length === 0
const fields = extraProps?.fields || this.fields
const gotFields = fields?.length > 0
let docs = paginate
? (await this.searchRaw(
{},
limit,
skip,
extraProps,
timeout
)).docs
: (await db.fetch({ keys: ids }))
.rows
.map(x => !isObj(x.doc) || !gotFields
? x.doc
: objClean(x.doc, fields))
// ignore not found documents
.filter(Boolean)
docs = await this.middleware(docs, false, 'getAll')
return asMap
? new Map(
docs.map(x => [
x._id,
x,
])
)
: docs
}
/**
* @name getDB
* @summary Connect to CouchDB and then create new or re-use existing `db` instance
* @description Will create new database, if does not exist.
*/
async getDB() {
if (this.db) return this.db
// if initialization is already in progress wait for it
if (this.dbPromise) return await this.dbPromise
const dbName = this.dbName
if (!dbName) throw new Error('CouchDB: missing database name')
const con = isObj(this.connectionOrUrl)
? await this.connectionOrUrl
: await getConnection(this.connectionOrUrl, this.useGlobalCon)
// database already initialized
if (!isObj(con)) throw new Error('CouchDB: invalid connection')
this.dbPromise = this.dbPromise || new PromisE((resolve, reject) => (async () => {
try {
// retrieve a list of all database names
const dbNames = await con.db.list()
// database already exists, use it
if (!dbNames.includes(dbName)) {
// database doesn't exist, create it
console.log(`${debugTag} new database created: ${dbName}`)
await con.db
.create(dbName)
.catch(err =>
`${err}`.includes('already exists')
? null
: Promise.reject(err)
)
}
this.db = await con.use(dbName)
resolve(this.db)
this.dbPromise = null
} catch (err) {
reject(err)
}
})())
return await this.dbPromise
}
/**
* @name getDoc
* @summary retrieve a document with all properties. This method can retrieve design documents.
* Middleware function is not invoked here.
*
* @param {String} id document ID. (the `_id` property)
*
* @returns {Object}
*/
async getDoc(id) {
const db = await this.getDB()
try {
return await db.get(id)
} catch (_) { }
}
/**
* @name search
* @summary search for documents using CouchDB mango query
*
* @param {Object} selector For documentation visit:
* https://docs.couchdb.org/en/stable/api/database/find.html#selector-syntax
* @param {Number} limit (optional) number of items to retrieve
* Default: 100
* @param {Number} skip (optional) number of items to skip
* Default: 0 (unlimited)
* @param {Boolean} asMap (optional) whether to return result as an `Array` or `Map`
* Default: true
* @param {Object} extraProps (optional) extra properties to be supplied to to the mango query
* @param {Number} timeout query timeout duration in milliseconds.
* Default: `30000`
*
* @returns {Map|Array}
*/
async search(selector = {}, limit = 0, skip = 0, asMap = true, extraProps, timeout) {
const invalid = !isObj(selector)
|| Object.keys(selector).length === 0
if (invalid) return asMap
? new Map()
: []
let { docs = [] } = await this.searchRaw(
selector,
limit,
skip,
extraProps,
timeout
) || {}
docs = await this.middleware(docs, false, 'search')
return !asMap
? docs
: new Map(
docs.map(doc => [
doc._id,
doc,
])
)
}
/**
* @name searchRaw
* @summary sugar for `db.find()`
*
* @param {Object} selector
* @param {Number} limit
* @param {Number} skip
* @param {Object} extraProps
* @param {Number} timeout query timeout duration in milliseconds.
* Default: `30000`
*
* @returns {Array}
*/
async searchRaw(selector = {}, limit = 0, skip = 0, extraProps = {}, timeout = 30000) {
const db = await this.getDB()
const queryPromise = db.find({
fields: this.fields,
...extraProps,
selector,
limit: limit === 0
? undefined
: limit,
skip,
})
return await !isPositiveInteger(timeout)
? queryPromise
: PromisE.timeout(queryPromise, timeout)
}
/**
* @name searchMulti
* @param {Object} selectors
* @param {Boolean} all if falsy, will return as soon as any of the selectors retuns one or more results
*
* @returns {Map}
*/
async searchMulti(selectors = [], limit, all = true) {
let result = new Map()
for (let i = 0;i < selectors.length;i++) {
if (result.size > 0 && !all) return result
const selector = selectors[i]
result = mapJoin(result, await this.search(selector, limit))
}
return result
}
/**
* @name set
* @summary create or update document
*
* @param {String} id (optional) if ID already exists in database, will attempt to update the document.
* If empty string or non-string provided, will create a new UUID with `v1()` function.
* @param {Object} value
* @param {Boolean} update (optional) whether to allow updating existing document.
* If truthy, will automatically check if `@id` already exists.
* If false and `@id` exists and correct `@value._rev` not already supplied, CouchDB
* will throw an error.
* Default: `true`
* @param {Boolean} merge (optional) whether to merge `@value` with exiting entry.
* Only applicable if `@update` is truthy.
* Default: `true`
* @param {Number} timeout timeout duration in milliseconds for save operation.
* Default: `3000`
*
*
* @returns {Object}
*/
async set(id, value, update = true, merge = true, timeout = 10000, updateTS = true) {
id = isStr(id) && id.trim().length
? id
: uuid.v1()
const db = await this.getDB()
const existingDoc = update && await this.get(id, [])
if (existingDoc) {
// attach `_rev` to execute an update operation
value = !merge
? value
: {
...existingDoc,
...value,
}
// make sure _rev is latest
value._rev = existingDoc._rev
}
value._id ??= id
updateTS && setTs(value, existingDoc)
value = (await this.middleware([value], true, 'set'))[0]
return await PromisE.timeout(
db.insert(value, id),
timeout,
)
}
/**
* @name setAll
* @summary bulk add or update documents in single request
*
* @param {Array|Map} docs documents to add or update
* @param {Boolean} ignoreIfExists (optional) if `true`, will prevent overriding existing documents.
* Default: `false`
* @param {Number} timeout bulk save operation timeout duration in milliseconds
*
* @returns {*}
*/
async setAll(docs, ignoreIfExists = false, timeout, updateTS = true) {
if (isMap(docs)) {
// convert Map to Array
docs = Array
.from(docs)
.map(([_id, item]) => ({
_id,
...item,
}))
}
if (!docs.length) return
const db = await this.getDB()
const ids = docs
.map(doc => doc._id)
.filter(Boolean)
const existingDocs = await this.getAll(ids, true)
for (let i = 0;i < docs.length;i++) {
const doc = docs[i]
const existingDoc = existingDocs.get(doc._id)
updateTS && setTs(doc, existingDoc)
if (!doc._id || !existingDoc) continue
if (ignoreIfExists) {
docs[i] = null
continue
}
// attach `_rev` to prevent conflicts when updating existing items
doc._rev = existingDoc._rev
}
docs = docs.filter(Boolean)
// zero docs
if (!docs.length) return []
docs = await this.middleware(
docs,
true, // save
'setAll',
true, // allow "_deleted"
)
const promise = db.bulk({ docs })
return await (
isValidNumber(timeout)
? PromisE.timeout(promise, timeout)
: promise
)
}
/**
* @name view
* @summary query a specific CouchDB "view"
*
* @param {String} designName
* @param {String} viewName
* @param {Object} params (optional)
* @param {Object} includeDocs (optional) use `false` for map-reduce functions.
* Default: `true`
*
* @returns {Array}
*/
async view(designName, viewName, params, includeDocs = true) {
const db = await this.getDB()
const { rows = [] } = await db.view(
designName,
viewName,
{
include_docs: includeDocs,
...params,
},
)
if (!includeDocs) return rows
const docs = rows.map(x =>
this.fields?.length > 0
? objClean(
x.doc,
this.fields,
true
)
: x.doc
)
return await this.middleware(docs, false, 'view')
}
/**
* @name viewCreateMap
* @summary create/update a design document with a map function. Ignores if exact same function already exists.
*
* @param {String} designName
* @param {String} viewName
* @param {String} mapFunc The map function as a string
* @param {String} reduceFunc (optional) redunce function as string.
* Built-in reduce functions: '_approx_count_distinct', '_count', '_stats' and '_sum'
*
* @returns {Object}
*/
async viewCreateMap(designName, viewName, mapFunc, reduceFunc) {
// create design document to enable case-insensitive search of twitter handles
if (!designName.startsWith('_design/')) designName = `_design/${designName}`
const designDoc = await this.getDoc(designName) || {
_id: designName,
language: 'javascript',
views: {},
}
const view = designDoc.views[viewName]
// map function already exists
if (!!view && view.map === mapFunc && view.reduce === reduceFunc) return
designDoc.views[viewName] = {
map: mapFunc
}
if (reduceFunc) {
designDoc.views[viewName].reduce = reduceFunc
}
const action = designDoc._rev
? 'Updating'
: 'Creating'
console.log(`${debugTag} ${action} design document: ${this.dbName}/${designName}`)
return await this.set(designName, designDoc)
}
}
/**
* @name getConnection
* @summary getConnection returns existing connection, if available.
* Otherwise, creates a new connection using the supplied URL.
*
* @param {String} url (optional) Couch DB connetion URL. Default: `process.env.CouchDB_URL`
* @param {Boolean} global (optional) Whether to use global connection. Default: `true`
*
* @returns {Objecct} CouchDB connection
*/
export const getConnection = async (url, global = true) => {
url ??= defaultUrl
if (global && connection) return connection
const con = connections[url] || await nano(url)
// set as global connection
if (global) connection = con
connections[url] = con
return con
}
/**
* @name isCouchDBStorage
* @summary checks if all arguments are instance of CouchDBStorage class
*
* @param {...} args values to check
*
* @returns {Boolean}
*/
export const isCouchDBStorage = (...args) => args
.flat()
.flat()
.every(x => x instanceof CouchDBStorage)
export const setDefaultUrl = url => defaultUrl = url
/**
* @name setTs
* @summary set created and updated timestamps to document
*
* @param {Object} doc
* @param {Object} existingDoc (optional)
*
* @returns {Object} doc
*/
const setTs = (doc, existingDoc) => {
const now = new Date()
// add/update creation and update time
doc.tsCreated = existingDoc?.tsCreated
|| doc.tsCreated
|| now
if (!!existingDoc || doc.tsUpdated) doc.tsUpdated = now
return doc
}