dgraph-js-extras
Advanced tools
Comparing version 0.6.4 to 0.7.0
@@ -0,1 +1,17 @@ | ||
<a name="0.7.0"></a> | ||
# [0.7.0](https://github.com/vespertilian/dgraph-js-extras/compare/v0.6.4...v0.7.0) (2019-01-28) | ||
### Features | ||
* **upsert-edge-list:** rename xUpsertEdgeListTxn to xUpsertEdgeListCommitTxn remove commit from xUp ([dd4a974](https://github.com/vespertilian/dgraph-js-extras/commit/dd4a974)) | ||
### BREAKING CHANGES | ||
* **upsert-edge-list:** xUpsertEdgeList no longer calls transaction.commit and the name of | ||
xUpsertEdgeListTxn was changed to xUpsertEdgeListCommitTxn | ||
<a name="0.6.4"></a> | ||
@@ -2,0 +18,0 @@ ## [0.6.4](https://github.com/vespertilian/dgraph-js-extras/compare/v0.6.3...v0.6.4) (2019-01-28) |
@@ -7,3 +7,3 @@ export { xQueryWithVars, xQueryWithVarsTxn, xQuery, xQueryTxn } from './query/query'; | ||
export { xUpsertTxn } from './upsert/upsert'; | ||
export { xUpsertEdgeList, xUpsertEdgeListTxn } from './upsert-edge-list/upsert-edge-list'; | ||
export { xUpsertEdgeList, xUpsertEdgeListCommitTxn } from './upsert-edge-list/upsert-edge-list'; | ||
export { xUpsertMapTxn } from './upsert-map/upsert-map'; | ||
@@ -10,0 +10,0 @@ export { xGetSchemaMapTxn } from './get-schema-map/get-schema-map'; |
@@ -21,3 +21,3 @@ "use strict"; | ||
exports.xUpsertEdgeList = upsert_edge_list_1.xUpsertEdgeList; | ||
exports.xUpsertEdgeListTxn = upsert_edge_list_1.xUpsertEdgeListTxn; | ||
exports.xUpsertEdgeListCommitTxn = upsert_edge_list_1.xUpsertEdgeListCommitTxn; | ||
var upsert_map_1 = require("./upsert-map/upsert-map"); | ||
@@ -24,0 +24,0 @@ exports.xUpsertMapTxn = upsert_map_1.xUpsertMapTxn; |
@@ -8,3 +8,3 @@ import * as dgraph from 'dgraph-js'; | ||
} | ||
export declare function xUpsertEdgeListTxn(upsertFn: (input?: any) => IUpsertFnReturnValues, upsertNode: IUpsertNode, nodes: object[], dgraphClient: dgraph.DgraphClient, _dgraph?: typeof dgraph): Promise<string[]>; | ||
export declare function xUpsertEdgeListCommitTxn(upsertFn: (input?: any) => IUpsertFnReturnValues, upsertNode: IUpsertNode, nodes: object[], dgraphClient: dgraph.DgraphClient, _dgraph?: typeof dgraph): Promise<any>; | ||
export declare function xUpsertEdgeList(upsertFn: (input?: any) => IUpsertFnReturnValues, {uid, predicate}: IUpsertNode, nodes: object[], transaction: Txn, _dgraph?: typeof dgraph): Promise<string[]>; |
@@ -43,13 +43,41 @@ "use strict"; | ||
var upsert_1 = require("../upsert/upsert"); | ||
function xUpsertEdgeListTxn(upsertFn, upsertNode, nodes, dgraphClient, _dgraph) { | ||
function xUpsertEdgeListCommitTxn(upsertFn, upsertNode, nodes, dgraphClient, _dgraph) { | ||
if (_dgraph === void 0) { _dgraph = dgraph; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var transaction; | ||
var transaction, result, error, e_1, e_2; | ||
return __generator(this, function (_a) { | ||
transaction = dgraphClient.newTxn(); | ||
return [2 /*return*/, xUpsertEdgeList(upsertFn, upsertNode, nodes, transaction, _dgraph)]; | ||
switch (_a.label) { | ||
case 0: | ||
transaction = dgraphClient.newTxn(); | ||
error = null; | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, 4, 8]); | ||
return [4 /*yield*/, xUpsertEdgeList(upsertFn, upsertNode, nodes, transaction, _dgraph)]; | ||
case 2: | ||
result = _a.sent(); | ||
transaction.commit(); | ||
return [3 /*break*/, 8]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
error = e_1; | ||
throw e_1; | ||
case 4: | ||
_a.trys.push([4, 6, , 7]); | ||
return [4 /*yield*/, transaction.discard()]; | ||
case 5: | ||
_a.sent(); | ||
return [3 /*break*/, 7]; | ||
case 6: | ||
e_2 = _a.sent(); | ||
// the error e here will be the transaction.discard() error. | ||
// not the error we want - the one that was thrown in the try catch block | ||
throw (error); | ||
case 7: return [7 /*endfinally*/]; | ||
case 8: return [2 /*return*/, result]; | ||
} | ||
}); | ||
}); | ||
} | ||
exports.xUpsertEdgeListTxn = xUpsertEdgeListTxn; | ||
exports.xUpsertEdgeListCommitTxn = xUpsertEdgeListCommitTxn; | ||
function xUpsertEdgeList(upsertFn, _a, nodes, transaction, _dgraph) { | ||
@@ -59,3 +87,3 @@ var uid = _a.uid, predicate = _a.predicate; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var upsertedNodes, error, nodeExists, deleteAllPredicateLinks, i, currentNode, result, addPredicateLinks, e_1, e_2, _b, _c; | ||
var upsertedNodes, nodeExists, deleteAllPredicateLinks, i, currentNode, result, addPredicateLinks, _b, _c; | ||
return __generator(this, function (_d) { | ||
@@ -65,3 +93,2 @@ switch (_d.label) { | ||
upsertedNodes = []; | ||
error = null; | ||
if (!Array.isArray(nodes)) { | ||
@@ -73,7 +100,4 @@ throw new Error('You must pass nodes as an array of objects'); | ||
} | ||
_d.label = 1; | ||
return [4 /*yield*/, validate_node_exists_1.xValidateNodeExists(uid, transaction)]; | ||
case 1: | ||
_d.trys.push([1, 10, 11, 15]); | ||
return [4 /*yield*/, validate_node_exists_1.xValidateNodeExists(uid, transaction)]; | ||
case 2: | ||
nodeExists = _d.sent(); | ||
@@ -91,18 +115,18 @@ if (!nodeExists) { | ||
return [4 /*yield*/, transaction.mutate(__1.xDeleteJSON(deleteAllPredicateLinks))]; | ||
case 3: | ||
case 2: | ||
_d.sent(); | ||
i = 0; | ||
_d.label = 4; | ||
case 4: | ||
if (!(i < nodes.length)) return [3 /*break*/, 7]; | ||
_d.label = 3; | ||
case 3: | ||
if (!(i < nodes.length)) return [3 /*break*/, 6]; | ||
currentNode = nodes[i]; | ||
return [4 /*yield*/, upsert_1.xUpsertObject(upsertFn, currentNode, transaction)]; | ||
case 5: | ||
case 4: | ||
result = _d.sent(); | ||
upsertedNodes.push(result); | ||
_d.label = 6; | ||
_d.label = 5; | ||
case 5: | ||
i++; | ||
return [3 /*break*/, 3]; | ||
case 6: | ||
i++; | ||
return [3 /*break*/, 4]; | ||
case 7: | ||
addPredicateLinks = (_c = { | ||
@@ -114,25 +138,5 @@ uid: uid | ||
return [4 /*yield*/, transaction.mutate(__1.xSetJSON(addPredicateLinks, _dgraph))]; | ||
case 8: | ||
case 7: | ||
_d.sent(); | ||
return [4 /*yield*/, transaction.commit()]; | ||
case 9: | ||
_d.sent(); | ||
return [3 /*break*/, 15]; | ||
case 10: | ||
e_1 = _d.sent(); | ||
error = e_1; | ||
throw e_1; | ||
case 11: | ||
_d.trys.push([11, 13, , 14]); | ||
return [4 /*yield*/, transaction.discard()]; | ||
case 12: | ||
_d.sent(); | ||
return [3 /*break*/, 14]; | ||
case 13: | ||
e_2 = _d.sent(); | ||
// the error e here will be the transaction.discard() error. | ||
// not the error we want - the one that was thrown in the try catch block | ||
throw (error); | ||
case 14: return [7 /*endfinally*/]; | ||
case 15: return [2 /*return*/, upsertedNodes]; | ||
return [2 /*return*/, upsertedNodes]; | ||
} | ||
@@ -139,0 +143,0 @@ }); |
{ | ||
"name": "dgraph-js-extras", | ||
"version": "0.6.4", | ||
"version": "0.7.0", | ||
"description": "Functions to use with dgraph", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -7,3 +7,3 @@ export {xQueryWithVars, xQueryWithVarsTxn, xQuery, xQueryTxn} from './query/query' | ||
export {xUpsertTxn} from './upsert/upsert' | ||
export {xUpsertEdgeList, xUpsertEdgeListTxn} from './upsert-edge-list/upsert-edge-list'; | ||
export {xUpsertEdgeList, xUpsertEdgeListCommitTxn} from './upsert-edge-list/upsert-edge-list'; | ||
export {xUpsertMapTxn} from './upsert-map/upsert-map' | ||
@@ -10,0 +10,0 @@ export {xGetSchemaMapTxn} from './get-schema-map/get-schema-map' |
import { xSetupWithSchemaDataNowTxn } from '../test-helpers/setup'; | ||
import { xExtractNamedUids } from '../extract-named-uid/extract-named-uids'; | ||
import { basicEqualityUpsertFn, IUpsertFnReturnValues, xQueryTxn } from '..'; | ||
import { IUpsertNode, xUpsertEdgeListTxn } from './upsert-edge-list'; | ||
import { IUpsertNode, xUpsertEdgeListCommitTxn } from './upsert-edge-list'; | ||
@@ -104,3 +104,3 @@ const addressesQuery = `{ | ||
await xUpsertEdgeListTxn(addressUpsert, upsertNode, newAddresses, dgraphClient); | ||
await xUpsertEdgeListCommitTxn(addressUpsert, upsertNode, newAddresses, dgraphClient); | ||
@@ -190,3 +190,3 @@ const cameronFinalResult = await xQueryTxn(userQuery(cameronUid), dgraphClient); | ||
try { | ||
await xUpsertEdgeListTxn(badQuery('streetName'), upsertNode, newAddresses, dgraphClient) | ||
await xUpsertEdgeListCommitTxn(badQuery('streetName'), upsertNode, newAddresses, dgraphClient) | ||
} catch(e) { | ||
@@ -216,3 +216,3 @@ error = e; | ||
try { | ||
await xUpsertEdgeListTxn(addressUpsert, upsertNode, newAddresses, dgraphClient); | ||
await xUpsertEdgeListCommitTxn(addressUpsert, upsertNode, newAddresses, dgraphClient); | ||
} catch(e) { | ||
@@ -248,3 +248,3 @@ error = e; | ||
try { | ||
await xUpsertEdgeListTxn(addressUpsert, upsertNode, newAddresses, dgraphClient); | ||
await xUpsertEdgeListCommitTxn(addressUpsert, upsertNode, newAddresses, dgraphClient); | ||
} catch(e) { | ||
@@ -281,3 +281,3 @@ error = e; | ||
try { | ||
await xUpsertEdgeListTxn(addressUpsert, upsertNode, newAddresses, dgraphClient); | ||
await xUpsertEdgeListCommitTxn(addressUpsert, upsertNode, newAddresses, dgraphClient); | ||
} catch(e) { | ||
@@ -284,0 +284,0 @@ error = e; |
@@ -13,11 +13,29 @@ import * as dgraph from 'dgraph-js' | ||
export async function xUpsertEdgeListTxn(upsertFn: (input?: any) => IUpsertFnReturnValues, upsertNode: IUpsertNode, nodes: object[], dgraphClient: dgraph.DgraphClient, _dgraph=dgraph) { | ||
export async function xUpsertEdgeListCommitTxn(upsertFn: (input?: any) => IUpsertFnReturnValues, upsertNode: IUpsertNode, nodes: object[], dgraphClient: dgraph.DgraphClient, _dgraph=dgraph) { | ||
const transaction = dgraphClient.newTxn(); | ||
return xUpsertEdgeList(upsertFn, upsertNode, nodes, transaction, _dgraph) | ||
let result; | ||
let error: null | Error = null; | ||
try { | ||
result = await xUpsertEdgeList(upsertFn, upsertNode, nodes, transaction, _dgraph); | ||
transaction.commit(); | ||
} | ||
catch(e) { | ||
error = e; | ||
throw e; | ||
} | ||
finally { | ||
try { | ||
await transaction.discard() | ||
} catch(e) { | ||
// the error e here will be the transaction.discard() error. | ||
// not the error we want - the one that was thrown in the try catch block | ||
throw(error); | ||
} | ||
} | ||
return result; | ||
} | ||
export async function xUpsertEdgeList(upsertFn: (input?: any) => IUpsertFnReturnValues, {uid, predicate}: IUpsertNode, nodes: object[], transaction: Txn, _dgraph=dgraph) { | ||
const upsertedNodes: string[] = []; | ||
let error: null | Error = null; | ||
@@ -31,50 +49,33 @@ if(!Array.isArray(nodes)) { | ||
} | ||
try { | ||
const nodeExists = await xValidateNodeExists(uid, transaction); | ||
if(!nodeExists) { | ||
// if the node does not exist you get an unhelpful error. | ||
// this one is better | ||
throw new Error(`You passed a uid for a node that does not exist, uid: ${uid}`) | ||
} | ||
// delete existing nodes off predicate | ||
const deleteAllPredicateLinks = { | ||
uid, | ||
[predicate]: null | ||
}; | ||
const nodeExists = await xValidateNodeExists(uid, transaction); | ||
if(!nodeExists) { | ||
// if the node does not exist you get an unhelpful error. | ||
// this one is better | ||
throw new Error(`You passed a uid for a node that does not exist, uid: ${uid}`) | ||
} | ||
await transaction.mutate(xDeleteJSON(deleteAllPredicateLinks)); | ||
// delete existing nodes off predicate | ||
const deleteAllPredicateLinks = { | ||
uid, | ||
[predicate]: null | ||
}; | ||
// find or create nodes | ||
for(let i=0; i < nodes.length; i++) { | ||
const currentNode = nodes[i]; | ||
const result = await xUpsertObject(upsertFn, currentNode, transaction); | ||
upsertedNodes.push(result); | ||
} | ||
await transaction.mutate(xDeleteJSON(deleteAllPredicateLinks)); | ||
// add updated list of predicates | ||
const addPredicateLinks = { | ||
uid, | ||
[predicate]: upsertedNodes.map(uid => ({uid})) | ||
}; | ||
// find or create nodes | ||
for(let i=0; i < nodes.length; i++) { | ||
const currentNode = nodes[i]; | ||
const result = await xUpsertObject(upsertFn, currentNode, transaction); | ||
upsertedNodes.push(result); | ||
} | ||
await transaction.mutate(xSetJSON(addPredicateLinks, _dgraph)); | ||
// add updated list of predicates | ||
const addPredicateLinks = { | ||
uid, | ||
[predicate]: upsertedNodes.map(uid => ({uid})) | ||
}; | ||
await transaction.commit() | ||
} | ||
catch(e) { | ||
error = e; | ||
throw e; | ||
} | ||
finally { | ||
try { | ||
await transaction.discard() | ||
} catch(e) { | ||
// the error e here will be the transaction.discard() error. | ||
// not the error we want - the one that was thrown in the try catch block | ||
throw(error); | ||
} | ||
} | ||
await transaction.mutate(xSetJSON(addPredicateLinks, _dgraph)); | ||
return upsertedNodes; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
193827
4119