1

I am trying to use Knex.js (http://knexjs.org/) to manage the SQLite database that comes with Expo (https://docs.expo.io/versions/latest/sdk/sqlite.html.) When I import knex, it crashes because of some missing node.js packages like "events", "streams", "assert", etc.

So clearly it seems to think it is in a node.js environment which isn't what create-react-native-app provides by default.

I only need it to generate SQL query strings for a better development interface (compared to string manipulation and concatenation).

Has anyone got knex.js to work on create-react-native-app?

X0r0N
  • 1,816
  • 6
  • 29
  • 50

2 Answers2

0

you won't be able to use it at all, so its not a matter of 'has anyone' done it yet. You can't get a full node API environment in a ReactNative app.

  • on the package.json, it mentions "react-native" explicitly, so i think there is some functionality there for it. – X0r0N Sep 28 '17 at 11:03
  • What is the connection with 'react-native'? –  Sep 28 '17 at 21:20
0

You could pack query builder parts of knex (.toNative() comes in knex 0.14) to your react app and then create queries like:

 knex('MyTable').where('id', 1).toSQL().toNative()

 // Will output 
 // {
 //    sql: 'select * from `MyTable` where `id` = ?',
 //    bindings: [1]
 // }

Knex documentation page is also using that way to render built queries in http://knexjs.org/

Probably you need to use webpack or something like that to build standalone version of knex.

Mikael Lepistö
  • 18,909
  • 3
  • 68
  • 70
  • knex fails when i import. before it is used. looking at the following pull request i may have to go though the missing modules individually and installing the nodejs dependencies from npm `https://github.com/tgriesser/knex/pull/1813` – X0r0N Sep 28 '17 at 11:05