1
+ /**
2
+ * @typedef {type:'basic'|'jwt', user:string, key:string } CredentialObj
3
+ */
4
+
1
5
/**
2
6
* @file Terminus DB connection configuration
3
7
* @license Apache Version 2
4
8
* @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
9
23
* provides methods for getting and setting connection parameters
10
24
*/
11
- function ConnectionConfig ( serverUrl , params ) {
25
+
26
+ function ConnectionConfig ( serverUrl , params = null ) {
27
+ /** @type {string | boolean } */
12
28
this . server = false
29
+
30
+ /** @type {CredentialObj | boolean } */
13
31
this . remote_auth = false //remote auth for authenticating to remote servers for push / fetch / clone
32
+
33
+ /** @type {CredentialObj | boolean } */
14
34
this . local_auth = false // basic auth string for authenticating to local server
35
+
15
36
//these operate as cursors - where within the connected server context, we currently are
16
37
this . organizationid = false
17
38
this . dbid = false
@@ -33,13 +54,15 @@ function ConnectionConfig(serverUrl, params) {
33
54
this . server = surl
34
55
if ( params ) this . update ( params )
35
56
} else {
57
+ //better throw an error
36
58
this . setError ( `Invalid Server URL: ${ serverUrl } ` )
37
59
}
38
60
//this.setServer(serverUrl);
39
61
}
40
62
41
63
/**
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 }
43
66
*/
44
67
ConnectionConfig . prototype . copy = function ( ) {
45
68
let other = new ConnectionConfig ( this . server )
@@ -54,77 +77,133 @@ ConnectionConfig.prototype.copy = function() {
54
77
}
55
78
56
79
/**
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
+ /
58
92
*/
59
93
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 )
71
104
}
72
105
73
106
/**
74
107
* Simple gets to retrieve current connection status
108
+ * Gets the current server url
109
+ * @returns {string }
75
110
*/
76
111
ConnectionConfig . prototype . serverURL = function ( ) {
77
112
return this . server
78
113
}
79
114
80
115
/**
81
- * Simple gets to retrieve current connection status
116
+ * @description Gets the server connection url
117
+ * @returns {string }
82
118
*/
83
119
ConnectionConfig . prototype . apiURL = function ( ) {
84
120
return this . server + this . api_extension
85
121
}
86
122
87
-
123
+ /**
124
+ * @description Gets the current database id
125
+ * @returns {string | boolean }
126
+ */
88
127
ConnectionConfig . prototype . db = function ( ) {
89
128
return this . dbid
90
129
}
91
130
131
+ /**
132
+ * @description Gets the current branch id
133
+ * @returns {string }
134
+ */
92
135
ConnectionConfig . prototype . branch = function ( ) {
93
136
return this . branchid
94
137
}
95
138
139
+ /**
140
+ * @description Gets the current commit ref id
141
+ * @returns {string | boolean }
142
+ */
96
143
ConnectionConfig . prototype . ref = function ( ) {
97
144
return this . refid
98
145
}
99
146
147
+ /**
148
+ * @description Gets the current organization id
149
+ * @returns {string | boolean }
150
+ */
100
151
ConnectionConfig . prototype . organization = function ( ) {
101
152
return this . organizationid
102
153
}
103
154
155
+
156
+ /**
157
+ * @description Gets the current organization id
158
+ * @returns {string }
159
+ */
104
160
ConnectionConfig . prototype . repo = function ( ) {
105
161
return this . repoid
106
162
}
107
163
164
+ /**
165
+ *@description Gets the local Authorization credentials
166
+ *return {CredentialObj | boolean}
167
+ */
108
168
ConnectionConfig . prototype . localAuth = function ( ) {
109
169
return this . local_auth
110
170
}
111
171
172
+ /**
173
+ *@description Gets the local user name
174
+ *return {string | boolean}
175
+ */
112
176
ConnectionConfig . prototype . local_user = function ( ) {
113
177
if ( this . local_auth ) return this . local_auth . user
178
+ return false
114
179
}
115
180
181
+ /**
182
+ *@description Gets the remote Authorization credentials
183
+ *to connect the local db with a remote terminusdb database
184
+ *return {CredentialObj | boolean}
185
+ */
116
186
ConnectionConfig . prototype . remoteAuth = function ( ) {
117
187
return this . remote_auth
118
188
}
119
189
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 ) {
121
196
if ( ! ignore_jwt && this . remote_auth && this . remote_auth . type == 'jwt' )
122
197
return this . remote_auth . user
123
198
if ( this . local_auth ) {
124
199
return this . local_auth . user
125
200
}
126
201
}
127
202
203
+ /**
204
+ * @param
205
+ */
206
+
128
207
ConnectionConfig . prototype . parseServerURL = function ( str ) {
129
208
if ( str && ( str . substring ( 0 , 7 ) === 'http://' || str . substring ( 0 , 8 ) === 'https://' ) ) {
130
209
if ( str . lastIndexOf ( '/' ) !== str . length - 1 ) {
@@ -163,49 +242,66 @@ ConnectionConfig.prototype.setError = function(str) {
163
242
}
164
243
165
244
/**
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
+ *
167
249
*/
168
- ConnectionConfig . prototype . setOrganization = function ( inputStr ) {
169
- this . organizationid = inputStr || false
250
+ ConnectionConfig . prototype . setOrganization = function ( organization ) {
251
+ this . organizationid = organization || false
170
252
return this . organizationid
171
253
}
172
254
173
255
/**
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}
175
261
*/
176
- ConnectionConfig . prototype . setDB = function ( inputStr ) {
177
- this . dbid = inputStr
262
+ ConnectionConfig . prototype . setDB = function ( db ) {
263
+ this . dbid = db
178
264
return this . dbid
179
265
}
180
266
181
267
/**
182
- * @param { String } inputStr - local identifier of repo
268
+ * @typedef { "local"|"remote" } RepoType
183
269
*/
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
186
277
return this . repoid
187
278
}
188
279
189
280
/**
190
- * @param {String } inputStr - id of branch
281
+ * @param {String | boolean } branch - id of branch
282
+ * @return {string }
191
283
*/
192
- ConnectionConfig . prototype . setBranch = function ( inputStr ) {
193
- this . branchid = inputStr
284
+ ConnectionConfig . prototype . setBranch = function ( branch ) {
285
+ this . branchid = branch
194
286
if ( ! this . branchid ) this . branchid = this . default_branch_id
195
287
return this . branchid
196
288
}
197
289
198
290
/**
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 }
200
295
*/
201
- ConnectionConfig . prototype . setRef = function ( inputStr ) {
202
- this . refid = inputStr
296
+ ConnectionConfig . prototype . setRef = function ( refId ) {
297
+ this . refid = refId
203
298
return this . refid
204
299
}
205
300
206
301
/**
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
209
305
*/
210
306
ConnectionConfig . prototype . setLocalBasicAuth = function ( userKey , userId ) {
211
307
if ( ! userKey ) {
@@ -219,8 +315,13 @@ ConnectionConfig.prototype.setLocalBasicAuth = function(userKey, userId) {
219
315
}
220
316
}
221
317
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
224
325
}
225
326
226
327
@@ -395,8 +496,14 @@ ConnectionConfig.prototype.repoBase = function(action) {
395
496
return b
396
497
}
397
498
499
+
500
+ // @typedef {"keyvalue" | "bar" | "timeseries" | "pie" | "table" } ActionString
501
+
502
+
398
503
/**
504
+ * @description Get database branch Url
399
505
* Generate base branch url consisting of server/action/organization/dbid/branchid
506
+ * @param {string } action
400
507
*/
401
508
ConnectionConfig . prototype . branchBase = function ( action ) {
402
509
let b = this . repoBase ( action )
@@ -416,10 +523,12 @@ ConnectionConfig.prototype.branchBase = function(action) {
416
523
}
417
524
418
525
/**
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 }
420
529
*/
421
530
ConnectionConfig . prototype . dbURLFragment = function ( ) {
422
- if ( this . db ( ) == this . system_db ) return this . db ( )
531
+ if ( this . db ( ) === this . system_db ) return this . db ( )
423
532
return this . organization ( ) + '/' + this . db ( )
424
533
}
425
534
0 commit comments