How to use dgraph-js - 10 common examples

To help you get started, we’ve selected a few dgraph-js examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dgraph-io / dgraph-js / examples / tls / index.js View on Github external
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
github dgraph-io / dgraph-js / examples / simple / index-pre-v7.6.js View on Github external
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) => {
github dgraph-io / dgraph-js / examples / simple / index.js View on Github external
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
github dgraph-io / dgraph-js / examples / tls / index.js View on Github external
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));
}
github astroband / astrograph / src / dgraph.ts View on Github external
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();
    }
  }
}
github astroband / astrograph / src / storage / connection.ts View on Github external
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);
github astroband / astrograph / src / storage / connection.ts View on Github external
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();
  }
}
github dgraph-io / dgraph-js / examples / simple / index.js View on Github external
async function dropAll(dgraphClient) {
    const op = new dgraph.Operation();
    op.setDropAll(true);
    await dgraphClient.alter(op);
}
github dgraph-io / dgraph-js / examples / tls / index.js View on Github external
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);
}
github astroband / astrograph / src / dgraph.ts View on Github external
const dropAll = async () => {
  const op = new Operation();
  op.setDropAll(true);
  await client.alter(op);
}

dgraph-js

Official javascript client for Dgraph

Apache-2.0
Latest version published 3 years ago

Package Health Score

54 / 100
Full package analysis

Similar packages