How to use the dgraph-js.Mutation function in dgraph-js

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 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 dpeek / dgraphql / src / client.js View on Github external
}
      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 {
github dpeek / dgraphql / src / mutate / delete.js View on Github external
.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)

dgraph-js

Official javascript client for Dgraph

Apache-2.0
Latest version published 3 years ago

Package Health Score

61 / 100
Full package analysis

Similar packages