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

feat(checkpoint-postgres): Add support for providing a custom schema during initialization #838

Conversation

nick-w-nick
Copy link
Contributor

@nick-w-nick nick-w-nick commented Feb 2, 2025

This PR adds support for specifying a custom schema in the checkpoint-postgres module during initialization. Instead of relying on the default public schema used by Postgres or modifying the connection string’s search path (which can be unreliable at best), SQL queries now explicitly reference the provided schema, ensuring compatibility across all PostgreSQL versions and configurations, while still allowing the original module to stay backward compatible with previous versions since public was already the default schema.

// you can optionally pass a configuration object as the second parameter
const checkpointer = PostgresSaver.fromConnString("postgresql://...", {
  schema: "schema_name" // defaults to "public"
});

Key Changes

  • Custom Schema Support
    • Added an extendable options parameter to specify a schema when creating a PostgresSaver instance via the fromConnString method
    • Consolidated SQL queries and migration scripts into reusable functions that accept a schema name
  • Documentation & Testing
    • Improved JSDoc comments for fromConnString with usage example
    • Enhanced integration tests to validate behavior with both the default and a custom schema

@benjamincburns
Copy link
Collaborator

Thanks for the contribution! I'll have a closer look tomorrow, but from a quick glance this looks really nice!

@benjamincburns
Copy link
Collaborator

Don't worry about the docs deploy - that's expected to fail on external PRs for the moment. 😅

Copy link
Collaborator

@benjamincburns benjamincburns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one small change requested, please & thanks. Handy change overall, though!

*/
static fromConnString(
connString: string,
options: PostgresSaverOptions = {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of a nit, but on first read through I thought that perhaps you were mistakenly causing default options to not be applied by specifying this default value.

It might be more readable/explicit if you have a module-scoped _defaultOptions, kept the options arg here and in the ctor optional, and then conditionally applied that at the start of the method.

Copy link
Contributor Author

@nick-w-nick nick-w-nick Feb 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benjamincburns I've pushed a change to update the default options behavior, including moving it to a module-scoped _defaultOptions variable.

Let me know if this works for you.

Thanks!

Copy link
Collaborator

@benjamincburns benjamincburns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for the contribution!

Comment on lines +101 to +114
/**
* Creates a new instance of PostgresSaver from a connection string.
*
* @param {string} connString - The connection string to connect to the Postgres database.
* @param {PostgresSaverOptions} [options] - Optional configuration object.
* @returns {PostgresSaver} A new instance of PostgresSaver.
*
* @example
* const connString = "postgresql://user:password@localhost:5432/db";
* const checkpointer = PostgresSaver.fromConnString(connString, {
* schema: "custom_schema" // defaults to "public"
* });
* await checkpointer.setup();
*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the reference docs, btw!

Comment on lines +30 to +37
const _ensureCompleteOptions = (
options?: Partial<PostgresSaverOptions>
): PostgresSaverOptions => {
return {
..._defaultOptions,
...options,
};
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. In the future you can also just in-line this, as it's a really common pattern for applying defaults. Having it be a well-named function call does express intent more clearly, however.

@benjamincburns benjamincburns force-pushed the feature/add-schema-support-to-postgres branch from 92d5b0f to 82e696d Compare February 21, 2025 02:11
@benjamincburns benjamincburns enabled auto-merge (squash) February 21, 2025 02:12
@benjamincburns benjamincburns merged commit c207756 into langchain-ai:main Feb 21, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants