Skip to content

Commit 85a9c72

Browse files
Merge pull request #121 from terminusdb/getDatabases
add getdatabases method and review documentation
2 parents c324df4 + cb32946 commit 85a9c72

File tree

4 files changed

+155
-97
lines changed

4 files changed

+155
-97
lines changed

docs/_sidebar.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
- [server](api/woqlClient.js?id=server)
1919
- [api](api/woqlClient.js?id=api)
2020
- [organization](api/woqlClient.js?id=organization)
21+
- [getDatabases](api/woqlClient.js?id=getDatabases)
22+
- [databases](api/woqlClient.js?id=databases)
2123
- [user](api/woqlClient.js?id=user)
2224
- [userOrganization](api/woqlClient.js?id=userOrganization)
23-
- [databases](api/woqlClient.js?id=databases)
24-
- [userOrganizations](api/woqlClient.js?id=userOrganizations)
2525
- [databaseInfo](api/woqlClient.js?id=databaseInfo)
2626
- [db](api/woqlClient.js?id=db)
2727
- [setSystemDb](api/woqlClient.js?id=setSystemDb)
@@ -58,6 +58,8 @@
5858
- [getEnums](api/woqlClient.js?id=getEnums)
5959
- [getClassDocuments](api/woqlClient.js?id=getClassDocuments)
6060
- [getBranches](api/woqlClient.js?id=getBranches)
61+
- [getUserOrganizations](api/woqlClient.js?id=getUserOrganizations)
62+
- [userOrganizations](api/woqlClient.js?id=userOrganizations)
6163
- [getDiff](api/woqlClient.js?id=getDiff)
6264
- [patch](api/woqlClient.js?id=patch)
6365
- [WOQL](api/woql.js?id=WOQL)

docs/api/woqlClient.js.md

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ const client = new TerminusClient.WOQLClient('SERVER_CLOUD_URL/mycloudTeam',
3232
{user:"[email protected]", organization:'mycloudTeam'})
3333

3434
client.setApiKey(MY_ACCESS_TOKEN)
35+
36+
//to get the list of all organization's databases
37+
async function callGetDatabases(){
38+
const dbList = await client.getDatabases()
39+
console.log(dbList)
40+
}
41+
3542
async function getSchema() {
3643
client.db("test")
3744
client.checkout("dev")
@@ -303,7 +310,8 @@ let api_url = client.api()
303310
304311
### organization
305312
#### woqlClient.organization([orgId]) ⇒ <code>string</code> \| <code>boolean</code>
306-
Gets/Sets the client’s internal organization context value
313+
Gets/Sets the client’s internal organization context value, if you change the organization name the
314+
databases list will be set to empty
307315
308316
309317
| Param | Type | Description |
@@ -315,46 +323,49 @@ Gets/Sets the client’s internal organization context value
315323
client.organization("admin")
316324
```
317325
318-
### user
319-
#### woqlClient.user() ⇒ <code>Object</code>
320-
Gets the current user object as returned by the connect capabilities response
321-
user has fields: [id, name, notes, author]
326+
### getDatabases
327+
#### woqlClient.getDatabases() ⇒ <code>string</code> \| <code>boolean</code>
328+
Gets the organization's databases list.
322329
330+
If no organization has been set up, the function throws an exception
323331
324-
### userOrganization
325-
#### woqlClient.userOrganization() ⇒ <code>string</code>
326-
**Returns**: <code>string</code> - the user organization name
327-
**Desription**: Gets the user's organization id
332+
**Example**
333+
```js
334+
async function callGetDatabases(){
335+
const dbList = await client.getDatabases()
336+
console.log(dbList)
337+
}
338+
```
328339
329340
### databases
330341
#### woqlClient.databases([dbList]) ⇒ <code>array</code>
331-
Retrieves a list of databases (id, organization, label, comment) that the current user has access to on the server.
342+
Set/Get the organization's databases list (id, label, comment) that the current user has access to on the server.
332343
333-
**Returns**: <code>array</code> - the user databases list
344+
**Returns**: <code>array</code> - the organization's databases list
334345
335346
| Param | Type | Description |
336347
| --- | --- | --- |
337348
| [dbList] | <code>array</code> | a list of databases the user has access to on the server, each having: |
338349
339350
**Example**
340351
```js
341-
const my_dbs = client.databases()
352+
//to get the list of all organization's databases
353+
async function callGetDatabases(){
354+
await client.getDatabases()
355+
console.log(client.databases())
356+
}
342357
```
343358
344-
### userOrganizations
345-
#### woqlClient.userOrganizations([orgList]) ⇒ <code>array</code>
346-
Retrieves a list of databases (id, organization, label, comment) that the current user has access to on the server.
347-
348-
**Returns**: <code>array</code> - the user databases list
359+
### user
360+
#### woqlClient.user() ⇒ <code>Object</code>
361+
Gets the current user object as returned by the connect capabilities response
362+
user has fields: [id, name, notes, author]
349363
350-
| Param | Type | Description |
351-
| --- | --- | --- |
352-
| [orgList] | <code>array</code> | a list of databases the user has access to on the server, each having: |
353364
354-
**Example**
355-
```js
356-
const my_dbs = client.databases()
357-
```
365+
### userOrganization
366+
#### woqlClient.userOrganization() ⇒ <code>string</code>
367+
**Returns**: <code>string</code> - the user organization name
368+
**Desription**: Gets the user's organization id
358369
359370
### databaseInfo
360371
#### woqlClient.databaseInfo([dbId]) ⇒ <code>object</code>
@@ -851,6 +862,37 @@ get the database collections list
851862
client.getBranches()
852863
```
853864
865+
### getUserOrganizations
866+
#### woqlClient.getUserOrganizations() ⇒ <code>Promise</code>
867+
Get the list of the user's organizations and the database related
868+
869+
**Returns**: <code>Promise</code> - A promise that returns the call response object, or an Error if rejected.
870+
**Example**
871+
```js
872+
async funtion callGetUserOrganizations(){
873+
await getUserOrganizations()
874+
console.log(client.userOrganizations())
875+
}
876+
```
877+
878+
### userOrganizations
879+
#### woqlClient.userOrganizations([orgList]) ⇒ <code>array</code>
880+
Get/Set the list of the user's organizations (id, organization, label, comment).
881+
882+
**Returns**: <code>array</code> - the user Organizations list
883+
884+
| Param | Type | Description |
885+
| --- | --- | --- |
886+
| [orgList] | <code>array</code> | a list of user's Organization |
887+
888+
**Example**
889+
```js
890+
async funtion callGetUserOrganizations(){
891+
await client.getUserOrganizations()
892+
console.log(client.userOrganizations())
893+
}
894+
```
895+
854896
### getDiff
855897
#### woqlClient.getDiff(before, after) ⇒ <code>Promise</code>
856898
Get the patch of difference between two documents.

lib/connectionConfig.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ ConnectionConfig.prototype.serverUrlEncoding = function(str){
229229
const orgArr = str.split("/")
230230
if(orgArr.length>3){
231231
//const org = encodeURI(orgArr[3])
232+
/*
233+
* if we pass the organization name like Francesca-Bit-9e73
234+
* I need to encode it 2 times because in the database
235+
* when we create an id from a property it encodes the property value
236+
*/
232237
const org = encodeURISegment(orgArr[3])
233238
return str.replace(orgArr[3], org)
234239
}
@@ -375,6 +380,8 @@ ConnectionConfig.prototype.userURL = function(user) {
375380
//encodeURIComponent
376381
ConnectionConfig.prototype.organizationURL = function(orgId, action) {
377382
let url = `${this.apiURL()}organization`
383+
//I have to encode the organization 2 times because it is saved encoded inside the database
384+
//
378385
if (orgId) url += `/${encodeURISegment(orgId)}`
379386
if (action) url += `/${encodeURISegment(action)}`
380387
return url

lib/woqlClient.js

Lines changed: 78 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
118126
WOQLClient.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

Comments
 (0)