Skip to content

Commit cd46167

Browse files
authored
Merge pull request #178 from terminusdb/getVersionDiff
Get version diff
2 parents 10d51fe + 56617b8 commit cd46167

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

docs/_sidebar.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
- [getUserOrganizations](api/woqlClient.js?id=getUserOrganizations)
6363
- [userOrganizations](api/woqlClient.js?id=userOrganizations)
6464
- [getDiff](api/woqlClient.js?id=getDiff)
65+
- [getVersionObjectDiff](api/woqlClient.js?id=getVersionObjectDiff)
66+
- [getVersionDiff](api/woqlClient.js?id=getVersionDiff)
6567
- [patch](api/woqlClient.js?id=patch)
6668
- [WOQL](api/woql.js?id=WOQL)
6769
- [eval](api/woql.js?id=eval)

docs/api/woqlclient.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,48 @@ const diff = await client.getDiff(
11371137
);
11381138
```
11391139
1140+
## getVersionObjectDiff
1141+
##### woqlClient.getVersionObjectDiff(id, beforeVersion, after) ⇒ <code>Promise</code>
1142+
Get the patch of difference between two documents.
1143+
1144+
**Returns**: <code>Promise</code> - A promise that returns the call response object, or an Error if rejected.
1145+
1146+
| Param | Type | Description |
1147+
| --- | --- | --- |
1148+
| id | <code>string</code> | The object id to be diffed |
1149+
| beforeVersion | <code>string</code> | The version from which to compare the object |
1150+
| after | <code>object</code> | The updated state of JSON document |
1151+
1152+
**Example**
1153+
```javascript
1154+
const diff = await client.getVersionObjectDiff(
1155+
"Person/Jane",
1156+
"branch:a73ssscfx0kke7z76083cgswszdxy6l",
1157+
{ "@id": "Person/Jane", "@type": "Person", name: "Janine" }
1158+
);
1159+
```
1160+
1161+
## getVersionDiff
1162+
##### woqlClient.getVersionDiff(id, beforeVersion, afterVersion) ⇒ <code>Promise</code>
1163+
Get the patch of difference between two documents.
1164+
1165+
**Returns**: <code>Promise</code> - A promise that returns the call response object, or an Error if rejected.
1166+
1167+
| Param | Type | Description |
1168+
| --- | --- | --- |
1169+
| id | <code>string</code> | The object id to be diffed |
1170+
| beforeVersion | <code>string</code> | The version from which to compare the object |
1171+
| afterVersion | <code>object</code> | The version to which to compare the object |
1172+
1173+
**Example**
1174+
```javascript
1175+
const diff = await client.getVersionDiff(
1176+
"Person/Jane",
1177+
"branch:a73ssscfx0kke7z76083cgswszdxy6l",
1178+
"branch:73rqpooz65kbsheuno5dsayh71x7wf4"
1179+
);
1180+
```
1181+
11401182
## patch
11411183
##### woqlClient.patch(before, patch) ⇒ <code>Promise</code>
11421184
Patch the difference between two documents.

lib/connectionConfig.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,15 @@ ConnectionConfig.prototype.pullURL = function () {
503503
return purl;
504504
};
505505

506+
/**
507+
* Generate URL for diff endpoint
508+
* @returns {string}
509+
*/
510+
ConnectionConfig.prototype.diffURL = function () {
511+
const purl = this.branchBase('diff');
512+
return purl;
513+
};
514+
506515
/**
507516
* Generate URL for fetch endpoint
508517
* @param {string} remoteName

lib/woqlClient.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,70 @@ WOQLClient.prototype.getDiff = function (before, after) {
14751475
).then((response) => response);
14761476
};
14771477

1478+
/**
1479+
* Get the patch of difference between two documents.
1480+
* @param {string} id - The object id to be diffed
1481+
* @param {string} beforeVersion - The version from which to compare the object
1482+
* @param {object} after - The updated state of JSON document
1483+
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
1484+
* @example
1485+
* const diff = await client.getVersionObjectDiff(
1486+
* "Person/Jane",
1487+
* "branch:a73ssscfx0kke7z76083cgswszdxy6l",
1488+
* { "@id": "Person/Jane", "@type": "Person", name: "Janine" }
1489+
* );
1490+
*/
1491+
WOQLClient.prototype.getVersionObjectDiff = function (id, beforeVersion, after) {
1492+
if (typeof id !== 'string' || typeof beforeVersion !== 'string' || typeof after !== 'object') {
1493+
const errmsg = '"id", "beforeVersion" or "after" parameter error - you must specify a valid after json document and valid id and version';
1494+
1495+
return Promise.reject(
1496+
new Error(ErrorMessage.getInvalidParameterMessage(CONST.GET_DIFF, errmsg)),
1497+
);
1498+
}
1499+
1500+
const payload = { document_id: id, before_data_version: beforeVersion, after };
1501+
return this.dispatch(
1502+
CONST.POST,
1503+
this.connectionConfig.diffURL(),
1504+
payload,
1505+
).then((response) => response);
1506+
};
1507+
1508+
/**
1509+
* Get the patch of difference between two documents.
1510+
* @param {string} id - The object id to be diffed
1511+
* @param {string} beforeVersion - The version from which to compare the object
1512+
* @param {object} afterVersion - The version to which to compare the object
1513+
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
1514+
* @example
1515+
* const diff = await client.getVersionDiff(
1516+
* "Person/Jane",
1517+
* "branch:a73ssscfx0kke7z76083cgswszdxy6l",
1518+
* "branch:73rqpooz65kbsheuno5dsayh71x7wf4"
1519+
* );
1520+
*/
1521+
WOQLClient.prototype.getVersionDiff = function (id, beforeVersion, afterVersion) {
1522+
if (typeof id !== 'string' || typeof beforeVersion !== 'string' || typeof afterVersion !== 'string') {
1523+
const errmsg = '"id", "beforeVersion" or "after" parameter error - you must specify a valid after json document and valid id and version';
1524+
1525+
return Promise.reject(
1526+
new Error(ErrorMessage.getInvalidParameterMessage(CONST.GET_DIFF, errmsg)),
1527+
);
1528+
}
1529+
1530+
const payload = {
1531+
document_id: id,
1532+
before_data_version: beforeVersion,
1533+
after_data_version: afterVersion,
1534+
};
1535+
return this.dispatch(
1536+
CONST.POST,
1537+
this.connectionConfig.diffURL(),
1538+
payload,
1539+
).then((response) => response);
1540+
};
1541+
14781542
/**
14791543
* Patch the difference between two documents.
14801544
* @param {object} before - The current state of JSON document

0 commit comments

Comments
 (0)