arangodb - using javascript in the database

27
ArangoDB /* Using Javascript in the database */ Jan Steemann, triAGENS, Cologne

Upload: arangodb

Post on 29-Aug-2014

4.660 views

Category:

Technology


14 download

DESCRIPTION

Jan Steemann had a talk at Javascript Everywhere in Paris 2012 on Javascript in Arangodb an open source NoSQL database. With ArangoDB you can use Javascript and/or Ruby (mruby) as embedded language

TRANSCRIPT

Page 1: ArangoDB - Using JavaScript in the database

ArangoDB

/* Using Javascriptin the database */

Jan Steemann, triAGENS, Cologne

Page 2: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

ArangoDBbasics

Page 3: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

ArangoDB.explain()

{ "type": "NoSQL database", "openSource": true, "version": 1.0, "builtWith": [ "C", "C++", "js" ], "Javascript": [ "client side", "server side" ], "uses": [ "V8" ]}

Page 4: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Documentsand collections

ArangoDB is a database that works with documents

Documents are sets of name / value pairs (think of JSON objects), e.g.

{ "name": "Jan", "age": 37 }

Documents are organised in collections (like tables in relational databases)

Page 5: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Data types: JSON!

ArangoDB operates on JSON documents

JSON data can be saved as-is:

db.users.save({ "name": "Jan", "uses": [ "vi", "js", "C++" ]});

Page 6: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Schema-freeDocuments can be heterogenous – even in the same collection, e.g.

{ "name": "Jan", "age": 37 },{ "name": "Tim", "likes": [ "js" ] },{ "name": { "first": "Patrick", "last": "Star" }}

No need to care about schemas

Page 7: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

API & protocolsArangoDB server functionality is exposed via an HTTP REST API

ArangoDB answers HTTP queries, so it can be used from the browser

Simple to use from Javascript clients

(3rd party) drivers available for node.js, Ruby, PHP, ...

Javascript-enabled client shell provided

Page 8: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Querying

Page 9: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Querying is simple

Query a document by its unique id / key:db._document("users/9558905");

Query by providing an example:db.users.byExample({ "name": "Jan", "age": 37});

Page 10: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Indexes

ArangoDB allows querying on any document attributes or sub-attributes

Secondary indexes can be created for higher query performance:

• Hash indexes (equality queries)

• Skiplists (range queries)

• 2d geo indexes (location queries)

Page 11: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Geo index examples

// get b a r n ea r es t t o coor din a t edb.bars.near(48.51, 2.21).limit(1);

// get a ll s h op s w it h in r a diu s// a r ou n d coor din a t edb.shops.within(48.51, 2.21, rad);

Page 12: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

References

ArangoDB can store references between documents

References are documents with two endpoints (the documents they connect)

Reference documents can carry own attributes

Page 13: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

References exampleUsers documents:

{ "name": "Jan", "age": 37} { "name": "Tim", "city": "Paris"}

Relations documents:(references)

{ "_from": "users/jan", "_to": "users/tim", "type": "knows",}

// qu er y ou t b ou n d r ela t ion s f r om "J a n "db.relations.outEdges(jan); // => tim

Page 14: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Graph queries

References make it possible to process graphs with ArangoDB

Some special functions are provided to determine paths in a graph etc.

Can write own Javascript functions for special traversals

Page 15: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Query language

ArangoDB can also answer complexSQL-like queries

Such queries are expressed in ArangoDB's textual query language

This language allows joins, sub-queries, aggregation etc.

Page 16: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Example queryFOR u IN users FILTER u.addr.country == "US" && (u.isConfirmed == true || u.age >= 18) LET userLogins = ( FOR l IN logins FILTER l.userId == u.id RETURN l ) RETURN { "user": u, "logins": LENGTH(userLogins) }

S u b qu er y

R es u lt

F ilt er

It er a t or

Page 17: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Server side „actions“

Page 18: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Application server

ArangoDB can answer web requests directly

This also makes it an application server

Users can extend its functionality with server side Javascript „actions“

Actions are user-defined functions that contain custom business logic

Page 19: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Actions

Actions are bound to URLs and executed when URLs are called, e.g.

/users?name=... => function(...)/users/.../friends => function(...)

Page 20: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Actions examplefunction (req, res) { // get r equ es t ed u s er f r om d b , // n a m e is r ea d f r om U R L p a r a m et er var u = db.users.byExample({ "username": req.urlParameters.name });

// r em ov e p a s s w or d h a s h delete u.hashedPassword;

// b ef or e r et u r n in g t o ca ller actions.resultOk(req, res, 200, u);}

Page 21: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Actions – Use casesFilter out sensitive data before responding

Validate input

Check privileges

Check and enforce constraints

Aggregate data from multiple queries into a single response

Carry out data-intensive operations

Page 22: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Summary

Page 23: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

ArangoDB - summary

Flexible in terms of data modelling and querying

API is based on web standards:HTTP, REST, JSON

Easy to use from Javascript clients

Can be used as application server

Functionality can be extended with server side Javascript „actions“

Page 24: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Thanks!Any questions?

Grazie

¡Gracias!

Хвала

Ευχαριστώ

고마워

Danke

Спасибо

Teşekkürler

Merci!

Dank je wel

ありがとう

Tack så mycket

Page 25: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Give it a try!ArangoDB source repository: https://www.github.com/triAGENS/ArangoDB(use master branch)

Website: http://www.arangodb.org/

Builds available for Homebrew and several Linux distributions

Twitter: #arangodb, @arangodb

Google group: ArangoDB

Page 26: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Roadmap

MVCC / ACID transactions

Triggers

Synchronous and asynchronous replication

Page 27: ArangoDB - Using JavaScript in the database

www.arangodb.org © [email protected]

Stay in Touch:

• Fork me on github

• Google Group: ArangoDB

• Twitter: @steemann & @arangodb

• www.arangodb.org

Thank you!