You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Glynn Bird <gl...@gmail.com> on 2017/06/12 11:49:25 UTC

SQL -> Mango converter

I thought I'd share a small JavaScript library I wrote the other day:
https://www.npmjs.com/package/sqltomango

It's mission is very simple: it takes an SQL query e.g.

  SELECT _id, age, breed FROM dogs WHERE owner = 'glynn' OR (name='towser'
AND colour='white') ORDER BY age DESC LIMIT 500,1500

parses it and returns the CouchDB Mango equivalent:


{"fields":["_id","age","breed"],"selector":{"$or":[{"owner":{"$eq":"glynn"}},{"$and":[{"name":{"$eq":"towser"}},{"colour":{"$eq":"white"}}]}]},"sort":[{"age":"desc"}],"limit":1500,"skip":500}

It doesn't magically make CouchDB support joins, unions, transactions etc,
but it is useful as a means of translating queries for new users.

I wrote it for a blog post for my developer advocacy work, but it's free,
open-source (Apache license obviously) and ready to go for anyone else to
build with or to extend.

Glynn

Re: SQL -> Mango converter

Posted by "Johannes J. Schmidt" <sc...@netzmerk.com>.
Nice one!


On 06/12/2017 01:49 PM, Glynn Bird wrote:
> I thought I'd share a small JavaScript library I wrote the other day:
> https://www.npmjs.com/package/sqltomango
>
> It's mission is very simple: it takes an SQL query e.g.
>
>    SELECT _id, age, breed FROM dogs WHERE owner = 'glynn' OR (name='towser'
> AND colour='white') ORDER BY age DESC LIMIT 500,1500
>
> parses it and returns the CouchDB Mango equivalent:
>
>
> {"fields":["_id","age","breed"],"selector":{"$or":[{"owner":{"$eq":"glynn"}},{"$and":[{"name":{"$eq":"towser"}},{"colour":{"$eq":"white"}}]}]},"sort":[{"age":"desc"}],"limit":1500,"skip":500}
>
> It doesn't magically make CouchDB support joins, unions, transactions etc,
> but it is useful as a means of translating queries for new users.
>
> I wrote it for a blog post for my developer advocacy work, but it's free,
> open-source (Apache license obviously) and ready to go for anyone else to
> build with or to extend.
>
> Glynn
>


Re: SQL -> Mango converter

Posted by Jan Lehnardt <ja...@apache.org>.
Nice one, I’ve wanted to do one of these for the longest time :)

Best
Jan
--

> On 12. Jun 2017, at 13:49, Glynn Bird <gl...@gmail.com> wrote:
> 
> I thought I'd share a small JavaScript library I wrote the other day:
> https://www.npmjs.com/package/sqltomango
> 
> It's mission is very simple: it takes an SQL query e.g.
> 
>  SELECT _id, age, breed FROM dogs WHERE owner = 'glynn' OR (name='towser'
> AND colour='white') ORDER BY age DESC LIMIT 500,1500
> 
> parses it and returns the CouchDB Mango equivalent:
> 
> 
> {"fields":["_id","age","breed"],"selector":{"$or":[{"owner":{"$eq":"glynn"}},{"$and":[{"name":{"$eq":"towser"}},{"colour":{"$eq":"white"}}]}]},"sort":[{"age":"desc"}],"limit":1500,"skip":500}
> 
> It doesn't magically make CouchDB support joins, unions, transactions etc,
> but it is useful as a means of translating queries for new users.
> 
> I wrote it for a blog post for my developer advocacy work, but it's free,
> open-source (Apache license obviously) and ready to go for anyone else to
> build with or to extend.
> 
> Glynn

-- 
Professional Support for Apache CouchDB:
https://neighbourhood.ie/couchdb-support/