1- const nodeRuntime = process . version . startsWith ( 'v8.' ) ? 'nodejs8' : 'nodejs10'
2- const isNodeJS8 = nodeRuntime === 'nodejs8' ? true : false
3- //var Cloudant = isNodeJS8 ? require("cloudant") : require("@cloudant/cloudant")
1+ // get the actual node version
2+ var nodeRuntime = "unsupported" ;
3+ switch ( true ) {
4+ case process . version . startsWith ( "v10." ) : nodeRuntime = "nodejs10" ; break ;
5+ case process . version . startsWith ( "v12." ) : nodeRuntime = "nodejs12" ; break ;
6+ case process . version . startsWith ( "v16." ) : nodeRuntime = "nodejs16" ; break ;
7+ case process . version . startsWith ( "v20." ) : nodeRuntime = "nodejs20" ; break ;
8+ }
49
5- if ( process . version . startsWith ( 'v8.' ) ) {
6- var Cloudant = require ( "cloudant" )
7- } else if ( process . version . startsWith ( 'v16.' ) || process . version . startsWith ( 'v20.' ) ) {
10+ // load the cloudant package
11+ if ( process . version . startsWith ( 'v16.' ) || process . version . startsWith ( 'v20.' ) ) {
812 console . log ( "------------------------- require @ibm-cloud/cloudant ----------------------" ) ;
913 var { CloudantV1 } = require ( '@ibm-cloud/cloudant' ) ;
1014} else {
1115 var Cloudant = require ( "@cloudant/cloudant" )
1216}
1317
14- function main ( args ) {
18+ // delete database
19+ async function deleteDatabase ( client , databaseName ) {
20+ try {
21+ console . log ( "Deleting possible existing database: " + databaseName ) ;
22+ const response = await client . deleteDatabase ( { db : databaseName } ) ;
23+ console . log ( "Returned from deleteDatabase response: %j" , response ) ;
24+ } catch ( err ) {
25+ if ( err . status = 404 ) {
26+ // Database not found, we tolerate this for the delete.
27+ console . log ( "Database not found, ignored during deleteDatabase." ) ;
28+ } else {
29+ // Other errors, progagte this to caller.
30+ throw new Error ( 'Delete database failed!' , { cause : err } ) ;
31+ } ;
32+ }
33+ return true ;
34+ }
35+
36+ // create database
37+ async function createDatabase ( client , databaseName ) {
38+ try {
39+ console . log ( "Creating database: " + databaseName ) ;
40+ const response = await client . putDatabase ( { db : databaseName } ) ;
41+ console . log ( "Returned from putDatabase" )
42+ if ( response . result . ok ) {
43+ console . log ( `"${ databaseName } " database created."` ) ;
44+ } else {
45+ throw new Error ( 'Error for client.putDatabase, response.result.ok=true expected!' , { cause : response } ) ;
46+ }
47+ } catch ( err ) {
48+ console . log ( "Error for client.putDatabase: %j" , err ) ;
49+ throw new Error ( 'Error for client.putDatabase!' , { cause : err } ) ;
50+ }
51+ return true ;
52+ }
53+
54+ // post a document into the database
55+ async function postDocument ( client , databaseName , doc ) {
56+ try {
57+ console . log ( "writing document to database: " + databaseName ) ;
58+ const response = await client . postDocument ( { db : databaseName , document : doc } )
59+ console . log ( "Returned from postDocument" )
60+ if ( response . result . ok ) {
61+ console . log ( `"${ databaseName } " document written."` ) ;
62+ } else {
63+ throw new Error ( 'Error for client.postDocument, response.result.ok=true expected!' , { cause : response } ) ;
64+ }
65+ } catch ( err ) {
66+ console . log ( "Error for client.postDocument: %j" , err ) ;
67+ throw new Error ( 'Error for client.putDatabase!' , { cause : err } ) ;
68+ }
69+ return true ;
70+ }
71+
72+ // get a document from database
73+ async function getDocument ( client , databaseName , id ) {
74+ var document = { } ;
75+ try {
76+ console . log ( "Get document from database: " + databaseName ) ;
77+ const response = await client . getDocument ( { db : databaseName , docId : id } )
78+ console . log ( "Returned from getDocument, response: %j" , response )
79+ if ( response . statusText = 'OK' ) {
80+ console . log ( `"${ databaseName } " document "${ id } " successfully read."` ) ;
81+ document = response . result ;
82+ } else {
83+ throw new Error ( 'Error for client.getDocument, response.statusText=OK expected!' , { cause : response } ) ;
84+ }
85+ } catch ( err ) {
86+ console . log ( "Error for client.getDocument: %j" , err ) ;
87+ throw new Error ( 'Error for client.getDocument!' , { cause : err } ) ;
88+ }
89+ return document ;
90+ }
91+
92+ // main action
93+ async function main ( args ) {
1594 var username = args . username ;
1695 var password = args . password ;
1796 var url = args . url ;
1897 var dbName = `test_cloud_functions_nodejs_${ nodeRuntime } _ibm_runtime`
1998
99+ console . log ( "runtime: " + nodeRuntime )
100+ console . log ( "database name: " + dbName )
101+ console . log ( "username: " + username )
102+
20103 if ( process . version . startsWith ( 'v16.' ) || process . version . startsWith ( 'v20.' ) ) {
21104 process . env [ 'CLOUDANT_AUTH_TYPE' ] = 'BASIC'
22105 process . env [ 'CLOUDANT_URL' ] = url
23106 process . env [ 'CLOUDANT_USERNAME' ] = username
24107 process . env [ 'CLOUDANT_PASSWORD' ] = password
25108
26- // 1. Create a client with `CLOUDANT` default service name
109+ // Create a client with `CLOUDANT` default service name
27110 const client = CloudantV1 . newInstance ( { } ) ;
28- // Create DB
29- const createDb = client . putDatabase ( { db : dbName } )
30- . then ( ( putDatabaseResult ) => {
31- if ( putDatabaseResult . result . ok ) {
32- console . log ( `"${ dbName } " database created."` ) ;
33- }
34- } )
35- . catch ( ( err ) => {
36- if ( err . code === 412 ) {
37- console . log (
38- `Cannot create "${ dbName } " database, it already exists.`
39- ) ;
40- }
41- } ) ;
42-
43- var friendinfo ;
44- // Writte to DB
45- return createDb . then ( ( ) => {
46- client
47- . postDocument ( {
48- db : dbName ,
49- document : {
50- "_id" : 'friend1' ,
51- "firstname" : "Suzzy" ,
52- "lastname" : "Queue"
53- }
54- } )
55- . then ( ( createDocumentResponse ) => {
56- rev = createDocumentResponse . result . rev ;
57- console . log ( 'You have created the document:\n' ) ;
58- } ) ;
59- } ) . then ( ( ) => {
60- // call service with predefined parameters:
61- console . log ( 'client.getDocument:\n' ) ;
62- return client . getDocument ( { db : dbName , docId : 'friend1' } ) . then ( ( document ) => {
63- return document ;
64- } ) ;
65- } ) . then ( ( data ) => {
66- friendinfo = data
67- console . log ( 'client.deleteDatabase friend=' , friendinfo ) ;
68- return client . deleteDatabase ( { db : dbName } ) //.then((db) => {return db})
69- } ) . then ( function ( ) {
70- console . log ( 'return the document=' , friendinfo ) ;
71- //return the document fetched from the db
72- return friendinfo ;
73- } ) . catch ( function ( err ) {
74- console . log ( 'error received:' , err ) ;
75- //If an error occurrs at any part in execution; return error
76- return { err : err }
77- } )
78111
79- //fetch from DB
112+ // Delete a possible existing database from a previous run.
113+ const delDB = await deleteDatabase ( client , dbName ) ;
80114
115+ // Create a database.
116+ const createDB = await createDatabase ( client , dbName ) ;
117+ console . log ( "createDatabase returned:" + createDB ) ;
118+
119+ // Post a document into the new database.
120+ const doc = {
121+ "_id" : 'friend1' ,
122+ "firstname" : "Suzzy" ,
123+ "lastname" : "Queue"
124+ }
125+ const postDoc = await postDocument ( client , dbName , doc ) ;
126+ console . log ( "postDocument returned:" + postDoc )
127+
128+ // Read the document from the database.
129+ const getDoc = await getDocument ( client , dbName , 'friend1' ) ;
130+ console . log ( "getDocument returned: %j" , getDoc )
131+
132+ // const delDB2= await deleteDatabase(client,dbName);
133+
134+ // Return the document read from the database.
135+ return getDoc ;
81136
82137 } else {
83- //Configuration to use Cloudant
84- var config = { account :username , password :password }
85- isNodeJS8 ? config . plugin = 'promises' : config . plugins = [ 'promises' ]
86- var cloudant = Cloudant ( config ) ;
87-
88- var beforeAction = new Promise ( function ( resolve , reject ) {
89- cloudant . db . destroy ( dbName )
90- . then ( function ( ) {
91- console . log ( "Previous database with name: " + dbName + "existed; it was cleaned up so that tests can run" ) ;
92- return resolve ( ) ;
93- } )
94- . catch ( function ( ) {
95- return resolve ( ) ;
96- } )
97- } ) ;
98-
99- //Create the cloudant database
100- return beforeAction . then ( function ( ) {
101- return cloudant . db . create ( dbName )
102- } )
103- . then ( function ( data ) {
104- //Switch to use that newly created database.
105- return cloudant . db . use ( dbName ) ;
106- } )
107- . then ( function ( db ) {
108- var friendinfo ;
109- //Inject a json document named friend1 into the database.
110- return db . insert ( { firstname : "Suzzy" , lastname : "Queue" } , 'friend1' )
111- . then ( function ( ) {
112- //fetch the newly injected document from the database
113- return db . get ( 'friend1' ) ;
138+
139+ //Configuration to use Cloudant
140+ var config = { account :username , password :password , plugins :[ 'promises' ] }
141+ var cloudant = Cloudant ( config ) ;
142+
143+ var beforeAction = new Promise ( function ( resolve , reject ) {
144+ cloudant . db . destroy ( dbName )
145+ . then ( function ( ) {
146+ console . log ( "Previous database with name: " + dbName + "existed; it was cleaned up so that tests can run" ) ;
147+ return resolve ( ) ;
148+ } )
149+ . catch ( function ( ) {
150+ return resolve ( ) ;
151+ } )
152+ } ) ;
153+
154+ //Create the cloudant database
155+ return beforeAction . then ( function ( ) {
156+ return cloudant . db . create ( dbName )
114157 } )
115158 . then ( function ( data ) {
116- friendinfo = data ;
117- //destroy the database
118- return cloudant . db . destroy ( dbName ) ;
159+ //Switch to use that newly created database.
160+ return cloudant . db . use ( dbName ) ;
119161 } )
120- . then ( function ( ) {
121- //return the document fetched from the db
122- return friendinfo ;
162+ . then ( function ( db ) {
163+ var friendinfo ;
164+ //Inject a json document named friend1 into the database.
165+ return db . insert ( { firstname : "Suzzy" , lastname : "Queue" } , 'friend1' )
166+ . then ( function ( ) {
167+ //fetch the newly injected document from the database
168+ return db . get ( 'friend1' ) ;
169+ } )
170+ . then ( function ( data ) {
171+ friendinfo = data ;
172+ //destroy the database
173+ return cloudant . db . destroy ( dbName ) ;
174+ } )
175+ . then ( function ( ) {
176+ //return the document fetched from the db
177+ return friendinfo ;
178+ } )
179+ } )
180+ . catch ( function ( err ) {
181+ //If an error occurrs at any part in execution; return error
182+ return { err : err }
123183 } )
124- } )
125- . catch ( function ( err ) {
126- //If an error occurrs at any part in execution; return error
127- return { err : err }
128- } )
129184 }
130- }
185+ }
0 commit comments