Skip to content

Commit c96ec9d

Browse files
committed
review woql
1 parent b8808a0 commit c96ec9d

File tree

5 files changed

+91
-41
lines changed

5 files changed

+91
-41
lines changed

lib/query/woqlCore.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,12 @@ WOQLQuery.prototype.cleanSubject = function(s) {
239239
* @type {any}
240240
*/
241241
let subj = false
242-
if (typeof s == 'object') {
242+
if (typeof s === 'object') {
243243
return s
244-
} else if (typeof s == 'string') {
245-
if (s.indexOf(':') != -1) subj = s
246-
else if (this.vocab && this.vocab[s]) subj = this.vocab[s]
247-
else subj = 'doc:' + s
244+
} else if (typeof s === 'string') {
245+
if (s.indexOf('v:') !== -1) subj = s
246+
//else if (this.vocab && this.vocab[s]) subj = this.vocab[s]
247+
else subj = s //'doc:' + s
248248
return this.expandVariable(subj)
249249
}
250250
this.parameterError('Subject must be a URI string')
@@ -259,14 +259,15 @@ WOQLQuery.prototype.cleanPredicate = function(p) {
259259
* @type {any}
260260
*/
261261
let pred = false
262-
if (typeof p == 'object') return p
263-
if (typeof p != 'string') {
262+
if (typeof p === 'object') return p
263+
if (typeof p !== 'string') {
264264
this.parameterError('Predicate must be a URI string')
265265
return '' + p
266266
}
267-
if (p.indexOf(':') != -1) pred = p
268-
else if (this.wellKnownPredicate(p)) pred = this.vocab[p]
269-
else pred = 'scm:' + p
267+
if (p.indexOf(':') !== -1) pred = p
268+
else if (this.wellKnownPredicate(p)) pred = p//this.vocab[p]
269+
else pred = p//'scm'
270+
//else pred = 'scm:' + p
270271
return this.expandVariable(pred)
271272
}
272273

@@ -282,9 +283,9 @@ WOQLQuery.prototype.wellKnownPredicate = function(p, noxsd) {
282283

283284
WOQLQuery.prototype.cleanPathPredicate = function(p) {
284285
let pred = false
285-
if (p.indexOf(':') != -1) pred = p
286+
if (p.indexOf(':') !== -1) pred = p
286287
else if (this.wellKnownPredicate(p)) pred = this.vocab[p]
287-
else pred = 'scm:' + p
288+
else pred = p //'scm:' + p
288289
return pred
289290
}
290291

@@ -294,7 +295,7 @@ WOQLQuery.prototype.cleanPathPredicate = function(p) {
294295
*/
295296
WOQLQuery.prototype.cleanObject = function(o, t) {
296297
let obj = {'@type': 'woql:Datatype'}
297-
if (typeof o == 'string') {
298+
if (typeof o === 'string') {
298299
if (this.looksLikeClass(o)) {
299300
return this.cleanClass(o)
300301
} else if (this.vocab && this.vocab[o]) {
@@ -312,8 +313,12 @@ WOQLQuery.prototype.cleanObject = function(o, t) {
312313
return obj
313314
}
314315

316+
/*
317+
* check if can be a class in the object
318+
*/
315319
WOQLQuery.prototype.looksLikeClass = function(o) {
316-
if (o.indexOf(':') === -1) return false
320+
//if it is like User or Person it is a class
321+
if (o.indexOf('v:') === -1) return true
317322

318323
let pref = o.split(':')[0]
319324
if (
@@ -324,8 +329,8 @@ WOQLQuery.prototype.looksLikeClass = function(o) {
324329
pref == 'terminusdb' ||
325330
pref == 'http' ||
326331
pref == 'https'
327-
)
328-
return true
332+
)return true
333+
329334
if (UTILS.standard_urls[pref]) return true
330335
return false
331336
}
@@ -361,10 +366,10 @@ WOQLQuery.prototype.expandVariable = function(varname, always) {
361366
}
362367

363368
WOQLQuery.prototype.cleanClass = function(c, stringonly) {
364-
if (typeof c != 'string') return ''
365-
if (c.indexOf(':') == -1) {
369+
if (typeof c !== 'string') return ''
370+
if (c.indexOf(':') === -1) {
366371
if (this.vocab && this.vocab[c]) c = this.vocab[c]
367-
else c = 'scm:' + c
372+
else c = c //'scm:' + c
368373
}
369374
return stringonly ? c : this.expandVariable(c)
370375
}
@@ -410,8 +415,8 @@ WOQLQuery.prototype.context = function(c) {
410415
*/
411416
WOQLQuery.prototype.loadDefaultVocabulary = function() {
412417
var vocab = {}
413-
vocab.type = 'rdf:type'
414-
vocab.label = 'rdfs:label'
418+
// vocab.type = 'rdf:type'
419+
// vocab.label = 'rdfs:label'
415420
vocab.Class = 'owl:Class'
416421
vocab.DatatypeProperty = 'owl:DatatypeProperty'
417422
vocab.ObjectProperty = 'owl:ObjectProperty'

lib/query/woqlLibrary.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ WOQLLibrary.prototype.properties = function(values, variables, schema_resource)
300300
* General Pattern 3: Graphs
301301
* Retrieves information about the graphs in existence at any commit (and whether the commit is the head of a branch)
302302
*/
303+
//we don't need this any more
303304
WOQLLibrary.prototype.graphs = function(values, variables, cresource) {
304305
cresource = cresource || this.default_commit_resource
305306
this.default_variables = [
@@ -343,7 +344,7 @@ WOQLLibrary.prototype.graphs = function(values, variables, cresource) {
343344
* General Pattern 4: Retrieves Branches, Their ID, Head Commit ID, Head Commit Time
344345
* (if present, new branches have no commits)
345346
*/
346-
WOQLLibrary.prototype.branches = function(values, variables, cresource) {
347+
/*WOQLLibrary.prototype.branches = function(values, variables, cresource) {
347348
cresource = cresource || this.default_commit_resource
348349
this.default_variables = ['Branch ID', 'Time', 'Commit ID', 'Branch IRI', 'Commit IRI']
349350
if (variables) this._set_user_variables(variables)
@@ -356,7 +357,7 @@ WOQLLibrary.prototype.branches = function(values, variables, cresource) {
356357
357358
let compiled = this._add_constraints(woql, values)
358359
return new WOQLQuery().using(cresource, compiled)
359-
}
360+
}*/
360361

361362
/**
362363
* General Pattern 5: Objects - just a list of object ids and their types
@@ -535,13 +536,13 @@ WOQLLibrary.prototype.commits = function(values, variables, cresource) {
535536
if (variables) this._set_user_variables(variables)
536537
let qpattern = new WOQLQuery()
537538
.order_by(this._varn('Time'), 'desc')
538-
.triple(this._varn('Commit IRI'), 'ref:commit_id', this._varn('Commit ID'))
539-
.triple(this._varn('Commit IRI'), 'ref:commit_timestamp', this._varn('Time'))
539+
.triple(this._varn('Commit IRI'), 'id', this._varn('Commit ID'))
540+
.triple(this._varn('Commit IRI'), 'timestamp', this._varn('Time'))
540541
.limit(1)
541542
.or(
542543
new WOQLQuery().triple(
543544
this._varn('Commit IRI'),
544-
'ref:commit_author',
545+
'author',
545546
this._varn('Author'),
546547
),
547548
new WOQLQuery().eq(this._varn('Author'), this.empty),
@@ -550,16 +551,16 @@ WOQLLibrary.prototype.commits = function(values, variables, cresource) {
550551
.or(
551552
new WOQLQuery().triple(
552553
this._varn('Commit IRI'),
553-
'ref:commit_message',
554+
'message',
554555
this._varn('Message'),
555556
),
556557
new WOQLQuery().eq(this._varn('Message'), this.empty),
557558
)
558559
.limit(1)
559560
.or(
560561
new WOQLQuery()
561-
.triple(this._varn('Commit IRI'), 'ref:commit_parent', this._varn('Parent IRI'))
562-
.triple(this._varn('Parent IRI'), 'ref:commit_id', this._varn('Parent ID')),
562+
.triple(this._varn('Commit IRI'), 'parent', this._varn('Parent IRI'))
563+
.triple(this._varn('Parent IRI'), 'id', this._varn('Parent ID')),
563564
new WOQLQuery()
564565
.eq(this._varn('Parent IRI'), this.empty)
565566
.eq(this._varn('Parent ID'), this.empty),
@@ -573,15 +574,15 @@ WOQLLibrary.prototype.commits = function(values, variables, cresource) {
573574
this._varn('Child ID'),
574575
this._varn('Children'),
575576
)
576-
.triple(this._varn('Child IRI'), 'ref:commit_parent', this._varn('Commit IRI'))
577-
.triple(this._varn('Child IRI'), 'ref:commit_id', this._varn('Child ID')),
577+
.triple(this._varn('Child IRI'), 'parent', this._varn('Commit IRI'))
578+
.triple(this._varn('Child IRI'), 'id', this._varn('Child ID')),
578579
new WOQLQuery().eq(this._varn('Children'), this.empty),
579580
)
580581
.limit(1)
581582
.or(
582583
new WOQLQuery()
583-
.triple(this._varn('Branch IRI'), 'ref:ref_commit', this._varn('Commit IRI'))
584-
.triple(this._varn('Branch IRI'), 'ref:branch_name', this._varn('Branch ID')),
584+
.triple(this._varn('Branch IRI'), 'head', this._varn('Commit IRI'))
585+
.triple(this._varn('Branch IRI'), 'name', this._varn('Branch ID')),
585586
new WOQLQuery()
586587
.eq(this._varn('Branch IRI'), this.empty)
587588
.eq(this._varn('Branch ID'), this.empty),
@@ -834,14 +835,14 @@ WOQLLibrary.prototype.first_commit = function() {
834835
.and(
835836
new WOQLQuery().triple(
836837
this._varn('Any Commit IRI'),
837-
'ref:commit_id',
838+
'id',
838839
this._varn('Commit ID'),
839840
),
840841
new WOQLQuery()
841842
.not()
842843
.triple(
843844
this._varn('Any Commit IRI'),
844-
'ref:commit_parent',
845+
'parent',
845846
this._varn('Parent IRI'),
846847
),
847848
)

lib/query/woqlSchema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ WOQLQuery.prototype.libs = function(libs, parent, graph, prefix) {
853853
*/
854854
WOQLQuery.prototype.boxClasses = function(prefix, classes, except, graph) {
855855
graph = this._sg(graph)
856-
prefix = prefix || 'scm:'
856+
prefix = prefix || '' //'scm:'
857857
var subs = []
858858
for (var i = 0; i < classes.length; i++) {
859859
subs.push(new WOQLQuery().sub(classes[i], 'v:Cid'))

lib/utils.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Utils.genBNID = function(base) {
9292
.toString(36)
9393
.substring(7)
9494
const d = new Date()
95-
const bnid = `doc:${base}${r}${d.getTime()}`
95+
const bnid = `${base}${r}${d.getTime()}`//`doc:${base}${r}${d.getTime()}`
9696
return bnid
9797
}
9898

@@ -136,16 +136,29 @@ Utils.compareIDs = function(ida, idb) {
136136
* @param {string} [prefixes]
137137
* @returns {string | undefined}
138138
*/
139+
//we don't have any more visualize prefix
140+
//terminusdb://layer/data/Layer_44ce76721ebe179fdf2c015b0d38eadfa49de3ba
139141
Utils.shorten = function(url, prefixes) {
140142
if (!url) return undefined
143+
if(url.indexOf("#") !== -1){
144+
const urlArr = url.split("#")
145+
return urlArr.pop()
146+
}
147+
if(url.indexOf("terminusdb://") !== -1){
148+
const urlArr = url.split("/")
149+
return urlArr.pop()
150+
}
151+
return url
152+
153+
/*if (!url) return undefined
141154
if (typeof url === 'function') return undefined
142155
prefixes = prefixes || Utils.standard_urls
143156
for (const pref in prefixes) {
144157
if (prefixes[pref] == url.substring(0, prefixes[pref].length)) {
145158
return `${pref}:${url.substring(prefixes[pref].length)}`
146159
}
147160
}
148-
return url
161+
return url*/
149162
}
150163

151164
/**

lib/woqlClient.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ WOQLClient.CONST = CONST
5959
*/
6060
WOQLClient.prototype.copy = function() {
6161
let other = new WOQLClient(this.server())
62-
other.connection = this.connection //keep same connection meta data - shared by copy
63-
//other.connectionConfig = this.connectionConfig.copy() //new copy of current connection data
62+
//other.connection = this.connection //keep same connection meta data - shared by copy
63+
other.connectionConfig = this.connectionConfig.copy() //new copy of current connection data
6464
other.databaseList = this.databaseList
6565
return other
6666
}
@@ -1243,12 +1243,28 @@ WOQLClient.prototype.updateDocument = function(json,params,dbId, message="update
12431243
const docParams = params || {}
12441244
docParams['overwrite']=true
12451245
docParams['author']=this.author()
1246-
paramsObj['message'] = message
1246+
docParams['message'] = message
12471247
if (dbId) {
12481248
this.db(dbId)
12491249
}
12501250
return this.dispatch(CONST.POST, this.connectionConfig.documentURL(docParams),json)
12511251
}
1252+
1253+
1254+
WOQLClient.prototype.deleteDocument = function(params,message="delete document"){
1255+
const docParams = params || {}
1256+
let payload = null
1257+
if(typeof params.id === 'object'){
1258+
payload=params.id
1259+
delete docParams.id
1260+
}
1261+
docParams['author']=this.author()
1262+
docParams['message'] = message
1263+
if (dbId) {
1264+
this.db(dbId)
1265+
}
1266+
return this.dispatch(CONST.DELETE, this.connectionConfig.documentURL(docParams),payload)
1267+
}
12521268
/**
12531269
*
12541270
* @param {string} [type] - for get the info only for a specific document type
@@ -1265,4 +1281,19 @@ WOQLClient.prototype.schema = function(type,dbId){
12651281
return this.dispatch(CONST.GET, this.connectionConfig.jsonSchemaURL(params))
12661282
}
12671283

1284+
WOQLClient.prototype.branches = async function(dbId){
1285+
const params={type:'Branch','as_list':true}
1286+
const branch = this.checkout()
1287+
const result = await this.getDocument(params,dbId,"_commits")
1288+
//reset branch
1289+
const branchesObj = {}
1290+
if(result){
1291+
result.forEach(item=>{
1292+
branchesObj[item.name]=item
1293+
})
1294+
}
1295+
this.checkout(branch)
1296+
return branchesObj
1297+
}
1298+
12681299
module.exports = WOQLClient

0 commit comments

Comments
 (0)