Giter VIP home page Giter VIP logo

rel's Introduction

Rel

https://github.com/yang/rel

Description

Rel is a SQL AST manager for Node JS. It is a straight port of https://github.com/rails/arel. Although it does have some changes of note. These are:

  1. No reliance on a database connection. This library builds queries only.
  2. It obviously can't use all of the ruby-isms like over-riding the array operator so methods are used instead.

It still holds the same goals as Arel which are:

  1. Simplifies the generation complex of SQL queries.
  2. Adapts to various RDBMS systems

Before you ask, there will also be a port of ActiveRecord coming in the next little bit.

Installation

npm install rel

Unfortunately the package is currently purely Coffeescript, with no accompanying JS compilation in the git repo. To use this with a JS project, you can compile the files in-place with:

npm run build

This is a bandaid for the time being.

Introduction

users = new Rel.Table 'users'
users.project(Rel.star()).toSql()

Will produce

SELECT * FROM users

A more complicated example of command queries is:

users.where(users.column('name').eq('amy'))
# => SELECT * FROM users WHERE users.name = 'amy'

In SQL the selection would contain what you are getting from the database, this is called a projection in Rel.

users.project(users.column('id')) # => SELECT users.id FROM users

Joins resemble SQL:

users.join(photos).on(users.column('id').eq(photos.column('user_id')))
# => SELECT * FROM users INNER JOIN photos ON users.id = photos.user_id

Limit and offset and called take and skip:

users.take(5) # => SELECT * FROM users LIMIT 5
users.skip(4) # => SELECT * FROM users OFFSET 4

GROUP BY is called group:

users.group(users.column('name')) # => SELECT * FROM users GROUP BY name

You can chain all operators, for example:

users.where(users.column('name').eq('amy')).project(users.column('id'))
# => SELECT users.id FROM users WHERE users.name = 'amy'

Another example:

users.where(users.column('name').eq('bob')).where(users.column('age').lt(25))

You can also pass in multiple arguments:

users.where(users.column('name').eq('bob'), users.column('age').lt(25))

The OR operator works like this:

users.where(users.column('name').eq('bob').or(users.column('age').lt(25)))

This is the same as the AS operator.

Contributors

Founded and majority of development to date by Carl Woodward (@carlwoodward).

Current developer and maintainer is Yang Zhang (@yang).

Development

To run tests, run npm test.

Setup notes

rel's People

Contributors

cjwoodwardtest avatar yang avatar carlwoodward avatar zacheryph avatar

Stargazers

曹文忠 avatar Josh Ferguson avatar Chen Yangjian avatar Angus H. avatar Florin Gogianu avatar Carter Cole avatar John Woo avatar Jakob Cosoroabă avatar  avatar Ali Sabil avatar luigi avatar Brant Watrous avatar Ben Smith avatar  avatar Jonathan Clem avatar Truong Hoang Dung avatar Viktor Evdokimov avatar Noah H. Smith avatar Michael Birk avatar  avatar Philipp Waldmann avatar Chao Gao avatar Hung Tran avatar Justin Butkowski avatar j-j.eth avatar Stefaney Roberts avatar Tolga Tezel avatar Sundarram P.V. (PVS) avatar Rodolfo Wilhelmy avatar  avatar Ben D'Angelo avatar Austin avatar  avatar Damon Oehlman avatar alan kaiser avatar Leon Guan avatar  avatar Michael Bearne avatar bengxia avatar Pete Hawkins avatar Philipp Borgers avatar Tavi avatar Rafa Leblic avatar Askar Yusupov avatar Breno Santos Salgado avatar Tarkus avatar  avatar Jens Grassel avatar Marco Süß avatar Max Brunsfeld avatar Jake Harding avatar Charles Ginzel avatar Sam Hon avatar xingkui wang avatar Philip Cole avatar Stanislaw Pusep avatar tjcjc avatar Adil Wali avatar Drew Purdy avatar Yuya Nishiyama avatar Craig Weber avatar Chandra Patni avatar Rafael Santos avatar  avatar Steve Lloyd avatar Lalit Kapoor avatar Dimitar Dimitrov avatar Thierry Henrio avatar Marlon Gomes Lopes avatar Drew LeSueur avatar Kris Williams avatar Marco Rogers avatar Sean Goheen avatar Mike Smullin avatar Tim Tyrrell avatar Demis Bellot avatar Lucas Caton avatar Gabriel de Oliveira Barbosa avatar Jean Carlo Nascimento avatar Takuya Kato / katton avatar Allen Madsen avatar Emerson Macedo avatar Mike Frey avatar Janne Hietamäki avatar Matteo Collina avatar Benjamin Thomas avatar Pierre Haufe avatar Stone Gao avatar Peter Wooley avatar  avatar Jim Newbery avatar Chris Hamant avatar Nicolas Dillmann avatar Thibaut Assus avatar Emelia Smith avatar Lance Pollard avatar Kevin van Zonneveld avatar Marc Harter avatar Juan Basso avatar travis avatar

Watchers

James Cloos avatar

rel's Issues

Error: Cannot find module 'rel'

I npm installed rel, ran a program, got the following error:

Error: Cannot find module 'rel'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)

parameterized sql generation

PostgreSQL recommends using parameterized queries. Would love to see this:

var result = users.where(users.column('name').eq('amy')).toSqlWithParameters();
result.text = "select * from users where name = $1"
result.parameters = ['amy']

Would need different parameter separators for postgres and mysql unfortunately, as postgres uses $<n> and mysql uses ?

Are you interested in this feature?

Drop CoffeeScript

Hey @yang!

Would you be open to a series of patches that moves rel away from CoffeeScript? I'm neither pro nor con CoffeeScript in general, but I don't like that libraries that want to make use of rel have to pull CoffeeScript in as a dependency.

I'd be happy to work on the conversion, if you're open to accepting patches for that.

update npm version

Can't build the npm version of rel. Please update! The npm version misses the right package.json file.

Does npm build the coffeescript files if like rel?

JS Proxy Objects instead of methods

Actually you could easily bring the whole ruby syntax in using proxy objects and getters/setters.

Could be a completely different module. But if queries are built only once (performance) and you want a cool ruby-like syntax it's actually doable.

To have a bettter insight search for Harmony Proxies and node specific API

MySQL Problem

I'm creating a simple select statement with rel and trying to execute it on MySQL but I can't seem to get it to work.
It seems to have to do with rel's use of double quotes everywhere.
My simple rel code is mytable.project(rel.star()).where(mytable.column('name').eq('test')).toSql() and I get a SQL statement of SELECT * FROM "mytable" WHERE "mytable"."name" = "test"
MySQL gives me an error of Unknown column 'test' in 'where clause'
Is there anything I can do to resolve this? I've tried setting the sql_mode in MySQL to just about every possible mode but nothing works.

stored procedures

Hey,

is it possible to use stored procedures to write code like this:

SELECT Y(nodes.geom) AS lat, X(nodes.geom) AS lon FROM nodes

currently i do this

(new Rel.Table('nodes'))
  .project('Y(nodes.geom) as lat')
  .project('X(nodes.geom) as long')

Am I missing a feature or are stored procedures with Tables not implemented?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.