Skip to content

Commit

Permalink
Documentation for knex/knex#3497 (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
oranoran authored and kibertoad committed Oct 27, 2019
1 parent 852269c commit a0ae1f7
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion sections/installation.js
Expand Up @@ -74,7 +74,7 @@ export default [
},
{
type: "text",
content: "The connection options are passed directly to the appropriate database client to create the connection, and may be either an object, or a connection string:"
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",
Expand Down Expand Up @@ -143,6 +143,47 @@ export default [
});
`
},
{
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."
Expand Down

0 comments on commit a0ae1f7

Please sign in to comment.