From 528b40daba6360cac0b885bca472ed7d60fd69c8 Mon Sep 17 00:00:00 2001 From: Francesca-Bit Date: Thu, 3 Feb 2022 15:57:46 +0000 Subject: [PATCH 1/4] fix getDatabases --- lib/connectionConfig.js | 7 +++++++ lib/woqlClient.js | 25 ++++++++++--------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/connectionConfig.js b/lib/connectionConfig.js index 71473ad1..52a62bfb 100644 --- a/lib/connectionConfig.js +++ b/lib/connectionConfig.js @@ -229,6 +229,11 @@ ConnectionConfig.prototype.serverUrlEncoding = function(str){ const orgArr = str.split("/") if(orgArr.length>3){ //const org = encodeURI(orgArr[3]) + /* + * if we pass the organization name like Francesca-Bit-9e73 + * I need to encode it 2 times because in the database + * when we create an id from a property it encodes the property value + */ const org = encodeURISegment(orgArr[3]) return str.replace(orgArr[3], org) } @@ -375,6 +380,8 @@ ConnectionConfig.prototype.userURL = function(user) { //encodeURIComponent ConnectionConfig.prototype.organizationURL = function(orgId, action) { let url = `${this.apiURL()}organization` + //I have to encode the organization 2 times because it is saved encoded inside the database + // if (orgId) url += `/${encodeURISegment(orgId)}` if (action) url += `/${encodeURISegment(action)}` return url diff --git a/lib/woqlClient.js b/lib/woqlClient.js index 108e9cf3..0302f9d9 100644 --- a/lib/woqlClient.js +++ b/lib/woqlClient.js @@ -1077,30 +1077,25 @@ WOQLClient.prototype.getBranches = function(dbId){ //this method is not documented /* * get the organizations and the database related - * @param {string} [defaultOrg] - the organizatation to set * @returns {Promise} A promise that returns the call response object, or an Error if rejected. * @example - * client.getUserOrganizations() + * const client = new TerminusDB.WOQLClient() + * client.getUserOrganizations().then(result=>{ + * console.log(result) + * }) + * //get back the datatabases for the current organization + * const organizationDatabases = client.databases() */ - WOQLClient.prototype.getUserOrganizations = function(defaultOrg){ - //this.databases(response) - // return response + WOQLClient.prototype.getUserOrganizations = function(){ + // return response return this.dispatch( CONST.GET, this.connectionConfig.userOrganizationsURL(), ).then(response => { const orgList = Array.isArray(response) ? response : [] this.userOrganizations(orgList) - if (orgList.length>0){ - let orgObj - if(defaultOrg){ - orgObj = orgList.find(element => element['name'] === defaultOrg) - } - //if I didn't find the default one I get the first result - if (!orgObj){ - orgObj = orgList[0] - } - this.organization(orgObj['name']) + if (orgList.length>0 && this.connectionConfig.organization()){ + this.organization(this.connectionConfig.organization()) } return orgList }) From 475b3aacf7e31cdf425f320f849145aaea563082 Mon Sep 17 00:00:00 2001 From: Francesca-Bit Date: Fri, 4 Feb 2022 09:53:27 +0000 Subject: [PATCH 2/4] added getDatabases --- lib/woqlClient.js | 62 ++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/lib/woqlClient.js b/lib/woqlClient.js index 6ce2ce0b..6b573b56 100644 --- a/lib/woqlClient.js +++ b/lib/woqlClient.js @@ -34,6 +34,11 @@ const { default: axios } = require('axios') * {user:"myemail@something.com", organization:'mycloudTeam'}) * * client.setApiKey(MY_ACCESS_TOKEN) + * //to get the list of all the team and databases + * client.getDatabases(result=>{ + * console.log(result) + * + * }) * async function getSchema() { * client.db("test") * client.checkout("dev") @@ -109,7 +114,8 @@ WOQLClient.prototype.api = function() { } /** - * Gets/Sets the client’s internal organization context value + * Gets/Sets the client’s internal organization context value, if you change the organization name the + * databases list will be set to empty * @param {string | boolean} [orgId] the organization id to set the context to * @returns {string | boolean} * @example @@ -117,15 +123,32 @@ WOQLClient.prototype.api = function() { */ WOQLClient.prototype.organization = function(orgId) { if (typeof orgId !== 'undefined') { - this.connectionConfig.setOrganization(orgId) - const orgObj = this.userOrganizations().find(element => element['name'] === orgId) - const dbs = orgObj ? orgObj.databases : [] - //change the database list - this.databases(dbs) + this.connectionConfig.setOrganization(orgId) + //we have to reset the databases list + this.databases([]) } return this.connectionConfig.organization() } +/** + * Gets the organization's databases list + * @returns {string | boolean} + * @example + * client.getDatabases() + */ +WOQLClient.prototype.getDatabases = async function(){ + // return response + if(!this.connectionConfig.organization()){ + throw new Error(`You need to set the organization name`); + } + //when we will have the end point to get the databases only for the current organization + //we'll change this call + await this.getUserOrganizations(); + const dbs = this.userOrganizations().find(element => element['name'] === this.connectionConfig.organization()) + this.databases(dbs) + return dbs +} + /** * Gets the current user object as returned by the connect capabilities response * user has fields: [id, name, notes, author] @@ -821,22 +844,6 @@ WOQLClient.prototype.prepareRevisionControlArgs = function(rc_args) { -/*** - * Server Version API - * Note: the below endpoints are not part of the terminusdb desktop package - * they belong to the server package version of the DB which is under construction. - * Until that package is released all of the below endpoints should be considered - * as unreliable and subject to change - they are provided complete with the desktop - * version to show users what is coming and to allow people to use them at their own risk - * Any use of them should be considered unsupported and at your own risk - * get all the database information from the remote server - * @returns {Promise} A promise that returns the call response object, or an Error if rejected. - */ - -WOQLClient.prototype.getDatabase = function() { - return this.dispatch(CONST.GET, this.connectionConfig.dbURL()) -} - /** * update the database details * @param {object} dbDoc - an object that describe the database details @@ -1075,17 +1082,15 @@ WOQLClient.prototype.getBranches = function(dbId){ } -//this method is not documented -/* + + +/** * get the organizations and the database related * @returns {Promise} A promise that returns the call response object, or an Error if rejected. * @example - * const client = new TerminusDB.WOQLClient() * client.getUserOrganizations().then(result=>{ * console.log(result) * }) - * //get back the datatabases for the current organization - * const organizationDatabases = client.databases() */ WOQLClient.prototype.getUserOrganizations = function(){ // return response @@ -1095,9 +1100,6 @@ WOQLClient.prototype.getBranches = function(dbId){ ).then(response => { const orgList = Array.isArray(response) ? response : [] this.userOrganizations(orgList) - if (orgList.length>0 && this.connectionConfig.organization()){ - this.organization(this.connectionConfig.organization()) - } return orgList }) } From e1b53fccda35ee22cf88b2871c406838a28e6b5d Mon Sep 17 00:00:00 2001 From: Francesca-Bit Date: Fri, 4 Feb 2022 10:09:30 +0000 Subject: [PATCH 3/4] review getDatabases --- docs/_sidebar.md | 2 ++ docs/api/woqlClient.js.md | 29 ++++++++++++++++++++++++++++- lib/woqlClient.js | 14 ++++++-------- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/docs/_sidebar.md b/docs/_sidebar.md index f7b3dddf..d0c85987 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -18,6 +18,7 @@ - [server](api/woqlClient.js?id=server) - [api](api/woqlClient.js?id=api) - [organization](api/woqlClient.js?id=organization) + - [getDatabases](api/woqlClient.js?id=getDatabases) - [user](api/woqlClient.js?id=user) - [userOrganization](api/woqlClient.js?id=userOrganization) - [databases](api/woqlClient.js?id=databases) @@ -58,6 +59,7 @@ - [getEnums](api/woqlClient.js?id=getEnums) - [getClassDocuments](api/woqlClient.js?id=getClassDocuments) - [getBranches](api/woqlClient.js?id=getBranches) + - [getUserOrganizations](api/woqlClient.js?id=getUserOrganizations) - [getDiff](api/woqlClient.js?id=getDiff) - [patch](api/woqlClient.js?id=patch) - [WOQL](api/woql.js?id=WOQL) diff --git a/docs/api/woqlClient.js.md b/docs/api/woqlClient.js.md index fd95a3be..75b7983c 100644 --- a/docs/api/woqlClient.js.md +++ b/docs/api/woqlClient.js.md @@ -32,6 +32,11 @@ const client = new TerminusClient.WOQLClient('SERVER_CLOUD_URL/mycloudTeam', {user:"myemail@something.com", organization:'mycloudTeam'}) client.setApiKey(MY_ACCESS_TOKEN) +//to get the list of all organization's databases +client.getDatabases(result=>{ + console.log(result) + +}) async function getSchema() { client.db("test") client.checkout("dev") @@ -303,7 +308,8 @@ let api_url = client.api() ### organization #### woqlClient.organization([orgId]) ⇒ string \| boolean -Gets/Sets the client’s internal organization context value +Gets/Sets the client’s internal organization context value, if you change the organization name the +databases list will be set to empty | Param | Type | Description | @@ -315,6 +321,15 @@ Gets/Sets the client’s internal organization context value client.organization("admin") ``` +### getDatabases +#### woqlClient.getDatabases() ⇒ string \| boolean +Gets the organization's databases list + +**Example** +```js +client.getDatabases() +``` + ### user #### woqlClient.user() ⇒ Object Gets the current user object as returned by the connect capabilities response @@ -851,6 +866,18 @@ get the database collections list client.getBranches() ``` +### getUserOrganizations +#### woqlClient.getUserOrganizations() ⇒ Promise +get the organizations and the database related + +**Returns**: Promise - A promise that returns the call response object, or an Error if rejected. +**Example** +```js +client.getUserOrganizations().then(result=>{ + console.log(result) +}) +``` + ### getDiff #### woqlClient.getDiff(before, after) ⇒ Promise Get the patch of difference between two documents. diff --git a/lib/woqlClient.js b/lib/woqlClient.js index 6b573b56..8854d6a6 100644 --- a/lib/woqlClient.js +++ b/lib/woqlClient.js @@ -34,7 +34,7 @@ const { default: axios } = require('axios') * {user:"myemail@something.com", organization:'mycloudTeam'}) * * client.setApiKey(MY_ACCESS_TOKEN) - * //to get the list of all the team and databases + * //to get the list of all organization's databases * client.getDatabases(result=>{ * console.log(result) * @@ -145,8 +145,9 @@ WOQLClient.prototype.getDatabases = async function(){ //we'll change this call await this.getUserOrganizations(); const dbs = this.userOrganizations().find(element => element['name'] === this.connectionConfig.organization()) - this.databases(dbs) - return dbs + const dbList = dbs && dbs.databases ? dbs.databases : [] + this.databases(dbList) + return dbList } /** @@ -1078,12 +1079,9 @@ WOQLClient.prototype.getBranches = function(dbId){ this.checkout(branch) return branchesObj }) - //reset branch - + //reset branch } - - /** * get the organizations and the database related * @returns {Promise} A promise that returns the call response object, or an Error if rejected. @@ -1093,7 +1091,7 @@ WOQLClient.prototype.getBranches = function(dbId){ * }) */ WOQLClient.prototype.getUserOrganizations = function(){ - // return response + // this will be change to give back only the organizations list return this.dispatch( CONST.GET, this.connectionConfig.userOrganizationsURL(), From cb32946e50de69c83cba19a34c4ea9353e8ef515 Mon Sep 17 00:00:00 2001 From: Francesca-Bit Date: Wed, 9 Feb 2022 10:50:31 +0000 Subject: [PATCH 4/4] add getDatabases and review documentation --- docs/_sidebar.md | 4 +- docs/api/woqlClient.js.md | 85 ++++++++++++++++++++++---------------- lib/woqlClient.js | 86 ++++++++++++++++++++++----------------- 3 files changed, 101 insertions(+), 74 deletions(-) diff --git a/docs/_sidebar.md b/docs/_sidebar.md index d0c85987..5005b679 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -19,10 +19,9 @@ - [api](api/woqlClient.js?id=api) - [organization](api/woqlClient.js?id=organization) - [getDatabases](api/woqlClient.js?id=getDatabases) + - [databases](api/woqlClient.js?id=databases) - [user](api/woqlClient.js?id=user) - [userOrganization](api/woqlClient.js?id=userOrganization) - - [databases](api/woqlClient.js?id=databases) - - [userOrganizations](api/woqlClient.js?id=userOrganizations) - [databaseInfo](api/woqlClient.js?id=databaseInfo) - [db](api/woqlClient.js?id=db) - [setSystemDb](api/woqlClient.js?id=setSystemDb) @@ -60,6 +59,7 @@ - [getClassDocuments](api/woqlClient.js?id=getClassDocuments) - [getBranches](api/woqlClient.js?id=getBranches) - [getUserOrganizations](api/woqlClient.js?id=getUserOrganizations) + - [userOrganizations](api/woqlClient.js?id=userOrganizations) - [getDiff](api/woqlClient.js?id=getDiff) - [patch](api/woqlClient.js?id=patch) - [WOQL](api/woql.js?id=WOQL) diff --git a/docs/api/woqlClient.js.md b/docs/api/woqlClient.js.md index 75b7983c..d8c81ac1 100644 --- a/docs/api/woqlClient.js.md +++ b/docs/api/woqlClient.js.md @@ -32,11 +32,13 @@ const client = new TerminusClient.WOQLClient('SERVER_CLOUD_URL/mycloudTeam', {user:"myemail@something.com", organization:'mycloudTeam'}) client.setApiKey(MY_ACCESS_TOKEN) + //to get the list of all organization's databases -client.getDatabases(result=>{ - console.log(result) +async function callGetDatabases(){ + const dbList = await client.getDatabases() + console.log(dbList) +} -}) async function getSchema() { client.db("test") client.checkout("dev") @@ -323,29 +325,23 @@ client.organization("admin") ### getDatabases #### woqlClient.getDatabases() ⇒ string \| boolean -Gets the organization's databases list +Gets the organization's databases list. + +If no organization has been set up, the function throws an exception **Example** ```js -client.getDatabases() +async function callGetDatabases(){ + const dbList = await client.getDatabases() + console.log(dbList) +} ``` -### user -#### woqlClient.user() ⇒ Object -Gets the current user object as returned by the connect capabilities response -user has fields: [id, name, notes, author] - - -### userOrganization -#### woqlClient.userOrganization() ⇒ string -**Returns**: string - the user organization name -**Desription**: Gets the user's organization id - ### databases #### woqlClient.databases([dbList]) ⇒ array -Retrieves a list of databases (id, organization, label, comment) that the current user has access to on the server. +Set/Get the organization's databases list (id, label, comment) that the current user has access to on the server. -**Returns**: array - the user databases list +**Returns**: array - the organization's databases list | Param | Type | Description | | --- | --- | --- | @@ -353,23 +349,23 @@ Retrieves a list of databases (id, organization, label, comment) that the curren **Example** ```js -const my_dbs = client.databases() +//to get the list of all organization's databases +async function callGetDatabases(){ + await client.getDatabases() + console.log(client.databases()) +} ``` -### userOrganizations -#### woqlClient.userOrganizations([orgList]) ⇒ array -Retrieves a list of databases (id, organization, label, comment) that the current user has access to on the server. - -**Returns**: array - the user databases list +### user +#### woqlClient.user() ⇒ Object +Gets the current user object as returned by the connect capabilities response +user has fields: [id, name, notes, author] -| Param | Type | Description | -| --- | --- | --- | -| [orgList] | array | a list of databases the user has access to on the server, each having: | -**Example** -```js -const my_dbs = client.databases() -``` +### userOrganization +#### woqlClient.userOrganization() ⇒ string +**Returns**: string - the user organization name +**Desription**: Gets the user's organization id ### databaseInfo #### woqlClient.databaseInfo([dbId]) ⇒ object @@ -868,14 +864,33 @@ client.getBranches() ### getUserOrganizations #### woqlClient.getUserOrganizations() ⇒ Promise -get the organizations and the database related +Get the list of the user's organizations and the database related **Returns**: Promise - A promise that returns the call response object, or an Error if rejected. **Example** ```js -client.getUserOrganizations().then(result=>{ - console.log(result) -}) +async funtion callGetUserOrganizations(){ + await getUserOrganizations() + console.log(client.userOrganizations()) +} +``` + +### userOrganizations +#### woqlClient.userOrganizations([orgList]) ⇒ array +Get/Set the list of the user's organizations (id, organization, label, comment). + +**Returns**: array - the user Organizations list + +| Param | Type | Description | +| --- | --- | --- | +| [orgList] | array | a list of user's Organization | + +**Example** +```js +async funtion callGetUserOrganizations(){ + await client.getUserOrganizations() + console.log(client.userOrganizations()) +} ``` ### getDiff diff --git a/lib/woqlClient.js b/lib/woqlClient.js index 8854d6a6..18a892a8 100644 --- a/lib/woqlClient.js +++ b/lib/woqlClient.js @@ -34,11 +34,13 @@ const { default: axios } = require('axios') * {user:"myemail@something.com", organization:'mycloudTeam'}) * * client.setApiKey(MY_ACCESS_TOKEN) + * * //to get the list of all organization's databases - * client.getDatabases(result=>{ - * console.log(result) + * async function callGetDatabases(){ + * const dbList = await client.getDatabases() + * console.log(dbList) + * } * - * }) * async function getSchema() { * client.db("test") * client.checkout("dev") @@ -131,10 +133,15 @@ WOQLClient.prototype.organization = function(orgId) { } /** - * Gets the organization's databases list + * Gets the organization's databases list. + * + * If no organization has been set up, the function throws an exception * @returns {string | boolean} * @example - * client.getDatabases() + * async function callGetDatabases(){ + * const dbList = await client.getDatabases() + * console.log(dbList) + * } */ WOQLClient.prototype.getDatabases = async function(){ // return response @@ -150,6 +157,23 @@ WOQLClient.prototype.getDatabases = async function(){ return dbList } +/** + * Set/Get the organization's databases list (id, label, comment) that the current user has access to on the server. + * @param {array} [dbList] a list of databases the user has access to on the server, each having: + * @returns {array} the organization's databases list + * @example + * //to get the list of all organization's databases + * async function callGetDatabases(){ + * await client.getDatabases() + * console.log(client.databases()) + * } + * + */ + WOQLClient.prototype.databases = function(dbList) { + if (dbList) this.databaseList = dbList + return this.databaseList || [] +} + /** * Gets the current user object as returned by the connect capabilities response * user has fields: [id, name, notes, author] @@ -170,32 +194,6 @@ WOQLClient.prototype.userOrganization = function() { } -/** - * Retrieves a list of databases (id, organization, label, comment) that the current user has access to on the server. - * @param {array} [dbList] a list of databases the user has access to on the server, each having: - * @returns {array} the user databases list - * @example - * const my_dbs = client.databases() - */ -WOQLClient.prototype.databases = function(dbList) { - if (dbList) this.databaseList = dbList - return this.databaseList || [] -} - -/** - * Retrieves a list of databases (id, organization, label, comment) that the current user has access to on the server. - * @param {array} [orgList] a list of databases the user has access to on the server, each having: - * @returns {array} the user databases list - * @example - * const my_dbs = client.databases() - */ - WOQLClient.prototype.userOrganizations = function(orgList) { - if (orgList) this.organizationList = orgList - return this.organizationList || [] -} - - - /** * Gets the database's details * @param {string} [dbId] - the datbase id @@ -220,8 +218,6 @@ WOQLClient.prototype.db = function(dbId) { this.connectionConfig.setDB(dbId) } return this.connectionConfig.dbid - - //this.connectionConfig.db() } /** @@ -1083,12 +1079,13 @@ WOQLClient.prototype.getBranches = function(dbId){ } /** - * get the organizations and the database related + * Get the list of the user's organizations and the database related * @returns {Promise} A promise that returns the call response object, or an Error if rejected. * @example - * client.getUserOrganizations().then(result=>{ - * console.log(result) - * }) + * async funtion callGetUserOrganizations(){ + * await getUserOrganizations() + * console.log(client.userOrganizations()) + * } */ WOQLClient.prototype.getUserOrganizations = function(){ // this will be change to give back only the organizations list @@ -1102,6 +1099,21 @@ WOQLClient.prototype.getBranches = function(dbId){ }) } +/** + * Get/Set the list of the user's organizations (id, organization, label, comment). + * @param {array} [orgList] a list of user's Organization + * @returns {array} the user Organizations list + * @example + * async funtion callGetUserOrganizations(){ + * await client.getUserOrganizations() + * console.log(client.userOrganizations()) + * } + */ + WOQLClient.prototype.userOrganizations = function(orgList) { + if (orgList) this.organizationList = orgList + return this.organizationList || [] +} + /** * Get the patch of difference between two documents. * @param {object} before - The current state of JSON document