You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.9 KiB

# node-sql
_sql string builder for node_
12 years ago
Let's be honest - hand generating SQL is no fun, especially in a language which has clumsy support for multi-line strings.
But SQL is so good. We need SQL. So let's save the day and generate it with a jQuery-esque fluent interface.
Maybe it's still not fun, but at least it's _less not fun_. That's at least worth a participation award.
[![Build Status](https://secure.travis-ci.org/brianc/node-sql.png)](http://travis-ci.org/brianc/node-sql)
## examples
```js
//require the module
var sql = require('sql');
12 years ago
//first we define our tables
var user = sql.define({
name: 'user',
columns: ['id', 'email', 'lastLogin']
});
12 years ago
var post = sql.define({
name: 'post',
columns: ['id', 'userId', 'date', 'title', 'body']
})
//now let's make a simple query
var query = user.select(user.star()).from(user).toQuery();
console.log(query.text); //SELECT "user".* FROM "user"
//something more interesting
var query = user
.select(user.id)
.from(user)
.where(
user.name.equals('boom').and(user.id.equals(1))
12 years ago
).or(
user.name.equals('bang').and(user.id.equals(2))
).toQuery();
12 years ago
//query is parameterized by default
console.log(query.text); //SELECT "user"."id" FROM "user" WHERE ((("user"."name" = $1) AND ("user"."id" = $2)) OR (("user"."name" = $3) AND ("user"."id" = $4)))
12 years ago
console.log(query.values); //['boom', 1, 'bang', 2]
12 years ago
//how about a join?
var query = user.select(user.name, post.content)
.from(user.join(post).on(user.id.equals(post.userId))).toQuery();
12 years ago
console.log(query.text); //'SELECT "user"."name", "post"."content" FROM "user" INNER JOIN "post" ON ("user"."id" = "post"."userId")'
```
There are a __lot__ more examples included in the `test` folder.
12 years ago
## contributing
12 years ago
I __love__ contributions. If I could, I would write __love__ 500 times, but that would be readme bloat.
Still, that's how much I love them. Let's work _together_ on this.
If you want to contribute here's what you do:
12 years ago
1. fork the repo
2. `git pull https://github.com/(your_username)/node-sql`
12 years ago
3. `cd node-sql`
4. `npm install`
5. `npm test`
At this point the tests should pass for you. If they don't pass please open an issue with the output or you can even send me an email directly. My email address is on my github profile and also on every commit I contributed in the repo.
12 years ago
Once the tests are passing, modify as you see fit. _Please_ make sure you write tests to cover your modifications. Once you're ready, commit your changes and submit a pull request.
12 years ago
__As long as your pull request doesn't have completely off-the-wall changes and it does have tests I will almost always merge it right away and push it to npm__
12 years ago
If you think your changes are too off-the-wall, open an issue or a pull-request without code so we can discuss them.
12 years ago
__Seriously:__
12 years ago
your contributions >= my contributions
13 years ago
12 years ago
I definitely need help with mysql and sqlite syntax. I'm not very familiar...so that's always a good place to start.
13 years ago
##license
MIT