react-native-quick-sqlite icon indicating copy to clipboard operation
react-native-quick-sqlite copied to clipboard

Using with TypeORM

Open eylamf opened this issue 1 year ago • 1 comments

Description

Hey! Thanks for making this great package. I'm attempting to use it with TypeORM but getting the following error when initializing

Screen Shot 2022-08-13 at 12 21 58 PM
  1. I have this package, typeorm, reflect-metadata and the required babel plugins installed (tried clearing all cache already)
  2. I did the patch mentioned in the readme by adding "./package.json": "./package.json" to the TypeORM package.json file

DataSource setup:

datasource = new DataSource({
    type: 'react-native',
    database: '<db_name>',
    location: 'default',
    // seems like the error is coming from here
    driver: require('react-native-quick-sqlite'),
    entities: [...],
    synchronize: true,
  });

Also looks like the latest commit commented out the TypeORM code from the example project, so I was wondering if the readme is up to date on the necessary steps for getting setup

Additonal info

"react-native": "0.68.2",

eylamf avatar Aug 13 '22 18:08 eylamf

You need to restart metro (and the cache) but cannot tell you more, I got it working once with the patch exporting the package.json on the TypeORM package.json, but you will have to debug any further issues yourself.

ospfranco avatar Aug 14 '22 11:08 ospfranco

Did you solve it?

ospfranco avatar Aug 19 '22 05:08 ospfranco

Same error. But when I debugging, it throw diffrent error.

Sometimes is:

Error: Requiring module "node_modules/typeorm/browser/driver/react-native/ReactNativeDriver.js", which threw an exception: Error: Requiring unknown module "undefined". If you are sure the module exists, try restarting Metro. You may also want to run yarn or npm install.

or

Error: Requiring unknown module "undefined". If you are sure the module exists, try restarting Metro. You may also want to run yarn or npm install.

Version info: "react-native-quick-sqlite": "^4.0.5" "typeorm": "^0.3.7"

LarenDorr avatar Aug 21 '22 09:08 LarenDorr

I found a solution, just add console.log('AbstractSqliteDriver: ', AbstractSqliteDriver); to node_modules/typeorm/browser/driver/react-native/ReactNativeDriver.js. Let AbstractSqliteDriver variable to be used. But I don't know why?? Maybe it has something wrong with compilation.

LarenDorr avatar Aug 21 '22 11:08 LarenDorr

@ospfranco I am using TypeScript and typeorm/typeorm#9178 does not fix it.

Instead I am having this weird error

Error: Requiring unknown module "undefined". If you are sure the module exists, try restarting Metro. You may also want to run `yarn` or `npm install`., js engine: hermes

TypeError: Cannot read property 'ReactNativeDriver' of undefined
TypeError: Cannot read property 'ReactNativeDriver' of undefined
screenshot

image

More digging into sources in node_modules I found that the most likely cause is metro trying to hoist the optional require("react-native-sqlite-storage") and some recursive requiring gives undefined in the way.

My current workaround is to also install react-native-sqlite-storage so it is able to move pass the require phase, but resolves to react-native-quick-sqlite in runtime.

This is sub-optimal because hoisting almost suggest the bundling of react-native-sqlite-storage which is never used.

Is there a better way to do it?

vicary avatar Sep 01 '22 08:09 vicary

No idea sorry, no time to debug issues right now

ospfranco avatar Sep 01 '22 08:09 ospfranco

TBH It looks very much like a TypeORM issue to me, I don't think the require phase and metro's behavior is within the scope of this project.

Much appreciated if you would take a quick look and confirm this later, please take your time.

vicary avatar Sep 01 '22 09:09 vicary

Yeah, this is a problem with Metro and the hardcoded requires of TypeORM, I already tried to debug it and found the instructions on the README working, but not using the library at the moment.

ospfranco avatar Sep 01 '22 09:09 ospfranco

OK I'll stick with my workaround for the time being, thanks for replying!

vicary avatar Sep 01 '22 10:09 vicary

If you don't want to edit your node_modules, I created a patch of TypeORM with a couple of the fixes mentioned: https://github.com/typeorm/typeorm/compare/master...elliotsayes:react-native-typeorm:dev This also adds hacky support for Buffer (sqlite "blob") to the react-native driver. Add to your deps like so:

"dependencies": {
    "typeorm": "https://github.com/elliotsayes/react-native-typeorm/blob/574e0b241fa61a1bb371ae553f497e84cd94e7a2/build/typeorm-0.3.10.tgz?raw=true"
}

elliotsayes avatar Oct 06 '22 04:10 elliotsayes

I have a bit of free time at the moment, looking into this.

I managed to get metro to correctly link typeorm by adding the "./package.json" export on the typeorm root package.json file.

I'm now facing the ReactNativeDriver driver issue, I don't remember how I patched it but I think I can create a babel alias for the package in order to trick typeorm to think sqlite-storage is installed.

Screenshot 001335

ospfranco avatar Dec 09 '22 09:12 ospfranco

Got it working. I fixed the driver and added new instructions on the Readme:

https://github.com/ospfranco/react-native-quick-sqlite#typeorm

The new driver is published under 6.1.0

ospfranco avatar Dec 09 '22 09:12 ospfranco