|
1 | | -# dgraph-orm |
2 | | -dGraph ORM written in TypeScript |
| 1 | +# DGraph ORM |
| 2 | + |
| 3 | +A decorator based object mapper, schema handler, mutation tracker to use with dgraph. |
| 4 | + |
| 5 | +This library handles objects and their relation and generates mutation/deletion strings based on |
| 6 | +changes. These strings can be used with any dgraph client to mutate the data. |
| 7 | + |
| 8 | +# Getting started |
| 9 | + |
| 10 | +``` |
| 11 | +yarn add @xanthous/dgraph-orm |
| 12 | +``` |
| 13 | + |
| 14 | +Here is an example to create a new graph using some of the public APIs exposed by the ORM. |
| 15 | + |
| 16 | +```typescript |
| 17 | +import { |
| 18 | + Uid, |
| 19 | + Node, |
| 20 | + Property, |
| 21 | + Predicate, |
| 22 | + IPredicate, |
| 23 | + QueryBuilder, |
| 24 | + SchemaBuilder, |
| 25 | + TransactionBuilder |
| 26 | +} from '@xanthous/dgraph-orm'; |
| 27 | + |
| 28 | +/** |
| 29 | + * A Node definition of person |
| 30 | + */ |
| 31 | +@Node() |
| 32 | +class Person { |
| 33 | + @Uid() |
| 34 | + id: string; |
| 35 | + |
| 36 | + @Property() |
| 37 | + name: string; |
| 38 | + |
| 39 | + @Predicate({ type: () => Person }) |
| 40 | + friends: IPredicate<Person>; |
| 41 | +} |
| 42 | + |
| 43 | +// Schema generated based on the node definitions. |
| 44 | +const schema = SchemaBuilder.build(); |
| 45 | +console.log(schema); |
| 46 | +// type Person { |
| 47 | +// Person.name: string |
| 48 | +// Person.friends: [Person] |
| 49 | +// } |
| 50 | +// Person.name: string . |
| 51 | +// Person.friends: [uid] @count . |
| 52 | + |
| 53 | +// Query builder can be used to easily create query fragments based on the definitions. |
| 54 | +const { handle, fragment } = QueryBuilder.buildFragment(Person); |
| 55 | +console.log(handle); |
| 56 | +// ...personDataFragment |
| 57 | + |
| 58 | +console.log(fragment); |
| 59 | +// fragment personDataFragment { |
| 60 | +// Person.name |
| 61 | +// Person.friends |
| 62 | +// id |
| 63 | +// } |
| 64 | + |
| 65 | +// Create a transaction |
| 66 | +const transaction = TransactionBuilder.build(); |
| 67 | + |
| 68 | +// Create some people |
| 69 | +const john = transaction.nodeFor(Person); |
| 70 | +const jane = transaction.nodeFor(Person); |
| 71 | +const kamil = transaction.nodeFor(Person); |
| 72 | + |
| 73 | +// A temporary uid is assigned during object creation. |
| 74 | +console.log(john.id); |
| 75 | +// b830c1f5ca09d466 ## random |
| 76 | + |
| 77 | +// Change their names |
| 78 | +john.name = 'John'; |
| 79 | +jane.name = 'Jane'; |
| 80 | +kamil.name = 'Kamil'; |
| 81 | + |
| 82 | +// Create connections between them |
| 83 | +kamil.friends.add(jane); |
| 84 | +kamil.friends.add(john); |
| 85 | + |
| 86 | +// Create a mutation string to use with dgraph js client. |
| 87 | +const mutation = transaction.getSetNQuadsString(); |
| 88 | +console.log(mutation); |
| 89 | +// _:b830c1f5c787c210 <dgraph.type> "Person" . |
| 90 | +// _:b830c1f5c787c210 <Person.name> "John"^^<xs:string> . |
| 91 | +// _:b830c1f5c78a5947 <dgraph.type> "Person" . |
| 92 | +// _:b830c1f5c78a5947 <Person.name> "Jane"^^<xs:string> . |
| 93 | +// _:b830c1f5c78afce1 <dgraph.type> "Person" . |
| 94 | +// _:b830c1f5c78afce1 <Person.name> "Kamil"^^<xs:string> . |
| 95 | +// _:b830c1f5c78afce1 <Person.friends> _:b830c1f5c78a5947 . |
| 96 | +// _:b830c1f5c78afce1 <Person.friends> _:b830c1f5c787c210 . |
| 97 | +``` |
3 | 98 |
|
4 | 99 | # Sponsors |
5 | 100 |
|
|
0 commit comments