Skip to content

Commit fef0f31

Browse files
Merge pull request #103 from terminusdb/addDP
adding diff and patch
2 parents 5009f36 + 2a5e09d commit fef0f31

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

docs/_sidebar.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
- [getEnums](api/woqlClient.js?id=getEnums)
6060
- [getClassDocuments](api/woqlClient.js?id=getClassDocuments)
6161
- [getBranches](api/woqlClient.js?id=getBranches)
62+
- [getDiff](api/woqlClient.js?id=getDiff)
63+
- [patch](api/woqlClient.js?id=patch)
6264
- [WOQL](api/woql.js?id=WOQL)
6365
- [eval](api/woql.js?id=eval)
6466
- [using](api/woql.js?id=using)

docs/api/woqlClient.js.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,3 +850,43 @@ get the database collections list
850850
```js
851851
client.getBranches()
852852
```
853+
854+
### getDiff
855+
#### woqlClient.getDiff(before, after) ⇒ <code>Promise</code>
856+
Get the patch of difference between two documents.
857+
858+
**Returns**: <code>Promise</code> - A promise that returns the call response object, or an Error if rejected.
859+
860+
| Param | Type | Description |
861+
| --- | --- | --- |
862+
| before | <code>object</code> | The current state of JSON document |
863+
| after | <code>object</code> | The updated state of JSON document |
864+
865+
**Example**
866+
```js
867+
const diff = await client.getDiff(
868+
{ "@id": "Person/Jane", "@type": "Person", name: "Jane" },
869+
{ "@id": "Person/Jane", "@type": "Person", name: "Janine" }
870+
);
871+
```
872+
873+
### patch
874+
#### woqlClient.patch(before, patch) ⇒ <code>Promise</code>
875+
Patch the difference between two documents.
876+
877+
**Returns**: <code>Promise</code> - A promise that returns the call response object, or an Error if rejected.
878+
879+
| Param | Type | Description |
880+
| --- | --- | --- |
881+
| before | <code>object</code> | The current state of JSON document |
882+
| patch | <code>object</code> | The patch object |
883+
884+
**Example**
885+
```js
886+
let diffPatch = await client.getDiff(
887+
{ "@id": "Person/Jane", "@type": "Person", name: "Jane" },
888+
{ "@id": "Person/Jane", "@type": "Person", name: "Janine" }
889+
);
890+
891+
let patch = await client.patch( { "@id": "Person/Jane", "@type": "Person", name: "Jane" }, diffPatch);
892+
```

lib/const.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@ module.exports = Object.freeze({
4848
ACTION: 'action',
4949
INFO: 'info',
5050
OPTIMIZE_SYSTEM: 'optimize_system',
51+
GET_DIFF: 'getDiff',
52+
PATCH: 'patch',
5153
})

lib/woqlClient.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,4 +1106,71 @@ WOQLClient.prototype.getBranches = function(dbId){
11061106
})
11071107
}
11081108

1109+
/**
1110+
* Get the patch of difference between two documents.
1111+
* @param {object} before - The current state of JSON document
1112+
* @param {object} after - The updated state of JSON document
1113+
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
1114+
* @example
1115+
* const diff = await client.getDiff(
1116+
* { "@id": "Person/Jane", "@type": "Person", name: "Jane" },
1117+
* { "@id": "Person/Jane", "@type": "Person", name: "Janine" }
1118+
* );
1119+
*/
1120+
WOQLClient.prototype.getDiff = function(before, after) {
1121+
1122+
if(typeof before !== "object" || typeof after !== "object") {
1123+
1124+
const errmsg = `"before" or "after" parameter error - you must specify a valid before or after json document`;
1125+
1126+
return Promise.reject(
1127+
new Error(ErrorMessage.getInvalidParameterMessage(CONST.GET_DIFF, errmsg)),
1128+
)
1129+
}
1130+
1131+
const payload = { before, after };
1132+
return this.dispatch(
1133+
CONST.POST,
1134+
this.connectionConfig.apiURL() + "diff",
1135+
payload
1136+
).then(response => {
1137+
return response;
1138+
});
1139+
};
1140+
1141+
/**
1142+
* Patch the difference between two documents.
1143+
* @param {object} before - The current state of JSON document
1144+
* @param {object} patch - The patch object
1145+
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
1146+
* @example
1147+
* let diffPatch = await client.getDiff(
1148+
* { "@id": "Person/Jane", "@type": "Person", name: "Jane" },
1149+
* { "@id": "Person/Jane", "@type": "Person", name: "Janine" }
1150+
* );
1151+
*
1152+
* let patch = await client.patch( { "@id": "Person/Jane", "@type": "Person", name: "Jane" }, diffPatch);
1153+
*/
1154+
WOQLClient.prototype.patch = function(before, patch){
1155+
1156+
if(typeof before !== "object" || typeof patch !== "object") {
1157+
1158+
const errmsg = `"before" or "patch" parameter error - you must specify a valid before or patch json document`;
1159+
1160+
return Promise.reject(
1161+
new Error(ErrorMessage.getInvalidParameterMessage(CONST.PATCH, errmsg)),
1162+
)
1163+
}
1164+
const payload = { before, patch };
1165+
1166+
return this.dispatch(
1167+
CONST.POST,
1168+
this.connectionConfig.apiURL() + 'patch',
1169+
payload
1170+
).then(response => {
1171+
return response
1172+
})
1173+
1174+
}
1175+
11091176
module.exports = WOQLClient

0 commit comments

Comments
 (0)