Giter Site home page Giter Site logo

dgraph-orm's Introduction

DGraph ORM

A decorator based object mapper, schema handler, mutation tracker to use with dgraph.

This library handles objects and their relation and generates mutation/deletion strings based on changes. These strings can be used with any dgraph client to mutate the data.

Getting started

yarn add @xanthous/dgraph-orm

Here is an example to create a new graph using some of the public APIs exposed by the ORM.

import {
  Uid,
  Node,
  Property,
  Predicate,
  IPredicate,
  QueryBuilder,
  SchemaBuilder,
  TransactionBuilder
} from '@xanthous/dgraph-orm';

/**
 * A Node definition of person
 */
@Node()
class Person {
  @Uid()
  id: string;

  @Property()
  name: string;

  @Predicate({ type: () => Person })
  friends: IPredicate<Person>;
}

// Schema generated based on the node definitions.
const schema = SchemaBuilder.build();
console.log(schema);
// type Person {
//   Person.name: string
//   Person.friends: [Person]
// }
// Person.name: string .
// Person.friends: [uid] @count .

// Query builder can be used to easily create query fragments based on the definitions.
const { handle, fragment } = QueryBuilder.buildFragment(Person);
console.log(handle);
// ...personDataFragment

console.log(fragment);
// fragment personDataFragment {
//    Person.name
//    Person.friends
//    id
// }

// Create a transaction
const transaction = TransactionBuilder.build();

// Create some people
const john = transaction.nodeFor(Person);
const jane = transaction.nodeFor(Person);
const kamil = transaction.nodeFor(Person);

// A temporary uid is assigned during object creation.
console.log(john.id);
// b830c1f5ca09d466 ## random

// Change their names
john.name = 'John';
jane.name = 'Jane';
kamil.name = 'Kamil';

// Create connections between them
kamil.friends.add(jane);
kamil.friends.add(john);

// Create a mutation string to use with dgraph js client.
const mutation = transaction.getSetNQuadsString();
console.log(mutation);
// _:b830c1f5c787c210 <dgraph.type> "Person" .
// _:b830c1f5c787c210 <Person.name> "John"^^<xs:string> .
// _:b830c1f5c78a5947 <dgraph.type> "Person" .
// _:b830c1f5c78a5947 <Person.name> "Jane"^^<xs:string> .
// _:b830c1f5c78afce1 <dgraph.type> "Person" .
// _:b830c1f5c78afce1 <Person.name> "Kamil"^^<xs:string> .
// _:b830c1f5c78afce1 <Person.friends> _:b830c1f5c78a5947 .
// _:b830c1f5c78afce1 <Person.friends> _:b830c1f5c787c210 .

Sponsors

Treelab

License

MIT

dgraph-orm's People

Contributors

andrejpavlovic avatar dependabot[bot] avatar haseebrabbani avatar lhr0909 avatar necmttn avatar nickcaminer avatar osmanmesutozcan avatar

Watchers

 avatar

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.