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
function newClientStub() {
// First create the appropriate TLS certs with dgraph cert:
// $ dgraph cert
// $ dgraph cert -n localhost
// $ dgraph cert -c user
const rootCaCert = fs.readFileSync(path.join(__dirname, 'tls', 'ca.crt'));
const clientCertKey = fs.readFileSync(path.join(__dirname, 'tls', 'client.user.key'));
const clientCert = fs.readFileSync(path.join(__dirname, 'tls', 'client.user.crt'));
return new dgraph.DgraphClientStub(
"localhost:9080",
grpc.credentials.createSsl(rootCaCert, clientCertKey, clientCert));
}
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();
}
}
async function dropAll(dgraphClient) {
const op = new dgraph.Operation();
op.setDropAll(true);
await dgraphClient.alter(op);
}
async function setSchema(dgraphClient) {
const schema = `
name: string @index(exact) .
age: int .
married: bool .
loc: geo .
dob: datetime .
friend: [uid] @reverse .
`;
const op = new dgraph.Operation();
op.setSchema(schema);
await dgraphClient.alter(op);
}
const dropAll = async () => {
const op = new Operation();
op.setDropAll(true);
await client.alter(op);
}