@@ -34,6 +34,13 @@ const { default: axios } = require('axios')
34
34
* {user:"myemail@something.com", organization:'mycloudTeam' })
35
35
*
36
36
* 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
+ *
37
44
* async function getSchema() {
38
45
* client.db("test")
39
46
* client.checkout("dev")
@@ -109,23 +116,64 @@ WOQLClient.prototype.api = function() {
109
116
}
110
117
111
118
/**
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
113
121
* @param {string | boolean } [orgId] the organization id to set the context to
114
122
* @returns {string | boolean }
115
123
* @example
116
124
* client.organization("admin")
117
125
*/
118
126
WOQLClient . prototype . organization = function ( orgId ) {
119
127
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 ( [ ] )
125
131
}
126
132
return this . connectionConfig . organization ( )
127
133
}
128
134
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
+
129
177
/**
130
178
* Gets the current user object as returned by the connect capabilities response
131
179
* user has fields: [id, name, notes, author]
@@ -146,32 +194,6 @@ WOQLClient.prototype.userOrganization = function() {
146
194
}
147
195
148
196
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
-
175
197
/**
176
198
* Gets the database's details
177
199
* @param {string } [dbId] - the datbase id
@@ -196,8 +218,6 @@ WOQLClient.prototype.db = function(dbId) {
196
218
this . connectionConfig . setDB ( dbId )
197
219
}
198
220
return this . connectionConfig . dbid
199
-
200
- //this.connectionConfig.db()
201
221
}
202
222
203
223
/**
@@ -821,22 +841,6 @@ WOQLClient.prototype.prepareRevisionControlArgs = function(rc_args) {
821
841
822
842
823
843
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
-
840
844
/**
841
845
* update the database details
842
846
* @param {object } dbDoc - an object that describe the database details
@@ -1071,42 +1075,45 @@ WOQLClient.prototype.getBranches = function(dbId){
1071
1075
this . checkout ( branch )
1072
1076
return branchesObj
1073
1077
} )
1074
- //reset branch
1075
-
1078
+ //reset branch
1076
1079
}
1077
1080
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
1082
1083
* @returns {Promise } A promise that returns the call response object, or an Error if rejected.
1083
1084
* @example
1084
- * client.getUserOrganizations()
1085
+ * async funtion callGetUserOrganizations(){
1086
+ * await getUserOrganizations()
1087
+ * console.log(client.userOrganizations())
1088
+ * }
1085
1089
*/
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
1089
1092
return this . dispatch (
1090
1093
CONST . GET ,
1091
1094
this . connectionConfig . userOrganizationsURL ( ) ,
1092
1095
) . then ( response => {
1093
1096
const orgList = Array . isArray ( response ) ? response : [ ]
1094
1097
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
- }
1106
1098
return orgList
1107
1099
} )
1108
1100
}
1109
1101
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
+
1110
1117
/**
1111
1118
* Get the patch of difference between two documents.
1112
1119
* @param {object } before - The current state of JSON document
0 commit comments