Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xanthous-tech/dgraph-orm
dGraph ORM written in TypeScript
https://github.com/xanthous-tech/dgraph-orm
dgraph dgraph-orm orm typescript
Last synced: 5 days ago
JSON representation
dGraph ORM written in TypeScript
- Host: GitHub
- URL: https://github.com/xanthous-tech/dgraph-orm
- Owner: xanthous-tech
- License: mit
- Created: 2020-01-16T02:38:26.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-11T22:22:14.000Z (almost 2 years ago)
- Last Synced: 2024-10-14T02:09:58.411Z (about 1 month ago)
- Topics: dgraph, dgraph-orm, orm, typescript
- Language: TypeScript
- Size: 1.56 MB
- Stars: 17
- Watchers: 7
- Forks: 6
- Open Issues: 26
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.
```typescript
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;
}// 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);
// ...personDataFragmentconsole.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 "Person" .
// _:b830c1f5c787c210 "John"^^ .
// _:b830c1f5c78a5947 "Person" .
// _:b830c1f5c78a5947 "Jane"^^ .
// _:b830c1f5c78afce1 "Person" .
// _:b830c1f5c78afce1 "Kamil"^^ .
// _:b830c1f5c78afce1 _:b830c1f5c78a5947 .
// _:b830c1f5c78afce1 _:b830c1f5c787c210 .
```# Sponsors
[Treelab](https://treelab.com.cn)
# License
[MIT](./LICENSE)