1+ const axios = require ( "axios" ) ;
2+
3+ let database = process . argv [ 2 ] ;
4+ let documentID = process . argv [ 3 ] ;
5+
6+ if ( ! process . argv [ 2 ] || ! process . argv [ 3 ] ) {
7+ throw new Error ( "Missing required CLI arguments..." ) ;
8+ }
9+
10+ let documentBaseURL = `http://localhost:5984/${ database } /` ;
11+ let revQuery = "?revs=true" ;
12+
13+ ( async function main ( ) {
14+
15+ try {
16+ // HTTP GET Request for Document
17+ let response = await axios . get ( documentBaseURL + documentID ) ;
18+
19+ //Get Revision List for A Document in CouchDB Database
20+ let document = response . data ;
21+ document . id = document . _id ;
22+
23+ let documentRevIDsArray = await getDocumentRevisionList ( document ) ;
24+ let documentRevArray = await buildRevisionList ( documentRevIDsArray ) ;
25+
26+ // Get All Document Versions
27+ let documentRev = { } ;
28+
29+ documentRev [ document . id ] = documentRevArray ;
30+
31+ let fullDocumentHistory = await gatherAllDocuments ( documentRev ) ;
32+
33+ // Log the Results
34+ for ( let documentGroup of fullDocumentHistory ) {
35+ console . log ( `Document ${ documentGroup . id } has ${ documentGroup . documents . length } versions:` ) ;
36+
37+ for ( var i = 0 ; i < documentGroup . documents . length ; i ++ ) {
38+ console . group ( ) ;
39+ console . log ( documentGroup . documents [ i ] ) ;
40+ console . groupEnd ( ) ;
41+ }
42+ }
43+
44+ } catch ( e ) {
45+ console . log ( "Error retrieving all documents: " , e ) ;
46+ }
47+
48+ } ( ) )
49+
50+ // Function to Get All Partial Revision IDs for a Document
51+ async function getDocumentRevisionList ( document ) {
52+ try {
53+ let response = await axios . get ( documentBaseURL + document . id + revQuery ) ;
54+ let documentRevIDsArray = response . data . _revisions ;
55+ return documentRevIDsArray ;
56+
57+ } catch ( e ) {
58+ console . log ( "Error getting partial document revision list: " , e ) ;
59+ }
60+ }
61+
62+ // Function to Get All Full Revision IDs for a Document
63+ async function buildRevisionList ( documentRevIDsArray ) {
64+
65+ let documentRevArray = [ ] ;
66+ let startRevNum = documentRevIDsArray . start ;
67+ let docCounter = 0 ;
68+
69+ for ( let i = startRevNum ; i >= 1 ; i -- ) {
70+ documentRevArray . push ( i + "-" + documentRevIDsArray . ids [ docCounter ] ) ;
71+ docCounter ++ ;
72+ }
73+
74+ return documentRevArray ;
75+ }
76+
77+ // Function to Get All Documents of All Revisions
78+ async function gatherAllDocuments ( fullDocumentRevArray ) {
79+ let allDocuments = [ ] ;
80+
81+ let documentIDs = Object . keys ( fullDocumentRevArray ) ;
82+
83+ for ( let documentID of documentIDs ) {
84+ let documentData = {
85+ id : documentID ,
86+ documents : [ ]
87+ }
88+
89+ for ( let rev of fullDocumentRevArray [ documentID ] ) {
90+ let documentRevURL = documentBaseURL + documentID + "?rev=" + rev ;
91+
92+ try {
93+ let document = await axios . get ( documentRevURL ) ;
94+ documentData . documents . push ( document . data ) ;
95+ } catch ( e ) {
96+ console . log ( "Error getting full document revision list: " , e ) ;
97+ }
98+ }
99+
100+ allDocuments . push ( documentData ) ;
101+ }
102+
103+ return allDocuments ;
104+ }
0 commit comments