Document-oriented DataBase. A full Crystal database management without SQL, storing data in plain files.
Go to file
2020-02-11 16:53:06 +01:00
spec Adds tests for DODB::Index(V)#update(String, V). 2020-01-30 03:23:02 +01:00
src Partition#delete(partition_index, &matcher) 2020-02-11 16:53:06 +01:00
README.md README update. 2020-01-05 13:54:03 +01:00
shard.yml Name change. 2019-12-11 23:18:20 +01:00

dodb.cr

DODB stands for Document Oriented DataBase.

Installation

Add the following to your shard.yml. You may want to add version informations to avoid unexpected breakages.

dependencies:
    dodb:
        git: https://git.karchnu.fr/WeirdOS/dodb.cr

Usage

db = DODB::DataBase(Thing).new "path/to/storage/directory"

db << Thing.new

db.each do |thing|
	pp! thing
end
# First, we define the thing well want to store.
# It *has* to be serializable through JSON, as everything in DODB is stored in JSON.
class Thing
	include JSON::Serializable

	property id       : String
	property category : String # In this example well assume a unique category.
	property tags     : Array(String)

	def initialize(@id, @category, @tags)
	end
end

# Then we create our database.
things = DODB::DataBase(Thing).new "path/to/storage/directory"

# Then we define indices to it. There are several ways to index things in DODB.
# Indices are the simplest way to do so. They represent attributes that are
# unique in the collection. They are “1-1” associations.
things_by_id = things.new_index "id", &.id

# Partitions represent attributes that are shared in the collection. They can
# be used to obtain entries grouped by value. They are “1-n” associations.
things_by_category = things.new_partition "category", &.category

# Tags are “n-n associations”.
things_by_tags = things.new_tags "tags", &.tags

# At this point, we can add or try to access data.
things << Thing.new "one",   "word", ["number"] of String
things << Thing.new "two",   "word", ["number"] of String
things << Thing.new "three", "word", ["number"] of String
things << Thing.new "hello, world", "sentence", [] of String

things_by_tags.get "number" # Will return an array of three things ("one", "two", "three").
things_by_category.get "sentence" # Will return an array of one thing ("hello, world")
things_by_id.get "one" # Will return a single thing ("one")