Skip to content

Commit c9d104a

Browse files
review diff methods, add apply method (#215)
* review diff method and add apply Co-authored-by: Francesca-Bit <francesca@terminusdb.com> Co-authored-by: Francesca-Bit <Francesca-Bit@users.noreply.github.com>
1 parent 8b224db commit c9d104a

File tree

4 files changed

+186
-58
lines changed

4 files changed

+186
-58
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ package-lock.json
88
.nyc_output
99
test/
1010
tutorial/
11+
local_test/

docs/api/woqlclient.md

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,8 +1122,8 @@ async funtion callGetUserOrganizations(){
11221122
}
11231123
```
11241124
1125-
## getDiff
1126-
##### woqlClient.getDiff(before, after, options) ⇒ <code>Promise</code>
1125+
## getJSONDiff
1126+
##### woqlClient.getJSONDiff(before, after, [options]) ⇒ <code>Promise</code>
11271127
Get the patch of difference between two documents.
11281128
11291129
**Returns**: <code>Promise</code> - A promise that returns the call response object, or an Error if rejected.
@@ -1132,58 +1132,103 @@ Get the patch of difference between two documents.
11321132
| --- | --- | --- |
11331133
| before | <code>object</code> | The current state of JSON document |
11341134
| after | <code>object</code> | The updated state of JSON document |
1135-
| options | <code>object</code> | [{}] - Options to send to the diff endpoint |
1135+
| [options] | <code>object</code> | {keep:{}} Options to send to the diff endpoint the diff api outputs the changes between the input (JSON Object), but you can list the properties that you would like to see in the diff result in any case. |
11361136
11371137
**Example**
11381138
```javascript
1139-
const diff = await client.getDiff(
1139+
client.getJSONDiff(
11401140
{ "@id": "Person/Jane", "@type": "Person", name: "Jane" },
11411141
{ "@id": "Person/Jane", "@type": "Person", name: "Janine" }
1142-
);
1142+
).then(diffResult=>{
1143+
console.log(diffResult)
1144+
})
1145+
//result example
1146+
//{'@id': 'Person/Jane',
1147+
// name: { '@after': 'Janine', '@before': 'Jane', '@op': 'SwapValue' }}
11431148
```
11441149
11451150
## getVersionObjectDiff
1146-
##### woqlClient.getVersionObjectDiff(id, beforeVersion, after, options) ⇒ <code>Promise</code>
1151+
##### woqlClient.getVersionObjectDiff(dataVersion, jsonObject, id, [options]) ⇒ <code>Promise</code>
11471152
Get the patch of difference between two documents.
11481153
11491154
**Returns**: <code>Promise</code> - A promise that returns the call response object, or an Error if rejected.
11501155
11511156
| Param | Type | Description |
11521157
| --- | --- | --- |
1158+
| dataVersion | <code>string</code> | The version from which to compare the object |
1159+
| jsonObject | <code>object</code> | The updated state of JSON document |
11531160
| id | <code>string</code> | The object id to be diffed |
1154-
| beforeVersion | <code>string</code> | The version from which to compare the object |
1155-
| after | <code>object</code> | The updated state of JSON document |
1156-
| options | <code>object</code> | [{}] - Options to send to the diff endpoint |
1161+
| [options] | <code>object</code> | {keep:{}} Options to send to the diff endpoint the diff api outputs the changes between the input (branches or commits), but you can list the properties that you would like to see in the diff result in any case. |
11571162
11581163
**Example**
11591164
```javascript
1160-
const diff = await client.getVersionObjectDiff(
1161-
"Person/Jane",
1162-
"branch:a73ssscfx0kke7z76083cgswszdxy6l",
1163-
{ "@id": "Person/Jane", "@type": "Person", name: "Janine" }
1164-
);
1165+
const jsonObj = { "@id": "Person/Jane", "@type": "Person", name: "Janine" }
1166+
client.getVersionObjectDiff("main",jsonObj
1167+
"Person/Jane").then(diffResp=>{
1168+
console.log(diffResp)
1169+
})
11651170
```
11661171
11671172
## getVersionDiff
1168-
##### woqlClient.getVersionDiff(id, beforeVersion, afterVersion, options) ⇒ <code>Promise</code>
1169-
Get the patch of difference between two documents.
1173+
##### woqlClient.getVersionDiff(beforeVersion, afterVersion, [id], [options]) ⇒ <code>Promise</code>
1174+
Get the patch of difference between branches or commits.
11701175
11711176
**Returns**: <code>Promise</code> - A promise that returns the call response object, or an Error if rejected.
11721177
11731178
| Param | Type | Description |
11741179
| --- | --- | --- |
1175-
| id | <code>string</code> | The object id to be diffed |
1176-
| beforeVersion | <code>string</code> | The version from which to compare the object |
1177-
| afterVersion | <code>string</code> | The version to which to compare the object |
1178-
| options | <code>object</code> | [{}] - Options to send to the diff endpoint |
1180+
| beforeVersion | <code>string</code> | Before branch/commit to compare |
1181+
| afterVersion | <code>string</code> | Before branch/commit to compare |
1182+
| [id] | <code>string</code> | The object id to be diffed, if it is omitted all the documents will be compared |
1183+
| [options] | <code>object</code> | {keep:{}} Options to send to the diff endpoint the diff api outputs the changes between the input (branches or commits), but you can list the properties that you would like to see in the diff result in any case. |
11791184
11801185
**Example**
11811186
```javascript
1182-
const diff = await client.getVersionDiff(
1183-
"Person/Jane",
1184-
"branch:a73ssscfx0kke7z76083cgswszdxy6l",
1185-
"branch:73rqpooz65kbsheuno5dsayh71x7wf4"
1186-
);
1187+
//This is to view all the changes between two commits
1188+
const beforeCommit = "a73ssscfx0kke7z76083cgswszdxy6l"
1189+
const afterCommit = "73rqpooz65kbsheuno5dsayh71x7wf4"
1190+
1191+
client.getVersionDiff( beforeCommit, afterCommit).then(diffResult=>{
1192+
console.log(diffResult)
1193+
})
1194+
1195+
//This is to view the changes between two commits but only for the given document
1196+
client.getVersionDiff( beforeCommit, afterCommit, "Person/Tom").then(diffResult=>{
1197+
console.log(diffResult)
1198+
})
1199+
1200+
//This is to view the changes between a branch (head) and a commit for the given document
1201+
client.getVersionDiff("main", afterCommit, "Person/Tom" ).then(diffResult=>{
1202+
console.log(diffResult)
1203+
})
1204+
1205+
//This is to view the changes between two branches with the keep options
1206+
const options = {"keep":{"@id":true, "name": true}}
1207+
client.getVersionDiff("main","mybranch",options).then(diffResult=>{
1208+
console.log(diffResult)
1209+
})
1210+
```
1211+
1212+
## apply
1213+
##### woqlClient.apply(beforeVersion, afterVersion, message, [match_final_state], [options])
1214+
Diff two different commits and apply changes on the current branch/commit
1215+
if you would like to change branch or commit before apply use client.checkout("branchName")
1216+
1217+
1218+
| Param | Type | Description |
1219+
| --- | --- | --- |
1220+
| beforeVersion | <code>string</code> | Before branch/commit to compare |
1221+
| afterVersion | <code>string</code> | Before branch/commit to compare |
1222+
| message | <code>string</code> | apply commit message |
1223+
| [match_final_state] | <code>boolean</code> | the deafult value is false |
1224+
| [options] | <code>object</code> | {keep:{}} Options to send to the apply endpoint |
1225+
1226+
**Example**
1227+
```javascript
1228+
client.checkout("mybranch")
1229+
client.apply("main","mybranch").then(result=>{
1230+
console.log(result)
1231+
})
11871232
```
11881233
11891234
## patch

lib/connectionConfig.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,15 @@ ConnectionConfig.prototype.diffURL = function () {
512512
return purl;
513513
};
514514

515+
/**
516+
* Generate URL for diff endpoint
517+
* @returns {string}
518+
*/
519+
ConnectionConfig.prototype.applyURL = function () {
520+
const purl = this.branchBase('apply');
521+
return purl;
522+
};
523+
515524
/**
516525
* Generate URL for fetch endpoint
517526
* @param {string} remoteName

lib/woqlClient.js

Lines changed: 106 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,15 +1468,22 @@ WOQLClient.prototype.userOrganizations = function (orgList) {
14681468
* Get the patch of difference between two documents.
14691469
* @param {object} before - The current state of JSON document
14701470
* @param {object} after - The updated state of JSON document
1471-
* @param {object} options [{}] - Options to send to the diff endpoint
1471+
* @param {object} [options] - {keep:{}} Options to send to the diff endpoint
1472+
* the diff api outputs the changes between the input (JSON Object),
1473+
* but you can list the properties that you would like to see in the diff result in any case.
14721474
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
14731475
* @example
1474-
* const diff = await client.getDiff(
1476+
* client.getJSONDiff(
14751477
* { "@id": "Person/Jane", "@type": "Person", name: "Jane" },
14761478
* { "@id": "Person/Jane", "@type": "Person", name: "Janine" }
1477-
* );
1478-
*/
1479-
WOQLClient.prototype.getDiff = function (before, after, options) {
1479+
* ).then(diffResult=>{
1480+
* console.log(diffResult)
1481+
* })
1482+
* //result example
1483+
* //{'@id': 'Person/Jane',
1484+
* // name: { '@after': 'Janine', '@before': 'Jane', '@op': 'SwapValue' }}
1485+
*/
1486+
WOQLClient.prototype.getJSONDiff = function (before, after, options) {
14801487
if (typeof before !== 'object' || typeof after !== 'object') {
14811488
const errmsg = '"before" or "after" parameter error - you must specify a valid before or after json document';
14821489

@@ -1496,30 +1503,37 @@ WOQLClient.prototype.getDiff = function (before, after, options) {
14961503

14971504
/**
14981505
* Get the patch of difference between two documents.
1506+
* @param {string} dataVersion - The version from which to compare the object
1507+
* @param {object} jsonObject - The updated state of JSON document
14991508
* @param {string} id - The object id to be diffed
1500-
* @param {string} beforeVersion - The version from which to compare the object
1501-
* @param {object} after - The updated state of JSON document
1502-
* @param {object} options [{}] - Options to send to the diff endpoint
1509+
* @param {object} [options] - {keep:{}} Options to send to the diff endpoint
1510+
* the diff api outputs the changes between the input (branches or commits),
1511+
* but you can list the properties that you would like to see in the diff result in any case.
15031512
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
15041513
* @example
1505-
* const diff = await client.getVersionObjectDiff(
1506-
* "Person/Jane",
1507-
* "branch:a73ssscfx0kke7z76083cgswszdxy6l",
1508-
* { "@id": "Person/Jane", "@type": "Person", name: "Janine" }
1509-
* );
1514+
* const jsonObj = { "@id": "Person/Jane", "@type": "Person", name: "Janine" }
1515+
* client.getVersionObjectDiff("main",jsonObj
1516+
* "Person/Jane").then(diffResp=>{
1517+
* console.log(diffResp)
1518+
* })
15101519
*/
1511-
WOQLClient.prototype.getVersionObjectDiff = function (id, beforeVersion, after, options) {
1512-
if (typeof id !== 'string' || typeof beforeVersion !== 'string' || typeof after !== 'object') {
1513-
const errmsg = '"id", "beforeVersion" or "after" parameter error - you must specify a valid after json document and valid id and version';
1520+
WOQLClient.prototype.getVersionObjectDiff = function (dataVersion, jsonObject, id, options) {
1521+
if (typeof jsonObject !== 'object' || typeof dataVersion !== 'string') {
1522+
const errmsg = 'Parameters error - you must specify a valid jsonObject document and valid branch or commit';
15141523

15151524
return Promise.reject(
15161525
new Error(ErrorMessage.getInvalidParameterMessage(CONST.GET_DIFF, errmsg)),
15171526
);
15181527
}
1519-
const opt = (typeof options === 'undefined') ? {} : options;
1528+
const opt = options || {};
15201529
const payload = {
1521-
document_id: id, before_data_version: beforeVersion, after, ...opt,
1530+
after: jsonObject,
1531+
before_data_version: dataVersion,
1532+
...opt,
15221533
};
1534+
if (id) {
1535+
payload.id = id;
1536+
}
15231537
return this.dispatch(
15241538
CONST.POST,
15251539
this.connectionConfig.diffURL(),
@@ -1528,41 +1542,100 @@ WOQLClient.prototype.getVersionObjectDiff = function (id, beforeVersion, after,
15281542
};
15291543

15301544
/**
1531-
* Get the patch of difference between two documents.
1532-
* @param {string} id - The object id to be diffed
1533-
* @param {string} beforeVersion - The version from which to compare the object
1534-
* @param {string} afterVersion - The version to which to compare the object
1535-
* @param {object} options [{}] - Options to send to the diff endpoint
1545+
* Get the patch of difference between branches or commits.
1546+
* @param {string} beforeVersion - Before branch/commit to compare
1547+
* @param {string} afterVersion - Before branch/commit to compare
1548+
* @param {string} [id] - The object id to be diffed,
1549+
* if it is omitted all the documents will be compared
1550+
* @param {object} [options] - {keep:{}} Options to send to the diff endpoint
1551+
* the diff api outputs the changes between the input (branches or commits),
1552+
* but you can list the properties that you would like to see in the diff result in any case.
15361553
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
15371554
* @example
1538-
* const diff = await client.getVersionDiff(
1539-
* "Person/Jane",
1540-
* "branch:a73ssscfx0kke7z76083cgswszdxy6l",
1541-
* "branch:73rqpooz65kbsheuno5dsayh71x7wf4"
1542-
* );
1555+
* //This is to view all the changes between two commits
1556+
* const beforeCommit = "a73ssscfx0kke7z76083cgswszdxy6l"
1557+
* const afterCommit = "73rqpooz65kbsheuno5dsayh71x7wf4"
1558+
*
1559+
* client.getVersionDiff( beforeCommit, afterCommit).then(diffResult=>{
1560+
* console.log(diffResult)
1561+
* })
1562+
*
1563+
* //This is to view the changes between two commits but only for the given document
1564+
* client.getVersionDiff( beforeCommit, afterCommit, "Person/Tom").then(diffResult=>{
1565+
* console.log(diffResult)
1566+
* })
1567+
*
1568+
* //This is to view the changes between a branch (head) and a commit for the given document
1569+
* client.getVersionDiff("main", afterCommit, "Person/Tom" ).then(diffResult=>{
1570+
* console.log(diffResult)
1571+
* })
1572+
*
1573+
* //This is to view the changes between two branches with the keep options
1574+
* const options = {"keep":{"@id":true, "name": true}}
1575+
* client.getVersionDiff("main","mybranch",options).then(diffResult=>{
1576+
* console.log(diffResult)
1577+
* })
15431578
*/
1544-
WOQLClient.prototype.getVersionDiff = function (id, beforeVersion, afterVersion, options) {
1545-
if (typeof id !== 'string' || typeof beforeVersion !== 'string' || typeof afterVersion !== 'string') {
1546-
const errmsg = '"id", "beforeVersion" or "after" parameter error - you must specify a valid after json document and valid id and version';
1579+
1580+
WOQLClient.prototype.getVersionDiff = function (beforeVersion, afterVersion, id, options) {
1581+
if (typeof beforeVersion !== 'string' || typeof afterVersion !== 'string') {
1582+
const errmsg = 'Error, you have to provide a beforeVersion and afterVersion input';
15471583

15481584
return Promise.reject(
15491585
new Error(ErrorMessage.getInvalidParameterMessage(CONST.GET_DIFF, errmsg)),
15501586
);
15511587
}
1552-
const opt = (typeof options === 'undefined') ? {} : options;
1588+
const opt = options || {};
15531589
const payload = {
1554-
document_id: id,
15551590
before_data_version: beforeVersion,
15561591
after_data_version: afterVersion,
15571592
...opt,
15581593
};
1594+
if (id) {
1595+
payload.document_id = id;
1596+
}
15591597
return this.dispatch(
15601598
CONST.POST,
15611599
this.connectionConfig.diffURL(),
15621600
payload,
15631601
).then((response) => response);
15641602
};
15651603

1604+
/**
1605+
* Diff two different commits and apply changes on the current branch/commit
1606+
* if you would like to change branch or commit before apply use client.checkout("branchName")
1607+
* @param {string} beforeVersion - Before branch/commit to compare
1608+
* @param {string} afterVersion - Before branch/commit to compare
1609+
* @param {string} message - apply commit message
1610+
* @param {boolean} [match_final_state] - the deafult value is false
1611+
* @param {object} [options] - {keep:{}} Options to send to the apply endpoint
1612+
* @example
1613+
* client.checkout("mybranch")
1614+
* client.apply("main","mybranch").then(result=>{
1615+
* console.log(result)
1616+
* })
1617+
*/
1618+
1619+
// eslint-disable-next-line max-len
1620+
WOQLClient.prototype.apply = function (beforeVersion, afterVersion, message, matchFinalState, options) {
1621+
const opt = options || {};
1622+
const commitMsg = this.generateCommitInfo(message);
1623+
const payload = {
1624+
before_commit: beforeVersion,
1625+
after_commit: afterVersion,
1626+
...commitMsg,
1627+
...opt,
1628+
};
1629+
if (matchFinalState) {
1630+
payload.match_final_state = matchFinalState;
1631+
}
1632+
return this.dispatch(
1633+
CONST.POST,
1634+
this.connectionConfig.applyURL(),
1635+
payload,
1636+
).then((response) => response);
1637+
};
1638+
15661639
/**
15671640
* Patch the difference between two documents.
15681641
* @param {object} before - The current state of JSON document

0 commit comments

Comments
 (0)