Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using with TypeORM #78

Closed
eylamf opened this issue Aug 13, 2022 · 12 comments
Closed

Using with TypeORM #78

eylamf opened this issue Aug 13, 2022 · 12 comments

Comments

@eylamf
Copy link

eylamf commented Aug 13, 2022

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",

@ospfranco
Copy link
Owner

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 ospfranco reopened this Aug 14, 2022
@eylamf eylamf closed this as completed Aug 18, 2022
@ospfranco
Copy link
Owner

Did you solve it?

@LarenDorr
Copy link

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
Copy link

LarenDorr commented Aug 21, 2022

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.

@vicary
Copy link

vicary commented Sep 1, 2022

@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?

@ospfranco
Copy link
Owner

No idea sorry, no time to debug issues right now

@vicary
Copy link

vicary commented Sep 1, 2022

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.

@ospfranco
Copy link
Owner

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.

@vicary
Copy link

vicary commented Sep 1, 2022

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

@elliotsayes
Copy link

If you don't want to edit your node_modules, I created a patch of TypeORM with a couple of the fixes mentioned: typeorm/typeorm@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"
}

@ospfranco ospfranco reopened this Dec 9, 2022
@ospfranco
Copy link
Owner

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
Copy link
Owner

ospfranco commented Dec 9, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants