Coder Social home page Coder Social logo

clj-orient's Introduction

clj-orient

clj-orient is a binding for the OrientDB document-graph NoSQL database management system. It is based on the OrientDB Java API.

Usage

Simply add this to your leiningen deps: [clj-orient "0.5.0"]

Documentation

The documentation can be found here: http://eduardoejp.github.com/clj-orient/ You can also check out the wiki at: https://github.com/eduardoejp/clj-orient/wiki

Structure

Currently the library is divided into 6 namespaces: core: The core functionality and the document-db functionality. graph: The graph-db functionality. object: The object-db functionality. query: Functionality for querying your database (either with native or SQL queries). schema: For easy definition of database schemas. script: For doing server-side scripting. script.clojure: For easy server-side scripting with Clojure.

Tests

There is a test suite in the test directory that right now tests almost all of the library functionality. It can be run using lein test.

Examples

Working with the database:

(use 'clj-orient.core)
; Opening the database as a document DB and setting the *db* var for global use.
; A database pool is used, to avoid the overhead of creating a DB object each time.
(set-db! (open-document-db! "remote:localhost/my-db" "writer" "writer"))

; Dynamically bind *db* to another DB.
; The DB is closed after all the forms are evaluated.
(with-db (open-document-db! "remote:localhost/another-db" "writer" "writer")
  (form-1 ...)
  (form-2 ...)
  (form-3 ...)
  ...
  (form-n ...))

; Close the DB
(close-db!)

; Using transactions (*db* must be bound to some database in the surrounding scope).
(with-tx
  (form-1 ...)
  (form-2 ...)
  (form-3 ...)
  ...
  (form-n ...))

Working with documents:

(use 'clj-orient.core)
(let [u (document :user {:first-name "Foo", :last-name "Bar", :age 10})
      u (assoc u :first-name "Mr. Foo", :age 20)]
  (save! u))

Working with classes:

(use '(clj-orient core graph))

(with-db (open-graph-db! "remote:localhost/my-graph-db" "writer" "writer")
  (create-class! :user)
  (derive! :user (vertex-base-class))
  (create-class! :knows (edge-base-class)))

Working with the graph-db:

(use 'clj-orient.graph)

(set-db! (open-graph-db! "remote:localhost/my-graph-db" "writer" "writer"))
(let [a (save! (vertex :user {:first-name "John", :last-name "Doe", :age 20, :country "USA"}))
      b (save! (vertex :user {:first-name "Jane", :last-name "Doe", :age 25, :country "USA"}))]
  (save! (link! a :knows {:since (java.util.Date.)} b)))

Querying a Database:

(use 'clj-orient.query)
(native-query :user {:country "USA", :age [:$>= 20], :first-name [:$like "J%"]})

(sql-query "SELECT FROM user WHERE country = :country AND age >= :age AND first-name LIKE :fname LIMIT 10"
           {:country "USA", :age 20, :fname "J%"})

; Pagination can be activated by passing a third optional boolean value of true.
; That will query the next X elements each time the seq runs out until there are no more results.
(sql-query "SELECT FROM user LIMIT 10" nil true)

(clj-query '{:from user :where [(= country ?country) (>= age ?age) (like? first-name ?fname)] :limit 10}
           {:country "USA", :age 20, :fname "J%"})

Hooks:

(use 'clj-orient.core)
(defhook my-hook
      (after-create [document]
        (println "Created new document:" document)))

(add-hook! my-hook)

Please note: This is not a comprehensive guide. Please read the library documentation to know what functions and macros are available.

clj-orient's People

Contributors

eduardoejp avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.