A function that takes a node and returns dgraphQuery and nodeFoundFn. fn => {dgraphQuery, nodeFoundFn}
The basicEqualityUpsertFn below will find any nodes that has skill and level predicates.
const updateJunior = {
skill: 'Javascript',
level: 10,
x: 'y',
y: 'y',
z: 'y'
};
// So if you already had a node in the db that looks like:
const existingNode = {
skill: 'Javascript',
level: 10,
x: 'foo',
y: 'foo',
z: 'foo'
};
/// x, y and z would be updated from 'foo' to 'y'
const updates = [updateJunior];
const upsertFn = basicEqualityUpsertFn(['skill', 'level']);
// takes a upsertFn as well as an array or single object returns a uid or an array of uids
await xUpsertCommitTxn(upsertFn, updates, dgraphClient);
If no node matched both skill 'Javascript' and level '10' a new node would be created
The same as xUpsertEdgeListCommitTxn but you have to pass in your own transaction.
Example
// upsert on streetName and postCode
const addressUpsert = basicEqualityUpsertFn(['streetName', 'postCode']);
const newAddresses = [
{ streetName: 'Clarence', postCode: 2444 },
{ streetName: 'New Street', postCode: 2444 },
{ streetName: 'William', postCode: 2444 },
];
const upsertNode: IUpsertNode = {
uid: 0x2345, // the node you want to update a list on
predicate: 'addresses' // the list predicate
};
await xUpsertEdgeListCommitTxn(addressUpsert, upsertNode, newAddresses, dgraphClient);
const map = {
howard: { // howard is an upsert key
name: 'Howard',
email: 'hb@gmail.com'
},
cameron: { // so is cameron
name: 'Cameron B',
email: 'cam@gmail.com'
}
};
const resultMap = await xUpsertMapCommitTxn(basicEqualityUpsertFn('email'), map, dgraphClient);
// returns an object that has the uid of the upserted values
const expectedResult: IUidMap = {
cameron: cameronUid,
howard: howardUid
};
Not sure if this function adds enough value to be kept in the lib. Please do comment in the issues
Generated using TypeDoc
Return a dgraph query and a node found function
The basicEqualityUpsertFn below will find any nodes that has skill and level predicates.
const updateJunior = { skill: 'Javascript', level: 10, x: 'y', }; // So if you already had a node in the db that looks like: const existingNode = { skill: 'Javascript', level: 10, x: 'foo', }; /// update will match the existing node as skill and level match const updates = [updateJunior]; const upsertFn = basicEqualityUpsertFn(['skill', 'level']);
See xUpsertCommitTxn