Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

diametric

> 编程语言
Open source

Diametric is a library for building schemas, queries, and transactions for Datomic from Ruby objects.

168 stars0 likes1 views
WebsiteGitHub

About

Diametric is a library for building schemas, queries, and transactions for Datomic from Ruby objects.

Diametric

Diametric is ActiveRecord style wrapper for Datomic, and a library for building schemas, queries, and transactions for Datomic from Ruby objects. It is also used to map Ruby objects as entities into a Datomic database.

Diametric supports both CRuby and JRuby. When Diametric is used on CRuby, Diametric connects to Datomic's REST service. Using Datomic's REST API, Diametric creates schema/data and makes a queries to Datomic. When Diametric is used on JRuby, both Datomic's REST and Peer services are supported. The core parts of Peer service are implemented using Datomic API.

For Rubyists who are familiar with object oriented programming, Diametric converts:

  • Ruby model definition to Datomic schema
  • Creating, saving and updating model data to Datomic transaction data
  • Ruby query to Datomic's datalog query
  • Datomic's dbid to model object (depends on the value of resolve option)

The biggest difference between REST and Peer services is available database types. REST service can use memory database only, which means we can't save data in any file as long as using free version. While Peer service can use memory and free version of transactor, which means we can save data in a file through Datomic's transactor. Other than that, Peer service has an excellent API to use Datomic's various features.

Installation

bash
gem install diametric

Diametric is a multi-platform gem. On CRuby, the above installs the CRuby version of the gem. On JRuby, the above installs the JRuby version of gem, with a Java extension.

[note] When you install diametric on JRuby, you'll see a message, "Building native extensions. This could take a while...." Although Diametric doesn't rely on a C library, it depends on several Java libraries. While installing gem, diametric downloads those jar archives, including dependencies on your local maven repo. The message shows up because of this.

Preparation for CRuby

On CRuby, you need to start Datomic's REST server.

Diametric has a command to start REST server. Type datomic-rest -p port -a db_alias -u uri, for example

datomic-rest -p 9000 -a free -u datomic:mem://

When you run the command at the very first time, it takes a while to start running. This is because Diametric downloads datomic if it doesn't exist in Diametric's directory tree. If it is the second time or later, the REST server starts quickly.

To learn more about the options in the above command, please go to http://docs.datomic.com/rest.html.

Once the REST server starts running, go to localhost:9000 on your browser. You can see Datomic's REST service is running.

Alternatively, you can download Datomic archive from https://my.datomic.com/downloads/free, and start REST server using the Datomic command, script/datomic-rest -p 9000 free datomic:mem://

Preparation for JRuby

You have 2 choices on JRuby, Peer and REST services. Peer service works on the same JVM as JRuby and looks like just a Java library for JRuby.

When you choose REST service, follow Preparation for CRuby section.

When you choose Peer service, you don't need to prepare anything. You even don't need to start Datomic. Diametric does everything for you. What you need to do is just coding in Ruby using Diametric API.

Although Diametric API makes coding very easy, you still have the freedom to hit Datomic's Java API directly.

Typical coding steps

Typical Diametric coding follows the steps below:

  1. Connect to Datomic
  2. Define entities
  3. Create schema on Datomic from entity definitions
  4. Create entities and save it on Datomic
  5. Make a query to Datomic

Connect to the Datomic REST Service

To establish a connection to Datomic REST Service, you can do as such:

ruby
require 'diametric'
Diametric::Persistence.establish_base_connection({:uri => 'http://localhost:9000', :storage => 'free', :database => 'sample'})

Optionally, you can connect using datomic-client gem:

ruby
require 'datomic/client'

@datomic_uri = 'http://localhost:9000'
@storage = 'free'
@dbname = "sample"
@client = Datomic::Client.new @datomic_uri, @storage
@client.create_database(@dbname)

Each parameter should be consistent to the ones used to start the REST service.

Connect to the Datomic Peer service (JRuby only)

ruby
require 'diametric'
Diametric::Persistence.establish_base_connection({:uri=>'datomic:mem://sample'})

Or in a peer service specific way,

ruby
datomic_uri = "datomic:mem://sample-#{SecureRandom.uuid}"
@conn = Diametric::Persistence::Peer.connect(datomic_uri)

Define an entity

While a relational databse inserts a record, Diametric saves an Entity. To save data by Diametric, you need to define Entity first. Defining entities loosely corresponds to defining database tables.

Below is an example of Entity definition for Peer service. Be aware, you should include Diametric::Entity and either one of Diametric::Persistence::REST or Diametric::Persistence::Peerin your Entity definition.

ruby
require 'diametric'

class Person
  include Diametric::Entity
  include Diametric::Persistence::Peer

  attribute :name, String, index: true
  attribute :birthday, DateTime
  attribute :awesomeness, Boolean, doc: "Is this person awesome?"
end

The attribute definition is consists of:

  • attribute name (required)
  • attribute type (required)
  • attribute options (optional)

This is the Diametric way of converting Datomic schema to a Ruby model definition.

The attribute name goes to :db/ident and the value will be :/. This is interpreted as :/ on Datomic.

Currently, Diametric supports the following data types:

Diametric Datomic
String :db.type/string
Symbol :db.type/keyword
Boolean :db.type/boolean
Integer :db.type/long
Float :db.type/double
Double :db.type/double
BigDecimal :db.type/bigdec
Ref :db.type/ref
DateTime :db.type/instant
UUID :db.type/uuid

In addition, Diametric supports an Enum type.

Available options are:

Diametric Datomic
:cardinality => :one (default) :db.cardinality/one
:cardinality => :many :db.cardinality/many
:doc => "some document here" :db/doc
:unique => :identity :db.unique/identity
:unique => :value :db.unique/value
:index => true :db/index
:fulltext => true :db/fulltext

Create a schema

Creating schema in Datomic is similar to a migration in Ruby on Rails. However, schema creation can be done as a part of a program. For Datomic, the difference between schema and entity is quite small. Like saving entities, schemas are saved in Datomic.

Creating schema is done by calling create_schema method of the entity.

ruby
Person.create_schema.get

Person.create_schema (not followed by get) also creates a schema, but it is a sort of piling up on the queue and not performed immediately. If get is not specified, schema creation is done by at some point implicitly. Adding get requests Datomic to create schema now. This is good to know whether the entity definition is correct or not. If some attribute definition has a problem, error will be raised.

Create, save and update an entity

Once the entity is defined, and schema is created on Datomic, you can create an entity instance like you create a new object in Ruby. Below is an example:

ruby
person = Person.new
person.name = "Sky"
person.birthday = DateTime.parse('2005-01-01')
person.awesomeness = true

puts "dbid (before save): #{person.dbid}"

person.save

puts "dbid (after save): #{person.dbid}"

The above prints:

dbid (before save): 
dbid (after save): 17592186045418

The dbid is equivalent to id of ordinary ActiveModel objects. Before save, the person object doesn't have dbid value. But after save, it will have an auto-generated value as dbid.

Alternatively, you can create an entity in one line:

ruby
Person.new(name: "Sky", birthday: DateTime.parse('2005-01-01'), awesomeness: true).save

To update attribue values of the instance, use update_attributes:

ruby
person.update_attributes(name: "Sky Jr.", awesomeness: false)

Make queries

Queries are done by Diametric::Query instance. A basic usage is:

  1. Create Diametric::Query instance with a required argument.
  2. Call methods of Query instance. This triggers a query to Datomic.
  3. Iterate the query result and do something to get data.

Diametric::Query has three arguments in constructor:

ruby
Diametric::Query.new(entity_class, db_or_conn=nil, resolve=false)

The second and the third options work only on Peer service.

On REST service, the query results are an array of entity objects. On Peer service, the result data type depends on the constructor arguments. In default setting, the query result is a Set of Arrays. Each array has an entity id (future Diametric version will have a couple of attributes depends on the query). This data structure is exactly the same as what Datomic returns. Also, to avoid the overhead that comes from converting to Ruby's Set and Arrays, Diametric wraps in Diametric::Persistence::Set or Diametric::Persistence::Collection. (Diametric's wrappers are not a perfect Ruby Array nor Ruby Set in this version. Those will be improved in future versions.)

The reason Diametric doesn't create entity instances by default is to save memory. When dealing with millions or billions of data, saving memory is key to running faster, and, more importantly, to avoid OutOfMemoryError. In terms of saving memory, Diametric returns minimum by default.

If you don't have a lot of data and want to get instances directly from query results, set true to the third option, resolve.

When resolve option is set to true, the query result will be an Array of entity instances. You should be careful when setting this option to true. When it is true, Diametric tries to create instances recursively following associations. If friends or friends of friends includes self, you'll likely to get StackOverflowError.

To resolve an entity from a dbid, use Entity's reify method. The reify method creates an instance from the dbid.

The second option, db_or_conn is to specify the database state at some time in the past. If it is not specified, Diametric uses the default database state, which means the latest state.

find all

Query example1:

…

Above prints:

ruby
name: Rain, dbid: 17592186045429, birthday: 2005-05-04 20:00:00 -0400, awesomeness: true
name: Breeze, dbid: 17592186045425, birthday: 1911-03-22 19:00:00 -0500, awesomeness: true
name: Sleet, dbid: 17592186045427, birthday: 2010-04-29 20:00:00 -0400, awesomeness: false
name: Sun, dbid: 17592186045421, birthday: 2004-12-31 19:00:00 -0500, awesomeness: true
name: Cloud, dbid: 17592186045423, birthday: 1980-02-11 19:00:00 -0500, awesomeness: false

Query example2 (with resolve to true):

ruby
query = Diametric::Query.new(Person, nil, true)
query.each do |entity|
  puts "name: #{entity.name}, dbid: #{entity.dbid}, birthday: #{entity.birthday}, awesomeness: #{entity.awesomeness}"
end

The example2 prints exactly the same as the example1.

where

To narrow down the query results, you can use where and filter methods.

Query example3:

ruby
query = Diametric::Query.new(Person, nil, true).where(awesomeness: true)
query.each do |entity|
  puts "name: #{entity.name}, dbid: #{entity.dbid}, birthday: #{entity.birthday}, awesomeness: #{entity.awesomeness}"
end

Above print

Issues· 25 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言