Skip to content

Commit d519ff7

Browse files
committed
review terminusdb-client
1 parent ecc5aef commit d519ff7

16 files changed

+17504
-10426
lines changed

.babelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2+
3+
"sourceType": "unambiguous",
24
"presets": ["@babel/preset-env"],
35
"plugins": ["@babel/plugin-transform-regenerator",
46
"@babel/plugin-transform-runtime"]

index.html

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
3-
<head>
4-
<meta charset="utf-8">
5-
<title>Woql Client</title>
6-
</head>
7-
<body>
8-
<script type="text/javascript">
9-
var dbClient= new TerminusClient.WOQLClient();
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Woql Client</title>
6+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
7+
</head>
8+
<body onload="startTerminusdb();">
9+
<div id="root" class="container">
10+
<div class="alert alert-primary" role="alert">WE ARE TESTING TerminusDB Client ......</div>
11+
<div class="alert alert-warning" role="alert" id="success-alert"></div>
12+
<div class="alert alert-warning" role="alert" id="success-alert01"></div>
13+
<div class="alert alert-success" role="alert" id="success-alert02"></div>
14+
</div>
15+
<script>
16+
async function startTerminusdb(){
17+
var dbClient= new TerminusDBClient.WOQLClient("http://127.0.0.1:6363",{user:"admin","key":"root",organization:"admin"});
18+
const db__01 = `myDB___${Date.now()}`
19+
try{
20+
await dbClient.createDatabase(db__01, {label: db__01 , comment: "add db", schema: true})
21+
const h5 = document.getElementById("success-alert");
22+
const newContent = document.createTextNode(`THE DATABASE ${db__01} HAS BEEN CREATED `);
23+
h5.appendChild(newContent)
24+
25+
await dbClient.deleteDatabase(db__01)
26+
27+
const h6 = document.getElementById("success-alert01");
28+
const newContent01 = document.createTextNode(`THE DATABASE ${db__01} HAS BEEN DELETED`);
29+
h6.appendChild(newContent01)
1030

11-
var connection=dbClient.connect("http://localhost");
12-
13-
connection.then((response)=>{
14-
console.log("I'm connect")
15-
dbClient.getSchema(getSchema,{"terminus:encoding":"terminus:turtle"}).then((response)=>
16-
console.log("getSchema RESPONSE OK")).catch((err)=>{
17-
console.log("PROMISE reject", 'GETSCHEMA');
18-
});
19-
20-
}).catch((err)=>{
21-
console.log("PROMISE reject", 'CONNECTION');
22-
});
23-
24-
var getSchema='terminus';
25-
26-
</script>
27-
28-
</body>
31+
const h7 = document.getElementById("success-alert02");
32+
const newContent02 = document.createTextNode(`CREATE/DELETE TerminusDB DATABASE TEST PASSED......`);
33+
h7.appendChild(newContent02)
34+
}catch(err){
35+
console.log(err)
36+
}
37+
}
38+
39+
</script>
40+
</body>
2941
</html>

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
/* eslint-disable global-require */
2+
// const { Vars } = require('./lib/query/woqlDoc');
3+
24
module.exports = {
35
/**
4-
* @type {typeof import('./lib/woqlClient')}
5-
*/
6+
* @type {typeof import('./lib/query/woqlDoc')}
7+
*/
8+
Vars: require('./lib/query/woqlDoc'),
9+
/**
10+
* @type {typeof import('./lib/woqlClient')}
11+
*/
612
WOQLClient: require('./lib/woqlClient'),
713
/**
814
* @type {typeof import('./lib/utils')}

lib/accessControl.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable max-len */
12
/* eslint-disable no-underscore-dangle */
23
const DispatchRequest = require('./dispatchRequest');
34
const ErrorMessage = require('./errorMessage');
@@ -289,20 +290,33 @@ AccessControl.prototype.deleteUser = function (userId) {
289290
/**
290291
* -- TerminusdDB API ---
291292
* Grant/Revoke Capability
292-
* @param {string} userId - the document user id
293-
* @param {string} resourceId - the resource id (database or team)
294-
* @param {array} rolesArr - the roles list
293+
* @param {string} userName - the document user id
294+
* @param {string} resourceName - the name of a (database or team)
295+
* @param {array} rolesArr - the roles name list
295296
* @param {typedef.CapabilityCommand} operation - grant/revoke operation
297+
* @param {typedef.ResourceType} resourceType - the resource type (database or organization)
296298
* @return {Promise} A promise that returns the call response object, or an Error if rejected.
297299
* @example
298-
{ "operation" : "grant",
299-
"scope" : "Organization/myteam",
300-
"user" : "User/myUser",
301-
"roles" : ["Role/reader"] }
300+
* //we add an user to an organization and manage users' access
301+
* //the user myUser can access the Organization and all the database under the organization with "reader" Role
302+
* client.manageCapability(myUser,myteam,[reader],"grant","organization").then(result=>{
303+
* consol.log(result)
304+
* })
305+
*
306+
* //the user myUser can access the database db__001 under the organization myteam
307+
* //with "writer" Role
308+
* client.manageCapability(myUser,myteam/db__001,[writer],"grant","database").then(result=>{
309+
* consol.log(result)
310+
* })
302311
*/
303-
AccessControl.prototype.manageCapability = function (userId, resourceId, rolesArr, operation) {
312+
313+
AccessControl.prototype.manageCapability = function (userName, resourceName, rolesArr, operation, resourceType) {
304314
const payload = {
305-
operation, user: userId, roles: rolesArr, scope: resourceId,
315+
operation,
316+
user: userName,
317+
roles: rolesArr,
318+
scope: resourceName,
319+
scope_type: resourceType,
306320
};
307321
return this.dispatch(`${this.baseURL}/capabilities`, CONST.POST, payload);
308322
};

lib/connectionConfig.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,14 @@ ConnectionConfig.prototype.queryURL = function () {
466466
return this.branchBase('woql');
467467
};
468468

469+
/**
470+
* Generate URL for get back the commits logs
471+
* @returns {string}
472+
*/
473+
ConnectionConfig.prototype.log = function () {
474+
return this.dbBase('log');
475+
};
476+
469477
/**
470478
* get the url to update the organization role in the system database
471479
* don't change the end point (this is a terminus db server end point)

lib/dispatchRequest.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ function getResultWithDataVersion(response) {
2929
: '',
3030
};
3131
}
32+
/**
33+
* Create the authorization header string
34+
* @param {object} auth_obj
35+
* @returns {object} Object with the Authorization header
36+
*/
37+
38+
function formatAuthHeader(auth_obj) {
39+
if (!auth_obj) return '';
40+
const authType = { jwt: 'Bearer', basic: 'Basic', apikey: 'Token' };
41+
let auth_key = auth_obj.key;
42+
43+
if (auth_obj.type === 'basic') {
44+
auth_key = btoaImplementation(`${auth_obj.user}:${auth_obj.key}`);
45+
}
46+
return `${authType[auth_obj.type]} ${auth_key}`;
47+
}
3248

3349
/**
3450
* @file Dispatch Request
@@ -41,6 +57,7 @@ function getResultWithDataVersion(response) {
4157
* @param {object} customHeaders the unique reqID
4258
* @param {boolean} compress If true, compress the data with gzip if its size is bigger than 1024
4359
*/
60+
4461
// eslint-disable-next-line max-len
4562
function DispatchRequest(url, action, payload, local_auth, remote_auth = null, customHeaders = null, getDataVersion = false, compress = false) {
4663
/*
@@ -72,24 +89,30 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
7289
* I can call the local database using the local authorization key or
7390
* a jwt token
7491
*/
75-
if (local_auth && local_auth.type === 'basic') {
92+
/* if (local_auth && local_auth.type === 'basic') {
7693
const encoded = btoaImplementation(`${local_auth.user}:${local_auth.key}`);
7794
options.headers = { Authorization: `Basic ${encoded}` };
7895
} else if (local_auth && local_auth.type === 'jwt') {
7996
options.headers = { Authorization: `Bearer ${local_auth.key}` };
8097
} else if (local_auth && local_auth.type === 'apikey') {
8198
options.headers = { Authorization: `Token ${local_auth.key}` };
99+
} */
100+
/*
101+
* I can call the local database or a custom installation using the local authorization key or
102+
* I Can call TerminusX using the jwt token or an apiToken
103+
*/
104+
if (local_auth && typeof local_auth === 'object') {
105+
options.headers.Authorization = formatAuthHeader(local_auth);
82106
}
83107

84108
/*
85-
* pass the Authorization information of another
86-
* terminusdb server to the local terminusdb
87-
*/
88-
if (remote_auth && remote_auth.type === 'jwt') {
89-
options.headers['Authorization-Remote'] = `Bearer ${remote_auth.key}`;
90-
} else if (remote_auth && remote_auth.type === 'basic') {
91-
const rencoded = btoaImplementation(`${remote_auth.user}:${remote_auth.key}`);
92-
options.headers['Authorization-Remote'] = `Basic ${rencoded}`;
109+
* pass the Authorization information of another
110+
* terminusdb server to the local terminusdb
111+
* for authentication you can use jwt or the apiKey token in TerminusX or
112+
* the Basic autentication if is allowed in the custom server
113+
*/
114+
if (remote_auth && typeof remote_auth === 'object') {
115+
options.headers['Authorization-Remote'] = formatAuthHeader(remote_auth);
93116
}
94117

95118
if (customHeaders && typeof customHeaders === 'object') {

lib/query/woqlDoc.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ function Doc(obj) {
7676
}
7777

7878
function Vars() {
79-
for(let k of arguments){
80-
this[k] = new Var(k)
79+
for (let arg = 0; arg < arguments.length; arg += 1) {
80+
// eslint-disable-next-line prefer-rest-params
81+
this[arguments[arg]] = new Var(arguments[arg]);
8182
}
8283
}
8384

lib/typedef.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
* @description Type definations
44
* */
55
/* eslint-disable no-unused-vars */
6-
const { ACTIONS } = require('./utils');
6+
const Utils = require('./utils');
7+
8+
const { ACTIONS } = Utils.ACTIONS;
79

810
/**
911
*@typedef {Object} DocParamsGet - the GET document interface query parameters
@@ -79,7 +81,7 @@ const { ACTIONS } = require('./utils');
7981

8082
/**
8183
* @typedef {Object} CredentialObj
82-
* @property {'basic'|'jwt'} type - the authorization type of an TerminusDB connection
84+
* @property {'basic'|'jwt'|'apikey'} type - the authorization type of an TerminusDB connection
8385
* @property {string | boolean} [user] - the user id | I don't need the user with the jwt token
8486
* @property {string} key - the connection key
8587
*/
@@ -113,19 +115,19 @@ const { ACTIONS } = require('./utils');
113115
* @property {string} [invitation] -
114116
*/
115117

118+
/**
119+
* @typedef {"database"|"organization"} ResourceType
120+
*/
121+
116122
/**
117123
* @typedef {"local"|"remote"} RepoType
118124
*/
119125
// sharing is a boolean
120126
/**
121127
* @typedef {Object} DbDetails
122-
* @property {string} [organization] - the db organization id
123-
* @property {string} id - The database identification name
124128
* @property {string} label - "Textual DB Name"
125129
* @property {string} [comment] - "Text description of DB"
126130
* @property {boolean} [public]
127-
* @property {string} [icon] - The database's icon
128-
* @property {object} [prefixes] - {scm: "http://url.to.use/for/scm", doc: "http://url.to.use/for/doc"}
129131
* @property {boolean} [schema] - if set to true, a schema graph will be created
130132
*/
131133

lib/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,10 +804,10 @@ Utils.DateHelper.parseXsdDateTime = function (val) {
804804
* Extracts the timezone data from an xsd date string
805805
*/
806806
Utils.DateHelper.extractXsdTimezone = function (val) {
807-
if (val && val.charAt(val.length - 1) === 'Z') {
807+
if (typeof val === 'string' && val.endsWith('Z')) {
808808
return 'Z';
809809
}
810-
if (val && (val.charAt(val.length - 6) === '+' || val.charAt(val.length - 6) === '-')) {
810+
if (typeof val === 'string' && (val.charAt(val.length - 6) === '+' || val.charAt(val.length - 6) === '-')) {
811811
val.substring(val.length - 6);
812812
}
813813
return false;

lib/woql.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -378,18 +378,6 @@ WOQL.remote = function (remoteObj, formatObj) {
378378
return new WOQLQuery().remote(remoteObj, formatObj);
379379
};
380380

381-
/**
382-
* Identifies a file resource as a path on the server and specifies the format through the options
383-
* @param {object} url - The Path on the server at which the file resource can be accessed
384-
* @param {typedef.DataFormatObj} [formatObj] - imput options
385-
* @returns {WOQLQuery} A WOQLQuery which contains the file resource identifier
386-
* @example
387-
* file("/path/to/file", {type: 'turtle'} )
388-
*/
389-
WOQL.file = function (url, formatObj) {
390-
return new WOQLQuery().file(url, formatObj);
391-
};
392-
393381
/**
394382
* Identifies a resource as a local path on the client, to be sent to the server through a
395383
* HTTP POST request, with the format defined through the options
@@ -1327,6 +1315,10 @@ WOQL.client = function (client) {
13271315
return this._client;
13281316
};
13291317

1318+
WOQL.Vars = function (...varNames) {
1319+
return new Vars(...varNames);
1320+
};
1321+
13301322
/**
13311323
*
13321324
* query module
@@ -1341,7 +1333,7 @@ WOQL.emerge = function (auto_eval) {
13411333
}`;
13421334
return str;
13431335
}
1344-
const funcs = [];
1336+
const funcs = [_emerge_str('Vars')];
13451337
// eslint-disable-next-line no-restricted-syntax
13461338
for (const k in this) {
13471339
if (typeof this[k] === 'function') {

0 commit comments

Comments
 (0)