UNPKG

5.88 kBMarkdownView Raw
1# node-sql
2_sql string builder for node_ - supports PostgreSQL, mysql, Microsoft SQL Server, Oracle and sqlite dialects.
3
4Building SQL statements by hand is no fun, especially in a language which has clumsy support for multi-line strings.
5
6So let's build it with JavaScript.
7
8Maybe it's still not fun, but at least it's _less not fun_.
9
10[![Build Status](https://secure.travis-ci.org/brianc/node-sql.png)](http://travis-ci.org/brianc/node-sql)
11
12## install
13
14```sh
15$ npm install sql
16```
17
18## use
19
20```js
21//require the module
22var sql = require('sql');
23
24//(optionally) set the SQL dialect
25sql.setDialect('postgres');
26//possible dialects: mssql, mysql, postgres (default), sqlite
27
28//first we define our tables
29var user = sql.define({
30 name: 'user',
31 columns: ['id', 'name', 'email', 'lastLogin']
32});
33
34var post = sql.define({
35 name: 'post',
36 columns: ['id', 'userId', 'date', 'title', 'body']
37});
38
39//now let's make a simple query
40var query = user.select(user.star()).from(user).toQuery();
41console.log(query.text); //SELECT "user".* FROM "user"
42
43//something more interesting
44var query = user
45 .select(user.id)
46 .from(user)
47 .where(
48 user.name.equals('boom').and(user.id.equals(1))
49 ).or(
50 user.name.equals('bang').and(user.id.equals(2))
51 ).toQuery();
52
53//query is parameterized by default
54console.log(query.text); //SELECT "user"."id" FROM "user" WHERE ((("user"."name" = $1) AND ("user"."id" = $2)) OR (("user"."name" = $3) AND ("user"."id" = $4)))
55
56console.log(query.values); //['boom', 1, 'bang', 2]
57
58//queries can be named
59var query = user.select(user.star()).from(user).toNamedQuery('user.all');
60console.log(query.name); //'user.all'
61
62//how about a join?
63var query = user.select(user.name, post.body)
64 .from(user.join(post).on(user.id.equals(post.userId))).toQuery();
65
66console.log(query.text); //'SELECT "user"."name", "post"."body" FROM "user" INNER JOIN "post" ON ("user"."id" = "post"."userId")'
67
68//this also makes parts of your queries composable, which is handy
69
70var friendship = sql.define({
71 name: 'friendship',
72 columns: ['userId', 'friendId']
73});
74
75var friends = user.as('friends');
76var userToFriends = user
77 .leftJoin(friendship).on(user.id.equals(friendship.userId))
78 .leftJoin(friends).on(friendship.friendId.equals(friends.id));
79
80//and now...compose...
81var friendsWhoHaveLoggedInQuery = user.from(userToFriends).where(friends.lastLogin.isNotNull());
82//SELECT * FROM "user"
83//LEFT JOIN "friendship" ON ("user"."id" = "friendship"."userId")
84//LEFT JOIN "user" AS "friends" ON ("friendship"."friendId" = "friends"."id")
85//WHERE "friends"."lastLogin" IS NOT NULL
86
87var friendsWhoUseGmailQuery = user.from(userToFriends).where(friends.email.like('%@gmail.com'));
88//SELECT * FROM "user"
89//LEFT JOIN "friendship" ON ("user"."id" = "friendship"."userId")
90//LEFT JOIN "user" AS "friends" ON ("friendship"."friendId" = "friends"."id")
91//WHERE "friends"."email" LIKE %1
92
93//Using different property names for columns
94//helpful if your column name is long or not camelCase
95var user = sql.define({
96 name: 'user',
97 columns: [{
98 name: 'id'
99 }, {
100 name: 'state_or_province',
101 property: 'state'
102 }
103 ]
104});
105
106//now, instead of user.state_or_province, you can just use user.state
107console.log(user.select().where(user.state.equals('WA')).toQuery().text);
108// "SELECT "user".* FROM "user" WHERE ("user"."state_or_province" = $1)"
109```
110
111There are a __lot__ more examples included in the [test/dialects](https://github.com/brianc/node-sql/tree/master/test/dialects) folder. We encourage you to read through them if you have any questions on usage!
112
113## from the command line
114You can use the [sql-generate module](https://github.com/tmont/node-sql-generate)
115to automatically generate definition files from a database instance. For example,
116running `node-sql-generate --dsn "mysql://user:password@host/database"` will generate
117something similar to:
118
119```javascript
120// autogenerated by node-sql-generate v0.0.1 on Tue May 21 2013 01:04:12 GMT-0700 (PDT)
121var sql = require('sql');
122
123/**
124 * SQL definition for database.bar
125 */
126exports.bar = sql.define({
127 name: 'bar',
128 columns: [
129 'id',
130 'foo_id'
131 ]
132});
133
134/**
135 * SQL definition for database.foo
136 */
137exports.foo = sql.define({
138 name: 'foo',
139 columns: [
140 'id',
141 'field_1',
142 'foo_bar_baz'
143 ]
144});
145
146/**
147 * Adding a column to an existing table:
148 */
149var model = sql.define({ name: 'foo', columns: [] });
150model.addColumn('id');
151
152// If you try to add another column "id", node-sql will throw an error.
153// You can suppress that error via:
154model.addColumn('id', { noisy: false });
155```
156
157Read the module's documentation for more details.
158
159## contributing
160
161We __love__ contributions.
162
163node-sql wouldn't be anything without all the contributors and collaborators who've worked on it.
164If you'd like to become a collaborator here's how it's done:
165
1661. fork the repo
1672. `git pull https://github.com/(your_username)/node-sql`
1683. `cd node-sql`
1694. `npm install`
1705. `npm test`
171
172At 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.
173My email address is on my github profile and also on every commit I contributed in the repo.
174
175Once 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.
176
177__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__
178
179If 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.
180
181Usually after a few high-quality pull requests and friendly interactions we will gladly share collaboration rights with you.
182
183After all, open source belongs to everyone.
184
185
186##license
187MIT