Skip to content

Expo driver#2240

Merged
pleerock merged 3 commits intotypeorm:masterfrom
cuibonobo:expo-driver
Jul 7, 2018
Merged

Expo driver#2240
pleerock merged 3 commits intotypeorm:masterfrom
cuibonobo:expo-driver

Conversation

@cuibonobo
Copy link
Contributor

Add a driver to support the Expo platform. In particular it is using the Expo SQLite API that ships with Expo.

A few things to note about this pull request:

  • Expo apps still need to install react-native-sqlite-storage as a dependency. The issue is that the Expo packager still attempts to include this dependency (even though it doesn't need it) because of the this.sqlite = require("react-native-sqlite-storage"); line in src/driver/react-native/ReactNativeDriver.ts. If someone knows how to make the Expo packager ignore this line, I'm all ears! Still, needing to install react-native-sqlite-storage isn't such a big deal. No linking or ejecting of the Expo app is required.
  • Expo has an odd way of handling transactions: basically every query to the database is in a transaction context. In fact, running a BEGIN TRANSACTION query throws a Transaction already started error. As a result, I needed to override the startTransaction, commitTransaction, and rollbackTransaction methods on the AbstractSqliteQueryRunner class to account for the weird behavior. Besides this, the Expo driver is almost identical to how React Native is being handled.

I have a working Expo example at https://github.com/cuibonobo/expo-example. To try it, download the Expo app from the App Store or Google Play Store, then cd into the expo-example directory and run:

npm install
npm start

If your phone is on the same network as your development machine, scanning the generated QR code will automatically open the Expo app and run the example.


Fixes #2183.

@pleerock pleerock requested a review from daniel-lang June 5, 2018 07:43
@pleerock
Copy link
Member

pleerock commented Jun 5, 2018

Thank you very much!

Looks good I think, but I need @daniel-lang confirmation on this PR since he is expert in sqlite databases and mobiles in TypeORM.

Also, once PR will be merged we'll need to move your expo-example repository into typeorm organization on github.

Also, you'll have to maintain this driver all your life (😆 ) because we bring a new functionality and we can't one day remove it if maintainer has gone, I hope you understand the responsibility.

@cuibonobo
Copy link
Contributor Author

Thanks for looking! I will be happy to tranfer the example repo and maintain the Expo driver. I have also been keeping an eye on changes to the SQLite API within Expo so I can make changes to the driver if necessary.

Copy link
Contributor

@daniel-lang daniel-lang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for providing this driver. Looks very good! There is just one small request I have.

/**
* Starts transaction.
*/
async startTransaction(): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you write a comment documenting why you had to override the transaction methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's a good idea. I'll update my PR today.

@stevefan1999-personal
Copy link

stevefan1999-personal commented Jul 3, 2018

Pretty cool, but technically isn't the Expo SQLite API a wrapper of react-native-sqlite-storage? So if we had Expo SQLite API installed, we could use react-native-sqlite-storage directly already.

@pleerock
Copy link
Member

pleerock commented Jul 3, 2018

yeah TBH I definitely would like to avoid adding specific sqlite3 drivers into typeorm. @cuibonobo what are exact expo specifics that required you to write a separate driver for it?

@cuibonobo
Copy link
Contributor Author

@stevefan1999 The API that is available in Expo.SQLite is a wrapper around react-native-sqlite-storage, but Expo projects are designed in such a way that you need to use Expo APIs if you want to access any native platform functionality (touching the file system, accessing sensors, etc). Expo abstracts these functions so that there are no android and ios projects in your repository. As a result, there is no way to link the react-native-sqlite-storage libraries to these platforms.

In fact, when you attempt to use the react-native driver with an Expo project, the error you get is: undefined is not an object (evaluating 'NativeModules["SQLite"][method]'). This is the same error you would get if you attempt to create a connection with react-native-sqlite-storage but haven't properly linked your libraries.

@cuibonobo
Copy link
Contributor Author

@daniel-lang I've updated the comments for those transaction lifecycle methods to give a little more insight into what is happening. Thanks for your suggestion!

I rebased my branch against current master to make sure everything still works ok.

@daniel-lang
Copy link
Contributor

The problem with all the different sqlite drivers that we have in TypeORM is that the way they are loaded and called is always a little different. Therefore if we want to support the different mobile platforms we will have to have a lot of sqlite drivers that are quite similar.

@pleerock
Copy link
Member

pleerock commented Jul 7, 2018

@cuibonobo I'll merge it, however in the future we might change the way we do drivers right now. Please track for #2479. Thank you very much for your contribution!

Can you please "send" your example repository to "typeorm" organization?

@cuibonobo cuibonobo deleted the expo-driver branch July 9, 2018 15:23
@cuibonobo
Copy link
Contributor Author

@pleerock Of course! At the moment the example repository uses the following line in package.json to point to a built TypeORM package that included the Expo driver:

"typeorm": "github:cuibonobo/typeorm-package"

Once the latest TypeORM release includes the Expo driver, this line should be changed. For the moment I will leave it as-is in the example so that it works. Will send the repo now.

@cuibonobo
Copy link
Contributor Author

@pleerock Err, I spoke too soon. I attempted to transfer the repository to the "typeorm" organization but received the following error:

You don’t have the permission to create repositories on typeorm

@cuibonobo
Copy link
Contributor Author

I've added you as a collaborator on the example repo. Hopefully after accepting you can transfer it to the typeorm org.

@pleerock
Copy link
Member

pleerock commented Jul 9, 2018

its not owner access, I can't do it. Let me just add you to typeorm organization and you can move it on your own.

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

Successfully merging this pull request may close these issues.

4 participants