Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a YAML Database?

Tags:

I like YAML's data model a lot, preserving most of the simplicity of JSON and extending it with a few sometimes-important features like custom data types and references.

So is there any way to store a lot of data in the YAML data model (or something very similar) and query it using an index? Like a YAML database, analogous to XML databases or JSON databases like Mongo DB. Or is there a mapper from YAML to something like Mongo DB that lets me transparently use it as a YAML store?

I haven't found anything so maybe there aren't any. Why would that be? Is it a stupid idea or is YAML simply too young and they will come?

like image 331
mb21 Avatar asked Feb 23 '13 15:02

mb21


People also ask

Can YAML be used as a database?

Yaml is rather used for config purposes. It is not used for database such as MongoDB because serialization takes longer than json .

Does MongoDB support YAML?

File Format MongoDB configuration files use the YAML format [1].

Does YAML have data types?

Basic Data Types YAML excels at working with mappings (hashes / dictionaries), sequences (arrays / lists), and scalars (strings / numbers). While it can be used with most programming languages, it works best with languages that are built around these data structure types.

Is YAML a data format?

YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain't markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.


1 Answers

Or is there a mapper from YAML to something like Mongo DB that lets me transparently use it as a YAML store?

Not directly as far as I know.

The main problem seems to be that Mongo DB's interface is based on a hash (i.e. key-value pairs). So in essence you are asking if there is a mapper from YAML to a hash. The answer is no simply because YAML can store arbitrary structures while hashes cannot.

But if you're willing to simplify your objects, you might be able to partially do this:

class SomeObject   def initialize     @bob = 'abc'     @fred = 'cde'   end    def to_hash     h = {}     instance_variables.sort.each do |v|     h[v] = instance_variable_get(v)   end   return h end 

If you look at the to_yaml code you'll see that it's very similar to the to_hash method (because that's where I got the idea).

Note you'll probably need a from_hash method as well. And you probably want to start working out a scheme for classes with instance variables that are objects (i.e. not just Strings, Symbols, etc.)

Now the mongo code to insert the item:

include Mongo db = MongoClient.new.db('test') stuff = db.collection('stuff') item = SomeClass.new() ... other code ... stuff.insert(item.to_hash) 

I realize this doesn't answer your question directly, but hopefully it still helps.

John

like image 123
JohnA Avatar answered Sep 21 '22 20:09

JohnA