/
installation.js
538 lines (535 loc) · 17.8 KB
/
installation.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
export default [
{
type: "heading",
size: "lg",
content: "Installation",
href: "Installation"
},
{
type: "text",
content: "Knex can be used as an SQL query builder in both Node.JS and the browser, limited to WebSQL's constraints (like the inability to drop tables or read schemas). Composing SQL queries in the browser for execution on the server is highly discouraged, as this can be the cause of serious security vulnerabilities. The browser builds outside of WebSQL are primarily for learning purposes - for example, you can pop open the console and build queries on this page using the <a href=\"javascript:alert(knex)\">knex</a> object."
},
{
type: "heading",
size: "md",
content: "Node.js",
href: "Installation-node"
},
{
type: "text",
content: "The primary target environment for Knex is Node.js, you will need to install the `knex` library, and then install the appropriate database library: [`pg`](https://github.com/brianc/node-postgres) for PostgreSQL and Amazon Redshift, [`mysql`](https://github.com/felixge/node-mysql) for MySQL or MariaDB, [`sqlite3`](https://github.com/mapbox/node-sqlite3) for SQLite3, or [`mssql`](https://github.com/patriksimek/node-mssql) for MSSQL."
},
{
type: "code",
content: `
$ npm install knex --save
# Then add one of the following (adding a --save) flag:
$ npm install pg
$ npm install sqlite3
$ npm install mysql
$ npm install mysql2
$ npm install oracledb
$ npm install mssql
`
},
{
type: "text",
content: "*If you want to use a MariaDB instance, you can use the `mysql` driver.*"
},
{
type: "heading",
size: "md",
content: "Browser",
href: "Installation-browser"
},
{
type: "text",
content: "Knex can be built using a JavaScript build tool such as [browserify](http://browserify.org/) or [webpack](https://github.com/webpack/webpack). In fact, this documentation uses a webpack build which [includes knex](https://github.com/knex/documentation/blob/a4de1b2eb50d6699f126be8d134f3d1acc4fc69d/components/Container.jsx#L3). View source on this page to see the browser build in-action (the global `knex` variable)."
},
{
type: "heading",
size: "md",
content: "Initializing the Library",
href: "Installation-client"
},
{
type: "text",
content: "The `knex` module is itself a function which takes a configuration object for Knex, accepting a few parameters. The `client` parameter is required and determines which client adapter will be used with the library."
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
});
`
},
{
type: "text",
content: "The connection options are passed directly to the appropriate database client to create the connection, and may be either an object, a connection string, or a function returning an object:"
},
{
type: "info",
content: "Note: Knex's PostgreSQL client allows you to set the initial search path for each connection automatically using an additional option \"searchPath\" as shown below."
},
{
type: "code",
language: "js",
content: `
var pg = require('knex')({
client: 'pg',
connection: process.env.PG_CONNECTION_STRING,
searchPath: ['knex', 'public'],
});
`
},
{
type: "info",
content: "Note: When you use the SQLite3 adapter, there is a filename required, not a network connection. For example:"
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
client: 'sqlite3',
connection: {
filename: "./mydb.sqlite"
}
});
`
},
{
type: "info",
content: "Note: The database version can be added in knex configuration, when you use the PostgreSQL adapter to connect a non-standard database."
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
client: 'pg',
version: '7.2',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
});
`
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
client: 'mysql',
version: '5.7',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
});
`
},
{
type: "text",
content: "A function can be used to determine the connection configuration dynamically. This function receives no parameters, and returns either a configuration object or a promise for a configuration object."
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
client: 'sqlite3',
connection: () => ({
filename: process.env.SQLITE_FILENAME
})
});
`
},
{
type: "text",
content: "By default, the configuration object received via a function is cached and reused for all connections. To change this behavior, an `expirationChecker` function can be returned as part of the configuration object. The `expirationChecker` is consulted before trying to create new connections, and in case it returns `true`, a new configuration object is retrieved. For example, to work with an authentication token that has a limited lifespan:"
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
client: 'postgres',
connection: async () => {
const { token, tokenExpiration } = await someCallToGetTheToken();
return {
host : 'your_host',
user : 'your_database_user',
password : token,
database : 'myapp_test',
expirationChecker: () => {
return tokenExpiration <= Date.now();
}
};
}
});
`
},
{
type: "text",
content: "You can also connect via an unix domain socket, which will ignore host and port."
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
client: 'mysql',
connection: {
socketPath : '/path/to/socket.sock',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
});
`
},
{
type: "text",
content: "`userParams` is an optional parameter that allows you to pass arbitrary parameters which will be accessible via `knex.userParams` property:"
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
},
userParams: {
userParam1: '451'
}
});
`
},
{
type: "info",
content: "Initializing the library should normally only ever happen once in your application, as it creates a connection pool for the current database, you should use the instance returned from the initialize call throughout your library."
},
{
type: "text",
content: "Specify the client for the particular flavour of SQL you are interested in."
},
{
type: "code",
language: "js",
content: `
var pg = require('knex')({client: 'pg'});
knex('table').insert({a: 'b'}).returning('*').toString();
// "insert into "table" ("a") values ('b')"
pg('table').insert({a: 'b'}).returning('*').toString();
// "insert into "table" ("a") values ('b') returning *"
`
},
{
type: "heading",
size: "md",
content: "Getting parametrized instance",
href: "Installation-parametrization"
},
{
type: "text",
content: "You can call method `withUserParams` on a Knex instance if you want to get a copy (with same connections) with custom parameters (e. g. to execute same migrations with different parameters)"
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
// Params
});
var knexWithParams = knex.withUserParams({customUserParam: 'table1'});
var customUserParam = knexWithParams.userParams.customUserParam;
`
},
{
type: "heading",
size: "md",
content: "Debugging",
href: "Installation-debug"
},
{
type: "text",
content: "Passing a `debug: true` flag on your initialization object will turn on [debugging](#Builder-debug) for all queries."
},
{
type: "heading",
size: "md",
content: "asyncStackTraces",
href: "Installation-asyncStackTraces"
},
{
type: "text",
content: "Passing an `asyncStackTraces: true` flag on your initialization object will turn on stack trace capture for all query builders, raw queries and schema builders. When a DB driver returns an error, this previously captured stack trace is thrown instead of a new one. This helps to mitigate default behaviour of `await` in node.js/V8 which blows the stack away. \n This has small performance overhead, so it is advised to use only for development. Turned off by default."
},
{
type: "heading",
size: "md",
content: "Pooling",
href: "Installation-pooling"
},
{
type: "text",
content: [
"The client created by the configuration initializes a connection pool, using the [tarn.js](https://github.com/vincit/tarn.js) library. This connection pool has a default setting of a `min: 2, max: 10` for the MySQL and PG libraries, and a single connection for sqlite3 (due to issues with utilizing multiple connections on a single file). To change the config settings for the pool, pass a `pool` option as one of the keys in the initialize block.",
"Checkout the [tarn.js](https://github.com/vincit/tarn.js) library for more information."
]
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
},
pool: { min: 0, max: 7 }
});
`
},
{
type: "text",
content: "If you ever need to explicitly teardown the connection pool, you may use `knex.destroy([callback])`. You may use `knex.destroy` by passing a callback, or by chaining as a promise, just not both. To manually initialize a destroyed connection pool, you may use knex.initialize([config]), if no config is passed, it will use the first knex configuration used."
},
{
type: "heading",
size: "md",
content: "afterCreate",
href: "Installation-pooling-afterCreate"
},
{
type: "text",
content: "`afterCreate` callback (rawDriverConnection, done) is called when the pool aquires a new connection from the database server. done(err, connection) callback must be called for `knex` to be able to decide if the connection is ok or if it should be discarded right away from the pool."
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
client: 'pg',
connection: {...},
pool: {
afterCreate: function (conn, done) {
// in this example we use pg driver's connection API
conn.query('SET timezone="UTC";', function (err) {
if (err) {
// first query failed, return error and don't try to make next query
done(err, conn);
} else {
// do the second query...
conn.query('SELECT set_limit(0.01);', function (err) {
// if err is not falsy, connection is discarded from pool
// if connection aquire was triggered by a query the error is passed to query promise
done(err, conn);
});
}
});
}
}
});
`
},
{
type: "heading",
size: "md",
content: "acquireConnectionTimeout",
href: "Installation-acquireConnectionTimeout"
},
{
type: "text",
content: "`acquireConnectionTimeout` defaults to 60000ms and is used to determine how long knex should wait before throwing a timeout error when acquiring a connection is not possible. The most common cause for this is using up all the pool for transaction connections and then attempting to run queries outside of transactions while the pool is still full. The error thrown will provide information on the query the connection was for to simplify the job of locating the culprit."
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
client: 'pg',
connection: {...},
pool: {...},
acquireConnectionTimeout: 10000
});
`
},
{
type: "heading",
size: "md",
content: "fetchAsString",
href: "Installation-fetchAsString"
},
{
type: "text",
content: "Utilized by Oracledb. An array of types. The valid types are 'DATE', 'NUMBER' and 'CLOB'. When any column having one of the specified types is queried, the column data is returned as a string instead of the default representation."
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
client: 'oracledb',
connection: {...},
fetchAsString: [ 'number', 'clob' ]
});
`
},
{
type: "heading",
size: "md",
content: "Migrations",
href: "Installation-migrations"
},
{
type: "text",
content: "For convenience, the any migration configuration may be specified when initializing the library. Read the [Migrations](#Migrations) section for more information and a full list of configuration options."
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
},
migrations: {
tableName: 'migrations'
}
});
`
},
{
type: "heading",
size: "md",
content: "postProcessResponse",
href: "Installation-post-process-response"
},
{
type: "text",
content: [
"Hook for modifying returned rows, before passing them forward to user. One can do for example",
"snake_case -> camelCase conversion for returned columns with this hook.",
"The `queryContext` is only available if configured for a query builder instance via [queryContext](#Builder-queryContext)."
].join(' ')
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
client: 'mysql',
// overly simplified snake_case -> camelCase converter
postProcessResponse: (result, queryContext) => {
// TODO: add special case for raw results (depends on dialect)
if (Array.isArray(result)) {
return result.map(row => convertToCamel(row));
} else {
return convertToCamel(result);
}
}
});
`
},
{
type: "heading",
size: "md",
content: "wrapIdentifier",
href: "Installation-wrap-identifier"
},
{
type: "text",
content: [
"Knex supports transforming identifier names automatically to quoted versions for each dialect.",
"For example `'Table.columnName as foo'` for PostgreSQL is converted to \"Table\".\"columnName\" as \"foo\".",
].join(' ')
},
{
type: "text",
content: [
"With `wrapIdentifier` one may override the way how identifiers are transformed.",
"It can be used to override default functionality and for example to help doing `camelCase` -> `snake_case` conversion.",
].join(' ')
},
{
type: "text",
content: [
"Conversion function `wrapIdentifier(value, dialectImpl, context): string` gets each part of the identifier",
"as a single `value`, the original conversion function from the dialect implementation and the `queryContext`,",
"which is only available if configured for a query builder instance via [builder.queryContext](#Builder-queryContext),",
"and for schema builder instances via [schema.queryContext](#Schema-queryContext) or [table.queryContext](#Schema-table-queryContext).",
"For example, with the query builder, `knex('table').withSchema('foo').select('table.field as otherName').where('id', 1)` will call",
"`wrapIdentifier` converter for following values `'table'`, `'foo'`, `'table'`, `'field'`, `'otherName'` and `'id'`.",
].join(' ')
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
client: 'mysql',
// overly simplified camelCase -> snake_case converter
wrapIdentifier: (value, origImpl, queryContext) => origImpl(convertToSnakeCase(value))
});
`
},
{
type: "heading",
size: "md",
content: "log",
href: "Installation-log"
},
{
type: "text",
content: [
"Knex contains some internal log functions for printing warnings, errors, deprecations, and debug information when applicable.",
"These log functions typically log to the console, but can be overwritten using the log option and providing alternative functions.",
"Different log functions can be used for separate knex instances.",
].join(' ')
},
{
type: "code",
language: "js",
content: `
var knex = require('knex')({
log: {
warn(message) {
},
error(message) {
},
deprecate(message) {
},
debug(message) {
},
}
});
`
},
]