Skip to content

Commit 7712afb

Browse files
committed
fix issues #267,#260,#86 and add patchResource method
1 parent 7e7cc07 commit 7712afb

File tree

3 files changed

+86
-9
lines changed

3 files changed

+86
-9
lines changed

lib/query/woqlQuery.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,7 @@ WOQLQuery.prototype.order_by = function (...orderedVarlist) {
14951495

14961496
for (let i = 0; i < orderedVarlist.length; i++) {
14971497
let obj;
1498-
if (typeof orderedVarlist[i] === 'string' && orderedVarlist[i] !== '') {
1498+
if ((typeof orderedVarlist[i] === 'string' || orderedVarlist[i] instanceof Var) && orderedVarlist[i] !== '') {
14991499
obj = {
15001500
'@type': 'OrderTemplate',
15011501
variable: this.rawVar(orderedVarlist[i]),

lib/woql.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,6 @@ WOQL.get = function (asvars, queryResource) {
353353
* @param {WOQLQuery} query - The query which will be executed to produce the results
354354
* @param {string} fileResource - an file resource local to the server
355355
* @returns {WOQLQuery} A WOQLQuery which contains the put expression
356-
* @example
357-
* let [s, p, o] = vars("Subject", "Predicate", "Object")
358-
* WOQL.put(WOQL.as("s", s).as("p", p).as("o", o), WOQL.all())
359-
* .file({file:"/app/local_files/dump.csv"})
360356
*/
361357
WOQL.put = function (varsToExp, query, fileResource) {
362358
return new WOQLQuery().put(varsToExp, query, fileResource);

lib/woqlClient.js

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,8 +1007,28 @@ WOQLClient.prototype.prepareRevisionControlArgs = function (rc_args) {
10071007
* "name" : "xsd:string",
10081008
* "perimeter" : { "@type" : "List",
10091009
* "@class" : "Coordinate" } }]
1010-
* client.addDocument(json,{"graph_type":"schema"},"mydb","add new schema")
1010+
* client.addDocument(json,{"graph_type":"schema"},"mydb","add new schema documents")
10111011
*
1012+
* //if we would like to override the intire schema
1013+
* const json = [
1014+
* {"@base": "terminusdb:///data/",
1015+
* "@schema": "terminusdb:///schema#",
1016+
* "@type": "@context"
1017+
* },
1018+
* {
1019+
* "@id": "Person",
1020+
* "@key": {
1021+
* "@type": "Random"
1022+
* },
1023+
* "@type": "Class",
1024+
* "name": {
1025+
* "@class": "xsd:string",
1026+
* "@type": "Optional"
1027+
* }
1028+
* }]
1029+
*
1030+
* // client.addDocument(json,{"graph_type":"schema","full_replace:tue"},
1031+
"mydb","update the all schema");
10121032
*
10131033
* // Here we will pass true to show how to get dataVersion
10141034
*
@@ -1559,7 +1579,7 @@ WOQLClient.prototype.userOrganizations = function (orgList) {
15591579
/**
15601580
* Apply a patch object to another object
15611581
* @param {object} before - The current state of JSON document
1562-
* @param {object} after - The patch object
1582+
* @param {object} patch - The patch object
15631583
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
15641584
* @example
15651585
* client.patch(
@@ -1571,15 +1591,76 @@ WOQLClient.prototype.userOrganizations = function (orgList) {
15711591
* //result example
15721592
* //{ "@id" : "Person/Jane", "@type" : "Person", "name" : "Jannet"}
15731593
*/
1574-
WOQLClient.prototype.patch = function (before, after) {
1594+
WOQLClient.prototype.patch = function (before, patch) {
15751595
if (typeof before !== 'object' || typeof after !== 'object') {
15761596
const errmsg = '"before" or "after" parameter error - you must specify a valid before and after json document';
15771597

15781598
return Promise.reject(
15791599
new Error(ErrorMessage.getInvalidParameterMessage(CONST.PATCH, errmsg)),
15801600
);
15811601
}
1582-
const payload = { before, after };
1602+
const payload = { before, patch };
1603+
1604+
return this.dispatch(
1605+
CONST.POST,
1606+
`${this.connectionConfig.apiURL()}patch`,
1607+
payload,
1608+
).then((response) => response);
1609+
};
1610+
1611+
/**
1612+
* Apply a patch object to the current resource
1613+
* @param {array} patch - The patch object
1614+
* @param {string} message - The commit message
1615+
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
1616+
* @example
1617+
* const patch = [
1618+
* {
1619+
* "@id": "Obj/id1",
1620+
* "name": {
1621+
* "@op": "SwapValue",
1622+
* "@before": "foo",
1623+
* "@after": "bar"
1624+
* }
1625+
* },
1626+
* {
1627+
* "@id": "Obj/id2",
1628+
* "name": {
1629+
* "@op": "SwapValue",
1630+
* "@before": "foo",
1631+
* "@after": "bar"
1632+
* }
1633+
* }
1634+
* ]
1635+
* client.db("mydb")
1636+
* client.checkout("mybranch")
1637+
* client.patchResource(patch,"apply patch to mybranch").then(patchResult=>{
1638+
* console.log(patchResult)
1639+
* })
1640+
* // result example
1641+
* // ["Obj/id1",
1642+
* // "Obj/id2"]
1643+
* // or conflict error 409
1644+
* // {
1645+
* // "@type": "api:PatchError",
1646+
* // "api:status": "api:conflict",
1647+
* // "api:witnesses": [
1648+
* // {
1649+
* // "@op": "InsertConflict",
1650+
* // "@id_already_exists": "Person/Jane"
1651+
* // }
1652+
* //]
1653+
* //}
1654+
*/
1655+
WOQLClient.prototype.patchResource = function (patch, message) {
1656+
if (!Array.isArray(patch)) {
1657+
const errmsg = '"patch" parameter error - you must specify a valid patch document';
1658+
1659+
return Promise.reject(
1660+
new Error(ErrorMessage.getInvalidParameterMessage(CONST.PATCH, errmsg)),
1661+
);
1662+
}
1663+
const payload = { patch, author: this.author(), message };
15831664

15841665
return this.dispatch(
15851666
CONST.POST,

0 commit comments

Comments
 (0)