dropAll working?

Moved from GitHub dgraph-js/31

Posted by labs20:

Hi! I’m new on board and trying DGraph for the first time. Really excited about it, must say! =]

I have the code below, but when I call resetDgraph, my data is still there. What I’m doing wrong here?

    __getDGraph(){
        const dgraph = require('dgraph-js')
            , grpc = require('grpc')
            , clientStub = new dgraph.DgraphClientStub()
            , client = new dgraph.DgraphClient(clientStub)
        ;

        return {
            client: client,
            dgraph: dgraph
        };
    }

    async resetDgraph(){
        try{
            let db = this.__getDGraph()
                , op = new db.dgraph.Operation()
            ;

            op.setDropAll(true);
            let r = await db.client.alter(op);

            // Retorna
            return {
                ok: r
            }

        } catch(e){
            log.erro(e, '');
        }
    }

Thanks, and kudos for the work.

gpahal commented :

This should work. What version of Dgraph are you using?

labs20 commented :

v1.0.4. Just downloaded the docker image as per docs and running via docker-compose (and being struggling the last hour trying to map dgraph data folder to a local mount).

gpahal commented :

I tried it out and it’s working on my machine. Here is the code:

const dgraph = require("dgraph-js");
const grpc = require("grpc");

function __getDGraph() {
    const clientStub = new dgraph.DgraphClientStub()
        , client = new dgraph.DgraphClient(clientStub)
    ;

    return {
        client: client,
        dgraph: dgraph
    };
}

async function resetDgraph(){
    try{
        let db = __getDGraph()
            , op = new db.dgraph.Operation()
        ;

        op.setDropAll(true);
        let r = await db.client.alter(op);

        // Retorna
        return {
            ok: r
        }
    } catch(e){
        console.log(e);
    }
}

async function setSchema(dgraphClient) {
    const schema = `
        name: string @index(exact) .
    `;
    const op = new dgraph.Operation();
    op.setSchema(schema);
    await dgraphClient.alter(op);
}

// Create data using JSON.
async function createData(dgraphClient) {
    // Create a new transaction.
    const txn = dgraphClient.newTxn();
    try {
        // Create data.
        const p = { name: "Alice" };

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

        // Commit transaction.
        await txn.commit();
    } finally {
        // Clean up. Calling this after txn.commit() is a no-op
        // and hence safe.
        await txn.discard();
    }
}

// Query for data.
async function queryData(dgraphClient) {
    // Run query.
    const query = `query all($a: string) {
        all(func: eq(name, $a)) {
            uid
            name
        }
    }`;
    const vars = { $a: "Alice" };
    const res = await dgraphClient.newTxn().queryWithVars(query, vars);
    const ppl = res.getJson();

    // Print results.
    console.log(`Number of people named "Alice": ${ppl.all.length}`);
    ppl.all.forEach((person) => console.log(person));
}

async function main() {
    await resetDgraph();
    const res = __getDGraph();
    await setSchema(res.client);
    await createData(res.client);
    console.log("BEFORE RESET:");
    await queryData(res.client);
    await resetDgraph();
    console.log("\nAFTER RESET:");
    await setSchema(res.client);
    await queryData(res.client);
}

main().then(() => {
    console.log("\nDONE!");
}).catch((e) => {
    console.log("ERROR: ", e);
});

gpahal commented :

Can you share a little bit more about how you are using the resetDgraph function?

labs20 commented :

Hi, thanks for your reply.

I just tried again today and it worked. Maybe it was just a noob thing, but maybe (and just maybe) could it be something related to some sort of timeout? I had about 40k vertices and each time I tried it took a little while to complete.

If it happen again I’ll try to be more specific about. Sorry for a probably false alarm.

Thank you very much.

gpahal commented :

Based on your description, it’s possible that it was an issue with Dgraph itself and not the client.

I am closing this issue right now as right now this doesn’t seem to be a client side problem.