react-native-quick-sqlite
react-native-quick-sqlite copied to clipboard
Using with TypeORM
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](https://user-images.githubusercontent.com/25336031/184507902-e1269255-0a82-4a27-abe1-f062703ddca3.png)
- I have this package,
typeorm
,reflect-metadata
and the required babel plugins installed (tried clearing all cache already) - 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",
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.
Did you solve it?
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"
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.
@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
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?
No idea sorry, no time to debug issues right now
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.
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.
OK I'll stick with my workaround for the time being, thanks for replying!
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"
}
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](https://user-images.githubusercontent.com/1634213/206666097-df89fd0b-084e-4ead-ac64-ee233d9e4aa8.png)
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