sql
sql builder
Last updated 9 years ago by brianc .
Repository · Bugs · Original npm · Tarball · package.json
$ npm install sql 
SYNC missed versions from official npm registry.

node-sql

sql string builder for node - supports PostgreSQL, mysql, Microsoft SQL Server, Oracle and sqlite dialects.

Building SQL statements by hand is no fun, especially in a language which has clumsy support for multi-line strings.

So let's build it with JavaScript.

Maybe it's still not fun, but at least it's less not fun.

Build Status

install

$ npm install sql

use

//require the module
var sql = require('sql');

//(optionally) set the SQL dialect
sql.setDialect('postgres');
//possible dialects: mssql, mysql, postgres (default), sqlite

//first we define our tables
var user = sql.define({
  name: 'user',
  columns: ['id', 'name', 'email', 'lastLogin']
});

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))
    ).or(
      user.name.equals('bang').and(user.id.equals(2))
    ).toQuery();

//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)))

console.log(query.values); //['boom', 1, 'bang', 2]

//queries can be named
var query = user.select(user.star()).from(user).toNamedQuery('user.all');
console.log(query.name); //'user.all'

//how about a join?
var query = user.select(user.name, post.body)
  .from(user.join(post).on(user.id.equals(post.userId))).toQuery();

console.log(query.text); //'SELECT "user"."name", "post"."body" FROM "user" INNER JOIN "post" ON ("user"."id" = "post"."userId")'

//this also makes parts of your queries composable, which is handy

var friendship = sql.define({
  name: 'friendship',
  columns: ['userId', 'friendId']
});

var friends = user.as('friends');
var userToFriends = user
  .leftJoin(friendship).on(user.id.equals(friendship.userId))
  .leftJoin(friends).on(friendship.friendId.equals(friends.id));

//and now...compose...
var friendsWhoHaveLoggedInQuery = user.from(userToFriends).where(friends.lastLogin.isNotNull());
//SELECT * FROM "user"
//LEFT JOIN "friendship" ON ("user"."id" = "friendship"."userId")
//LEFT JOIN "user" AS "friends" ON ("friendship"."friendId" = "friends"."id")
//WHERE "friends"."lastLogin" IS NOT NULL

var friendsWhoUseGmailQuery = user.from(userToFriends).where(friends.email.like('%@gmail.com'));
//SELECT * FROM "user"
//LEFT JOIN "friendship" ON ("user"."id" = "friendship"."userId")
//LEFT JOIN "user" AS "friends" ON ("friendship"."friendId" = "friends"."id")
//WHERE "friends"."email" LIKE %1

//Using different property names for columns
//helpful if your column name is long or not camelCase
var user = sql.define({
  name: 'user',
  columns: [{
      name: 'id'
    }, {
      name: 'state_or_province',
      property: 'state'
    }
  ]
});

//now, instead of user.state_or_province, you can just use user.state
console.log(user.select().where(user.state.equals('WA')).toQuery().text);
// "SELECT "user".* FROM "user" WHERE ("user"."state_or_province" = $1)"

There are a lot more examples included in the test/dialects folder. We encourage you to read through them if you have any questions on usage!

from the command line

You can use the sql-generate module to automatically generate definition files from a database instance. For example, running node-sql-generate --dsn "mysql://user:password@host/database" will generate something similar to:

// autogenerated by node-sql-generate v0.0.1 on Tue May 21 2013 01:04:12 GMT-0700 (PDT)
var sql = require('sql');

/**
 * SQL definition for database.bar
 */
exports.bar = sql.define({
    name: 'bar',
    columns: [
        'id',
        'foo_id'
    ]
});

/**
 * SQL definition for database.foo
 */
exports.foo = sql.define({
    name: 'foo',
    columns: [
        'id',
        'field_1',
        'foo_bar_baz'
    ]
});

/**
 * Adding a column to an existing table:
 */
var model = sql.define({ name: 'foo', columns: [] });
model.addColumn('id');

// If you try to add another column "id", node-sql will throw an error.
// You can suppress that error via:
model.addColumn('id', { noisy: false });

Read the module's documentation for more details.

contributing

We love contributions.

node-sql wouldn't be anything without all the contributors and collaborators who've worked on it. If you'd like to become a collaborator here's how it's done:

  1. fork the repo
  2. git pull https://github.com/(your_username)/node-sql
  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.

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.

As long as your pull request doesn't have completely off-the-wall changes and it does have tests we will almost always merge it and push it to npm

If you think your changes are too off-the-wall, open an issue or a pull-request without code so we can discuss them before you begin.

Usually after a few high-quality pull requests and friendly interactions we will gladly share collaboration rights with you.

After all, open source belongs to everyone.

license

MIT

Current Tags

  • 0.78.0                                ...           latest (7 years ago)

101 Versions

  • 0.78.0                                ...           7 years ago
  • 0.77.0                                ...           7 years ago
  • 0.76.1                                ...           8 years ago
  • 0.76.0                                ...           8 years ago
  • 0.75.0                                ...           8 years ago
  • 0.74.0                                ...           8 years ago
  • 0.73.0                                ...           8 years ago
  • 0.72.0                                ...           8 years ago
  • 0.71.0                                ...           8 years ago
  • 0.70.1                                ...           8 years ago
  • 0.70.0                                ...           9 years ago
  • 0.69.0                                ...           9 years ago
  • 0.68.0                                ...           9 years ago
  • 0.67.0                                ...           9 years ago
  • 0.66.0                                ...           9 years ago
  • 0.65.0                                ...           9 years ago
  • 0.64.1                                ...           9 years ago
  • 0.64.0                                ...           9 years ago
  • 0.63.1                                ...           9 years ago
  • 0.63.0                                ...           9 years ago
  • 0.62.0                                ...           9 years ago
  • 0.61.0                                ...           9 years ago
  • 0.60.0                                ...           9 years ago
  • 0.59.0                                ...           9 years ago
  • 0.58.0                                ...           9 years ago
  • 0.57.0                                ...           9 years ago
  • 0.56.0                                ...           9 years ago
  • 0.55.0                                ...           9 years ago
  • 0.54.0                                ...           9 years ago
  • 0.52.0                                ...           10 years ago
  • 0.51.0                                ...           10 years ago
  • 0.50.0                                ...           10 years ago
  • 0.49.0                                ...           10 years ago
  • 0.48.0                                ...           10 years ago
  • 0.47.0                                ...           10 years ago
  • 0.46.0                                ...           10 years ago
  • 0.45.3                                ...           10 years ago
  • 0.45.2                                ...           10 years ago
  • 0.45.1                                ...           10 years ago
  • 0.45.0                                ...           10 years ago
  • 0.44.0                                ...           10 years ago
  • 0.43.2                                ...           10 years ago
  • 0.43.1                                ...           10 years ago
  • 0.43.0                                ...           10 years ago
  • 0.42.0                                ...           10 years ago
  • 0.41.0                                ...           10 years ago
  • 0.40.0                                ...           10 years ago
  • 0.39.0                                ...           11 years ago
  • 0.38.0                                ...           11 years ago
  • 0.37.0                                ...           11 years ago
  • 0.36.0                                ...           11 years ago
  • 0.35.0                                ...           11 years ago
  • 0.34.0                                ...           11 years ago
  • 0.33.0                                ...           11 years ago
  • 0.32.0                                ...           11 years ago
  • 0.31.0                                ...           11 years ago
  • 0.30.0                                ...           11 years ago
  • 0.29.0                                ...           11 years ago
  • 0.28.0                                ...           11 years ago
  • 0.26.0                                ...           11 years ago
  • 0.25.0                                ...           11 years ago
  • 0.24.0                                ...           12 years ago
  • 0.23.0                                ...           12 years ago
  • 0.22.0                                ...           12 years ago
  • 0.21.0                                ...           12 years ago
  • 0.20.0                                ...           12 years ago
  • 0.19.0                                ...           12 years ago
  • 0.18.0                                ...           12 years ago
  • 0.17.0                                ...           12 years ago
  • 0.16.0                                ...           12 years ago
  • 0.15.0                                ...           12 years ago
  • 0.14.0                                ...           12 years ago
  • 0.13.0                                ...           12 years ago
  • 0.12.0                                ...           12 years ago
  • 0.11.0                                ...           12 years ago
  • 0.10.0                                ...           12 years ago
  • 0.9.0                                ...           12 years ago
  • 0.8.2                                ...           12 years ago
  • 0.8.1                                ...           12 years ago
  • 0.8.0                                ...           12 years ago
  • 0.7.1                                ...           12 years ago
  • 0.7.0                                ...           12 years ago
  • 0.6.0                                ...           12 years ago
  • 0.5.0                                ...           12 years ago
  • 0.4.0                                ...           12 years ago
  • 0.3.0                                ...           12 years ago
  • 0.2.4                                ...           12 years ago
  • 0.2.3                                ...           12 years ago
  • 0.2.2                                ...           12 years ago
  • 0.2.1                                ...           12 years ago
  • 0.1.6                                ...           12 years ago
  • 0.2.0                                ...           12 years ago
  • 0.1.5                                ...           12 years ago
  • 0.1.4                                ...           12 years ago
  • 0.1.3                                ...           12 years ago
  • 0.1.2                                ...           12 years ago
  • 0.1.0                                ...           12 years ago
  • 0.0.5                                ...           12 years ago
  • 0.0.4                                ...           13 years ago
  • 0.0.2                                ...           13 years ago
  • 0.0.1                                ...           13 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (2)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE