Skip to content

Commit

Permalink
search_path pg closes #118
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars-Erik Roald committed Nov 21, 2024
1 parent 7b17403 commit 1479717
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 3 deletions.
23 changes: 21 additions & 2 deletions src/pg/pool/newPgPool.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
/* eslint-disable no-prototype-builtins */
//slightly modified code from github.com/brianc/node-postgres
var log = require('../../table/log');
var EventEmitter = require('events').EventEmitter;

var defaults = require('./defaults');
var genericPool = require('../../generic-pool');
var _pg = require('pg');
var parseSearchPathParam = require('./parseSearchPathParam');

function newPgPool(connectionString, poolOptions) {
poolOptions = poolOptions || {};
let pg = poolOptions.native ? _pg.native : _pg;

// @ts-ignore
var pool = genericPool.Pool({
max: poolOptions.size || poolOptions.poolSize || defaults.poolSize,
idleTimeoutMillis: poolOptions.idleTimeout || defaults.poolIdleTimeout,
Expand Down Expand Up @@ -41,7 +45,8 @@ function newPgPool(connectionString, poolOptions) {
}
});
client.poolCount = 0;
return cb(null, client);
negotiateSearchPath(client, connectionString, (err) => cb(err, client));

});
},
destroy: function(client) {
Expand All @@ -65,7 +70,8 @@ function newPgPool(connectionString, poolOptions) {
cb = domain.bind(cb);
}
if (err) return cb(err, null, function() {
/*NOOP*/ });
/*NOOP*/
});
client.poolCount++;
cb(null, client, function(err) {
if (err) {
Expand All @@ -79,4 +85,17 @@ function newPgPool(connectionString, poolOptions) {
return pool;
}

function negotiateSearchPath(client, connectionString, cb) {
const searchPath = parseSearchPathParam(connectionString);
if (searchPath) {
const sql = `set search_path to ${searchPath}`;
log.emitQuery({sql, parameters: []});
return client.query(sql, cb);
}
else
cb();


}

module.exports = newPgPool;
10 changes: 10 additions & 0 deletions src/pg/pool/parseSearchPathParam.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function parseSearchPathParam(connectionString = '') {
const [, queryString] = connectionString.split('?');
if (!queryString)
return;
const params = new URLSearchParams(queryString);
const searchPath = params.get('search_path');
return searchPath;
}

module.exports = parseSearchPathParam;
4 changes: 4 additions & 0 deletions tests/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const map = rdb.map(x => ({
balance: column('balance').numeric(),
isActive: column('isActive').boolean(),
})),
withSchema: x.table('withSchema').map(({ column }) => ({
id: column('id').numeric().primary().notNullExceptInsert(),
name: column('name').string(),
})),

vendor: x.table('vendor').map(({ column }) => ({
id: column('id').numeric().primary().notNull(),
Expand Down
9 changes: 8 additions & 1 deletion tests/initPg.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const sql = `
drop schema if exists custom cascade;
drop schema if exists public cascade;
create schema public;
create schema custom;
CREATE TABLE datetest (
id SERIAL PRIMARY KEY,
Expand Down Expand Up @@ -68,8 +71,12 @@ CREATE TABLE "deliveryAddress" (
"postalCode" TEXT,
"postalPlace" TEXT,
"countryCode" TEXT
)
);
CREATE TABLE custom."withSchema" (
id SERIAL PRIMARY KEY,
name TEXT
)
`;

module.exports = async function(db) {
Expand Down
51 changes: 51 additions & 0 deletions tests/schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, test, beforeAll, expect } from 'vitest';
const map = require('./db');

const initPg = require('./initPg');

beforeAll(async () => {
await insertData('pg');

async function insertData(dbName) {
const { db, init } = getDb(dbName);
await init(db);
}
});

describe('search path custom', () => {
test('pgWithSchema', async () => await verify('pgWithSchema'));

async function verify(dbName) {
const { db } = getDb(dbName);

const customer = await db.withSchema.insert({
name: 'Voldemort',
});

const expected = {
id: 1,
name: 'Voldemort',
};

expect(customer).toEqual(expected);
}
});

const connections = {
pg: {
db: map({ db: con => con.postgres('postgres://postgres:postgres@postgres/postgres', { size: 1 }) }),
init: initPg
},
pgWithSchema: {
db: map({ db: con => con.postgres('postgres://postgres:postgres@postgres/postgres?search_path=custom', { size: 1 }) }),
}
};

function getDb(name) {
if (name === 'pg')
return connections.pg;
if (name === 'pgWithSchema')
return connections.pgWithSchema;
else
throw new Error('unknown');
}

0 comments on commit 1479717

Please sign in to comment.