electric-sql / pglite

Lightweight WASM Postgres with real-time, reactive bindings.
https://pglite.dev
Apache License 2.0
9.57k stars 209 forks source link

Error loading uuid_ossp extensions #453

Open sergio-milu opened 1 day ago

sergio-milu commented 1 day ago

Hey,

Version: @electric-sql/pglite": "^0.2.14

I'm trying to setup PGLite in my typeorm project for testing. I'm using jest + SWC. The error that I'm getting is

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { d as PGliteInterface } from '../pglite-CCwqaSmq.js';
                                                                                      ^^^^^^

SyntaxError: Cannot use import statement outside a module

The setup

import * as uuid_ossp from '@electric-sql/pglite/dist/contrib/uuid_ossp';
import { TypeOrmModule } from '@nestjs/typeorm';
import { PGliteDriver } from 'typeorm-pglite';

console.log(uuid_ossp);

export const getRootTypeormModule = () => {
  return TypeOrmModule.forRoot({
    type: 'postgres',
    driver: new PGliteDriver({ extensions: { uuid_ossp: uuid_ossp.uuid_ossp } })
      .driver,
  });
};

This is my tsconfig

const config = {
  displayName: 'backend',
  restoreMocks: true,
  clearMocks: true,
  resetMocks: true,
  testEnvironment: 'node',
  preset: '../../jest.preset.js',
  globalSetup: '<rootDir>/jest.global-setup.ts',
  setupFiles: [
    'jest-plugin-unhandled-promise/setup',
    '<rootDir>/jest.setup.ts',
  ],
  transform: {
    '^.+\\.spec\\.[tj]s?$': '@swc/jest',
  },
  moduleFileExtensions: ['ts', 'js', 'html'],
  roots: ['<rootDir>', './src/__mocks__'],
  workerIdleMemoryLimit: 0.2,
  setupFilesAfterEnv: ['../../jest.setup-after-env.ts'],
};

SWC config

{
  "$schema": "http://json.schemastore.org/swcrc",
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "decorators": true,
      "dynamicImport": true
    },
    "transform": { "decoratorMetadata": true, "legacyDecorator": true },
    "loose": false
  }
}

Dont know why I need to import the uuid extensions from /dist (this is not what's shown in the docs)

Also, I tried to add this to jest config

transformIgnorePatterns: [`node_modules/(?!(@electric-sql/pglite)/)`]

and that error goes away but the import is an empty object

seems kind of an issue of ESmodule , but after a long time I didn't find the issue and not sure if it's up to me or some misconfiguration in the packages.

Any clue?

Thanks in advance

copiltembel commented 20 hours ago

There are some uuid_ossp tests in packages/pglite/tests/contrib/uuid_ossp.test.js. Does that help?