Skip to content

Commit b9f5d93

Browse files
authored
Merge pull request #53 from terminusdb/dev
add multi organization
2 parents d2860cb + d082b7f commit b9f5d93

File tree

5 files changed

+90
-15
lines changed

5 files changed

+90
-15
lines changed

lib/connectionConfig.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function ConnectionConfig(serverUrl, params) {
5656
*/
5757
ConnectionConfig.prototype.copy = function() {
5858
let other = new ConnectionConfig(this.server)
59+
other.api_extension = this.api_extension
5960
other.remote_auth = this.remote_auth
6061
other.local_auth = this.local_auth
6162
other.organizationid = this.organizationid
@@ -362,6 +363,18 @@ ConnectionConfig.prototype.organizationURL = function(orgId, action) {
362363
return url
363364
}
364365

366+
/**
367+
* Generate URL for the user's organization api endpoint
368+
* @param {string} orgId - the organization id
369+
* @param {string} [action] - the organization id
370+
* @returns {string}
371+
*/
372+
373+
ConnectionConfig.prototype.userOrganizationsURL = function() {
374+
let url = `${this.apiURL()}user_organizations`
375+
return url
376+
}
377+
365378
/**
366379
* Generate URL for the user's roles api endpoint
367380
* @returns {string}

lib/query/woqlQuery.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ let WOQLQuery = require('./woqlCore')
99
}
1010
}*/
1111

12+
WOQLQuery.prototype.read_object = function(IRI, OutputVar, Format) {
13+
//if (IRI && IRI == 'args') return ['document_uri', 'document']
14+
if (this.cursor['@type']) this.wrapCursorWithAnd()
15+
this.cursor['@type'] = 'ReadObject'
16+
this.cursor['document_uri'] = this.expandValueVariable(IRI)
17+
this.cursor['document'] = this.expandValueVariable(OutputVar)
18+
return this.wform(Format)
19+
}
20+
1221
/**
1322
* Contains definitions of the WOQL functions which map directly to JSON-LD types
1423
* All other calls and queries can be composed from these
@@ -799,7 +808,6 @@ WOQLQuery.prototype.length = function(va, vb) {
799808
* Negation of passed or chained query
800809
*/
801810
WOQLQuery.prototype.not = function(query) {
802-
//if (query && query == 'args') return ['query']
803811
if (this.cursor['@type']) this.wrapCursorWithAnd()
804812
this.cursor['@type'] = 'Not'
805813
return this.addSubQuery(query)

lib/typedef.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
//typedef
22
/**
33
*@typedef {Object} DocParamsGet - the GET document interface query parameters
4-
*@property {string} [graph_type=instance] - instance|schema default value is instance. Used to switch between getting documents from the instance or the schema graph.
4+
*@property {GraphType} [graph_type] - instance|schema, default value is instance. Used to switch between getting documents from the instance or the schema graph.
55
*@property {string} [type] - only documents of the given type are returned.
66
*@property {string} [id] - only the document with the given ID is returned.
7-
*@property {boolean} [prefixed=true] - return IRIs using a prefixed notation wherever possible. If false, full IRIs are used.
8-
*@property {boolean} [minimized=false] - return the documents with very little whitespace. Each json document will be on its own line.
9-
*@property {boolean} [unfold=true] - any subdocuments contained in the returned document are returned too. If false, these are referred to by their ID instead.
10-
*@property {number} [skip=0] - How many results to skip
7+
*@property {boolean} [prefixed] - default is true, return IRIs using a prefixed notation wherever possible. If false, full IRIs are used.
8+
*@property {boolean} [minimized] - default is false, return the documents with very little whitespace. Each json document will be on its own line.
9+
*@property {boolean} [unfold] - default is false, any subdocuments contained in the returned document are returned too. If false, these are referred to by their ID instead.
10+
*@property {number} [skip] - default is 0, How many results to skip
1111
*@property {number} [count] count - How many results to return. If this option is absent, all results are returned.
12-
*@property {boolean} [as_list=false] If true, don't return a stream of json objects, but a json list.
12+
*@property {boolean} [as_list] default is false, If true, don't return a stream of json objects, but a json list.
1313
*@property {string} [graph_type] - instance|schema default value is instance
1414
*/
1515

1616
/**
1717
*@typedef {Object} DocParamsPost - the POST document interface query parameters
18-
*@property {string} [graph_type=instance] - instance|schema Used to switch between getting documents from the instance or the schema graph.
19-
*@property {boolean} [full_replace=false] - If true, all existing documents are deleted before inserting the posted documents. This allows the full replacement of the contents of a database. This is especially useful for replacing the schema.
18+
*@property {GraphType} [graph_type] - default is instance instance|schema Used to switch between getting documents from the instance or the schema graph.
19+
*@property {boolean} [full_replace] - default is false, If true, all existing documents are deleted before inserting the posted documents. This allows the full replacement of the contents of a database. This is especially useful for replacing the schema.
2020
*/
2121

2222
/**
2323
*@typedef {Object} DocParamsPut - the PUT document interface query parameters
24-
*@property {string} [graph_type=instance] - instance|schema Used to switch between getting documents from the instance or the schema graph.
24+
*@property {GraphType} [graph_type] - default is instance, instance|schema Used to switch between getting documents from the instance or the schema graph.
2525
*/
2626

2727
/**
2828
*@typedef {Object} DocParamsDelete - the DELETE document interface query parameters
29-
*@property {string} [graph_type=instance] - instance|schema Used to switch between getting documents from the instance or the schema graph.
29+
*@property {GraphType} [graph_type] - default is instance, instance|schema Used to switch between getting documents from the instance or the schema graph.
3030
*@property {string|array} id - a single id or a list of ids to delete.
31-
*@property {booleam} [nuke=false] - If true, delete everything at this resource location (dangerous!).
31+
*@property {booleam} [nuke] - default is false, If true, delete everything at this resource location (dangerous!).
3232
*/
3333

3434

lib/woql.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,10 @@ WOQL.string = function(val) {
11151115
return new WOQLQuery().string(val)
11161116
}
11171117

1118+
WOQL.read_object = function(IRI, output, formatObj) {
1119+
return new WOQLQuery().read_object(IRI, output, formatObj)
1120+
}
1121+
11181122
/**
11191123
* Generates explicitly a JSON-LD string literal from the input
11201124
* @param {string} val - any literal type

lib/woqlClient.js

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function WOQLClient(serverUrl, params) {
4747
// current connection context variables
4848
this.connectionConfig = new ConnectionConfig(serverUrl, params)
4949
this.databaseList = []
50+
this.organizationList =[]
5051
}
5152

5253

@@ -115,7 +116,11 @@ WOQLClient.prototype.api = function() {
115116
*/
116117
WOQLClient.prototype.organization = function(orgId) {
117118
if (typeof orgId !== 'undefined') {
118-
this.connectionConfig.setOrganization(orgId)
119+
this.connectionConfig.setOrganization(orgId)
120+
const orgObj = this.userOrganizations().find(element => element['name'] === orgId)
121+
const dbs = orgObj ? orgObj.databases : []
122+
//change the database list
123+
this.databases(dbs)
119124
}
120125
return this.connectionConfig.organization()
121126
}
@@ -152,6 +157,20 @@ WOQLClient.prototype.databases = function(dbList) {
152157
return this.databaseList || []
153158
}
154159

160+
/**
161+
* Retrieves a list of databases (id, organization, label, comment) that the current user has access to on the server. Note that this requires the client to call connect() first.
162+
* @param {array} [orgList] a list of databases the user has access to on the server, each having:
163+
* @returns {array} the user databases list
164+
* @example
165+
* const my_dbs = client.databases()
166+
*/
167+
WOQLClient.prototype.userOrganizations = function(orgList) {
168+
if (orgList) this.organizationList = orgList
169+
return this.organizationList || []
170+
}
171+
172+
173+
155174
/**
156175
* Gets the database's details
157176
* @param {string} [dbId] - the datbase id
@@ -332,9 +351,8 @@ WOQLClient.prototype.resource = function(resourceType, resourceId) {
332351
WOQLClient.prototype.connect = function(params) {
333352
if (params) this.connectionConfig.update(params)
334353
// unset the current server setting until successful connect
335-
return this.dispatch(CONST.GET, this.api()).then(response => {
354+
return this.dispatch(CONST.GET, this.api()).then(response => {
336355
this.databases(response)
337-
return response
338356
})
339357
}
340358

@@ -1048,4 +1066,36 @@ WOQLClient.prototype.getBranches = function(dbId){
10481066

10491067
}
10501068

1069+
//this method is not documented
1070+
/*
1071+
* get the organizations and the database related
1072+
* @param {string} [defaultOrg] - the organizatation to set
1073+
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
1074+
* @example
1075+
* client.getUserOrganizations()
1076+
*/
1077+
WOQLClient.prototype.getUserOrganizations = function(defaultOrg){
1078+
//this.databases(response)
1079+
// return response
1080+
return this.dispatch(
1081+
CONST.GET,
1082+
this.connectionConfig.userOrganizationsURL(),
1083+
).then(response => {
1084+
const orgList = Array.isArray(response) ? response : []
1085+
this.userOrganizations(orgList)
1086+
if (orgList.length>0){
1087+
//let orgObj = {}
1088+
if(defaultOrg){
1089+
orgObj = response.find(element => element['name'] === defaultOrg)
1090+
1091+
}else{
1092+
orgObj = response[0]
1093+
}
1094+
this.organization(orgObj['name'])
1095+
}
1096+
return response
1097+
})
1098+
1099+
}
1100+
10511101
module.exports = WOQLClient

0 commit comments

Comments
 (0)