Skip to content

Commit

Permalink
Minimize diff
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Nov 15, 2024
1 parent 6eadc36 commit 10a74f1
Showing 1 changed file with 44 additions and 44 deletions.
88 changes: 44 additions & 44 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,50 @@ export function processSharedOptions<

export type Releasers = Array<() => void | Promise<void>>;

export async function assertPool(
compiledSharedOptions: CompiledSharedOptions,
releasers: Releasers,
): Promise<Pool> {
const {
resolvedPreset: {
worker: { maxPoolSize, connectionString },
},
_rawOptions,
} = compiledSharedOptions;
assert.ok(
// NOTE: we explicitly want `_rawOptions.connectionString` here - we don't
// mind if `connectionString` is set as part of the preset.
!_rawOptions.pgPool || !_rawOptions.connectionString,
"Both `pgPool` and `connectionString` are set, at most one of these options should be provided",
);
let pgPool: Pool;
if (_rawOptions.pgPool) {
pgPool = _rawOptions.pgPool;
if (pgPool.listeners("error").length === 0) {
console.warn(
`Your pool doesn't have error handlers! See: https://err.red/wpeh`,
);
installErrorHandlers(compiledSharedOptions, releasers, pgPool);
}
} else if (connectionString) {
pgPool = makeNewPool(compiledSharedOptions, releasers, {
connectionString,
max: maxPoolSize,
});
} else if (process.env.PGDATABASE) {
pgPool = makeNewPool(compiledSharedOptions, releasers, {
/* Pool automatically pulls settings from envvars */
max: maxPoolSize,
});
} else {
throw new Error(
"You must either specify `pgPool` or `connectionString`, or you must make the `DATABASE_URL` or `PG*` environmental variables available.",
);
}

return pgPool;
}

function makeNewPool(
compiledSharedOptions: CompiledSharedOptions,
releasers: Releasers,
Expand Down Expand Up @@ -340,50 +384,6 @@ function installErrorHandlers(
return pgPool;
}

export async function assertPool(
compiledSharedOptions: CompiledSharedOptions,
releasers: Releasers,
): Promise<Pool> {
const {
resolvedPreset: {
worker: { maxPoolSize, connectionString },
},
_rawOptions,
} = compiledSharedOptions;
assert.ok(
// NOTE: we explicitly want `_rawOptions.connectionString` here - we don't
// mind if `connectionString` is set as part of the preset.
!_rawOptions.pgPool || !_rawOptions.connectionString,
"Both `pgPool` and `connectionString` are set, at most one of these options should be provided",
);
let pgPool: Pool;
if (_rawOptions.pgPool) {
pgPool = _rawOptions.pgPool;
if (pgPool.listeners("error").length === 0) {
console.warn(
`Your pool doesn't have error handlers! See: https://err.red/wpeh`,
);
installErrorHandlers(compiledSharedOptions, releasers, pgPool);
}
} else if (connectionString) {
pgPool = makeNewPool(compiledSharedOptions, releasers, {
connectionString,
max: maxPoolSize,
});
} else if (process.env.PGDATABASE) {
pgPool = makeNewPool(compiledSharedOptions, releasers, {
/* Pool automatically pulls settings from envvars */
max: maxPoolSize,
});
} else {
throw new Error(
"You must either specify `pgPool` or `connectionString`, or you must make the `DATABASE_URL` or `PG*` environmental variables available.",
);
}

return pgPool;
}

export type Release = () => PromiseOrDirect<void>;

export async function withReleasers<T>(
Expand Down

0 comments on commit 10a74f1

Please sign in to comment.