sql string builder for node - supports PostgreSQL, mysql, Microsoft SQL Server, and sqlite dialects.
Building SQL statements by hand is no fun, especially in a language which has clumsy support for multi-line strings.
Maybe it's still not fun, but at least it's less not fun.
//require the modulevar sql = require'sql';//(optionally) set the SQL dialectsqlsetDialect'postgres';//possible dialects: mssql, mysql, postgres (default), sqlite//first we define our tablesvar user = sqldefinename: 'user'columns: 'id' 'name' 'email' 'lastLogin';var post = sqldefinename: 'post'columns: 'id' 'userId' 'date' 'title' 'body';//now let's make a simple queryvar query = userselectuserstarfromusertoQuery;console.logquerytext; //SELECT "user".* FROM "user"//something more interestingvar query = userselectuseridfromuserwhereusernameequals'boom'anduseridequals1orusernameequals'bang'anduseridequals2toQuery;//query is parameterized by defaultconsole.logquerytext; //SELECT "user"."id" FROM "user" WHERE ((("user"."name" = $1) AND ("user"."id" = $2)) OR (("user"."name" = $3) AND ("user"."id" = $4)))console.logqueryvalues; //['boom', 1, 'bang', 2]//queries can be namedvar query = userselectuserstarfromusertoNamedQuery'user.all';console.logqueryname; //'user.all'//how about a join?var query = userselectusername postbodyfromuserjoinpostonuseridequalspostuserIdtoQuery;console.logquerytext; //'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 handyvar friendship = sqldefinename: 'friendship'columns: 'userId' 'friendId';var friends = useras'friends';var userToFriends = userleftJoinfriendshiponuseridequalsfriendshipuserIdleftJoinfriendsonfriendshipfriendIdequalsfriendsid;//and now...compose...var friendsWhoHaveLoggedInQuery = userfromuserToFriendswherefriendslastLoginisNotNull;//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 NULLvar friendsWhoUseGmailQuery = userfromuserToFriendswherefriendsemaillikeemail@example.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 camelCasevar user = sqldefinename: 'user'columns:name: 'id'name: 'state_or_province'property: 'state';//now, instead of user.state_or_province, you can just use user.stateconsole.loguserselectwhereuserstateequals'WA'toQuerytext;// "SELECT "user".* FROM "user" WHERE ("user"."state_or_province" = $1)"
There are a lot more examples included in the
You can use the sql-generate module
to automatically generate definition files from a database instance. For example,
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*/exportsbar = sqldefinename: 'bar'columns:'id''foo_id';/*** SQL definition for database.foo*/exportsfoo = sqldefinename: 'foo'columns:'id''field_1''foo_bar_baz';/*** Adding a column to an existing table:*/var model = sqldefine name: 'foo' columns: ;modeladdColumn'id';// If you try to add another column "id", node-sql will throw an error.// You can suppress that error via:modeladdColumn'id' noisy: false ;
Read the module's documentation for more details.
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:
git pull https://github.com/(your_username)/node-sql
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.