Issues with TypeORM, CodePush and React Native in Android Build

130 Views Asked by At

I've developed a project using React Native. It works fine when I build it for Android. However, when I try to update using CodePush, I get the following error:

Entity metadata for t#user was not found. Check if you specified a correct entity object and if it's connected in the connection options.

To solve this issue, I initially had:

@ManyToOne('User')
@JoinColumn({name: 'user_pk'})
user!: User;

I updated it to:

@ManyToOne(() => User)
@JoinColumn({name: 'user_pk'})
user!: User;

Note: On iOS, using @ManyToOne(() => User) gave me a "Cyclic dependency: 't'" error.

Now, I am encountering another issue:

migration name is wrong. Migration class name should have a JavaScript timestamp appended.

Here's the configuration and the database initialization code that might be relevant to the issue.

AppDataSource.ts

import {DataSource} from 'typeorm';
import {User} from '~/database/entities/user';
import {CreateUsersTable0000000000001} from '~/database/migrations/0000000000001_create_users_table';

export const AppDataSource = new DataSource({
  type: 'react-native',
  database: 'my_database.db',
  location: 'default',
  synchronize: false,
  logging: ['error'],
  entities: [
    User,
  ],
  migrations: [
    CreateUsersTable0000000000001,
  ],
});

tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "isolatedModules": true,
    "jsx": "react-jsx",
    "lib": ["es6"],
    "moduleResolution": "node",
    "noEmit": true,
    "strict": true,
    "target": "esnext",
    "skipLibCheck": true,
    "resolveJsonModule": true,
    "noImplicitAny": false,
    "module": "commonjs",
    "baseUrl": "./src",
    "paths": {
      "~/*": ["*"]
    },
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "exclude": [
    "node_modules",
    "babel.config.js",
    "metro.config.js",
    "jest.config.js"
  ]
}

babel.config.js

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    'react-native-reanimated/plugin',

    ['@babel/plugin-proposal-decorators', {legacy: true}],

    [
      'module-resolver',
      {
        root: ['./src'],
        extensions: ['.ios.js', '.android.js', '.js', '.ts', '.tsx', '.json'],
        alias: {
          '~': './src',
        },
      },
    ],
  ],
};

Is there something wrong with my build process or configuration? Any help would be greatly appreciated.

What did I try and what was I expecting?

I was expecting the CodePush update to work seamlessly on Android as it does normally after a build. However, the update leads to an error that seems to be related to TypeORM.

Steps Taken:

  1. I updated the @ManyToOne decorator as I thought the issue might be related to how the entity was defined. This didn't resolve the issue but introduced another error on iOS build about cyclic dependencies.

  2. I looked into the tsconfig.json and babel.config.js files to see if they were affecting the CodePush updates or the TypeORM entity metadata. Couldn't find anything directly related.

Expected Outcome:

  • I expected the CodePush updates to apply without any issues and the database to be able to recognize the t#user entity.

Actual Outcome:

  • The CodePush update throws an error about missing t#user entity metadata.
  • On changing the @ManyToOne decorator, a new error appears that says the migration name should have a JavaScript timestamp appended.
1

There are 1 best solutions below

0
Rain On

I also met the "Cyclic dependency" error, and this solved my issue : add following in the metro.config.js.

 module.exports = {
      transformer: {
        minifierConfig: {
          keep_fnames: true, // To avoid cyclic dependency "t" error of typeorm in release build
        },
      },
    };     

As for the migration error, it seems your migration class name is not a valid timestamp, you can generate the migration by typeorm-cli : https://typeorm.io/migrations#generating-migrations