-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwoqlLibrary.js
137 lines (123 loc) · 4.66 KB
/
woqlLibrary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/// /@ts-check
// we can not import woqlBuilder because woqlBuilder import WOQLLibrary
const WOQLQuery = require('./woqlBuilder');
/**
* @license Apache Version 2
* @module WOQLLibrary
* @constructor WOQLLibrary
* @description Library Functions to manage the commits graph
* @example
* const woqlLib = WOQLLibrary()
* woqlLib.branches()
*
* //or you can call this functions using WOQL Class
* WOQL.lib().branches()
* */
class WOQLLibrary {
default_schema_resource = 'schema/main';
default_commit_resource = '_commits';
default_meta_resource = '_meta';
masterdb_resource = '_system';
empty = '';
}
/**
* General Pattern 4: Retrieves Branches, Their ID, Head Commit ID, Head Commit Time
* (if present, new branches have no commits)
*/
WOQLLibrary.prototype.branches = function () { // values, variables, cresource) {
const woql = new WOQLQuery().using('_commits').triple('v:Branch', 'rdf:type', '@schema:Branch')
.triple('v:Branch', '@schema:name', 'v:Name')
.opt()
.triple('v:Branch', '@schema:head', 'v:Head')
.triple('v:Head', '@schema:identifier', 'v:commit_identifier')
.triple('v:Head', '@schema:timestamp', 'v:Timestamp');
return woql;
};
/**
* get all the commits of a specific branch
* if a timestamp is given, gets all the commits before the specified timestamp
* @param {string} [branch] - the branch name
* @param {number} [limit] - the max number of result
* @param {number} [start] - the start of the pagination
* @param {number} [timestamp] - Unix timestamp in seconds
*/
WOQLLibrary.prototype.commits = function (branch = 'main', limit = 0, start = 0, timestamp = 0) {
const woql = new WOQLQuery().using('_commits');
if (limit) woql.limit(limit);
if (start) woql.start(start);
woql.select('v:Parent ID', 'v:Commit ID', 'v:Time', 'v:Author', 'v:Branch ID', 'v:Message');
const andArr = [new WOQLQuery().triple('v:Branch', 'name', new WOQLQuery().string(branch))
.triple('v:Branch', 'head', 'v:Active Commit ID')
.path('v:Active Commit ID', 'parent*', 'v:Parent', 'v:Path')
.triple('v:Parent', 'timestamp', 'v:Time')];
if (timestamp) {
andArr.push(new WOQLQuery().less('v:Time', timestamp));
}
andArr.push(new WOQLQuery().triple('v:Parent', 'identifier', 'v:Commit ID')
.triple('v:Parent', 'author', 'v:Author')
.triple('v:Parent', 'message', 'v:Message')
.opt()
.triple('v:Parent', 'parent', 'v:Parent ID'));
return woql.and(...andArr);
};
/**
*get commits older than the specified commit id
* @param {string} [commit_id] - the commit id
* @param {number} [limit] - the max number of result
*/
// eslint-disable-next-line camelcase
WOQLLibrary.prototype.previousCommits = function (commit_id, limit = 10) {
return new WOQLQuery().using('_commits').limit(limit).select('v:Parent ID', 'v:Message', 'v:Commit ID', 'v:Time', 'v:Author')
.and(
new WOQLQuery().and(
new WOQLQuery().triple('v:Active Commit ID', '@schema:identifier', new WOQLQuery().string(commit_id)),
new WOQLQuery().path('v:Active Commit ID', '@schema:parent+', 'v:Parent', 'v:Path'),
new WOQLQuery().triple('v:Parent', '@schema:identifier', 'v:Commit ID'),
new WOQLQuery().triple('v:Parent', '@schema:timestamp', 'v:Time'),
new WOQLQuery().triple('v:Parent', '@schema:author', 'v:Author'),
new WOQLQuery().triple('v:Parent', '@schema:message', 'v:Message'),
new WOQLQuery().triple('v:Parent', '@schema:parent', 'v:Parent ID'),
new WOQLQuery().opt().triple('v:Parent', 'parent', 'v:Parent ID'),
),
);
};
/**
* Finds the id of the very first commit in a database's history
*
* This is useful for finding information about when, by who and why the database was created
* The first commit is the only commit in the database that does not have a parent commit
*
*/
WOQLLibrary.prototype.first_commit = function () {
const noparent = new WOQLQuery()
.using('_commits').select('v:Any Commit IRI')
.and(
new WOQLQuery().triple('v:Branch', 'name', new WOQLQuery().string('main'))
.triple('v:Branch', 'head', 'v:Active Commit ID')
.path('v:Active Commit ID', 'parent*', 'v:Any Commit IRI', 'v:Path'),
new WOQLQuery().triple(
'v:Any Commit IRI',
'@schema:identifier',
'v:Commit ID',
),
new WOQLQuery().triple(
'v:Any Commit IRI',
'@schema:author',
'v:Author',
),
new WOQLQuery().triple(
'v:Any Commit IRI',
'@schema:message',
'v:Message',
),
new WOQLQuery()
.not()
.triple(
'v:Any Commit IRI',
'@schema:parent',
'v:Parent IRI',
),
);
return noparent;
};
module.exports = WOQLLibrary;