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

Support for dynamic extensions #1

Open
psychemedia opened this issue Aug 8, 2024 · 2 comments
Open

Support for dynamic extensions #1

psychemedia opened this issue Aug 8, 2024 · 2 comments

Comments

@psychemedia
Copy link
Contributor

See https://lantern.dev/blog/pglite-lantern for examples of a fork that supports loading postgres extensions.

@psychemedia
Copy link
Contributor Author

Alternatively, allow switches in the magoc that creates the initial pglite instance and connection to install one or more extensions.

Also have a magic that lists the available extensions.

@psychemedia
Copy link
Contributor Author

psychemedia commented Dec 7, 2024

I've made a start on supporting extensions.

Check availability as:

from jupyter_anywidget_pglite import AVAILABLE_EXTENSIONS

AVAILABLE_EXTENSIONS
>>>['fuzzystrmatch', 'pg_trgm', 'vector']

Then load via the extensions= parameter:

from jupyter_anywidget_pglite import pglite_panel

# Launch it
pg_panel = pglite_panel(extensions=["vector", "fuzzystrmatch", "pg_trgm"])

The required packaes are loaded dynamically:

  async function loadExtensions(extensions) {
    const extensionsObj = {};
    const extensionModules = [
      {
        name: "fuzzystrmatch",
        url: "https://cdn.jsdelivr.net/npm/@electric-sql/pglite/dist/contrib/fuzzystrmatch.js",
      },
      {
        name: "pg_trgm",
        url: "https://cdn.jsdelivr.net/npm/@electric-sql/pglite/dist/contrib/pg_trgm.js",
      },
      {
        name: "vector",
        url: "https://cdn.jsdelivr.net/npm/@electric-sql/pglite/dist/vector/index.js",
      },
    ];

    // Prepare an array of promises for the imports
    const importPromises = extensionModules.map(async ({ name, url }) => {
      if (extensions.includes(name)) {
        try {
          const module = await import(url);
          extensionsObj[name] = module[name];
        } catch (error) {
          handle_error(error);
          console.error(`Failed to load ${name}:`, error);
        }
      }
    });

    // Wait for all import promises to complete
    await Promise.all(importPromises);

    return extensionsObj;
  }

So this approach should allow for us passing in additional, arbitrary extensions, e.g. via a (name, url) two tuple?

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

No branches or pull requests

1 participant