Skip to content

Commit edc62d2

Browse files
committed
update docjs
1 parent 10ce41e commit edc62d2

File tree

6 files changed

+245
-76
lines changed

6 files changed

+245
-76
lines changed

lib/connectionCapabilities.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ ConnectionCapabilities.prototype.set_databases = function(newdbs) {
6262
ConnectionCapabilities.prototype.get_database = function(dbid, orgid) {
6363
if(!this.databases) this.databases = this._databases_from_dbdocs()
6464
for(var i = 0; i<this.databases.length; i++){
65-
if(this.databases[i].id == dbid && this.databases[i].organization == orgid) return this.databases[i]
65+
if(this.databases[i].id == dbid && this.databases[i].organization == orgid){
66+
return this.databases[i]
67+
}
6668
}
6769
}
6870

6971
ConnectionCapabilities.prototype._databases_from_dbdocs = function() {
7072
let dbs = []
7173
for(var docid in this.dbdocs){
72-
if(this.dbdocs[docid].system == false) {
74+
if(this.dbdocs[docid].system === false) {
7375
dbs.push(this._get_db_rec(this.dbdocs[docid]))
7476
}
7577
}

lib/connectionConfig.js

Lines changed: 151 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
1+
/**
2+
* @typedef {type:'basic'|'jwt', user:string, key:string } CredentialObj
3+
*/
4+
15
/**
26
* @file Terminus DB connection configuration
37
* @license Apache Version 2
48
* @description Object representing the state of a connection to a terminus db - these are:
5-
* 1. server url (set on connect)
6-
* 2. current organization id
7-
* 2. dbid, organization, api key, remote auth
8-
* along with some configuration information (key, connected mode, client_side_access_control)
9+
* @constructor
10+
* @param {string} serverUrl - the terminusdb server url
11+
* @param {object | undefined} params - an object with the following connection parameters:
12+
* @param {string} params.key - api key for basic auth
13+
* @param {string} params.user - basic auth user id
14+
* @param {string} params.organization - set organization to this id
15+
* @param {string} params.db - set cursor to this db
16+
* @param {?string} params.repo - set cursor to this repo
17+
* @param {?string} params.branch - set branch to this id
18+
* @param {?ref} params.ref - set commit ref
19+
* @param {?string} params.jwt - jwt token
20+
* @param {?string} params.jwt_user - jwt user id
21+
//KEVIN
22+
* @param {?string} params.default_branch_id - set the default branch id
923
* provides methods for getting and setting connection parameters
1024
*/
11-
function ConnectionConfig(serverUrl, params) {
25+
26+
function ConnectionConfig(serverUrl, params=null) {
27+
/** @type {string | boolean} */
1228
this.server = false
29+
30+
/** @type {CredentialObj | boolean} */
1331
this.remote_auth = false //remote auth for authenticating to remote servers for push / fetch / clone
32+
33+
/** @type {CredentialObj | boolean} */
1434
this.local_auth = false // basic auth string for authenticating to local server
35+
1536
//these operate as cursors - where within the connected server context, we currently are
1637
this.organizationid = false
1738
this.dbid = false
@@ -33,13 +54,15 @@ function ConnectionConfig(serverUrl, params) {
3354
this.server = surl
3455
if (params) this.update(params)
3556
} else {
57+
//better throw an error
3658
this.setError(`Invalid Server URL: ${serverUrl}`)
3759
}
3860
//this.setServer(serverUrl);
3961
}
4062

4163
/**
42-
* Creates a new connection config object and copies all the state information from this one into it
64+
* @description Creates a new connection config object and copies all the state information from this one into it
65+
* @returns {ConnectionConfig}
4366
*/
4467
ConnectionConfig.prototype.copy = function() {
4568
let other = new ConnectionConfig(this.server)
@@ -54,77 +77,133 @@ ConnectionConfig.prototype.copy = function() {
5477
}
5578

5679
/**
57-
* updates connection config with new values set in json params object
80+
* updates connection config with new parameters
81+
* @param {object} params - an object with the following connection parameters:
82+
* @param {string} params.key - api key for basic auth
83+
* @param {string} params.user - basic auth user id
84+
* @param {string} params.organization - set organization to this id
85+
* @param {string} params.db - set cursor to this db
86+
* @param {?string} params.repo - set cursor to this repo
87+
* @param {?string} params.branch - set branch to this id
88+
* @param {?ref} params.ref - set commit ref
89+
* @param {?string} params.jwt - jwt token
90+
* @param {?string} params.jwt_user - jwt user id
91+
/
5892
*/
5993
ConnectionConfig.prototype.update = function(params) {
60-
const newParams = params || {}
61-
//if (typeof newParams.server != "undefined") this.setServer(newParams.server);
62-
if (typeof newParams.organization != 'undefined') this.setOrganization(newParams.organization)
63-
else if(newParams.user) this.setOrganization(newParams.user)
64-
if (typeof newParams.db != 'undefined') this.setDB(newParams.db)
65-
if (typeof newParams.remote_auth != 'undefined') this.setRemoteAuth(newParams.remote_auth)
66-
if (typeof newParams.local_auth != 'undefined') this.setLocalAuth(newParams.local_auth)
67-
if (typeof newParams.key != 'undefined') this.setLocalBasicAuth(newParams.key, newParams.user)
68-
if (typeof newParams.branch != 'undefined') this.setBranch(newParams.branch)
69-
if (typeof newParams.ref != 'undefined') this.setRef(newParams.ref)
70-
if (typeof newParams.repo != 'undefined') this.setRepo(newParams.repo)
94+
if(!params)return
95+
if (typeof params.organization !== 'undefined') this.setOrganization(params.organization)
96+
else if(params.user) this.setOrganization(params.user)
97+
if (typeof params.db !== 'undefined') this.setDB(params.db)
98+
//if (params.remote_auth !== undefined) this.setRemoteAuth(newParams.remote_auth)
99+
//if (params.local_auth !== undefined) this.setLocalAuth(newParams.local_auth)
100+
if (typeof params.key !== 'undefined') this.setLocalBasicAuth(params.key, params.user)
101+
if (typeof params.branch !== 'undefined') this.setBranch(params.branch)
102+
if (typeof params.ref !== 'undefined') this.setRef(params.ref)
103+
if (typeof params.repo !== 'undefined') this.setRepo(params.repo)
71104
}
72105

73106
/**
74107
* Simple gets to retrieve current connection status
108+
* Gets the current server url
109+
* @returns {string}
75110
*/
76111
ConnectionConfig.prototype.serverURL = function() {
77112
return this.server
78113
}
79114

80115
/**
81-
* Simple gets to retrieve current connection status
116+
* @description Gets the server connection url
117+
* @returns {string}
82118
*/
83119
ConnectionConfig.prototype.apiURL = function() {
84120
return this.server + this.api_extension
85121
}
86122

87-
123+
/**
124+
* @description Gets the current database id
125+
* @returns {string | boolean}
126+
*/
88127
ConnectionConfig.prototype.db = function() {
89128
return this.dbid
90129
}
91130

131+
/**
132+
* @description Gets the current branch id
133+
* @returns {string}
134+
*/
92135
ConnectionConfig.prototype.branch = function() {
93136
return this.branchid
94137
}
95138

139+
/**
140+
* @description Gets the current commit ref id
141+
* @returns {string | boolean}
142+
*/
96143
ConnectionConfig.prototype.ref = function() {
97144
return this.refid
98145
}
99146

147+
/**
148+
* @description Gets the current organization id
149+
* @returns {string | boolean}
150+
*/
100151
ConnectionConfig.prototype.organization = function() {
101152
return this.organizationid
102153
}
103154

155+
156+
/**
157+
* @description Gets the current organization id
158+
* @returns {string}
159+
*/
104160
ConnectionConfig.prototype.repo = function() {
105161
return this.repoid
106162
}
107163

164+
/**
165+
*@description Gets the local Authorization credentials
166+
*return {CredentialObj | boolean}
167+
*/
108168
ConnectionConfig.prototype.localAuth = function() {
109169
return this.local_auth
110170
}
111171

172+
/**
173+
*@description Gets the local user name
174+
*return {string | boolean}
175+
*/
112176
ConnectionConfig.prototype.local_user = function() {
113177
if (this.local_auth) return this.local_auth.user
178+
return false
114179
}
115180

181+
/**
182+
*@description Gets the remote Authorization credentials
183+
*to connect the local db with a remote terminusdb database
184+
*return {CredentialObj | boolean}
185+
*/
116186
ConnectionConfig.prototype.remoteAuth = function() {
117187
return this.remote_auth
118188
}
119189

120-
ConnectionConfig.prototype.user = function(ignore_jwt) {
190+
/**
191+
*@description Gets the current user name
192+
*@param {boolean} ignoreJwt
193+
*return {string}
194+
*/
195+
ConnectionConfig.prototype.user = function(ignoreJwt) {
121196
if (!ignore_jwt && this.remote_auth && this.remote_auth.type == 'jwt')
122197
return this.remote_auth.user
123198
if (this.local_auth) {
124199
return this.local_auth.user
125200
}
126201
}
127202

203+
/**
204+
* @param
205+
*/
206+
128207
ConnectionConfig.prototype.parseServerURL = function(str) {
129208
if (str && (str.substring(0, 7) === 'http://' || str.substring(0, 8) === 'https://')) {
130209
if (str.lastIndexOf('/') !== str.length - 1) {
@@ -163,49 +242,66 @@ ConnectionConfig.prototype.setError = function(str) {
163242
}
164243

165244
/**
166-
* @param {String} inputStr - organization to which the connected db belongs (not the users organization - set in capabilities)
245+
* @description Set the organization to which the connected db belongs
246+
* (not the users organization - set in capabilities)
247+
* @param {String} organization
248+
*
167249
*/
168-
ConnectionConfig.prototype.setOrganization = function(inputStr) {
169-
this.organizationid = inputStr || false
250+
ConnectionConfig.prototype.setOrganization = function(organization) {
251+
this.organizationid = organization || false
170252
return this.organizationid
171253
}
172254

173255
/**
174-
* @param {String} inputStr - local identifier of db
256+
* @description Set the local identifier of db
257+
* @param {string | boolean} db - database Id
258+
//KEVIN we need that return ???
259+
provably before you did a control but we don't need that return
260+
* return {string | boolean}
175261
*/
176-
ConnectionConfig.prototype.setDB = function(inputStr) {
177-
this.dbid = inputStr
262+
ConnectionConfig.prototype.setDB = function(db) {
263+
this.dbid = db
178264
return this.dbid
179265
}
180266

181267
/**
182-
* @param {String} inputStr - local identifier of repo
268+
* @typedef {"local"|"remote"} RepoType
183269
*/
184-
ConnectionConfig.prototype.setRepo = function(inputStr) {
185-
this.repoid = inputStr
270+
271+
/**
272+
* @description Set the repository type |local|remote|
273+
* @param {RepoType} repo - for the local server - identifier of repo
274+
*/
275+
ConnectionConfig.prototype.setRepo = function(repo) {
276+
this.repoid = repo
186277
return this.repoid
187278
}
188279

189280
/**
190-
* @param {String} inputStr - id of branch
281+
* @param {String | boolean} branch - id of branch
282+
* @return {string}
191283
*/
192-
ConnectionConfig.prototype.setBranch = function(inputStr) {
193-
this.branchid = inputStr
284+
ConnectionConfig.prototype.setBranch = function(branch) {
285+
this.branchid = branch
194286
if(!this.branchid) this.branchid = this.default_branch_id
195287
return this.branchid
196288
}
197289

198290
/**
199-
* @param {String} inputStr - id of ref
291+
* Set an Reference ID or Commit ID.
292+
* Commit IDs are unique hashes that are created whenever a new commit is recorded
293+
* @param {string | boolean} refId - commit reference id
294+
* @return {string | boolean}
200295
*/
201-
ConnectionConfig.prototype.setRef = function(inputStr) {
202-
this.refid = inputStr
296+
ConnectionConfig.prototype.setRef = function(refId) {
297+
this.refid = refId
203298
return this.refid
204299
}
205300

206301
/**
207-
* @param {String} userKey - api key
208-
* @param {String} userId - basic auth user id key
302+
* @description set the local database connection credential
303+
* @param {?string} userKey - basic auth api key
304+
* @param {?string} userId - user id
209305
*/
210306
ConnectionConfig.prototype.setLocalBasicAuth = function(userKey, userId) {
211307
if (!userKey) {
@@ -219,8 +315,13 @@ ConnectionConfig.prototype.setLocalBasicAuth = function(userKey, userId) {
219315
}
220316
}
221317

222-
ConnectionConfig.prototype.setLocalAuth = function(details) {
223-
this.local_auth = details
318+
319+
/**
320+
* @description set the local database connection credential
321+
* @param {CredentialObj} newCredential
322+
*/
323+
ConnectionConfig.prototype.setLocalAuth = function(newCredential) {
324+
this.local_auth = newCredential
224325
}
225326

226327

@@ -395,8 +496,14 @@ ConnectionConfig.prototype.repoBase = function(action) {
395496
return b
396497
}
397498

499+
500+
// @typedef {"keyvalue" | "bar" | "timeseries" | "pie" | "table"} ActionString
501+
502+
398503
/**
504+
* @description Get database branch Url
399505
* Generate base branch url consisting of server/action/organization/dbid/branchid
506+
* @param {string} action
400507
*/
401508
ConnectionConfig.prototype.branchBase = function(action) {
402509
let b = this.repoBase(action)
@@ -416,10 +523,12 @@ ConnectionConfig.prototype.branchBase = function(action) {
416523
}
417524

418525
/**
419-
* Generate url portion consisting of organization/dbid (unless dbid = terminus in which case there is no organization)
526+
* @description Generate url portion consisting of organization/dbid
527+
* (unless dbid = system dbname in which case there is no organization)
528+
* @return {string}
420529
*/
421530
ConnectionConfig.prototype.dbURLFragment = function() {
422-
if (this.db() == this.system_db) return this.db()
531+
if (this.db() === this.system_db) return this.db()
423532
return this.organization() + '/' + this.db()
424533
}
425534

lib/dispatchRequest.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,20 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null) {
4747
options.httpsAgent = agent
4848
}
4949

50+
/*
51+
* I can call the local database using the local authorization key or
52+
* a jwt token
53+
*/
5054
if (local_auth && local_auth.type == 'basic') {
5155
let encoded = btoaImplementation(local_auth.user + ':' + local_auth.key)
5256
options.headers = {Authorization: `Basic ${encoded}`}
5357
} else if (local_auth && local_auth.type == 'jwt') {
5458
options.headers = {Authorization: `Bearer ${local_auth.key}`}
5559
}
60+
/*
61+
* pass the Authorization information of another
62+
* terminusdb server to the local terminusdb
63+
*/
5664
if (remote_auth && remote_auth.type == 'jwt') {
5765
if (!options.headers) options.headers = {}
5866
options.headers['Authorization-Remote'] = `Bearer ${remote_auth.key}`

0 commit comments

Comments
 (0)