See lib/example_squeal.rb for the example.
Squealer is for standalone operation. Do not use it from within your application. To make the DSL easy to use, we alter
Hash#method_missing- You prefer dot notation. JSON uses dot notation. You are importing from a data store which represents collections as arrays of hashmaps. Dot notation for navigating those collections is convenient. If you use a field name that happens to be a method on Hash you will have to use index notation. (e.g.
kitten.toysis good, however
kitten.freezeis not good. Use
NilClass#each- As you are importing from schemaless repositories and you may be trying to iterate on fields that contain embedded collections, if a specific parent does not contain one of those child collections, the driver will be returning "nil" as the value for that field. Having
for a nil is convenient, semantically correct in this context, and removes the need for many nil checks in the block you provide to
#assign"keywords" are provided for convenience
Time#to_s- As you are exporting to a SQL database, we represent your timestamp in a format that it will parse unequivocally (mongodb stores all temporal data as a timestamp)
To run standalone, simply make your data squeal thusly:
where the squeal script requires 'squealer'.
Squealer doesn't use your application classes. It doesn't use your ActiveRecord models. It's an ETL tool. It could even be called a HRM (Hashmap-Relational-Mapper), but only in hushed tones in the corner boothes of dark pubs.
For now, this is specifically for MongoDB exporting to mySQL with the assumption that the data will be heavily denormalized - particularly that the hierarchy keys for embedded documents are flattened. This means that a document from
office.room.box will be exported to a record containing the
room and the
Since version 1.1, the primary key value is infered from the source document
_id field based on the
table_name argument matching the name of a variable holding the source document,
row_id is no longer a parameter on
target(). It will be invalid in version 1.3 and above.
The target SQL database must have no foreign keys (because it can't rely on the primary key values and referential integrity is the responsibility of the source data store or the application that uses it).
The target SQL database must use a primary key of char(24) with value of the MongoDB id.
It is assumed no indexes are present in the target database table (performance drag). You may want to create indexes for pulling data out of the database Squealer exports to. You should drop them again when you've done the pull.
The target row is inserted, or updated if present. We are using MySQL
INSERT ... UPDATE ON DUPLICATE KEY extended syntax to achieve this for now. This allows an event-driven update of exported data as well as a bulk batch process.