@@ -34,6 +34,13 @@ const { default: axios } = require('axios')
3434 * {user:"myemail@something.com", organization:'mycloudTeam' })
3535 *
3636 * client.setApiKey(MY_ACCESS_TOKEN)
37+ *
38+ * //to get the list of all organization's databases
39+ * async function callGetDatabases(){
40+ * const dbList = await client.getDatabases()
41+ * console.log(dbList)
42+ * }
43+ *
3744 * async function getSchema() {
3845 * client.db("test")
3946 * client.checkout("dev")
@@ -109,23 +116,64 @@ WOQLClient.prototype.api = function() {
109116}
110117
111118/**
112- * Gets/Sets the client’s internal organization context value
119+ * Gets/Sets the client’s internal organization context value, if you change the organization name the
120+ * databases list will be set to empty
113121 * @param {string | boolean } [orgId] the organization id to set the context to
114122 * @returns {string | boolean }
115123 * @example
116124 * client.organization("admin")
117125 */
118126WOQLClient . prototype . organization = function ( orgId ) {
119127 if ( typeof orgId !== 'undefined' ) {
120- this . connectionConfig . setOrganization ( orgId )
121- const orgObj = this . userOrganizations ( ) . find ( element => element [ 'name' ] === orgId )
122- const dbs = orgObj ? orgObj . databases : [ ]
123- //change the database list
124- this . databases ( dbs )
128+ this . connectionConfig . setOrganization ( orgId )
129+ //we have to reset the databases list
130+ this . databases ( [ ] )
125131 }
126132 return this . connectionConfig . organization ( )
127133}
128134
135+ /**
136+ * Gets the organization's databases list.
137+ *
138+ * If no organization has been set up, the function throws an exception
139+ * @returns {string | boolean }
140+ * @example
141+ * async function callGetDatabases(){
142+ * const dbList = await client.getDatabases()
143+ * console.log(dbList)
144+ * }
145+ */
146+ WOQLClient . prototype . getDatabases = async function ( ) {
147+ // return response
148+ if ( ! this . connectionConfig . organization ( ) ) {
149+ throw new Error ( `You need to set the organization name` ) ;
150+ }
151+ //when we will have the end point to get the databases only for the current organization
152+ //we'll change this call
153+ await this . getUserOrganizations ( ) ;
154+ const dbs = this . userOrganizations ( ) . find ( element => element [ 'name' ] === this . connectionConfig . organization ( ) )
155+ const dbList = dbs && dbs . databases ? dbs . databases : [ ]
156+ this . databases ( dbList )
157+ return dbList
158+ }
159+
160+ /**
161+ * Set/Get the organization's databases list (id, label, comment) that the current user has access to on the server.
162+ * @param {array } [dbList] a list of databases the user has access to on the server, each having:
163+ * @returns {array } the organization's databases list
164+ * @example
165+ * //to get the list of all organization's databases
166+ * async function callGetDatabases(){
167+ * await client.getDatabases()
168+ * console.log(client.databases())
169+ * }
170+ *
171+ */
172+ WOQLClient . prototype . databases = function ( dbList ) {
173+ if ( dbList ) this . databaseList = dbList
174+ return this . databaseList || [ ]
175+ }
176+
129177/**
130178 * Gets the current user object as returned by the connect capabilities response
131179 * user has fields: [id, name, notes, author]
@@ -146,32 +194,6 @@ WOQLClient.prototype.userOrganization = function() {
146194}
147195
148196
149- /**
150- * Retrieves a list of databases (id, organization, label, comment) that the current user has access to on the server.
151- * @param {array } [dbList] a list of databases the user has access to on the server, each having:
152- * @returns {array } the user databases list
153- * @example
154- * const my_dbs = client.databases()
155- */
156- WOQLClient . prototype . databases = function ( dbList ) {
157- if ( dbList ) this . databaseList = dbList
158- return this . databaseList || [ ]
159- }
160-
161- /**
162- * Retrieves a list of databases (id, organization, label, comment) that the current user has access to on the server.
163- * @param {array } [orgList] a list of databases the user has access to on the server, each having:
164- * @returns {array } the user databases list
165- * @example
166- * const my_dbs = client.databases()
167- */
168- WOQLClient . prototype . userOrganizations = function ( orgList ) {
169- if ( orgList ) this . organizationList = orgList
170- return this . organizationList || [ ]
171- }
172-
173-
174-
175197/**
176198 * Gets the database's details
177199 * @param {string } [dbId] - the datbase id
@@ -196,8 +218,6 @@ WOQLClient.prototype.db = function(dbId) {
196218 this . connectionConfig . setDB ( dbId )
197219 }
198220 return this . connectionConfig . dbid
199-
200- //this.connectionConfig.db()
201221}
202222
203223/**
@@ -821,22 +841,6 @@ WOQLClient.prototype.prepareRevisionControlArgs = function(rc_args) {
821841
822842
823843
824- /***
825- * Server Version API
826- * Note: the below endpoints are not part of the terminusdb desktop package
827- * they belong to the server package version of the DB which is under construction.
828- * Until that package is released all of the below endpoints should be considered
829- * as unreliable and subject to change - they are provided complete with the desktop
830- * version to show users what is coming and to allow people to use them at their own risk
831- * Any use of them should be considered unsupported and at your own risk
832- * get all the database information from the remote server
833- * @returns {Promise } A promise that returns the call response object, or an Error if rejected.
834- */
835-
836- WOQLClient . prototype . getDatabase = function ( ) {
837- return this . dispatch ( CONST . GET , this . connectionConfig . dbURL ( ) )
838- }
839-
840844/**
841845 * update the database details
842846 * @param {object } dbDoc - an object that describe the database details
@@ -1071,42 +1075,45 @@ WOQLClient.prototype.getBranches = function(dbId){
10711075 this . checkout ( branch )
10721076 return branchesObj
10731077 } )
1074- //reset branch
1075-
1078+ //reset branch
10761079}
10771080
1078- //this method is not documented
1079- /*
1080- * get the organizations and the database related
1081- * @param {string } [defaultOrg] - the organizatation to set
1081+ /**
1082+ * Get the list of the user's organizations and the database related
10821083 * @returns {Promise } A promise that returns the call response object, or an Error if rejected.
10831084 * @example
1084- * client.getUserOrganizations()
1085+ * async funtion callGetUserOrganizations(){
1086+ * await getUserOrganizations()
1087+ * console.log(client.userOrganizations())
1088+ * }
10851089 */
1086- WOQLClient . prototype . getUserOrganizations = function ( defaultOrg ) {
1087- //this.databases(response)
1088- // return response
1090+ WOQLClient . prototype . getUserOrganizations = function ( ) {
1091+ // this will be change to give back only the organizations list
10891092 return this . dispatch (
10901093 CONST . GET ,
10911094 this . connectionConfig . userOrganizationsURL ( ) ,
10921095 ) . then ( response => {
10931096 const orgList = Array . isArray ( response ) ? response : [ ]
10941097 this . userOrganizations ( orgList )
1095- if ( orgList . length > 0 ) {
1096- let orgObj
1097- if ( defaultOrg ) {
1098- orgObj = orgList . find ( element => element [ 'name' ] === defaultOrg )
1099- }
1100- //if I didn't find the default one I get the first result
1101- if ( ! orgObj ) {
1102- orgObj = orgList [ 0 ]
1103- }
1104- this . organization ( orgObj [ 'name' ] )
1105- }
11061098 return orgList
11071099 } )
11081100}
11091101
1102+ /**
1103+ * Get/Set the list of the user's organizations (id, organization, label, comment).
1104+ * @param {array } [orgList] a list of user's Organization
1105+ * @returns {array } the user Organizations list
1106+ * @example
1107+ * async funtion callGetUserOrganizations(){
1108+ * await client.getUserOrganizations()
1109+ * console.log(client.userOrganizations())
1110+ * }
1111+ */
1112+ WOQLClient . prototype . userOrganizations = function ( orgList ) {
1113+ if ( orgList ) this . organizationList = orgList
1114+ return this . organizationList || [ ]
1115+ }
1116+
11101117/**
11111118 * Get the patch of difference between two documents.
11121119 * @param {object } before - The current state of JSON document
0 commit comments