Slide 1

Slide 1 text

Not everything can fit in rows and columns Ashok Vishwakarma GDE India @_avishwakarma Dgraph and draph-orm GDE Summit 2019 Francesc Campoy GDE / VP Product at Dgraph Labs @francesc

Slide 2

Slide 2 text

Graphs Graph in RDBMS Graph in NoSQL Graph Databases Dgraph Architecture Query Language DgraphORM Agenda

Slide 3

Slide 3 text

A representation of objects and their relation called node and edges. Graphs

Slide 4

Slide 4 text

name: Steven Spielberg name: Jaws year: 1975 name: Jurassic Park year: 1993 name: Thriller name: Science Fiction name: Comedy directed directed genre genre genre genre Node Relationship A Movie Graph

Slide 5

Slide 5 text

Complex Mapping Process one-to-one: foreign keys one-to-many: repeated foreign keys many-to-many: seperate table Traversals require joins Graphs in RDBMS

Slide 6

Slide 6 text

ID int Name string Movie ID int Name string Year int … DirectorID int FK Director Fetching all the movies directed by a director requires an index for performance. Movie - Director in RDBMS

Slide 7

Slide 7 text

Twilight Zone - The Movie Directed By Steven Spielberg John Landis Joe Dante George Miller Good luck migrating that table :) Movies can have multiple director

Slide 8

Slide 8 text

MovieID int DirectorID Movie ID int Name string Year int MovieDirector We had to modify our logical model to fit the technology, bringing in unnecessary complexity. ID int Name string Director Movie - Director in RDBMS

Slide 9

Slide 9 text

No foreign keys Many copies of information Keep them all up to date Seriously are you kidding? Traversals require multiple queries Graphs in NoSQL

Slide 10

Slide 10 text

No mapping No joins Traversals are fast Deep traversal are possible Graph Databases

Slide 11

Slide 11 text

World’s most advanced Graph Database - written in Go https://dgraph.io/ Meet Dgraph

Slide 12

Slide 12 text

Universal Identifiers (UIDs) 0x1 “Jaws” 0x1 1975 0x1 0x2 0x2 “Steven Spielberg” 0x1 and 0x2 are UIDs (Universal IDentifiers). , , etc. are predicates. “Jaws”, “Steven Spielberg”, and 1975 are values. Dgraph Data Modeling

Slide 13

Slide 13 text

GraphQL +- Inspired by GraphQL Modified for better support Pure GraphQL Native support (coming soon) Playground - https://play.dgraph.io/ Dgraph Query Language

Slide 14

Slide 14 text

Demo time!

Slide 15

Slide 15 text

Mutation Dgraph Query Language { “set”: { “name”: "Alice" } } Creates a new UID and associate its value “Alice” with predicate { "data": { "code": "Success", "message": "Done", "uids": { "alice": "0x1" } }, "extensions": {...} }

Slide 16

Slide 16 text

Mutation Dgraph Query Language { “set”: { “name”: "Bob", “knows”: [ “uid”: “0x1” ] } } Creates a new UID and associate its value “Bob” with predicate and associate predicate with 0x1 UID { "data": { "code": "Success", "message": "Done", "uids": { "bob": "0x2" } }, "extensions": {...} } https://play.dgraph.io/

Slide 17

Slide 17 text

Current Dataset 0x1 0x2 knows Alice Bob name name https://play.dgraph.io/

Slide 18

Slide 18 text

Query Dgraph Query Language { q(func: uid(0x1, 0x2)) { uid name } } { "data": { "q": [ { "uid": "0x1", "name": "Alice" }, { "uid": "0x2", "name": "Bob" }] }, "extensions": {...} } 0x1 0x2 Alice Bob name name Fetches uid and name of nodes of UIDs 0x1 and 0x2 https://play.dgraph.io/

Slide 19

Slide 19 text

{ q(func: uid(0x2)) { uid name knows { uid name } } } Query Dgraph Query Language { "data": { "q": [{ "uid": "0x2", "name": "Bob", "knows": [{ "uid": "0x1", "name": "Alice" }] }] }, "extensions": {...} } 0x1 0x2 Alice Bob name name knows https://play.dgraph.io/

Slide 20

Slide 20 text

Simplified Schema creation, queries and mutations for Dgraph Inspired by Mongoose and Sequelize Built on top of dgraph-js http://bit.ly/dgraph-orm-docs Introducing Dgraph ORM

Slide 21

Slide 21 text

Schema creation // User.js import { Schema, model, Types } from ‘dgraph-orm’; const UserSchema = new Schema(‘user’, { name: { type: Types.STRING }, ... knows: { Type: Types.UID, model: ‘user’ } }); export default const User = model(UserSchema) http://bit.ly/dgraph-orm-docs user.name: string . ... user.knows [uid] .

Slide 22

Slide 22 text

Schema creation import User from ‘./User’; // Create User Alice const alice = await User.create({ name: ‘Alice’ }); // Create User bob const bob = await User.create({ name: ‘Bob’, knows: alice.uid }); http://bit.ly/dgraph-orm-docs // Querying const users = await User.uid(‘0x2’, { include: { knows: { as: ‘knows’ } } }); console.log(users); { "data": { "user": [{ "uid": "0x2", "name": "Bob", "knows": [{ "uid": "0x1", "name": "Alice" }] }] }, "extensions": {...} }

Slide 23

Slide 23 text

Demo time!

Slide 24

Slide 24 text

Thank You! GDE Summit 2019 Ashok Vishwakarma GDE India @_avishwakarma Francesc Campoy GDE / VP Product at Dgraph Labs @francesc