Skip to content

Instantly share code, notes, and snippets.

@dpkirchner
Created July 6, 2021 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpkirchner/c4f91d912d14a7077680cc5b41a7e905 to your computer and use it in GitHub Desktop.
Save dpkirchner/c4f91d912d14a7077680cc5b41a7e905 to your computer and use it in GitHub Desktop.
const dgraph = require('dgraph-js');
const clientStubs = [
new dgraph.DgraphClientStub('127.0.0.1:9080'),
];
const client = new dgraph.DgraphClient(...clientStubs);
const cleanup = async () => {
const txn = client.newTxn();
try {
const req = new dgraph.Request();
req.setQuery(`
query {
bars as var(func: type(Bar))
foos as var(func: type(Foo))
}
`);
const mu1 = new dgraph.Mutation();
mu1.setDeleteJson({
uid: 'uid(bars)',
});
req.addMutations(mu1);
const mu2 = new dgraph.Mutation();
mu2.setDeleteJson({
uid: 'uid(foos)',
});
req.addMutations(mu2);
await txn.doRequest(req);
await txn.commit();
} finally {
await txn.discard();
}
};
const initBar = async () => {
const txn = client.newTxn();
try {
const req = new dgraph.Request();
['bar 1', 'bar 2', 'bar 3'].forEach(name => {
const mu = new dgraph.Mutation();
mu.setSetJson({
bar_name: name,
'dgraph.type': 'Bar',
});
req.addMutations(mu);
});
await txn.doRequest(req);
await txn.commit();
} finally {
await txn.discard();
}
};
const initFoo = async () => {
const txn = client.newTxn();
try {
const req = new dgraph.Request();
req.setQuery(`
query {
bar2 as var(func: eq(bar_name, "bar 2"))
}
`);
const mu = new dgraph.Mutation();
mu.setSetJson({
'bar': 'uid(bar2)',
'dgraph.type': 'Foo',
});
req.addMutations(mu);
await txn.doRequest(req);
await txn.commit();
} catch (err) {
console.error(err);
} finally {
await txn.discard();
}
};
const preCommit = async () => {
const txn = client.newTxn();
try {
const req = new dgraph.Request();
// Switch to bar 3
req.setQuery(`
query {
foo as var(func: type(Foo))
bar3 as var(func: eq(bar_name, "bar 3"))
}
`);
const mu = new dgraph.Mutation();
mu.setSetJson({
bar: 'uid(bar3)',
'dgraph.type': 'Foo',
uid: 'uid(foo)',
});
req.addMutations(mu);
await txn.doRequest(req);
const result = await txn.queryWithVars(`
query {
foo(func: type(Foo)) {
uid
bar {
bar_name
}
}
}
`);
console.log('Pre-commit');
console.log(JSON.stringify(result.getJson(), null, 2));
await txn.commit();
} catch (err) {
console.error(err);
} finally {
await txn.discard();
}
};
const postCommit = async () => {
const txn = client.newTxn();
try {
const result = await txn.queryWithVars(`
query {
foo(func: type(Foo)) {
uid
bar {
bar_name
}
}
}
`);
console.log('Post-commit');
console.log(JSON.stringify(result.getJson(), null, 2));
} catch (err) {
console.error(err);
} finally {
await txn.discard();
}
};
const doItAll = async () => {
await cleanup();
await initBar();
await initFoo();
await preCommit();
await postCommit();
};
doItAll();
# Ran tmp.js repeatedly to show what's going on:
bash-3.2$ node tmp.js
Pre-commit
{
"foo": [
{
"uid": "0x1fd",
"bar": {
"bar_name": "bar 3"
}
}
]
}
Post-commit
{
"foo": [
{
"uid": "0x1fd",
"bar": {
"bar_name": "bar 3"
}
}
]
}
bash-3.2$ node tmp.js
Pre-commit
{
"foo": [
{
"uid": "0x201",
"bar": {
"bar_name": [
"bar 2",
"bar 3"
]
}
}
]
}
Post-commit
{
"foo": [
{
"uid": "0x201",
"bar": {
"bar_name": "bar 3"
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment