Skip to content

Commit

Permalink
knex: rename connection object (#1364)
Browse files Browse the repository at this point in the history
Renames the knex config object passed as `connection` to `knex()` from the generic `connectionObject` to `knexConnection`.

This renaming will:

* make intention/use of `connectionObject` clearer
* ease finding of knex-related code across the repo
* aid deprecating and finally removing knex dependency
  • Loading branch information
alxndrsn authored Jan 16, 2025
1 parent bec06ac commit 6324ebe
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/model/knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ NODE_CONFIG_DIR=../../config DEBUG=knex:query,knex:bindings npx knex migrate:up
*/

const config = require('config');
const { connectionObject } = require('../util/db');
const { knexConnection } = require('../util/db');

module.exports = {
client: 'pg',
connection: connectionObject(config.get('default.database'))
connection: knexConnection(config.get('default.database'))
};

4 changes: 2 additions & 2 deletions lib/model/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
// top-level operations with a database, like migrations.

const knex = require('knex');
const { connectionObject } = require('../util/db');
const { knexConnection } = require('../util/db');

// Connects to the postgres database specified in configuration and returns it.
const connect = (config) => knex({ client: 'pg', connection: connectionObject(config) });
const connect = (config) => knex({ client: 'pg', connection: knexConnection(config) });

// Connects to a database, passes it to a function for operations, then ensures its closure.
const withDatabase = (config) => (mutator) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/util/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const connectionString = (config) => {
};

// Returns an object that Knex will use to connect to the database.
const connectionObject = (config) => {
const knexConnection = (config) => {
const problem = validateConfig(config);
if (problem != null) throw problem;
// We ignore maximumPoolSize when using Knex.
Expand Down Expand Up @@ -588,7 +588,7 @@ const postgresErrorToProblem = (x) => {
};

module.exports = {
connectionString, connectionObject,
connectionString, knexConnection,
unjoiner, extender, sqlEquals, page, queryFuncs,
insert, insertMany, updater, markDeleted, markUndeleted,
QueryOptions,
Expand Down
18 changes: 9 additions & 9 deletions test/unit/util/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ describe('util/db', () => {
});
});

describe('connectionObject', () => {
const { connectionObject } = util;
describe('knexConnection', () => {
const { knexConnection } = util;

it('should return an object with the required options', () => {
const result = connectionObject({
const result = knexConnection({
host: 'localhost',
database: 'foo',
user: 'bar',
Expand All @@ -116,7 +116,7 @@ describe('util/db', () => {
});

it('should include the port if one is specified', () => {
const result = connectionObject({
const result = knexConnection({
host: 'localhost',
database: 'foo',
user: 'bar',
Expand All @@ -133,7 +133,7 @@ describe('util/db', () => {
});

it('should return the correct object if ssl is true', () => {
const result = connectionObject({
const result = knexConnection({
host: 'localhost',
database: 'foo',
user: 'bar',
Expand All @@ -150,7 +150,7 @@ describe('util/db', () => {
});

it('should throw if ssl is false', () => {
const result = () => connectionObject({
const result = () => knexConnection({
host: 'localhost',
database: 'foo',
user: 'bar',
Expand All @@ -161,7 +161,7 @@ describe('util/db', () => {
});

it('should throw if ssl is an object', () => {
const result = () => connectionObject({
const result = () => knexConnection({
host: 'localhost',
database: 'foo',
user: 'bar',
Expand All @@ -172,7 +172,7 @@ describe('util/db', () => {
});

it('should allow (but ignore) maximumPoolSize', () => {
const result = connectionObject({
const result = knexConnection({
host: 'localhost',
database: 'foo',
user: 'bar',
Expand All @@ -188,7 +188,7 @@ describe('util/db', () => {
});

it('should throw for an unsupported option', () => {
const result = () => connectionObject({
const result = () => knexConnection({
host: 'localhost',
database: 'foo',
user: 'bar',
Expand Down

0 comments on commit 6324ebe

Please sign in to comment.