orientechnologies / orientjs

The official fast, lightweight node.js client for OrientDB
http://orientdb.com
Other
326 stars 68 forks source link

[Documentation] Questions #108

Closed gyzerok closed 8 years ago

gyzerok commented 9 years ago

Is there a way to update record and return updated with one query?

dehbmarques commented 9 years ago

Hi @gyzerok,

You can do this:

db
  .update('#1:1')
  .set({ a: 'b' })
  .return('after @this')
  .one();
gyzerok commented 9 years ago

@dehbmarques Awesome! Thank you for help. This driver really needs better documentation :)

lvca commented 9 years ago

@maggiolo00 is taking care of writing better docs. @maggiolo00 what's the status? Could you include this?

wolf4ood commented 9 years ago

@lvca i've started the new docs here. I will structure it and make more example.

gyzerok commented 9 years ago

I've renamed the issue and suggest to use it for questions. So later on it would some roadmap about what should be added to the docs.

wolf4ood commented 9 years ago

:+1:

gyzerok commented 9 years ago

Another question is about upsert. Currently all over the place I need to do upsert on VERTEXes. Currently I do it in the following way and it feels like I am missing something. Mb there is a common solution for this or built-in method?

const { id, categoryId, ...params } = args;
let product;
if (id) {
  product = await root.update(id).set(params).return('after @this').one();
}
else {
  product = await root.create('VERTEX', 'Product').set(params).one();
  await root.create('EDGE', 'CategorizedAs')
    .from(product['@rid'])
    .to(categoryId)
    .scalar();
}
return product;

As I've understood from source there is no special upsert for Graph API.

wolf4ood commented 9 years ago

@gyzerok the id is a @rid right?

wolf4ood commented 9 years ago

there is the upsert on update command http://orientdb.com/docs/2.1/SQL-Update.html should be supported in orientjs let me check

gyzerok commented 9 years ago

@maggiolo00 yeah sure. Btw I'm using Orient with GraphQL so I'm on a way to discover best practises here. Real problem is I have no previous experience in Orient :)

gyzerok commented 9 years ago

Forgot to say that I have to rename @rid to id since GraphQL do not support @ for field names.

wolf4ood commented 9 years ago

how is your query with GraphQL ?

gyzerok commented 9 years ago

For now I do not have any strong opinion. Need to dig deeper in Orient first. The real problem is that even official documentation cannot provide me as much as I need and StackOverflow lacks ready to go answers. So I have to do a lot of experiments in studio. I hope later on I would be pretty experienced to write some tips about both technologies usage together.

Currently most people tries to use MongoDB with GraphQL. In my mind OrientDB might fit better with its Graph API and uniq ids through whole db.

wolf4ood commented 9 years ago

@gyzerok i'm not familiar with GraphQL. How do people use GraphQL with Mongo? How could we use GraphQL with OrientDB?

gyzerok commented 9 years ago

@maggiolo00 when you define GraphQL schema you need to define resolve logic for your fields.

export const productType = new GraphQLObjectType({
  name: 'Product',
  fields: () => ({
    id: {
      type: new GraphQLNonNull(GraphQLID),
      resolve: (user) => user['@rid'].toString(),
    },
    title: {
      type: new GraphQLNonNull(GraphQLString),
    },
    category: {
      type: categoryType,
      resolve: (product, _, { rootValue: root }) => {
        return root.select(`expand(out('CategorizedAs'))`).from(product['@rid']).one();
      },
    },
  }),
});

For the scalar fields GraphQL simply use result field. The intresting part hides inside edges. By default GraphQL resolve top vertex before its edges. When you use MongoDB its ok since you have to do additional selects to populate edges. With Orient we may highly optimize it by executing whole GraphQL query with only one SQL query. And because of Orients beautiful edges as links we can gain awesome performance here!

The things to explore here:

gyzerok commented 9 years ago

Btw assuming you have categoryType and queryType defined you can query your server like this:

query {
  product(id: "#12:0")  {
    id,
    title,
    category {
      id,
      title
    }
  }
}

// or you dont need to fetch categories

query {
  product(id: "#12:0")  {
    id,
    title
  }
}

And you would get JSON answer with exact that structure.

wolf4ood commented 9 years ago

@gyzerok how are you integrating GraphQL with OrientDB?

gyzerok commented 9 years ago

@maggiolo00 for now I simply use this driver inside resolve functions. You can see root - its simple db instance. Now I do not face performance problems since my product would start with a limited amount of users.

I would investigate in better integration a little later if I do like Orient :) Gonna start my investigation from https://github.com/facebook/dataloader

wolf4ood commented 9 years ago

That is cool. I will investigate more this GraphQL. BTW Keep me posted :D

wuxianliang commented 9 years ago

Please pay attention to http://www.edgedb.com . Someone is working on this subject too. @maggiolo00

gyzerok commented 9 years ago

@maggiolo00 Guess we need to discuss Orient and GraphQL in a separate issue.

gyzerok commented 9 years ago

How do I select by date in where clause? It seems like orientjs doesn't support any javascript date string representation.

wolf4ood commented 9 years ago

which query did you run?

have you tried with The Date object?

gyzerok commented 9 years ago

@maggiolo00 I've tried a lot of different ways: Strings, ISOStrings, pass date to date function.

return root.select()
        .from('Order')
        .where(`createdAt between date('${startDate}') and date('${endDate}')`)
        .order('createdAt desc')
        .all();

In this particular example startDate and endDate are ISO Strings.

wolf4ood commented 9 years ago

Why do you use the date function?

Have you declared the property createdAt in your schema?

wolf4ood commented 9 years ago

@gyzerok

look at this test. It works fine. Hope it helps

describe("Database API - Query Date", function () {
    before(function () {
        return CREATE_TEST_DB(this, 'testdb_query_date')
            .bind(this)
            .then(function () {
                return this.db.class.create('Date', 'V');
            })
            .then(function (item) {
                this.class = item;
                return this.class.property.create([
                    {
                        name: 'name',
                        type: 'String'
                    },
                    {
                        name: 'creation',
                        type: 'DateTime'
                    }
                ]);
            })
            .then(function () {
                return this.class.create([
                    {
                        name: 'a',
                        creation: '2001-01-01 00:00:01'
                    },
                    {
                        name: 'b',
                        creation: '2001-01-02 12:00:01'
                    },
                    {
                        name: 'c',
                        creation: '2009-01-01 00:12:01'
                    },
                    {
                        name: 'd',
                        creation: '2014-09-01 00:01:01'
                    },
                    {
                        name: 'e',
                        creation: '2014-09-01 00:24:01'
                    }
                ])
            });
    });
    after(function () {
        return DELETE_TEST_DB('testdb_query_date');
    });

    it('should return  a', function () {
        var date = new Date(2001, 0, 1, 0, 0, 1);
        var query = this.db.select().from('Date').where({'creation': date});
        return query.one()
            .then(function (res) {
                res.name.should.eql('a');
            });
    });

    it('should return d and e', function () {
        var query = this.db.select().from('Date').where("creation between '2014-09-01 00:01:00' and '2014-09-01 00:24:02' ");
        return query.all()
            .map(function (res) {
                return res.name;
            })
            .then(function (results) {
                results.should.eql(['d', 'e']);
            });
    });
});
gyzerok commented 9 years ago

@maggiolo00 any good docs about using transactions? Suppose its highly needed since transactions are one of the key feature of Orient in comparison to other NoSQL.

wolf4ood commented 8 years ago

@gyzerok

are you still working with OrientJS and GraphQL?

BTW here there is the new docs of OrientJS

http://orientdb.com/docs/last/OrientJS.html

smolinari commented 8 years ago

@gyzerok - did you get any further with your GraphQL work?

Scott

gyzerok commented 8 years ago

@smolinari if you mean work with Orient and GraphQL, I stopped about a year ago. Don't know about now, but at that time I would highly recommend to not use OrientDB because of poor documentation and painful searching answers to your questions. And this does not mean that technology itself is bad.

smolinari commented 8 years ago

Thanks for your reply.

ODB has gotten better since then, but the docs are still somewhat lacking. ODB is so powerful in so many ways and the docs fail to convert that to the public, unfortunately.

Is any of your work public? I'd just like to see how you tackled working with GraphQL. Have you seen Apollo Server? I am going to be using it.

Scott

wolf4ood commented 8 years ago

@smolinari

Apollo Server is really cool, i'm playing with it and ODB, i hope i will release some example app soon to help people starts with Graphql and ODB.

smolinari commented 8 years ago

That would be great Enrico.

If you guys could show how ODB can make GraphQL work on the application server, (hopefully also much easier), then you'd have a growing technology helping you sell ODB.

A PostgreSQL fan created this too: https://github.com/calebmer/postgraphql

It doesn't have anything to do with Apollo, but it shows the excitement for GraphQL as a data access and manipulation language/ API layer.

Scott

Chimba123 commented 5 years ago

@wolf4ood how far with that ODB and graphql example/tutorial it would help a lot.

Chimba123 commented 5 years ago

@wolf4ood I really need it I don't want to shift to Neo4j and Cypher because of the licensing issues and I don't want to learn another query language though Neo4j is really good but I ran away from it and i do not want to go back to it ODB is great just docs