oguimbal / pg-mem

An in memory postgres DB instance for your unit tests
MIT License
1.98k stars 97 forks source link

TypeError: DataSource is not a constructor #371

Open goodstemy opened 10 months ago

goodstemy commented 10 months ago

Describe the bug

Trying to write tests based on this example using createTypeormDataSource.

 FAIL  src/controllers/order/order.service.spec.ts
  Order module
    ✕ Should work

  ● Order module › Should work

    TypeError: DataSource is not a constructor

      50 |     })
      51 |
    > 52 |     const conn = await db.adapters.createTypeormDataSource({
         |                                    ^
      53 |       type: 'postgres',
      54 |       entities: [Wallet, User, UserRole, Role, RolePermission, Permission, Session, UserSettings, ProductMiddleWallet, Product, NetworkCurrency, Network, Currency],
      55 |     });

      at Adapters.createTypeormDataSource (node_modules/pg-mem/src/adapters/adapters.ts:208:25)
      at Object.createTypeormDataSource (src/controllers/order/order.service.spec.ts:52:36)

Also createTypeormConnection works fine beside another problem from my other issue.

To Reproduce

    const db = newDb({
      autoCreateForeignKeyIndices: true,
    });

    db.public.registerFunction({
      implementation: () => 'test',
      name: 'current_database',
    });

    db.public.registerFunction({
      name: 'version',
      args: [],
      returns: DataType.text,
      implementation: (x) => `hello world: ${x}`
    })

    db.registerExtension('uuid-ossp', (schema) => {
      schema.registerFunction({
        name: 'uuid_generate_v4',
        returns: DataType.uuid,
        implementation: v4,
        impure: true
      })
    })

    const conn = await db.adapters.createTypeormDataSource({
      type: 'postgres',
      entities: [Wallet, User, UserRole, Role, RolePermission, Permission, Session, UserSettings, ProductMiddleWallet, Product, NetworkCurrency, Network, Currency],
    });

pg-mem version and another deps

"pg-mem": "^2.7.2",
"typeorm": "0.2.45",
"pg": "^8.7.3",
goodstemy commented 10 months ago

Actually problem because typeorm added DataSource class only in version 0.3.0 and higher.

I think really matter update package json and update peer deps with 0.3.0 version of typeorm.

  "peerDependencies": {
    "knex": ">=0.20",
    "pg-promise": ">=10.8.7",
    "slonik": ">=23.0.1",
    "typeorm": ">=0.2.29" // here
  },

And I think really matter to update wiki for usage pg-mem with typeorm.

https://github.com/oguimbal/pg-mem/wiki/Libraries-adapters#-typeorm

Change createTypeormConnection -> createTypeormDataSource.