-
Notifications
You must be signed in to change notification settings - Fork 3
/
examples.js
221 lines (152 loc) · 6.45 KB
/
examples.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Create an instance of Query Builder
// ==================================================
// Require package and instantiate a new Qb.
// Pass in model definitions and SQL flavor.
// More examples in define.test.js.
var definitions = require('./example-definitions');
var Qb = require('./lib');
var qb = new Qb(definitions, 'postgres');
// Register custom functions (optional)
// ==================================================
// Qb already includes common aggregates like COUNT
// and SUM, but not functions like MONTH.
// More examples in register-functions.test.js.
// Register a MONTH function.
qb.registerFunction('MONTH');
// Register a function with prefilled argument.
// TO_MONTH will become DATE_TRUNC('MONTH', [arg])
qb.registerFunction('TO_MONTH', 'DATE_TRUNC', 'MONTH');
// Advanced: combine two functions.
qb.functions.COUNT_DISTINCT = function(field) {
return field.count().distinct();
};
// Building simple SELECT statements
// ==================================================
// More examples in select.test.js and join.test.js.
// Select a single field.
var spec = { select: 'id', from: 'users' };
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Users"."id" AS "User ID"
// FROM "users" AS "Users"
// Select multiple fields.
var spec = { select: ['id', 'name'], from: 'users' };
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Users"."id" AS "User ID", "Users"."name" AS "Full name"
// FROM "users" AS "Users"
// Select using a different alias from that defined.
var spec = { select: { name: 'id', as: 'uid' }, from: 'users' };
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Users"."id" AS "uid"
// FROM "users" AS "Users"
// Using functions
// ==================================================
// More examples in register-functions.test.js.
// Using a builtin function.
var spec = { select: ['user_id', { name: 'id', functions: 'count' }], from: 'posts' };
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Blog Posts"."user_id" AS "Author", COUNT("Blog Posts"."id") AS "Post ID_count"
// FROM "posts" AS "Blog Posts"
// Using a registered function.
var spec = { select: ['id', { name: 'created_at', functions: 'TO_MONTH', as: 'month' }], from: 'users' };
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Users"."id" AS "User ID", DATE_TRUNC('MONTH', "Users"."created_at") AS "month"
// FROM "users" AS "Users"
// Using nested functions.
var spec = { select: ['user_id', { name: 'id', functions: ['count', 'distinct'] }], from: 'posts' };
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Blog Posts"."user_id" AS "Author", COUNT(DISTINCT("Blog Posts"."id")) AS "Post ID_distinct_count"
// FROM "posts" AS "Blog Posts"
// Adding JOIN clauses
// ==================================================
// More examples in join.test.js.
// Simple JOIN.
var spec = { select: 'id', from: 'users', join: 'posts' };
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Users"."id" AS "User ID"
// FROM "users" AS "Users"
// INNER JOIN "posts" AS "Blog Posts" ON ("Users"."id" = "Blog Posts"."user_id")
// Select columns from two tables.
var spec = { select: ['id', { name: 'id', on: 'posts' }], from: 'users', join: 'posts' };
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Users"."id" AS "User ID", "Blog Posts"."id" AS "Post ID"
// FROM "users" AS "Users"
// INNER JOIN "posts" AS "Blog Posts" ON ("Users"."id" = "Blog Posts"."user_id")
// JOIN via an intermediate table.
var spec = { select: 'tag', from: 'tags', join: 'posts' };
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Tags"."tag" AS "Tag"
// FROM "tags" AS "Tags"
// INNER JOIN "posts_tags" ON ("Tags"."id" = "posts_tags"."tag_id")
// INNER JOIN "posts" AS "Blog Posts" ON ("posts_tags"."post_id" = "Blog Posts"."id")
// Adding WHERE clauses
// ==================================================
// More examples in where.test.js.
// Filter against a constant.
var spec = { select: 'id', from: 'users', where: { field: 'id', match: { value: 1 } } };
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Users"."id" AS "User ID"
// FROM "users" AS "Users"
// WHERE ("Users"."id" = 1)
// Filter against a column.
var spec = { select: 'id', from: 'users', where: { field: 'id', match: 'name' } };
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Users"."id" AS "User ID"
// FROM "users" AS "Users"
// WHERE ("Users"."id" = "Users"."name")
// Filter with AND logic.
var spec = { select: 'id', from: 'users', where: [{ field: 'id', match: { value: 1 } }, { field: 'name', match: { value: 'Bork' }}] };
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Users"."id" AS "User ID"
// FROM "users" AS "Users"
// WHERE (("Users"."id" = 1) AND ("Users"."name" = 'Bork'))
// Filter with OR logic.
var spec = { select: 'id', from: 'users', where: { or: [{ field: 'id', match: { value: 1 } }, { field: 'name', match: { value: 'Bork' }}] }};
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Users"."id" AS "User ID"
// FROM "users" AS "Users"
// WHERE (("Users"."id" = 1) OR ("Users"."name" = 'Bork'))
// Filter with a comparison operator (see where.test.js for a complete list)
var spec = { select: 'id', from: 'users', where: { field: 'id', op: 'gt', match: { value: 42 }}};
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Users"."id" AS "User ID"
// FROM "users" AS "Users"
// WHERE ("Users"."id" > 42)
// Using GROUP BY, ORDER BY, & LIMIT
// ==================================================
// More examples group-by.test.js, order-by.test.js,
// & limit.test.js
// GROUP BY.
var spec = { select: { name: 'id', groupBy: true }, from: 'users' };
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Users"."id" AS "User ID"
// FROM "users" AS "Users"
// GROUP BY "Users"."id"
// ORDER BY.
var spec = { select: { name: 'id', orderBy: 'desc' }, from: 'users' };
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Users"."id" AS "User ID"
// FROM "users" AS "Users"
// ORDER BY "Users"."id" DESC
// LIMIT.
var spec = { select: 'id', from: 'users', limit: 1000 };
var query = qb.query(spec);
console.log(query.formatted + '\n');
// SELECT "Users"."id" AS "User ID"
// FROM "users" AS "Users"
// LIMIT 1000