Giter Club home page Giter Club logo

dgraph-orm's Introduction

dgraph-orm

Simplified schema creation, queries and mutations for Dgraph.

Installation

npm install dgraph-orm

Depending on the version of Dgraph that you are connecting to, you will have to use a different version.

Dgraph version dgraph-orm version
1.0.X 1.X.Y
1.1.X 2.X.Y

Note: Only API breakage from v1.X.Y to v2.X.Y is in dependency -dgraph-js. Function DgraphClient.newTxn().mutate() returns a messages.Assigned type in v1.X but a messages.Response type in v2.X.

Full Documentation

https://avishwakarma.github.io/dgraph-orm

Your first schema and model

import dgraph from 'dgraph-orm';

const UserSchema = new dgraph.Schema('user', {
  name: {
    type: dgraph.Types.STRING,
    index: true,
    token: {
      term: true
    }
  },
  email: {
    type: dgraph.Types.STRING,
    index: true,
    unique: true,
    token: {
      exact: true
    }
  },
  password: dgraph.Types.PASSWORD,
  bio: dgraph.Types.STRING,
  friend: {
    type: dgraph.Types.UID,
    model: 'user', // related model name
    count: true,
    reverse: true
  }
});

/**
 * Set and create model out of the schema
 */
const User = dgraph.model(UserSchema);

/**
 * Creates a new user with passed fields
 *
 * Returns the created user along with the generated uid
 */
const user = await User.create({
  name: 'Ashok Vishwakarma',
  email: '[email protected]',
  bio: 'My bio ...'
});

console.log(user);
// {
//    uid: '0x1',
//    name: 'Ashok Vishwakarma',
//    email: '[email protected]',
//    bio: 'My bio ...'
// }

For the full documentation please visit the below link

https://ashokvishwakarma.github.io/dgraph-orm

Futute releases

  • Other geo queries within, intersects
  • Group by
  • Aggregation

Contribution

Issues and pull requests are welcome for

  • Unit test cases
  • Feature and query method implementation
  • Bug fixes

Author

my_pic

Ashok Vishwakarma

LinkedIn โ€ข Twitter โ€ข Medium

dgraph-orm's People

Contributors

ashokvishwakarma avatar avishwakarma avatar kamikazechaser avatar praveenkumarpalai avatar vadistic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

dgraph-orm's Issues

Error after create new record.

Error: Transaction has already been committed or discarded
at Object.<anonymous> (/Users/sheetalkumar/WebRoot/ok-dgraph/node_modules/dgraph-js/lib/errors.js:4:24)
at Module._compile (internal/modules/cjs/loader.js:721:30)
at Module._compile (/Users/sheetalkumar/WebRoot/ok-dgraph/node_modules/pirates/lib/index.js:83:24)
at Module._extensions..js (internal/modules/cjs/loader.js:732:10)
at Object.newLoader [as .js] (/Users/sheetalkumar/WebRoot/ok-dgraph/node_modules/pirates/lib/index.js:88:7)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Module.require (internal/modules/cjs/loader.js:657:17)
at require (internal/modules/cjs/helpers.js:22:18)

Not able to query for nested relations using dgraph-orm

I am using dgraph-orm for fetching nested relational values but it works for single level but not multiple level.
I am getting the page details but unable to fetch the avatar of the page.

Here is my snippet:

let posts = await PagePost.has('page_id', {
      filter: {
        page_id: {
          uid_in: [page_id]
        }
      },
      include: {
        page_id: {
          as: 'page',
          include: {
            avatar: {
              as: 'avatar'
            }
          }
        },
        owner_id: {
          as: 'postedBy'
        }
      },
      order: [], // accepts order like the above example
      first: perPage, // accepts first
      offset: offset, // accepts offset
    });

I am not getting avatar for the attribute page_id:

 {
            "uid": "0x75b4",
            "title": "",
            "content": "haha",
            "created_at": "2019-09-23T08:50:52.957Z",
            "status": true,
            "page": [
                {
                    "uid": "0x75ac",
                    "name": "Saregamaapaaaa...",
                    "description": "This a is place where you can listen ti thrilling music.",
                    "created_at": "2019-09-23T06:46:50.756Z",
                    "status": true
                }
            ],
            "postedBy": [
                {
                    "uid": "0x3",
                    "first_name": "Mohit",
                    "last_name": "Talwar",
                    "created_at": "2019-07-11T11:37:33.853Z",
                    "status": true
                }
            ]
        }

Is there a support for multilevel field querying in the orm??

Model.delete() not working!

When using Model.delete()..
I am getting the following error message.

// Delete edges and values of single model
Model.delete('0x1');

Error: device has no attribute 0
at Model._check_attributes (/Users/praveenkumarpalai/WebRoot/hiilife-backend/node_modules/dgraph-orm/src/model.ts:733:15)
at Model. (/Users/praveenkumarpalai/WebRoot/hiilife-backend/node_modules/dgraph-orm/src/model.ts:552:10)
at step (/Users/praveenkumarpalai/WebRoot/hiilife-backend/node_modules/dgraph-orm/lib/model.js:39:23)
at Object.next (/Users/praveenkumarpalai/WebRoot/hiilife-backend/node_modules/dgraph-orm/lib/model.js:20:53)


// Delete edges and values of mutliple model
Model.delete(['0x1']);

Error: device has no attribute 0x1
at Model._check_attributes (/Users/praveenkumarpalai/WebRoot/hiilife-backend/node_modules/dgraph-orm/src/model.ts:733:15)
at Model. (/Users/praveenkumarpalai/WebRoot/hiilife-backend/node_modules/dgraph-orm/src/model.ts:552:10)
at step (/Users/praveenkumarpalai/WebRoot/hiilife-backend/node_modules/dgraph-orm/lib/model.js:39:23)
at Object.next (/Users/praveenkumarpalai/WebRoot/hiilife-backend/node_modules/dgraph-orm/lib/model.js:20:53)


Missing AND condition in uid_in of filter

Conversation.has('members', {
   filter: {
      members: {
         uid_in: ['0x1', '0xadc']
      }
}); 

This query checks only for OR condition.
Need to have a query where I can check the AND condition.

Sample querys form Dgraph.

Available:

conversation (func: has(conversation.members))  @filter(uid_in(conversation.members, 0x3) OR uid_in(conversation.members, 0x7543) AND eq(conversation.type, "single") AND eq(conversation.approved, false)) 

Required:

conversation (func: has(conversation.members))  @filter(uid_in(conversation.members, 0x3) AND uid_in(conversation.members, 0x7543) AND eq(conversation.type, "single") AND eq(conversation.approved, false)) 

`

Model.create() is creating a node with only uid

Model.create() is returning a node with only uid. It is not storing the other data passed.

For Example,

let insert = {
      title: ctx.request.body.title,
      description: ctx.request.body.description,
      color: ctx.request.body.color,
      created_at: new Date(),
      created_at_ts: new Date().getTime(),
    }
let note = await Note.create(insert);

Returns this..

        {
            "uid": "0x30d4a"
        }

If I update this node it is being updated.

Use in native JS

Package does not work if you want to use in native JS; throws this error:
Error: Cannot find module 'babel-runtime/regenerator'

License

Thanks for the amazing work! Was curious if you were planning on adding a license to this repo? Hopefully open-source ๐Ÿ’ฏ

Unable to delete record.

I am getting error on delete:

{ Error: 2 UNKNOWN: uid must be present and non-zero while deleting edges.
at Object.exports.createStatusError (/Users/sheetalkumar/WebRoot/ok-dgraph/node_modules/grpc/src/common.js:91:15)
at Object.onReceiveStatus (/Users/sheetalkumar/WebRoot/ok-dgraph/node_modules/grpc/src/client_interceptors.js:1204:28)
at InterceptingListener._callNext (/Users/sheetalkumar/WebRoot/ok-dgraph/node_modules/grpc/src/client_interceptors.js:568:42)
at InterceptingListener.onReceiveStatus (/Users/sheetalkumar/WebRoot/ok-dgraph/node_modules/grpc/src/client_interceptors.js:618:8)
at callback (/Users/sheetalkumar/WebRoot/ok-dgraph/node_modules/grpc/src/client_interceptors.js:845:24)

code: 2,
metadata: Metadata { _internal_repr: {} },
details: 'uid must be present and non-zero while deleting edges.' }

Implement TypeORM style entity definition

E.g. http://typeorm.io

Instead of the example on the README, something like:

import {Entity, Property} from "dgraph-orm";

@Entity()
export class User {
    @Property()
    firstName: string;

    @Property()
    lastName: string;

    @Property()
    age: number;
}

Not sure how this would work in a more complex case.
Would love to help implement this

It's not clear in documentation how to create new record with relationships

Suppose you have this schema

const userSchema = new dgraph.Schema('user', {
  name: {
    type: dgraph.Types.STRING,
    index: true,
    token: {
      term: true
    }
  },
  email: {
    type: dgraph.Types.STRING,
    index: true,
    unique: true,
    token: {
      exact: true
    }
  },
  password: dgraph.Types.PASSWORD,
  bio: dgraph.Types.STRING,
  friend: {
    type: dgraph.Types.UID,
    model: 'user', // related model name
    count: true,
    reverse: true
  }
});

then you want to create new user with relationship(friend) with existing one
how do you create it ?
eg: tried

{
	"name": "John Doe",
	"email": "[email protected]",
	"password": "Dr0w$$@p",
	"bio": "Am student, fast learner",
	"friend": ["0x2"]
}

return Error: friend is a realtion and must be in include
tried: "friend": ["0x2"], "friend": {"uid": "0x2"}
is there a way to create new record with relation to existing one ?
because in dgraph-js they accept it

        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",
                }
            ]
        };

https://github.com/dgraph-io/dgraph-js/blob/21563532863a01a639b4ddf4848e3de3de77b3e0/examples/simple/index.js#L41

Regression: Previous filter not working on updating to 1.1.7

I am using the following snippet:

Here user_name, mobile, and email are all trigrams.

 const users = await User.has("mobile", {
        filter: {
          $or: {
            user_name: {
              $regexp: `/^${login}.*$/`
            },
            mobile: {
              $regexp: `/^${login}.*$/`
            },
            email: {
              $regexp: `/^${login}.*$/`
            }
          }
        }
    });

getting users = [ ]

but I have the login saved in Dgraph.

Why don't You return a database connection on connect method ?

Why don't you return connection so that we can re-use it across the whole application
suppose we have node MVC application I don't see how I can connect to dgraph once with

import dgraph from 'dgraph-orm';
import grpc from 'grpc';
dgraph.connect('HOST', 'PORT', grpc.credentials.createInsecure());

do I have to add above connection config in every and each file that talks to dgraph
why don't you return a connection so that I can connect to dgraph when am bootstrapping application and once connected use that connection across the whole application likes in dgraph-js
eg:

import * as dgraphjs from 'dgraph-js';
const clientStub = new dgraphjs.DgraphClientStub(
  process.env.DGRAPH_HOST_URI,
  grpc.credentials.createInsecure(),
);
export const db = new dgraphjs.DgraphClient(clientStub);

likes the above i can use that connection returned by DgraphClient for subsequent db queries
What do you think?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.