NoSequel
Today I gave a lightning talk at MountainWest RubyConf 2010 entitled “NoSequel”. The talk focused on showing off the capabilities of a new Sequel adapter I wrote for MongoDB, called sequel-mongo. MongoDB is a NoSQL document store, and sequel-mongo is the only adapter for Sequel that targets a NoSQL database.
Supporting NoSQL databases has never been a goal of Sequel, and it still isn’t. sequel-mongo is mostly a proof of concept to show how flexible Sequel is, and how Sequel is not tied to SQL. As long as the database supports the same kind of concepts that a standard SQL database supports, it’s likely that Sequel could support it, assuming someone coded an Sequel adapter for the database, which isn’t actually that much work. For example, sequel-mongo is about 200 lines of code.
So how does sequel-mongo work? Well, it overrides the insert, update, delete, count, and fetch_rows dataset methods to call methods of the Mongo::Connection object exposed by Mongo’s ruby API. Because Sequel represents SQL concepts as objects internally, instead of as SQL strings, it’s fairly easy to introspect the dataset’s internal object tree and map the SQL concepts to Mongo’s API. For example, selecting only certain columns, ordering by columns, and using a limit or offset, all work just fine.
What makes sequel-mongo special, is that it can use a large part of Sequel’s DSL for filtering datasets, and instead of compiling the filter to SQL, it compiles it to MongoDB’s javascript format. This allows you to use complex statements (AND/OR to any level of nesting), mathematical operators (e.g. +, -, «), and string operators (e.g. | , LIKE). It translates things like IS NULL and IN/NOT IN to appropriate javascript expressions. It even handles things like SQL CASE statements using nested javascript ternary operators, and simple SQL CASTs using javascript’s implicit casting. |
While sequel-mongo remains at a proof of concept stage, and I don’t currently have much interest in improving it further, it should give a good blueprint for how other NoSQL databases could be supported by Sequel.