Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for https://github.com/knex/knex/pull/3497 #242

Merged
merged 1 commit into from Oct 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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