examples/tls/index.js
async function createData(dgraphClient) {
    // Create a new transaction.
    const txn = dgraphClient.newTxn();
    try {
        // Create data.
        const p = {
            name: "Alice",
            age: 26,
            married: true,
            loc: {
                type: "Point",
                coordinates: [1.1, 2],
            },
            dob: new Date(1980, 1, 1, 23, 0, 0, 0),
            friend: [
                {
                    name: "Bob",
                    age: 24,
                },
                {
                    name: "Charlie",
                    age: 29,
                }
            ],
            school: [
                {
                    name: "Crown Public School",
                }
            ]
        };

        // Run mutation.
        const mu = new dgraph.Mutation();
        mu.setSetJson(p);
        const assigned = await txn.mutate(mu);

        // Commit transaction.
        await txn.commit();

        // Get uid of the outermost object (person named "Alice").
        // Assigned#getUidsMap() returns a map from blank node names to uids.
        // For a json mutation, blank node names "blank-0", "blank-1", ... are used
        // for all the created nodes.
        console.log(`Created person named "Alice" with uid = ${assigned.getUidsMap().get("blank-0")}\n`);

        console.log("All created nodes (map from blank node names to uids):");
        assigned.getUidsMap().forEach((uid, key) => console.log(`${key} => ${uid}`));
        console.log();
    } finally {
        // Clean up. Calling this after txn.commit() is a no-op
        // and hence safe.
        await txn.discard();
    }
}
async function createData(dgraphClient) {
    // Create a new transaction.
    const txn = dgraphClient.newTxn();
    try {
        // Create data.
        const p = {
            name: "Alice",
            age: 26,
            married: true,
            loc: {
                type: "Point",
                coordinates: [1.1, 2],
            },
            dob: new Date(1980, 1, 1, 23, 0, 0, 0),
            friend: [
                {
                    name: "Bob",
                    age: 24,
                },
                {
                    name: "Charlie",
                    age: 29,
                }
            ],
            school: [
                {
                    name: "Crown Public School",
                }
            ]
        };

        // Run mutation.
        const mu = new dgraph.Mutation();
        mu.setSetJson(p);
        const assigned = await txn.mutate(mu);

        // Commit transaction.
        await txn.commit();

        // Get uid of the outermost object (person named "Alice").
        // Assigned#getUidsMap() returns a map from blank node names to uids.
        // For a json mutation, blank node names "blank-0", "blank-1", ... are used
        // for all the created nodes.
        console.log(`Created person named "Alice" with uid = ${assigned.getUidsMap().get("blank-0")}\n`);

        console.log("All created nodes (map from blank node names to uids):");
        assigned.getUidsMap().forEach((uid, key) => console.log(`${key} => ${uid}`));
        console.log();
    } finally {
        // Clean up. Calling this after txn.commit() is a no-op
        // and hence safe.
        await txn.discard();
    }
}
async function createData ( dgraphClient ) { // Create a new transaction. const txn = dgraphClient . newTxn ( ) ; try { // Create data. const p = { name : "Alice" , age : 26 , married : true , loc : { type : "Point" , coordinates : [ 1.1 , 2 ] , } , dob : new Date ( 1980 , 1 , 1 , 23 , 0 , 0 , 0 ) , friend : [ { name : "Bob" , age : 24 , } , { name : "Charlie" , age : 29 , } ] , school : [ { name : "Crown Public School" , } ] } ; // Run mutation. const mu = new dgraph . Mutation ( ) ; mu . setSetJson ( p ) ; const assigned = await txn . mutate ( mu ) ; // Commit transaction. await txn . commit ( ) ; // Get uid of the outermost object (person named "Alice"). // Assigned#getUidsMap() returns a map from blank node names to uids. // For a json mutation, blank node names "blank-0", "blank-1", ... are used // for all the created nodes. console . log ( ` ${ assigned . getUidsMap ( ) . get ( "blank-0" ) } \n ` ) ; console . log ( "All created nodes (map from blank node names to uids):" ) ; assigned . getUidsMap ( ) . forEach ( ( uid , key ) => console . log ( ` ${ key } ${ uid } ` ) ) ; console . log ( ) ; } finally { // Clean up. Calling this after txn.commit() is a no-op // and hence safe. await txn . discard ( ) ; } }
Create data using JSON.
Create data using JSON .