Building nested JSON from graph objects

30 views
Skip to first unread message

Peter Brown

unread,
Dec 17, 2012, 11:24:00 AM12/17/12
to pacer...@googlegroups.com
Hi,

I'm building a JSON API on top of OrientDB using Pacer, and was curious if anyone had any suggestions for how to handle this situation. My application has 3 main types of objects, Project, Device, and Measurement. A project is the root of all relationships, and can have many devices. Each device can be attached to other devices through a similar "has many" relationship and every device has many measurements. If I were building this in a relational database with ActiveRecord, it would be something like this (ignoring the device-to-device relationship):

class Project < AR::Base
  has_many :devices
end

class Device < AR::Base
  has_many :measurements
  belongs_to :project
end

class Measurement < AR::Base
  belongs_to :device
end

Is there is a best practices way to represent this relationship as a nested JSON object? I'm new to graph databases and wasn't sure if I'm missing some fundamental concept with graphs or Pacer. Right now I'm handling it by defining an as_json method on both the Route and Vertex modules which just returns the properties object. Then in my controller, I'm basically merging all the objects together manually:

json = project.as_json.merge(
  devices: devices.to_a.map { |device|
    device.as_json.merge(measurements: device.measurements.to_a.as_json)
  }
)
Reply all
Reply to author
Forward
0 new messages