TypeError when initializing entity using typeorm with React Native

122 Views Asked by At

I have set up typeorm with React Native & quick SQLLite according to the instructions, and I seem to be having an issue even registering an entity. My setup code looks like:

import { typeORMDriver } from 'react-native-quick-sqlite'
import { DataSource, Entity, Column, PrimaryColumn, In } from 'typeorm'
import * as Sentry from '@sentry/react-native'

const databaseName = 'MyDatabase'

@Entity()
export class KeyValueStore {
    @PrimaryColumn('text')
    key: string

    @Column('text')
    value: string
}

const AppDataSource = new DataSource({
    type: 'react-native',
    database: databaseName,
    location: 'default',,
    driver: typeORMDriver,
    logging: ['error', 'query', 'schema'],
    entities: [KeyValueStore],
    synchronize: true,
})

class DatabaseController {
    private database: true | null = null

    public async initDatabase(): Promise<void> {
        try {
             await AppDataSource.initialize().then(() => {
                 this.database = true
             })
        } catch (error) {
            console.error(error)
            Sentry.captureException(error)
        }
    }
    
    // other operational functions here...
}

When any file references this one (even if I comment out all code), I get this error: TypeError: , js engine: hermes. The error is being thrown from the reflect-metadata shim here. It's being called from Primary Column and the object variable is undefined.

I have followed the instructions in quick-SQLLite as well as the instructions in typeorm itself. I have also checked the react native example but some of the connection functions are now deprecated.

I've installed the relevant babel stuff like decorator support - see .babelrc:

{
    "presets": ["module:metro-react-native-babel-preset"],
    "plugins": [
        ["jest-hoist"],
        ["babel-plugin-idx"],
        ["@babel/plugin-proposal-decorators", { "version": "2023-05" }],
        ["@babel/plugin-transform-class-static-block"],
        [
            "module-resolver",
            {
                "alias": {
                    "react-native-sqlite-storage": "react-native-quick-sqlite"
                },
                "root": ["./App"],
                "extensions": [".js", ".ts", ".tsx", ".ios.js", ".android.js"]
            }
        ]
    ],
    "env": {
        "production": {
            "plugins": ["transform-remove-console"]
        },
        "test": {
            "plugins": ["dynamic-import-node"]
        }
    }
}

Versions are:

  • React Native 0.68.5
  • Node 14.18.3
  • reflect-metadata 0.1.13
  • typeorm 0.3.17
  • babel core 7.21.8
  • babel plugin-proposal-decorators 7.22.5
  • babel plugin-transform-class-static-block 7.22.5

Is there anything else I can try? Any idea what area the problem might be in?

0

There are 0 best solutions below