Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeORM with React Native

I created a new dummy app with react-native init test, and then followed the instructions to add typeorm. In my App.js I included import {getManager} from 'typeorm', and then ran react-native run-ios.

I see the following error in metro-bundler:

Error: Unable to resolve module path from /Users/amit/Code/test/node_modules/typeorm/platform/PlatformTools.js: Module path does not exist in the Haste module map

Here's a sample repository to show the problem: enter link description here

Not sure if I missed something in the setup! Any help is really welcome!

like image 711
Amit Avatar asked May 01 '18 13:05

Amit


1 Answers

Unfortunately import from 'typeorm' module does not work because react-native projects does not use node platform excatly. Imports from 'typeorm/browser' will work. Here is a sample project.: https://github.com/typeorm/react-native-example

Make sure you create a connection object that does not use any references to project file system. Avoid using something like:

import { CountSession } from '../biopro-mobile-database/entities/count_session';

   const connection = await createConnection({
            name: 'liteDb_3',
            type: 'react-native',
            database: 'biopro_mobile.sqlite',
            location: 'default',
            synchronize: false,
            logging: true,
            entities: ["../biopro-mobile-database/entities/**/*.ts"],
          })

Avoid entities: ["../biopro-mobile-database/entities//*.ts"],** Instead use something like:

import { EquipmentCounted } from '../biopro-mobile-database/entities/equipment_counted';
import { CountSession } from '../biopro-mobile-database/entities/count_session';

   const connection = await createConnection({
            name: 'liteDb_3',
            type: 'react-native',
            database: 'biopro_mobile.sqlite',
            location: 'default',
            synchronize: false,
            logging: true,
            entities: [
              CountSession,
              EquipmentCounted,
            ],
          })
like image 145
kayhan.ogretir Avatar answered Oct 21 '22 18:10

kayhan.ogretir