typeorm: TypeError: Cannot read properties of undefined (reading 'type')

Issue description

Error in EntityManagerFactory using better-sqlite3 when calling AppDataSource.initialize()

Expected Behavior

DB should initialize and code should run

Actual Behavior

Error thrown in EntityManagerFactory: Screenshot 2023-03-20 210359

BetterSqliteDriver does not have an options property (see image)

Steps to reproduce

See code listed below

My Environment

Dependency Version
Operating System Windows 11
Node.js version 18.12.1
Typescript version 4.8.2
TypeORM version 0.3.12
Electron 23.1.1
Angular 15.0.0

Additional Context

AppDataSource:

export const AppDataSource = new DataSource({
    type: "better-sqlite3",
    database: "foobar2.db",
    entities: [TestEntity],
    synchronize: true,
    logging: false,

})

Test Entity:

@Entity()
export class TestEntity{
    @Column() name: string = '';
    @PrimaryColumn() id: number = 0;
}

Initialization Code:

AppDataSource.initialize()
.then(() => {
    console.log('db initialized!');
})
.catch((error) => console.log(error))

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, but I don’t know how to start. I would need guidance.

About this issue

Most upvoted comments

So the problem is typeorm in ‘browser’ only supports mongodb or sqljs data source types.

I found out that vite compiles typeorm drivers wrong all drives will be compiled to this:

class MongoDriver { } class MongoEntityManager { } class MongoRepository { } class PostgresDriver { } class CockroachDriver { } class SqlServerDriver { } class SapDriver { } class MysqlDriver { } class OracleDriver { } class SqliteDriver { } class BetterSqlite3Driver { }

thats end in undefined variables. but i dont found out why vite is doing this.

You can get around with it, with this vite config:

{
    build: {
        rollupOptions: {
            external: "typeorm"
        }
    },
}

This will temporale fix it for local development

+1

Same thing with ‘sqlite3’ data source type. The only difference is I am using electron-vite-vue

I had this exact exception when using this for Postgres, and the workaround that worked for me for me was to pass the driver directly into the options for to the DataSource like this:

import { DataSource, DataSourceOptions } from "typeorm"
import pg from "pg";//ATTN: This is the driver for postgres

const postgresOptions: DataSourceOptions = {
    type: "postgres",
    host: "localhost",
    port: 5432,
    username: "postgres",
    password: "fakepassword",
    database: "postgres",
    entities: ["./entity/*.ts", "./entity/**/*.ts"],
    driver: pg, //ATTN: I pass the driver object in directly here.
    synchronize: true
};
const PostgresDB = new DataSource(postgresOptions);
export {
    PostgresDB
}

Let me know if this workaround works for any of you.

Has anyone solved this problem yet?

So the problem is typeorm in ‘browser’ only supports mongodb or sqljs data source types.

mongodb doesn’ t work in vue either.

I am also facing the same problem. Has anyone solved this issue?

I have solved the problem. Introducing repositories in preload not in renderer

Do you initialize database connection everytime you introduce repositories in the preload?

any update to this, same problem with electron vite vue

+1

Same thing with ‘sqlite3’ data source type. The only difference is I am using electron-vite-vue

同样的事情与’sqlite3’数据源类型。唯一的区别是我用的是电子视频

me too,Have you solved it?

+1