Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
age: 24,
},
{
name: "Charlie",
age: 29,
}
],
school: [
{
name: "Crown Public School",
}
]
};
// Run mutation.
const mu = new dgraph.Mutation();
mu.setSetJson(p);
const response = await txn.mutate(mu);
// Commit transaction.
await txn.commit();
// Get uid of the outermost object (person named "Alice").
// Response#getUidsMap() returns a map from blank node names to uids.
// For a json mutation, blank node label is used for the name of the created nodes.
console.log(`Created person named "Alice" with uid = ${response.getUidsMap().get("alice")}\n`);
console.log("All created nodes (map from blank node names to uids):");
response.getUidsMap().forEach((uid, key) => console.log(`${key} => ${uid}`));
console.log();
} finally {
// Clean up. Calling this after txn.commit() is a no-op
name: "Charlie",
age: 29,
}
],
school: [
{
name: "Crown Public School",
}
]
};
let response;
let err;
// Run mutation.
const mu = new dgraph.Mutation();
mu.setSetJson(p);
return txn.mutate(mu).then((res) => {
response = res;
// Commit transaction.
return txn.commit();
}).then(() => {
// Get uid of the outermost object (person named "Alice").
// Response#getUidsMap() returns a map from blank node names to uids.
// For a json mutation, blank node label is used for the name of the created nodes.
console.log(`Created person named "Alice" with uid = ${response.getUidsMap().get("alice")}\n`);
console.log("All created nodes (map from blank node names to uids):");
response.getUidsMap().forEach((uid, key) => console.log(`${key}: ${uid}`));
console.log();
}).catch((e) => {
age: 24,
},
{
name: "Charlie",
age: 29,
}
],
school: [
{
name: "Crown Public School",
}
]
};
// Run mutation.
const mu = new dgraph.Mutation();
mu.setSetJson(p);
const response = await txn.mutate(mu);
// Commit transaction.
await txn.commit();
// Get uid of the outermost object (person named "Alice").
// Response#getUidsMap() returns a map from blank node names to uids.
// For a json mutation, blank node label is used for the name of the created nodes.
console.log(`Created person named "Alice" with uid = ${response.getUidsMap().get("alice")}\n`);
console.log("All created nodes (map from blank node names to uids):");
response.getUidsMap().forEach((uid, key) => console.log(`${key} => ${uid}`));
console.log();
} finally {
// Clean up. Calling this after txn.commit() is a no-op
private async push(nquads: string): Promise {
const txn = client.newTxn();
try {
const mu = new Mutation();
mu.setSetNquads(nquads);
const assigned = await txn.mutate(mu);
await txn.commit();
return assigned;
} catch(e) {
console.log(`ERROR: ${e}`);
} finally {
await txn.discard();
}
}
}
public async push(nquads: string | any[]): Promise {
const start = Date.now();
const txn = this.client.newTxn();
const mu = new Mutation();
const payload: string = Array.isArray(nquads) ? nquads.join("\n") : nquads;
mu.setSetNquads(payload);
const assigns = await txn.mutate(mu);
try {
await txn.commit();
const eta = Date.now() - start;
logger.debug(`[DGraph] Transaction commited, ${nquads.length} triples, took ${eta / 1000} s.`);
return assigns;
} catch (err) {
try {
if (err === ERR_ABORTED) {
logger.debug(`[DGraph] Transaction aborted, retrying...`);
logger.debug(payload);
public async deleteOffers(offerIds: number[]): Promise {
if (offerIds.length === 0) {
return;
}
const fetchUidsQuery = `{
offers(func: eq(offer.id, [${offerIds.join(",")}])) {
uid
}
}`;
const response: { offers: Array<{ uid: string }> } = await this.query(fetchUidsQuery);
const uids = response.offers.map(offer => offer.uid);
const mu = new Mutation();
mu.setDelNquads(uids.map((uid: string) => `<${uid}> * * .`).join("\n"));
const txn = this.client.newTxn();
await txn.mutate(mu);
await txn.commit();
}
}
}
gql += ' .\n'
}
const op = new Operation()
op.setSchema(gql)
await client.alter(op)
const version = 1
schema = schema.replace(/\n/g, '\\n')
schema = schema.replace(/"/g, '\\"')
let sets = `_:node "" .\n`
sets += `_:node <__typename> "${updateTypeName}" .\n`
sets += `_:node "${schema}" .\n`
sets += `_:node "${version}" .\n`
const mutation = new Mutation()
mutation.setCommitNow(true)
mutation.setSetNquads(new Uint8Array(new Buffer(sets)))
const txn = client.newTxn()
await txn.mutate(mutation)
}
getReversePredicate (predicate: string): ?string {
.then(edges => {
const subject = edges.node[0]
let deletes = `<${id}> * * .\n`
Object.keys(subject).forEach(key => {
const results = subject[key]
const reverse = client.getReversePredicate(key)
if (reverse && Array.isArray(results)) {
results.forEach(node => {
deletes += `<${node.uid}> <${reverse}> <${id}> .\n`
})
}
})
const mutation = new Mutation()
mutation.setDelNquads(new Uint8Array(new Buffer(deletes)))
return client.mutate(mutation)
})
.then(() => payload)