- Notifications
You must be signed in to change notification settings - Fork8
A modern TypeScript/JavaScript Object Graph Mapper for the Dgraph Graph Database
License
gverse/gverse
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Note: This project is looking for maintainers. If you're interested, please create an issue. 🙏
Gverse is an Object Graph Mapper (OGM) for theDgraph, the high-performance open-sourceGraph Database. Gverse is written in TypeScript and supports TypeScript 3 and JavaScript ES6.
An OGM enables developers to work with their graph models through idiomatic objects provided by their native programming language. It is similar in concept to Object-Relational Mapping (ORM) libraries such asTypeORM,Sequelize orHibernate.See Gverse vs ORMs).
- Simple API for working with graphs vertices (nodes) and edges (links)
- Strongly typed classes and predicates (attributes) when using TypeScript
- Automatically marshal and unmarshal JavaScript objects to and from Graph vertices
- Support for custom marshaling and unmarshaling methods
- Support for directed and undirected edges
- Support for transactions and batch updates
- Before and after hooks for create, update and delete operations
- Query options for ordering and pagination
The current version of Gverse supports Dgraph version 1.2.x.
For compatibility with Dgraph 1.0.x, use Gverse version 1.0.2. You can specify theversion in your packages.json.
Here's a quick start guide to get you up and running.
Make sure you haveDgraph installed and running. This guide assumes you have Dgraph running on default ports (8080 for HTTP and 9080 gRPC).
Gverse requires ES6 with class properties plugin or TypeScript ≥ 2.0.
- Configure your project forTypeScript (seegetting started guide),– or –
- ConfigureBabel (seequick start guide) and add theClass Properties Plugin.
npm install gverse
or if you prefer,yarn add gverse
. The package includes TypeScript types.
importGversefrom"gverse"constgraph=newGverse.Graph(newGverse.Connection({host:"localhost",port:9080}))
constindices=` name: string @index(exact) @lang . owner: [uid] . repos: [uid] . contributors: [uid] . repositories: [uid] . `consttypes=` type User { name repositories } type Repository { name contributors owner } `awaitgraph.applySchema(indices+types)
classUserextendsGverse.Vertex{type="User"name:string=""}
constuser=newUser()user.name="Zak"awaitgraph.create(user)
constuser=(awaitgraph.get(uid))asUserconsole.log(user.name)// = "Zak"
For detailed examples, please see the integration tests under./test/integration
.
classRepo{type:"Repository"name:stringowner:Usercontributors:User[]_edges:{owner:Edge.toVertex(User),contributors:Edge.toVertices(User)}}
Edges can be directed or undirected (reversible), and can have a cardinality of one or many. For detailed examples, please see the integration tests under./test/integration
.
Thegraph.newTransaction().upsert
method enables you to execute upsert block having a query and a mutation:
constquery=`{vertex as var(func: eq(name,"John"))}`constvalues={uid:"uid(vertex)",name:"Smith"}awaitgraph.newTransaction().upsert(query,values)
An optional parametercondition
can be used to run a conditional upsert:
constcondition=`eq(len(vertex), 1)`awaitgraph.newTransaction().upsert(query,values,condition)
Test coverage for Gverse comes from integration tests.Docker andDocker-Compose are required for running integration tests.
./run-integration-tests.sh
Gverse has some fundamental differences to popular ORMs. It's helpful to understand the key differences:
- Gverse works with vertices and edges in a Graph structure instead of tables, columns and rows in RDBMS like MySQL, Postgres, Oracle, or documents in MongoDB, CouchDB, etc. (learn more).
- Gverse schema supports dynamic typing. You do not need to define and migrate schemas. Predicates (attributes) can be added as needed, with their data types inferred by value.
- A schema definition is required for any type that is part of aquery or filter function. Both the type and the index needs to be defined and applied. .
- Advanced graph queries in Gverse are written using GraphQL± (a variant ofGraphQL).
About
A modern TypeScript/JavaScript Object Graph Mapper for the Dgraph Graph Database